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
|
const std = @import("std");
const assert = std.debug.assert;
const fs = @import("fs.zig");
const shell = @import("shell.zig");
const cli = @import("cli.zig");
const Env = @import("env.zig");
const fatal = cli.fatal;
pub const Commit = struct {
hash: []const u8,
timestamp: u64,
pub fn init_empty() Commit {
return .{ .hash = "", .timestamp = 0 };
}
pub fn parse(version_str: []const u8) !Commit {
const s = std.mem.trim(u8, version_str, " \t");
if (s.len == 0) return .init_empty();
const pos = std.mem.indexOfScalar(u8, s, '@') orelse return error.invalid_commit;
return .{
.hash = s[0..pos],
.timestamp = std.fmt.parseInt(u64, s[pos + 1 ..], 10) catch return error.invalid_commit,
};
}
pub fn format(self: Commit, w: *std.io.Writer) std.io.Writer.Error!void {
if (self.hash.len > 0) {
try w.print("{s}@{d}", .{ self.hash, self.timestamp });
}
}
};
pub const Repo = struct {
path: fs.Path,
url: []const u8,
branch: []const u8,
pub fn clone(self: Repo, env: Env) !void {
const dir = try fs.dir_info(self.path);
if (!dir.exists or dir.empty) {
var branch_buf: [std.fs.max_name_bytes]u8 = undefined;
const branch_param = branch_buf[0 .. 9 + self.branch.len];
@memcpy(branch_param[0..9], "--branch=");
@memcpy(branch_param[9..], self.branch);
const res = shell.exec(.{
.env = env,
.argv = &.{ "git", "clone", "--quiet", "--depth=1", branch_param, self.url, self.path.name() },
});
switch (res) {
.success => |out| env.dealloc(out),
.failure => |out| {
env.err("failed to clone {s}\n", .{self.url});
fatal("{s}", .{out});
},
}
}
}
pub fn reset(self: Repo, env: Env) void {
var res = shell.exec(.{
.env = env,
.argv = &.{ "git", "reset", "--hard" },
.cwd = self.path,
});
switch (res) {
.success => |out| env.dealloc(out),
.failure => |out| {
env.err("failed to reset repository: {s}\n", .{self.path.name()});
fatal("{s}", .{out});
},
}
res = shell.exec(.{
.env = env,
.argv = &.{ "git", "clean", "-fdx" },
.cwd = self.path,
});
switch (res) {
.success => |out| env.dealloc(out),
.failure => |out| {
env.err("failed to clean repository: {s}\n", .{self.path.name()});
fatal("{s}", .{out});
},
}
}
pub fn update(self: Repo, env: Env) !Commit {
const dir = try fs.dir_info(self.path);
if (!dir.exists or dir.empty) {
try self.clone(env);
} else {
var res = shell.exec(.{
.env = env,
.argv = &.{ "git", "fetch", "--quiet", "--depth=1", "origin", self.branch },
.cwd = self.path,
});
switch (res) {
.success => |out| env.dealloc(out),
.failure => |out| {
env.err("failed to fetch branch {s}: {s}\n", .{ self.branch, self.url });
fatal("{s}", .{out});
},
}
const branch_spec: fs.Path = .join(.{ "origin", self.branch });
res = shell.exec(.{
.env = env,
.argv = &.{ "git", "reset", "--hard", branch_spec.name() },
.cwd = self.path,
});
switch (res) {
.success => |out| env.dealloc(out),
.failure => |out| {
env.err("failed to reset branch {s}: {s}\n", .{ self.branch, self.path.name() });
fatal("{s}", .{out});
},
}
}
// We leak `out` intentionally here.
const out = self.must_run(env, &.{ "git", "show", "--no-patch", "--format=format:%H %at", self.branch });
const space = std.mem.indexOfScalar(u8, out, ' ') orelse {
fatal("unexpected output for 'git show'", .{});
};
return .{
.hash = out[0..space],
.timestamp = ts: {
var timestamp: u64 = 0;
for (out[space + 1 ..]) |c| {
if (c < '0' or c > '9') {
fatal("unexpected timestamp in 'git show'", .{});
}
timestamp *= 10;
timestamp += @intCast(c - '0');
}
break :ts timestamp;
},
};
}
fn must_run(self: Repo, env: Env, argv: []const []const u8) []const u8 {
const res = shell.exec(.{
.env = env,
.argv = argv,
.cwd = self.path,
});
switch (res) {
.success => |out| return out,
.failure => |out| {
var errmsg = out;
if (std.mem.indexOfScalar(u8, errmsg, '\n')) |eol| {
errmsg = errmsg[0..eol];
if (errmsg.len > 0 and errmsg[errmsg.len - 1] == '\r') {
errmsg = errmsg[0 .. errmsg.len - 1];
}
}
fatal("{s}", .{errmsg});
},
}
}
};
pub fn is_valid_branch_name(name: []const u8) bool {
var last_c: u8 = 0;
for (name) |c| {
switch (c) {
0...31 => return false,
':', '?', '[', '\\', '^', '~', ' ', '*' => return false,
'.' => if (last_c == '.') return false,
'{' => if (last_c == '@') return false,
else => {},
}
last_c = c;
}
return true;
}
|