[PATCH 43/49] repack: extract `write_pack_opts_is_local()`

Taylor Blau
2025-09-28T22:10:12Z

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()`.

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>
---
builtin/repack.c | 6 ++----
repack.c | 6 ++++++
repack.h | 1 +
3 files changed, 9 insertions(+), 4 deletions(-)

diff --git a/builtin/repack.c b/builtin/repack.c
index 4d86920618..be8e6689fc 100644
--- a/builtin/repack.c
+++ b/builtin/repack.c
@@ -147,8 +147,7 @@ static int write_filtered_pack(struct write_pack_opts *opts,
FILE *in;
int ret;
const char *caret;
- const char *scratch;
- int local = skip_prefix(opts->destination, opts->packdir, &scratch);
+ int 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(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);
+ int 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);
diff --git a/repack.c b/repack.c
index c4326a532d..7af297ae48 100644
--- a/repack.c
+++ b/repack.c
@@ -77,6 +77,12 @@ const char *write_pack_opts_pack_prefix(struct write_pack_opts *opts)
return pack_prefix;
}

+int write_pack_opts_is_local(struct write_pack_opts *opts)
+{
+ const char *scratch;
+ return skip_prefix(opts->destination, opts->packdir, &scratch);
+}
+
#define DELETE_PACK 1
#define RETAIN_PACK 2

diff --git a/repack.h b/repack.h
index 46d2312fa9..16f2de2ea9 100644
--- a/repack.h
+++ b/repack.h
@@ -40,6 +40,7 @@ struct write_pack_opts {
};

const char *write_pack_opts_pack_prefix(struct write_pack_opts *opts);
+int write_pack_opts_is_local(struct write_pack_opts *opts);

struct repository;
struct packed_git;
--
2.51.0.243.g16eca91f2c0