Skip to content

Commit a015d4f

Browse files
committed
Port to Zig 0.16.0
1 parent 6d30b02 commit a015d4f

9 files changed

Lines changed: 99 additions & 84 deletions

File tree

.github/workflows/main.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ jobs:
2323

2424
- uses: mlugg/setup-zig@d1434d08867e3ee9daa34448df10607b98908d29 # v2 tag
2525
with:
26-
version: 0.15.2
26+
version: 0.16.0
2727

2828
- name: Build
2929
run: |

.github/workflows/release.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ jobs:
1818

1919
- uses: mlugg/setup-zig@d1434d08867e3ee9daa34448df10607b98908d29 # v2 tag
2020
with:
21-
version: 0.15.2
21+
version: 0.16.0
2222
# I only want to disable the build cache, but don't see a good way to do
2323
# that besides disabling all of the caching
2424
use-cache: false

build.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ pub fn build(b: *std.Build) !void {
77
.preferred_optimize_mode = .ReleaseSafe,
88
});
99
var version: []const u8 = @import("build.zig.zon").version;
10-
if (git.isInstalled(b.allocator)) {
10+
if (git.isInstalled(b.allocator, b.graph.io)) {
1111
version = try std.fmt.allocPrint(
1212
b.allocator,
1313
"{s} ({s})",
14-
.{ version, try git.currentCommit(b.allocator) },
14+
.{ version, try git.currentCommit(b.allocator, b.graph.io) },
1515
);
1616
}
1717
const options = b.addOptions();

build.zig.zon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
.name = .github_stats,
33
.version = "2.0.0",
44
.fingerprint = 0x80bb05a632422e37, // Changing this has security and trust implications.
5-
.minimum_zig_version = "0.15.2",
5+
.minimum_zig_version = "0.16.0",
66
.dependencies = .{},
77
.paths = .{
88
"build.zig",

src/argparse.zig

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,22 @@ var stdout: *std.Io.Writer = undefined;
66
var stderr: *std.Io.Writer = undefined;
77

88
pub fn parse(
9-
allocator: std.mem.Allocator,
9+
init: std.process.Init,
1010
T: type,
1111
errorCheck: ?fn (args: T, stderr: *std.Io.Writer) anyerror!bool,
1212
) !T {
13-
var stdout_writer = std.fs.File.stdout().writer(&.{});
13+
const allocator = init.gpa;
14+
const io = init.io;
15+
16+
var stdout_writer = std.Io.File.stdout().writer(io, &.{});
1417
stdout = &stdout_writer.interface;
15-
var stderr_writer = std.fs.File.stderr().writer(&.{});
18+
var stderr_writer = std.Io.File.stderr().writer(io, &.{});
1619
stderr = &stderr_writer.interface;
1720

1821
var arena: std.heap.ArenaAllocator = .init(allocator);
1922
defer arena.deinit();
2023
const a = arena.allocator();
24+
const args = try init.minimal.args.toSlice(a);
2125

2226
const fields = @typeInfo(T).@"struct".fields;
2327
var seen = [_]bool{false} ** fields.len;
@@ -30,10 +34,8 @@ pub fn parse(
3034
}
3135
}
3236

33-
const args = try std.process.argsAlloc(a);
34-
defer std.process.argsFree(a, args);
3537
try setFromCli(T, allocator, &arena, args, &seen, &result);
36-
try setFromEnv(T, allocator, &arena, &seen, &result);
38+
try setFromEnv(T, allocator, &arena, &seen, &result, init.environ_map);
3739
try setFromDefaults(T, allocator, &seen, &result);
3840

3941
inline for (fields, seen) |field, seen_field| {
@@ -139,10 +141,9 @@ fn setFromEnv(
139141
arena: *std.heap.ArenaAllocator,
140142
seen: []bool,
141143
result: *T,
144+
env: *std.process.Environ.Map,
142145
) !void {
143146
const a = arena.allocator();
144-
var env = try std.process.getEnvMap(a);
145-
defer env.deinit();
146147
var iterator = env.iterator();
147148
while (iterator.next()) |entry| {
148149
const key = try a.dupe(u8, entry.key_ptr.*);

src/git.zig

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,45 +2,44 @@ const std = @import("std");
22

33
var is_installed: ?bool = null;
44

5-
pub fn isInstalled(gpa: std.mem.Allocator) bool {
5+
pub fn isInstalled(gpa: std.mem.Allocator, io: std.Io) bool {
66
if (is_installed) |v| {
77
return v;
88
}
99
var arena = std.heap.ArenaAllocator.init(gpa);
1010
defer arena.deinit();
11-
const run = std.process.Child.run(.{
12-
.allocator = arena.allocator(),
11+
const run = std.process.run(arena.allocator(), io, .{
1312
.argv = &.{ "git", "--version" },
1413
}) catch {
1514
is_installed = false;
1615
return is_installed.?;
1716
};
1817
is_installed = switch (run.term) {
19-
.Exited => |v| v == 0,
18+
.exited => |v| v == 0,
2019
else => false,
2120
};
2221
return is_installed.?;
2322
}
2423

25-
pub fn currentCommit(gpa: std.mem.Allocator) ![]const u8 {
26-
if (!isInstalled(gpa)) return error.GitNotInstalled;
24+
pub fn currentCommit(gpa: std.mem.Allocator, io: std.Io) ![]const u8 {
25+
if (!isInstalled(gpa, io)) return error.GitNotInstalled;
2726
var arena = std.heap.ArenaAllocator.init(gpa);
2827
defer arena.deinit();
29-
const run = try std.process.Child.run(.{
30-
.allocator = arena.allocator(),
28+
const run = try std.process.run(arena.allocator(), io, .{
3129
.argv = &.{ "git", "rev-parse", "HEAD" },
3230
});
3331
return try gpa.dupe(u8, run.stdout[0..8]);
3432
}
3533

3634
pub fn getLinesChanged(
3735
gpa: std.mem.Allocator,
36+
io: std.Io,
3837
login: []const u8,
3938
token: []const u8,
4039
repo: []const u8,
4140
emails: []const []const u8,
4241
) !u32 {
43-
if (!isInstalled(gpa)) return error.GitNotInstalled;
42+
if (!isInstalled(gpa, io)) return error.GitNotInstalled;
4443
var arena = std.heap.ArenaAllocator.init(gpa);
4544
defer arena.deinit();
4645
const allocator = arena.allocator();
@@ -51,8 +50,7 @@ pub fn getLinesChanged(
5150
"https://{s}:{s}@github.com/{s}.git",
5251
.{ login, token, repo },
5352
);
54-
const clone = try std.process.Child.run(.{
55-
.allocator = allocator,
53+
const clone = try std.process.run(allocator, io, .{
5654
.argv = &.{
5755
"git",
5856
"clone",
@@ -65,10 +63,10 @@ pub fn getLinesChanged(
6563
},
6664
});
6765
switch (clone.term) {
68-
.Exited => |v| if (v != 0) return error.CloneFailed,
66+
.exited => |v| if (v != 0) return error.CloneFailed,
6967
else => return error.CloneFailed,
7068
}
71-
defer std.fs.cwd().deleteTree(repo_path) catch {};
69+
defer std.Io.Dir.cwd().deleteTree(io, repo_path) catch {};
7270

7371
const email_args = try allocator.alloc([]const u8, emails.len * 2);
7472
for (emails, 0..) |email, i| {
@@ -86,13 +84,9 @@ pub fn getLinesChanged(
8684
},
8785
email_args,
8886
});
89-
const log = try std.process.Child.run(.{
90-
.allocator = allocator,
91-
.argv = log_args,
92-
.max_output_bytes = 64 * 1024 * 1024,
93-
});
87+
const log = try std.process.run(allocator, io, .{ .argv = log_args });
9488
switch (log.term) {
95-
.Exited => |v| if (v != 0) return error.LogFailed,
89+
.exited => |v| if (v != 0) return error.LogFailed,
9690
else => return error.LogFailed,
9791
}
9892

src/http_client.zig

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
const std = @import("std");
66

77
allocator: std.mem.Allocator,
8+
io: std.Io,
89
client: std.http.Client,
910
bearer: []const u8,
1011
token: []const u8,
@@ -21,14 +22,15 @@ const Request = struct {
2122
extra_headers: []const std.http.Header = &.{},
2223
};
2324

24-
pub fn init(allocator: std.mem.Allocator, token: []const u8) !Self {
25+
pub fn init(allocator: std.mem.Allocator, io: std.Io, token: []const u8) !Self {
2526
const bearer = try std.fmt.allocPrint(allocator, "Bearer {s}", .{token});
2627
errdefer allocator.free(bearer);
2728
const cloned_token = try allocator.dupe(u8, token);
2829
errdefer allocator.free(cloned_token);
2930
return .{
3031
.allocator = allocator,
31-
.client = .{ .allocator = allocator },
32+
.io = io,
33+
.client = .{ .allocator = allocator, .io = io },
3234
.bearer = bearer,
3335
.token = cloned_token,
3436
};
@@ -49,7 +51,7 @@ pub fn fetch(self: *Self, request: Request, retries: isize) !Response {
4951
try std.Io.Writer.Allocating.initCapacity(self.allocator, 1024);
5052
var writer_initialized = true;
5153
errdefer if (writer_initialized) writer.deinit();
52-
const status = (try (self.client.fetch(.{
54+
const status = (self.client.fetch(.{
5355
.location = .{ .url = request.url },
5456
.response_writer = &writer.writer,
5557
.payload = request.body,
@@ -67,13 +69,13 @@ pub fn fetch(self: *Self, request: Request, retries: isize) !Response {
6769
.{},
6870
);
6971
self.client.deinit();
70-
self.client = .{ .allocator = self.allocator };
72+
self.client = .{ .allocator = self.allocator, .io = self.io };
7173
writer.deinit();
7274
writer_initialized = false;
7375
return self.fetch(request, retries - 1);
7476
},
75-
else => err,
76-
})).status;
77+
else => return err,
78+
}).status;
7779
return .{
7880
.body = try writer.toOwnedSlice(),
7981
.status = status,

0 commit comments

Comments
 (0)