From de9de0a7679d6e71e938f2807b7a792e302ef550 Mon Sep 17 00:00:00 2001 From: Meghan Denny Date: Thu, 28 Oct 2021 03:48:10 -0700 Subject: [PATCH] remove global allocator from u.run_cmd --- src/cmd/fetch.zig | 2 +- src/common.zig | 18 +++++++++--------- src/util/dep_type.zig | 20 ++++++++++---------- src/util/funcs.zig | 12 ++++++------ 4 files changed, 26 insertions(+), 26 deletions(-) diff --git a/src/cmd/fetch.zig b/src/cmd/fetch.zig index 64a73054d68bced9ecc5c2dfb6733bd8a24458b8..8a061c24765cebf88df4fffcb626888926fbdb79 100644 --- a/src/cmd/fetch.zig +++ b/src/cmd/fetch.zig @@ -144,7 +144,7 @@ fn diff_lockfile() !void { const max = std.math.maxInt(usize); if (try u.does_folder_exist(".git")) { - const result = try u.run_cmd_raw(null, &.{ "git", "diff", "zigmod.lock" }); + const result = try u.run_cmd_raw(gpa, null, &.{ "git", "diff", "zigmod.lock" }); const r = std.io.fixedBufferStream(result.stdout).reader(); while (try r.readUntilDelimiterOrEofAlloc(gpa, '\n', max)) |line| { if (std.mem.startsWith(u8, line, "@@")) break; diff --git a/src/common.zig b/src/common.zig index c36b6415b4f16484dbe8d718a152983f60eab505..86dd791d32c75d4e685bad7273d7dc2d9ebb344c 100644 --- a/src/common.zig +++ b/src/common.zig @@ -136,13 +136,13 @@ pub fn get_modpath(cachepath: string, d: zigmod.Dep, options: *CollectOptions) ! if (try u.does_folder_exist(pv)) { if (vers.id == .branch) { if (options.update) { - try d.type.update(pv, d.path); + try d.type.update(gpa, pv, d.path); } } return pv; } - try d.type.pull(d.path, pv); - if ((try u.run_cmd(pv, &.{ "git", "checkout", vers.string })) > 0) { + try d.type.pull(gpa, d.path, pv); + if ((try u.run_cmd(gpa, pv, &.{ "git", "checkout", vers.string })) > 0) { u.fail("fetch: git: {s}: {s} {s} does not exist", .{ d.path, @tagName(vers.id), vers.string }); } if (builtin.os.tag != .windows and vers.id != .branch) { @@ -152,20 +152,20 @@ pub fn get_modpath(cachepath: string, d: zigmod.Dep, options: *CollectOptions) ! return pv; } if (!try u.does_folder_exist(p)) { - try d.type.pull(d.path, p); + try d.type.pull(gpa, d.path, p); } else { if (options.update) { - try d.type.update(p, d.path); + try d.type.update(gpa, p, d.path); } } return p; }, .hg => { if (!try u.does_folder_exist(p)) { - try d.type.pull(d.path, p); + try d.type.pull(gpa, d.path, p); } else { if (options.update) { - try d.type.update(p, d.path); + try d.type.update(gpa, p, d.path); } } return p; @@ -180,7 +180,7 @@ pub fn get_modpath(cachepath: string, d: zigmod.Dep, options: *CollectOptions) ! return pv; } const file_path = try std.fs.path.join(gpa, &.{ pv, file_name }); - try d.type.pull(d.path, pv); + try d.type.pull(gpa, d.path, pv); if (try u.validate_hash(d.version, file_path)) { try std.fs.cwd().deleteFile(file_path); return pv; @@ -193,7 +193,7 @@ pub fn get_modpath(cachepath: string, d: zigmod.Dep, options: *CollectOptions) ! try std.fs.cwd().deleteTree(p); } const file_path = try std.fs.path.join(gpa, &.{ p, file_name }); - try d.type.pull(d.path, p); + try d.type.pull(gpa, d.path, p); try std.fs.deleteFileAbsolute(file_path); return p; }, diff --git a/src/util/dep_type.zig b/src/util/dep_type.zig index d9dc51d2e05e2a0a011ffdf630dbbe77edb788d9..040bf6fbcc6e2d2ab6ffa6eefb0cf383b054cd7f 100644 --- a/src/util/dep_type.zig +++ b/src/util/dep_type.zig @@ -32,43 +32,43 @@ pub const DepType = enum { // hypercore, // https://hypercore-protocol.org/ // zig fmt: on - pub fn pull(self: DepType, rpath: string, dpath: string) !void { + pub fn pull(self: DepType, alloc: *std.mem.Allocator, rpath: string, dpath: string) !void { switch (self) { .local => {}, .system_lib => {}, .git => { - u.assert((try u.run_cmd(null, &.{ "git", "clone", "--recurse-submodules", rpath, dpath })) == 0, "git clone {s} failed", .{rpath}); + u.assert((try u.run_cmd(alloc, null, &.{ "git", "clone", "--recurse-submodules", rpath, dpath })) == 0, "git clone {s} failed", .{rpath}); }, .hg => { - u.assert((try u.run_cmd(null, &.{ "hg", "clone", rpath, dpath })) == 0, "hg clone {s} failed", .{rpath}); + u.assert((try u.run_cmd(alloc, null, &.{ "hg", "clone", rpath, dpath })) == 0, "hg clone {s} failed", .{rpath}); }, .http => { try std.fs.cwd().makePath(dpath); - u.assert((try u.run_cmd(dpath, &.{ "wget", rpath })) == 0, "wget {s} failed", .{rpath}); + u.assert((try u.run_cmd(alloc, dpath, &.{ "wget", rpath })) == 0, "wget {s} failed", .{rpath}); const f = rpath[std.mem.lastIndexOf(u8, rpath, "/").? + 1 ..]; if (std.mem.endsWith(u8, f, ".zip")) { - u.assert((try u.run_cmd(dpath, &.{ "unzip", f, "-d", "." })) == 0, "unzip {s} failed", .{f}); + u.assert((try u.run_cmd(alloc, dpath, &.{ "unzip", f, "-d", "." })) == 0, "unzip {s} failed", .{f}); } if (std.mem.endsWith(u8, f, ".tar") or std.mem.endsWith(u8, f, ".tar.gz") or std.mem.endsWith(u8, f, ".tar.xz") or std.mem.endsWith(u8, f, ".tar.zst")) { - u.assert((try u.run_cmd(dpath, &.{ "tar", "-xf", f, "-C", "." })) == 0, "un-tar {s} failed", .{f}); + u.assert((try u.run_cmd(alloc, dpath, &.{ "tar", "-xf", f, "-C", "." })) == 0, "un-tar {s} failed", .{f}); } }, } } // zig fmt: on - pub fn update(self: DepType, dpath: string, rpath: string) !void { + pub fn update(self: DepType, alloc: *std.mem.Allocator, dpath: string, rpath: string) !void { _ = rpath; switch (self) { .local => {}, .system_lib => {}, .git => { - u.assert((try u.run_cmd(dpath, &.{ "git", "fetch" })) == 0, "git fetch failed", .{}); - u.assert((try u.run_cmd(dpath, &.{ "git", "pull" })) == 0, "git pull failed", .{}); + u.assert((try u.run_cmd(alloc, dpath, &.{ "git", "fetch" })) == 0, "git fetch failed", .{}); + u.assert((try u.run_cmd(alloc, dpath, &.{ "git", "pull" })) == 0, "git pull failed", .{}); }, .hg => { - u.assert((try u.run_cmd(dpath, &.{ "hg", "pull" })) == 0, "hg pull failed", .{}); + u.assert((try u.run_cmd(alloc, dpath, &.{ "hg", "pull" })) == 0, "hg pull failed", .{}); }, .http => { // diff --git a/src/util/funcs.zig b/src/util/funcs.zig index 766e709648275e643805de4a9e64a03908fd4160..878df7ff97196f3e5331683ea2013b6e3be6e944 100644 --- a/src/util/funcs.zig +++ b/src/util/funcs.zig @@ -134,8 +134,8 @@ pub fn file_list(alloc: *std.mem.Allocator, dpath: string) ![]const string { return list.toOwnedSlice(); } -pub fn run_cmd_raw(dir: ?string, args: []const string) !std.ChildProcess.ExecResult { - return std.ChildProcess.exec(.{ .allocator = gpa, .cwd = dir, .argv = args, .max_output_bytes = std.math.maxInt(usize) }) catch |e| switch (e) { +pub fn run_cmd_raw(alloc: *std.mem.Allocator, dir: ?string, args: []const string) !std.ChildProcess.ExecResult { + return std.ChildProcess.exec(.{ .allocator = alloc, .cwd = dir, .argv = args, .max_output_bytes = std.math.maxInt(usize) }) catch |e| switch (e) { error.FileNotFound => { u.fail("\"{s}\" command not found", .{args[0]}); }, @@ -143,10 +143,10 @@ pub fn run_cmd_raw(dir: ?string, args: []const string) !std.ChildProcess.ExecRes }; } -pub fn run_cmd(dir: ?string, args: []const string) !u32 { - const result = try run_cmd_raw(dir, args); - gpa.free(result.stdout); - gpa.free(result.stderr); +pub fn run_cmd(alloc: *std.mem.Allocator, dir: ?string, args: []const string) !u32 { + const result = try run_cmd_raw(alloc, dir, args); + alloc.free(result.stdout); + alloc.free(result.stderr); return result.term.Exited; } -- 2.54.0