]> git.ipfire.org Git - thirdparty/git.git/commitdiff
repack: extract `write_pack_opts_is_local()`
authorTaylor Blau <me@ttaylorr.com>
Wed, 15 Oct 2025 22:29:24 +0000 (18:29 -0400)
committerJunio C Hamano <gitster@pobox.com>
Thu, 16 Oct 2025 17:08:56 +0000 (10:08 -0700)
Similar to the previous commit, the functions `write_cruft_pack()` and
`write_filtered_pack()` both compute a "local" variable via the exact
same mechanism:

    const char *scratch;
    int local = skip_prefix(opts->destination, opts->packdir, &scratch);

Not only does this cause us to repeat the same pair of lines, it also
introduces an unnecessary "scratch" variable that is common between both
functions.

Instead of repeating ourselves, let's extract that functionality into a
new function in the repack.h API called "write_pack_opts_is_local()".
That function takes a pointer to a "struct write_pack_opts" (which has
as fields both "destination" and "packdir"), and can encapsulate the
dangling "scratch" field.

Extract that function and make it visible within the repack.h API, and
use it within both `write_cruft_pack()` and `write_filtered_pack()`.
While we're at it, match our modern conventions by returning a "bool"
instead of "int", and use `starts_with()` instead of `skip_prefix()` to
avoid storing the dummy "scratch" variable.

The remaining duplication (that is, that both `write_cruft_pack()` and
`write_filtered_pack()` still both call `write_pack_opts_is_local()`)
will be addressed in the following commit.

Signed-off-by: Taylor Blau <me@ttaylorr.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/repack.c
repack.c
repack.h

index b21799c650e0e3301080ceb4a5a58b5d9469a25c..d1449cfe13717ed823c8031ee8a4d061b4f55758 100644 (file)
@@ -147,8 +147,7 @@ static int write_filtered_pack(const struct write_pack_opts *opts,
        FILE *in;
        int ret;
        const char *caret;
-       const char *scratch;
-       int local = skip_prefix(opts->destination, opts->packdir, &scratch);
+       bool local = write_pack_opts_is_local(opts);
        const char *pack_prefix = write_pack_opts_pack_prefix(opts);
 
        prepare_pack_objects(&cmd, opts->po_args, opts->destination);
@@ -232,8 +231,7 @@ static int write_cruft_pack(const struct write_pack_opts *opts,
        struct string_list_item *item;
        FILE *in;
        int ret;
-       const char *scratch;
-       int local = skip_prefix(opts->destination, opts->packdir, &scratch);
+       bool local = write_pack_opts_is_local(opts);
        const char *pack_prefix = write_pack_opts_pack_prefix(opts);
 
        prepare_pack_objects(&cmd, opts->po_args, opts->destination);
index 19fd1d6d5ba96a73d0071a245bded2888b77bb79..d2ee9f2460f3a3de901a2e88da568cc78cacb8c7 100644 (file)
--- a/repack.c
+++ b/repack.c
@@ -77,6 +77,11 @@ const char *write_pack_opts_pack_prefix(const struct write_pack_opts *opts)
        return pack_prefix;
 }
 
+bool write_pack_opts_is_local(const struct write_pack_opts *opts)
+{
+       return starts_with(opts->destination, opts->packdir);
+}
+
 #define DELETE_PACK 1
 #define RETAIN_PACK 2
 
index 5852e2407f8da5331cf2be3b05d3f12055f4e949..26d1954ae28dddf5036780684db4a689e545504e 100644 (file)
--- a/repack.h
+++ b/repack.h
@@ -40,6 +40,7 @@ struct write_pack_opts {
 };
 
 const char *write_pack_opts_pack_prefix(const struct write_pack_opts *opts);
+bool write_pack_opts_is_local(const struct write_pack_opts *opts);
 
 struct repository;
 struct packed_git;