[PATCH 40/49] builtin/repack.c: introduce `struct write_pack_opts`

Taylor Blau
2025-09-28T22:09:56Z

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.

Signed-off-by: Taylor Blau <me@ttaylorr.com>
---
builtin/repack.c | 28 +++++++++++++++-------------
repack.h | 8 ++++++++
2 files changed, 23 insertions(+), 13 deletions(-)

diff --git a/builtin/repack.c b/builtin/repack.c
index 5fed79e826..6df7c88085 100644
--- a/builtin/repack.c
+++ b/builtin/repack.c
@@ -138,9 +138,7 @@ static int finish_pack_objects_cmd(const struct git_hash_algo *algop,
return finish_command(cmd);
}

-static int write_filtered_pack(const struct pack_objects_args *args,
- const char *destination,
- const char *pack_prefix,
+static int write_filtered_pack(struct write_pack_opts *opts,
struct existing_packs *existing,
struct string_list *names)
{
@@ -150,9 +148,9 @@ static int write_filtered_pack(const struct pack_objects_args *args,
int ret;
const char *caret;
const char *scratch;
- int local = skip_prefix(destination, packdir, &scratch);
+ int local = skip_prefix(opts->destination, opts->packdir, &scratch);

- prepare_pack_objects(&cmd, args, destination);
+ prepare_pack_objects(&cmd, opts->po_args, opts->destination);

strvec_push(&cmd.args, "--stdin-packs");

@@ -175,7 +173,7 @@ static int write_filtered_pack(const struct pack_objects_args *args,
*/
in = xfdopen(cmd.in, "w");
for_each_string_list_item(item, names)
- fprintf(in, "^%s-%s.pack\n", pack_prefix, item->string);
+ fprintf(in, "^%s-%s.pack\n", opts->pack_prefix, item->string);
for_each_string_list_item(item, &existing->non_kept_packs)
fprintf(in, "%s.pack\n", item->string);
for_each_string_list_item(item, &existing->cruft_packs)
@@ -665,14 +663,18 @@ int cmd_repack(int argc,
}

if (po_args.filter_options.choice) {
- if (!filter_to)
- filter_to = packtmp;
+ struct write_pack_opts opts = {
+ .po_args = &po_args,
+ .destination = filter_to,
+ .pack_prefix = find_pack_prefix(packdir, packtmp),
+ .packdir = packdir,
+ .packtmp = packtmp,
+ };

- ret = write_filtered_pack(&po_args,
- filter_to,
- find_pack_prefix(packdir, packtmp),
- &existing,
- &names);
+ if (!opts.destination)
+ opts.destination = packtmp;
+
+ ret = write_filtered_pack(&opts, &existing, &names);
if (ret)
goto cleanup;
}
diff --git a/repack.h b/repack.h
index 25a31ac0a0..6ef503f623 100644
--- a/repack.h
+++ b/repack.h
@@ -32,6 +32,14 @@ void pack_objects_args_release(struct pack_objects_args *args);
void repack_remove_redundant_pack(struct repository *repo, const char *dir_name,
const char *base_name);

+struct write_pack_opts {
+ struct pack_objects_args *po_args;
+ const char *destination;
+ const char *pack_prefix;
+ const char *packdir;
+ const char *packtmp;
+};
+
struct repository;
struct packed_git;

--
2.51.0.243.g16eca91f2c0