-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit.zig
More file actions
364 lines (330 loc) · 10.9 KB
/
git.zig
File metadata and controls
364 lines (330 loc) · 10.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
const std = @import("std");
const contribution_languages = @import("contribution_languages.zig");
pub const LanguageContribution = contribution_languages.LanguageContribution;
pub const ContributionStats = struct {
lines_changed: u32,
languages: []LanguageContribution,
pub fn deinit(self: ContributionStats, allocator: std.mem.Allocator) void {
for (self.languages) |language| language.deinit(allocator);
allocator.free(self.languages);
}
};
var is_installed: ?bool = null;
pub fn isInstalled(gpa: std.mem.Allocator, io: std.Io) bool {
if (is_installed) |v| {
return v;
}
var arena = std.heap.ArenaAllocator.init(gpa);
defer arena.deinit();
const run = std.process.run(arena.allocator(), io, .{
.argv = &.{ "git", "--version" },
}) catch {
is_installed = false;
return is_installed.?;
};
is_installed = switch (run.term) {
.exited => |v| v == 0,
else => false,
};
return is_installed.?;
}
pub fn currentCommit(gpa: std.mem.Allocator, io: std.Io) ![]const u8 {
if (!isInstalled(gpa, io)) return error.GitNotInstalled;
var arena = std.heap.ArenaAllocator.init(gpa);
defer arena.deinit();
const run = try std.process.run(arena.allocator(), io, .{
.argv = &.{ "git", "rev-parse", "HEAD" },
});
return try gpa.dupe(u8, run.stdout[0..8]);
}
pub fn getLinesChanged(
gpa: std.mem.Allocator,
io: std.Io,
login: []const u8,
token: []const u8,
repo: []const u8,
emails: []const []const u8,
) !u32 {
const stats = try getContributionStats(gpa, io, login, token, repo, emails);
defer stats.deinit(gpa);
return stats.lines_changed;
}
pub fn getContributionLanguages(
gpa: std.mem.Allocator,
io: std.Io,
login: []const u8,
token: []const u8,
repo: []const u8,
emails: []const []const u8,
) ![]LanguageContribution {
const stats = try getContributionStats(gpa, io, login, token, repo, emails);
return stats.languages;
}
pub fn getContributionStats(
gpa: std.mem.Allocator,
io: std.Io,
login: []const u8,
token: []const u8,
repo: []const u8,
emails: []const []const u8,
) !ContributionStats {
if (!isInstalled(gpa, io)) return error.GitNotInstalled;
var arena = std.heap.ArenaAllocator.init(gpa);
defer arena.deinit();
const allocator = arena.allocator();
const repo_path = try std.mem.replaceOwned(u8, allocator, repo, "/", "_");
const repo_url = try std.fmt.allocPrint(
allocator,
"https://{s}:{s}@github.com/{s}.git",
.{ login, token, repo },
);
const clone = try std.process.run(allocator, io, .{
.argv = &.{
"git",
"clone",
"--bare",
"--filter=blob:limit=1m",
"--no-tags",
"--single-branch",
repo_url,
repo_path,
},
});
switch (clone.term) {
.exited => |v| if (v != 0) return error.CloneFailed,
else => return error.CloneFailed,
}
defer std.Io.Dir.cwd().deleteTree(io, repo_path) catch {};
return try contributionStatsFromRepoPath(gpa, io, repo_path, login, emails);
}
fn contributionStatsFromRepoPath(
gpa: std.mem.Allocator,
io: std.Io,
repo_path: []const u8,
login: []const u8,
emails: []const []const u8,
) !ContributionStats {
var arena = std.heap.ArenaAllocator.init(gpa);
defer arena.deinit();
const allocator = arena.allocator();
const author_patterns = try buildAuthorPatterns(allocator, login, emails);
const author_args = try allocator.alloc([]const u8, author_patterns.len * 2);
for (author_patterns, 0..) |author, i| {
author_args[i * 2] = "--author";
author_args[i * 2 + 1] = author;
}
const log_args = try std.mem.concat(allocator, []const u8, &.{
&.{
"git",
"-C",
repo_path,
"log",
"--all",
"--numstat",
"--pretty=tformat:",
},
author_args,
});
const log = try std.process.run(allocator, io, .{ .argv = log_args });
switch (log.term) {
.exited => |v| if (v != 0) return error.LogFailed,
else => return error.LogFailed,
}
const lines_changed = countChangedLines(log.stdout);
const languages = try contribution_languages.parseNumstat(gpa, log.stdout);
errdefer {
for (languages) |language| language.deinit(gpa);
gpa.free(languages);
}
return .{
.lines_changed = lines_changed,
.languages = languages,
};
}
fn countChangedLines(numstat: []const u8) u32 {
var lines_changed: u32 = 0;
var lines = std.mem.tokenizeScalar(u8, numstat, '\n');
while (lines.next()) |raw_line| {
const line = std.mem.trim(u8, raw_line, "\r");
var fields = std.mem.splitScalar(u8, line, '\t');
const additions_text = fields.next() orelse continue;
const deletions_text = fields.next() orelse continue;
if (std.mem.eql(u8, additions_text, "-") or
std.mem.eql(u8, deletions_text, "-"))
{
continue;
}
const additions = std.fmt.parseUnsigned(u32, additions_text, 10) catch continue;
const deletions = std.fmt.parseUnsigned(u32, deletions_text, 10) catch continue;
lines_changed += additions + deletions;
}
return lines_changed;
}
fn buildAuthorPatterns(
allocator: std.mem.Allocator,
login: []const u8,
emails: []const []const u8,
) ![][]const u8 {
var patterns = try std.ArrayList([]const u8).initCapacity(
allocator,
emails.len + 5,
);
for (emails) |email| {
try appendUnique(allocator, &patterns, email);
}
try appendLoginPatterns(allocator, &patterns, login);
const lower_login = try std.ascii.allocLowerString(allocator, login);
if (!std.mem.eql(u8, lower_login, login)) {
try appendLoginPatterns(allocator, &patterns, lower_login);
}
return try patterns.toOwnedSlice(allocator);
}
fn appendLoginPatterns(
allocator: std.mem.Allocator,
patterns: *std.ArrayList([]const u8),
login: []const u8,
) !void {
try appendUnique(allocator, patterns, login);
try appendUnique(
allocator,
patterns,
try std.fmt.allocPrint(
allocator,
"{s}@users\\.noreply\\.github\\.com",
.{login},
),
);
try appendUnique(
allocator,
patterns,
try std.fmt.allocPrint(
allocator,
"[0-9][0-9]*+{s}@users\\.noreply\\.github\\.com",
.{login},
),
);
}
fn appendUnique(
allocator: std.mem.Allocator,
patterns: *std.ArrayList([]const u8),
pattern: []const u8,
) !void {
for (patterns.items) |existing| {
if (std.mem.eql(u8, existing, pattern)) return;
}
try patterns.append(allocator, pattern);
}
test "git integration attributes changed file languages to selected author emails" {
if (!isInstalled(std.testing.allocator, std.testing.io)) {
return error.SkipZigTest;
}
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
try tmp.dir.createDirPath(std.testing.io, "repo");
const repo_path = try std.fmt.allocPrint(
std.testing.allocator,
".zig-cache/tmp/{s}/repo",
.{tmp.sub_path},
);
defer std.testing.allocator.free(repo_path);
var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
defer arena.deinit();
const allocator = arena.allocator();
try runGit(allocator, &.{ "git", "init", repo_path });
try runGit(allocator, &.{ "git", "-C", repo_path, "config", "user.name", "Committer" });
try runGit(allocator, &.{ "git", "-C", repo_path, "config", "user.email", "committer@example.com" });
try tmp.dir.writeFile(std.testing.io, .{
.sub_path = "repo/owned.zig",
.data = "pub fn owned() void {}\nconst x = 1;\n",
});
try runGit(allocator, &.{ "git", "-C", repo_path, "add", "owned.zig" });
try runGit(allocator, &.{
"git",
"-C",
repo_path,
"commit",
"--author",
"Selected <selected@example.com>",
"-m",
"selected zig",
});
try tmp.dir.writeFile(std.testing.io, .{
.sub_path = "repo/README.md",
.data = "# Notes\n",
});
try runGit(allocator, &.{ "git", "-C", repo_path, "add", "README.md" });
try runGit(allocator, &.{
"git",
"-C",
repo_path,
"commit",
"--author",
"Selected <selected@example.com>",
"-m",
"selected docs",
});
try tmp.dir.writeFile(std.testing.io, .{
.sub_path = "repo/other.c",
.data = "int other(void) { return 1; }\n",
});
try runGit(allocator, &.{ "git", "-C", repo_path, "add", "other.c" });
try runGit(allocator, &.{
"git",
"-C",
repo_path,
"commit",
"--author",
"Other <other@example.com>",
"-m",
"other c",
});
const stats = try contributionStatsFromRepoPath(
std.testing.allocator,
std.testing.io,
repo_path,
"selected",
&.{"selected@example.com"},
);
defer stats.deinit(std.testing.allocator);
try std.testing.expectEqual(@as(u32, 3), stats.lines_changed);
try std.testing.expectEqual(@as(usize, 1), stats.languages.len);
try std.testing.expectEqualStrings("Zig", stats.languages[0].name);
try std.testing.expectEqual(@as(u32, 2), stats.languages[0].lines_changed);
}
test "numstat line totals include unclassified text files" {
try std.testing.expectEqual(
@as(u32, 15),
countChangedLines(
"10\t2\tsrc/main.zig\n3\t0\tREADME.md\n-\t-\tassets/logo.png\n",
),
);
}
test "author patterns include login and GitHub noreply variants" {
var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
defer arena.deinit();
const patterns = try buildAuthorPatterns(
arena.allocator(),
"SampleUser",
&.{"real@example.com"},
);
try expectPattern(patterns, "real@example.com");
try expectPattern(patterns, "SampleUser");
try expectPattern(patterns, "SampleUser@users\\.noreply\\.github\\.com");
try expectPattern(patterns, "[0-9][0-9]*+SampleUser@users\\.noreply\\.github\\.com");
try expectPattern(patterns, "sampleuser");
try expectPattern(patterns, "sampleuser@users\\.noreply\\.github\\.com");
try expectPattern(patterns, "[0-9][0-9]*+sampleuser@users\\.noreply\\.github\\.com");
}
fn expectPattern(patterns: []const []const u8, expected: []const u8) !void {
for (patterns) |pattern| {
if (std.mem.eql(u8, pattern, expected)) return;
}
return error.ExpectedPatternMissing;
}
fn runGit(allocator: std.mem.Allocator, argv: []const []const u8) !void {
const run = try std.process.run(allocator, std.testing.io, .{ .argv = argv });
switch (run.term) {
.exited => |code| if (code != 0) return error.GitCommandFailed,
else => return error.GitCommandFailed,
}
}