builtin/repack.c: introduce `struct write_pack_opts`

There are various functions within the 'repack' builtin which are responsible for writing different kinds of packs. They include: - `static int write_filtered_pack(...)` - `static int write_cruft_pack(...)` as well as the function `finish_pack_objects_cmd()`, which is responsible for finalizing a new pack write, and recording the checksum of its contents in the 'names' list. Both of these `write_` functions have a few things in common. They both take a pointer to the 'pack_objects_args' struct, as well as a pair of character pointers for `destination` and `pack_prefix`. Instead of repeating those arguments for each function, let's extract an options struct called "write_pack_opts" which has these three parameters as member fields. While we're at it, add fields for "packdir," and "packtmp", both of which are static variables within the builtin, and need to be read from within these two functions. This will shorten the list of parameters that callers have to provide to `write_filtered_pack()`, avoid ambiguity when passing multiple variables of the same type, and provide a unified interface for the two functions mentioned earlier. (Note that "pack_prefix" can be derived on the fly as a function of "packdir" and "packtmp", making it unnecessary to store "pack_prefix" explicitly. This commit ignores that potential cleanup in the name of doing as few things as possible, but a later commit will make that change.) Signed-off-by: Taylor Blau <me@ttaylorr.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Taylor Blau committed Oct 15, 2025 at 18:29 UTC 7a9c81a38ddb3b382103ccd45345c4892053fdfc
2 files changed +24 -14
builtin/repack.c
+16 -14
@@ -138,9 +138,7 @@ static int finish_pack_objects_cmd(const struct git_hash_algo *algop,
138 return finish_command(cmd);
139 }
140
141 -static int write_filtered_pack(const struct pack_objects_args *args,
142 - const char *destination,
143 - const char *pack_prefix,
141 +static int write_filtered_pack(const struct write_pack_opts *opts,
142 struct existing_packs *existing,
143 struct string_list *names)
144 {
@@ -150,9 +148,9 @@ static int write_filtered_pack(const struct pack_objects_args *args,
148 int ret;
149 const char *caret;
150 const char *scratch;
153 - int local = skip_prefix(destination, packdir, &scratch);
151 + int local = skip_prefix(opts->destination, opts->packdir, &scratch);
152
155 - prepare_pack_objects(&cmd, args, destination);
153 + prepare_pack_objects(&cmd, opts->po_args, opts->destination);
154
155 strvec_push(&cmd.args, "--stdin-packs");
156
@@ -175,7 +173,7 @@ static int write_filtered_pack(const struct pack_objects_args *args,
173 */
174 in = xfdopen(cmd.in, "w");
175 for_each_string_list_item(item, names)
178 - fprintf(in, "^%s-%s.pack\n", pack_prefix, item->string);
176 + fprintf(in, "^%s-%s.pack\n", opts->pack_prefix, item->string);
177 for_each_string_list_item(item, &existing->non_kept_packs)
178 fprintf(in, "%s.pack\n", item->string);
179 for_each_string_list_item(item, &existing->cruft_packs)
@@ -665,14 +663,18 @@ int cmd_repack(int argc,
663 }
664
665 if (po_args.filter_options.choice) {
668 - if (!filter_to)
669 - filter_to = packtmp;
670 -
671 - ret = write_filtered_pack(&po_args,
672 - filter_to,
673 - find_pack_prefix(packdir, packtmp),
674 - &existing,
675 - &names);
666 + struct write_pack_opts opts = {
667 + .po_args = &po_args,
668 + .destination = filter_to,
669 + .pack_prefix = find_pack_prefix(packdir, packtmp),
670 + .packdir = packdir,
671 + .packtmp = packtmp,
672 + };
673 +
674 + if (!opts.destination)
675 + opts.destination = packtmp;
676 +
677 + ret = write_filtered_pack(&opts, &existing, &names);
678 if (ret)
679 goto cleanup;
680 }
repack.h
+8
@@ -32,6 +32,14 @@ void pack_objects_args_release(struct pack_objects_args *args);
32 void repack_remove_redundant_pack(struct repository *repo, const char *dir_name,
33 const char *base_name);
34
35 +struct write_pack_opts {
36 + struct pack_objects_args *po_args;
37 + const char *destination;
38 + const char *pack_prefix;
39 + const char *packdir;
40 + const char *packtmp;
41 +};
42 +
43 struct repository;
44 struct packed_git;
45