authorgravatar for christofer@nolander.meChristofer Nolander <christofer@nolander.me> 2024-03-18 19:48:11+01:00
committergravatar for christofer@nolander.meChristofer Nolander <christofer@nolander.me> 2024-03-18 19:48:11+01:00
log57cfe0778c41565223ddc2abf4508a4ad43f1923
treeeb071e46f27499cdfd2290112bf5cf713eb46b21
parent8e7d9afdacef9d3a9443fc4b70cfe6aa229ecee5

`zig fetch`: resolve branch/tag names to commit SHA


2 files changed, 30 insertions(+), 2 deletions(-)

src/Package/Fetch.zig+9-1
......@@ -44,6 +44,8 @@ omit_missing_hash_error: bool,
4444/// which specifies inclusion rules. This is intended to be true for the first
4545/// fetch task and false for the recursive dependencies.
4646allow_missing_paths_field: bool,
47/// If true and URL points to a Git repository, will use the latest commit.
48use_latest_commit: bool,
4749
4850// Above this are fields provided as inputs to `run`.
4951// Below this are fields populated by `run`.
......@@ -59,6 +61,8 @@ actual_hash: Manifest.Digest,
5961has_build_zig: bool,
6062/// Indicates whether the task aborted due to an out-of-memory condition.
6163oom_flag: bool,
64/// If `use_latest_commit` was true, this will be the commit that was used.
65latest_commit: ?git.Oid,
6266
6367// This field is used by the CLI only, untouched by this file.
6468
......@@ -687,6 +691,7 @@ fn queueJobsForDeps(f: *Fetch) RunError!void {
687691 .job_queue = f.job_queue,
688692 .omit_missing_hash_error = false,
689693 .allow_missing_paths_field = true,
694 .use_latest_commit = false,
690695
691696 .package_root = undefined,
692697 .error_bundle = undefined,
......@@ -695,6 +700,7 @@ fn queueJobsForDeps(f: *Fetch) RunError!void {
695700 .actual_hash = undefined,
696701 .has_build_zig = false,
697702 .oom_flag = false,
703 .latest_commit = undefined,
698704
699705 .module = null,
700706 };
......@@ -987,7 +993,9 @@ fn initResource(f: *Fetch, uri: std.Uri, server_header_buffer: []u8) RunError!Re
987993 }
988994 return f.fail(f.location_tok, try eb.printString("ref not found: {s}", .{want_ref}));
989995 };
990 if (uri.fragment == null) {
996 if (f.use_latest_commit) {
997 f.latest_commit = want_oid;
998 } else if (uri.fragment == null) {
991999 const notes_len = 1;
9921000 try eb.addRootErrorMessage(.{
9931001 .msg = try eb.addString("url field is missing an explicit ref"),
src/main.zig+21-1
......@@ -5098,6 +5098,7 @@ fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !void {
50985098 .job_queue = &job_queue,
50995099 .omit_missing_hash_error = true,
51005100 .allow_missing_paths_field = false,
5101 .use_latest_commit = false,
51015102
51025103 .package_root = undefined,
51035104 .error_bundle = undefined,
......@@ -5106,6 +5107,7 @@ fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !void {
51065107 .actual_hash = undefined,
51075108 .has_build_zig = true,
51085109 .oom_flag = false,
5110 .latest_commit = undefined,
51095111
51105112 .module = build_mod,
51115113 };
......@@ -6757,6 +6759,7 @@ const usage_fetch =
67576759 \\ --debug-hash Print verbose hash information to stdout
67586760 \\ --save Add the fetched package to build.zig.zon
67596761 \\ --save=[name] Add the fetched package to build.zig.zon as name
6762 \\ --resolve-commit Before saving, replace the name of a Git branch/tag with its commit SHA
67606763 \\
67616764;
67626765
......@@ -6772,6 +6775,7 @@ fn cmdFetch(
67726775 var override_global_cache_dir: ?[]const u8 = try EnvVar.ZIG_GLOBAL_CACHE_DIR.get(arena);
67736776 var debug_hash: bool = false;
67746777 var save: union(enum) { no, yes, name: []const u8 } = .no;
6778 var resolve_commit: bool = false;
67756779
67766780 {
67776781 var i: usize = 0;
......@@ -6792,6 +6796,8 @@ fn cmdFetch(
67926796 save = .yes;
67936797 } else if (mem.startsWith(u8, arg, "--save=")) {
67946798 save = .{ .name = arg["--save=".len..] };
6799 } else if (mem.eql(u8, arg, "--resolve-commit")) {
6800 resolve_commit = true;
67956801 } else {
67966802 fatal("unrecognized parameter: '{s}'", .{arg});
67976803 }
......@@ -6803,6 +6809,9 @@ fn cmdFetch(
68036809 }
68046810 }
68056811
6812 if (resolve_commit and save == .no)
6813 warn("'--resolve-commit' has no effect unless used with '--save'", .{});
6814
68066815 const path_or_url = opt_path_or_url orelse fatal("missing url or path parameter", .{});
68076816
68086817 var thread_pool: ThreadPool = undefined;
......@@ -6851,6 +6860,7 @@ fn cmdFetch(
68516860 .job_queue = &job_queue,
68526861 .omit_missing_hash_error = true,
68536862 .allow_missing_paths_field = false,
6863 .use_latest_commit = resolve_commit,
68546864
68556865 .package_root = undefined,
68566866 .error_bundle = undefined,
......@@ -6859,6 +6869,7 @@ fn cmdFetch(
68596869 .actual_hash = undefined,
68606870 .has_build_zig = false,
68616871 .oom_flag = false,
6872 .latest_commit = undefined,
68626873
68636874 .module = null,
68646875 };
......@@ -6915,13 +6926,22 @@ fn cmdFetch(
69156926 var fixups: Ast.Fixups = .{};
69166927 defer fixups.deinit(gpa);
69176928
6929 const saved_path_or_url = if (resolve_commit) blk: {
6930 // replace the refspec with the latest commit SHA
6931 var uri = try std.Uri.parse(path_or_url);
6932 uri.fragment = try std.fmt.allocPrint(arena, "{}", .{
6933 std.fmt.fmtSliceHexLower(&fetch.latest_commit.?),
6934 });
6935 break :blk try std.fmt.allocPrint(arena, "{}", .{uri});
6936 } else path_or_url;
6937
69186938 const new_node_init = try std.fmt.allocPrint(arena,
69196939 \\.{{
69206940 \\ .url = "{}",
69216941 \\ .hash = "{}",
69226942 \\ }}
69236943 , .{
6924 std.zig.fmtEscapes(path_or_url),
6944 std.zig.fmtEscapes(saved_path_or_url),
69256945 std.zig.fmtEscapes(&hex_digest),
69266946 });
69276947