| ... | ... | @@ -5097,6 +5097,7 @@ fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !void { |
| 5097 | 5097 | .job_queue = &job_queue, |
| 5098 | 5098 | .omit_missing_hash_error = true, |
| 5099 | 5099 | .allow_missing_paths_field = false, |
| 5100 | .use_latest_commit = false, |
| 5100 | 5101 | |
| 5101 | 5102 | .package_root = undefined, |
| 5102 | 5103 | .error_bundle = undefined, |
| ... | ... | @@ -5105,6 +5106,7 @@ fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !void { |
| 5105 | 5106 | .actual_hash = undefined, |
| 5106 | 5107 | .has_build_zig = true, |
| 5107 | 5108 | .oom_flag = false, |
| 5109 | .latest_commit = undefined, |
| 5108 | 5110 | |
| 5109 | 5111 | .module = build_mod, |
| 5110 | 5112 | }; |
| ... | ... | @@ -6894,6 +6896,7 @@ const usage_fetch = |
| 6894 | 6896 | \\ --debug-hash Print verbose hash information to stdout |
| 6895 | 6897 | \\ --save Add the fetched package to build.zig.zon |
| 6896 | 6898 | \\ --save=[name] Add the fetched package to build.zig.zon as name |
| 6899 | \\ --preserve-url Store a verbatim copy of the URL in build.zig.zon |
| 6897 | 6900 | \\ |
| 6898 | 6901 | ; |
| 6899 | 6902 | |
| ... | ... | @@ -6909,6 +6912,7 @@ fn cmdFetch( |
| 6909 | 6912 | var override_global_cache_dir: ?[]const u8 = try EnvVar.ZIG_GLOBAL_CACHE_DIR.get(arena); |
| 6910 | 6913 | var debug_hash: bool = false; |
| 6911 | 6914 | var save: union(enum) { no, yes, name: []const u8 } = .no; |
| 6915 | var preserve_url: bool = false; |
| 6912 | 6916 | |
| 6913 | 6917 | { |
| 6914 | 6918 | var i: usize = 0; |
| ... | ... | @@ -6929,6 +6933,8 @@ fn cmdFetch( |
| 6929 | 6933 | save = .yes; |
| 6930 | 6934 | } else if (mem.startsWith(u8, arg, "--save=")) { |
| 6931 | 6935 | save = .{ .name = arg["--save=".len..] }; |
| 6936 | } else if (mem.startsWith(u8, arg, "--preserve-url")) { |
| 6937 | preserve_url = true; |
| 6932 | 6938 | } else { |
| 6933 | 6939 | fatal("unrecognized parameter: '{s}'", .{arg}); |
| 6934 | 6940 | } |
| ... | ... | @@ -6940,6 +6946,8 @@ fn cmdFetch( |
| 6940 | 6946 | } |
| 6941 | 6947 | } |
| 6942 | 6948 | |
| 6949 | if (preserve_url and save == .no) fatal("use of '--preserve-url' requires '--save'", .{}); |
| 6950 | |
| 6943 | 6951 | const path_or_url = opt_path_or_url orelse fatal("missing url or path parameter", .{}); |
| 6944 | 6952 | |
| 6945 | 6953 | var thread_pool: ThreadPool = undefined; |
| ... | ... | @@ -6988,6 +6996,7 @@ fn cmdFetch( |
| 6988 | 6996 | .job_queue = &job_queue, |
| 6989 | 6997 | .omit_missing_hash_error = true, |
| 6990 | 6998 | .allow_missing_paths_field = false, |
| 6999 | .use_latest_commit = true, |
| 6991 | 7000 | |
| 6992 | 7001 | .package_root = undefined, |
| 6993 | 7002 | .error_bundle = undefined, |
| ... | ... | @@ -6996,6 +7005,7 @@ fn cmdFetch( |
| 6996 | 7005 | .actual_hash = undefined, |
| 6997 | 7006 | .has_build_zig = false, |
| 6998 | 7007 | .oom_flag = false, |
| 7008 | .latest_commit = undefined, |
| 6999 | 7009 | |
| 7000 | 7010 | .module = null, |
| 7001 | 7011 | }; |
| ... | ... | @@ -7052,13 +7062,43 @@ fn cmdFetch( |
| 7052 | 7062 | var fixups: Ast.Fixups = .{}; |
| 7053 | 7063 | defer fixups.deinit(gpa); |
| 7054 | 7064 | |
| 7065 | var saved_path_or_url = path_or_url; |
| 7066 | |
| 7067 | if (fetch.latest_commit) |*latest_commit| { |
| 7068 | var uri = try std.Uri.parse(path_or_url); |
| 7069 | const target_ref = uri.fragment orelse ""; |
| 7070 | if (!std.mem.eql(u8, target_ref, latest_commit)) { |
| 7071 | std.log.info("resolved ref '{s}' to commit {s}", .{ |
| 7072 | target_ref, |
| 7073 | std.fmt.fmtSliceHexLower(latest_commit), |
| 7074 | }); |
| 7075 | |
| 7076 | if (!preserve_url) { |
| 7077 | if (target_ref.len != 0) { |
| 7078 | // include the target ref in a query parameter |
| 7079 | var query = try std.ArrayList(u8).initCapacity(arena, 4 + target_ref.len); |
| 7080 | try std.Uri.writeEscapedQuery(query.writer(), "ref="); |
| 7081 | try std.Uri.writeEscapedQuery(query.writer(), target_ref); |
| 7082 | uri.query = try query.toOwnedSlice(); |
| 7083 | } |
| 7084 | |
| 7085 | // replace the refspec with the resolved commit SHA |
| 7086 | uri.fragment = try std.fmt.allocPrint(arena, "{}", .{ |
| 7087 | std.fmt.fmtSliceHexLower(latest_commit), |
| 7088 | }); |
| 7089 | |
| 7090 | saved_path_or_url = try std.fmt.allocPrint(arena, "{}", .{uri}); |
| 7091 | } |
| 7092 | } |
| 7093 | } |
| 7094 | |
| 7055 | 7095 | const new_node_init = try std.fmt.allocPrint(arena, |
| 7056 | 7096 | \\.{{ |
| 7057 | 7097 | \\ .url = "{}", |
| 7058 | 7098 | \\ .hash = "{}", |
| 7059 | 7099 | \\ }} |
| 7060 | 7100 | , .{ |
| 7061 | | std.zig.fmtEscapes(path_or_url), |
| 7101 | std.zig.fmtEscapes(saved_path_or_url), |
| 7062 | 7102 | std.zig.fmtEscapes(&hex_digest), |
| 7063 | 7103 | }); |
| 7064 | 7104 | |
| ... | ... | @@ -7078,7 +7118,7 @@ fn cmdFetch( |
| 7078 | 7118 | if (dep.hash) |h| { |
| 7079 | 7119 | switch (dep.location) { |
| 7080 | 7120 | .url => |u| { |
| 7081 | | if (mem.eql(u8, h, &hex_digest) and mem.eql(u8, u, path_or_url)) { |
| 7121 | if (mem.eql(u8, h, &hex_digest) and mem.eql(u8, u, saved_path_or_url)) { |
| 7082 | 7122 | std.log.info("existing dependency named '{s}' is up-to-date", .{name}); |
| 7083 | 7123 | process.exit(0); |
| 7084 | 7124 | } |