]> git.ipfire.org Git - thirdparty/git.git/commitdiff
builtin/repack.c: introduce `struct write_pack_opts`
authorTaylor Blau <me@ttaylorr.com>
Wed, 15 Oct 2025 22:29:16 +0000 (18:29 -0400)
committerJunio C Hamano <gitster@pobox.com>
Thu, 16 Oct 2025 17:08:56 +0000 (10:08 -0700)
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>
builtin/repack.c
repack.h

index 2f49a18283535c4f0de4412eb421478d8a92d2f6..45ce46989895dd2552bd47aa1ee1308c80a6b4fb 100644 (file)
@@ -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(const 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;
-
-               ret = write_filtered_pack(&po_args,
-                                         filter_to,
-                                         find_pack_prefix(packdir, packtmp),
-                                         &existing,
-                                         &names);
+               struct write_pack_opts opts = {
+                       .po_args = &po_args,
+                       .destination = filter_to,
+                       .pack_prefix = find_pack_prefix(packdir, packtmp),
+                       .packdir = packdir,
+                       .packtmp = packtmp,
+               };
+
+               if (!opts.destination)
+                       opts.destination = packtmp;
+
+               ret = write_filtered_pack(&opts, &existing, &names);
                if (ret)
                        goto cleanup;
        }
index 25a31ac0a05fe26f13fd68a38032f0276aefe065..6ef503f62376c5eb413212b7f29708f0d36ef07b 100644 (file)
--- 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;