authorgravatar for christofer@nolander.meChristofer Nolander <christofer@nolander.me> 2024-03-21 07:54:08+01:00
committergravatar for christofer@nolander.meChristofer Nolander <christofer@nolander.me> 2024-03-21 07:54:08+01:00
logdcffa7b2990046926aae782819a53dcd505aa4a3
treec5b210c306d627f52ba5a373650c1d3f2a06deb5
parent57cfe0778c41565223ddc2abf4508a4ad43f1923

`zig fetch`: resolve ref to commit by default

Stores the original ref as a query parameter in the URL so that it is possible to automatically check the upstream if there are any newer commits. Also adds a flag which opts-out of the new behaivour, restoring the old.

1 files changed, 35 insertions(+), 15 deletions(-)

src/main.zig+35-15
......@@ -6759,7 +6759,7 @@ const usage_fetch =
67596759 \\ --debug-hash Print verbose hash information to stdout
67606760 \\ --save Add the fetched package to build.zig.zon
67616761 \\ --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
6762 \\ --preserve-url Store a verbatim copy of the URL in build.zig.zon
67636763 \\
67646764;
67656765
......@@ -6775,7 +6775,7 @@ fn cmdFetch(
67756775 var override_global_cache_dir: ?[]const u8 = try EnvVar.ZIG_GLOBAL_CACHE_DIR.get(arena);
67766776 var debug_hash: bool = false;
67776777 var save: union(enum) { no, yes, name: []const u8 } = .no;
6778 var resolve_commit: bool = false;
6778 var preserve_url: bool = false;
67796779
67806780 {
67816781 var i: usize = 0;
......@@ -6796,8 +6796,8 @@ fn cmdFetch(
67966796 save = .yes;
67976797 } else if (mem.startsWith(u8, arg, "--save=")) {
67986798 save = .{ .name = arg["--save=".len..] };
6799 } else if (mem.eql(u8, arg, "--resolve-commit")) {
6800 resolve_commit = true;
6799 } else if (mem.startsWith(u8, arg, "--preserve-url")) {
6800 preserve_url = true;
68016801 } else {
68026802 fatal("unrecognized parameter: '{s}'", .{arg});
68036803 }
......@@ -6809,8 +6809,7 @@ fn cmdFetch(
68096809 }
68106810 }
68116811
6812 if (resolve_commit and save == .no)
6813 warn("'--resolve-commit' has no effect unless used with '--save'", .{});
6812 if (preserve_url and save == .no) fatal("use of '--preserve-url' requires '--save'", .{});
68146813
68156814 const path_or_url = opt_path_or_url orelse fatal("missing url or path parameter", .{});
68166815
......@@ -6860,7 +6859,7 @@ fn cmdFetch(
68606859 .job_queue = &job_queue,
68616860 .omit_missing_hash_error = true,
68626861 .allow_missing_paths_field = false,
6863 .use_latest_commit = resolve_commit,
6862 .use_latest_commit = true,
68646863
68656864 .package_root = undefined,
68666865 .error_bundle = undefined,
......@@ -6926,14 +6925,35 @@ fn cmdFetch(
69266925 var fixups: Ast.Fixups = .{};
69276926 defer fixups.deinit(gpa);
69286927
6929 const saved_path_or_url = if (resolve_commit) blk: {
6930 // replace the refspec with the latest commit SHA
6928 var saved_path_or_url = path_or_url;
6929
6930 if (fetch.latest_commit) |*latest_commit| {
69316931 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;
6932 const target_ref = uri.fragment orelse "";
6933 if (!std.mem.eql(u8, target_ref, latest_commit)) {
6934 std.log.info("resolved ref '{s}' to commit {s}", .{
6935 target_ref,
6936 std.fmt.fmtSliceHexLower(latest_commit),
6937 });
6938
6939 if (!preserve_url) {
6940 if (target_ref.len != 0) {
6941 // include the target ref in a query parameter
6942 var query = try std.ArrayList(u8).initCapacity(arena, 4 + target_ref.len);
6943 try std.Uri.writeEscapedQuery(query.writer(), "ref=");
6944 try std.Uri.writeEscapedQuery(query.writer(), target_ref);
6945 uri.query = try query.toOwnedSlice();
6946 }
6947
6948 // replace the refspec with the resolved commit SHA
6949 uri.fragment = try std.fmt.allocPrint(arena, "{}", .{
6950 std.fmt.fmtSliceHexLower(latest_commit),
6951 });
6952
6953 saved_path_or_url = try std.fmt.allocPrint(arena, "{}", .{uri});
6954 }
6955 }
6956 }
69376957
69386958 const new_node_init = try std.fmt.allocPrint(arena,
69396959 \\.{{
......@@ -6961,7 +6981,7 @@ fn cmdFetch(
69616981 if (dep.hash) |h| {
69626982 switch (dep.location) {
69636983 .url => |u| {
6964 if (mem.eql(u8, h, &hex_digest) and mem.eql(u8, u, path_or_url)) {
6984 if (mem.eql(u8, h, &hex_digest) and mem.eql(u8, u, saved_path_or_url)) {
69656985 std.log.info("existing dependency named '{s}' is up-to-date", .{name});
69666986 process.exit(0);
69676987 }