Skip to content

Commit 2f79c79

Browse files
ttaylorrgitster
authored andcommitted
repack: extract write_pack_opts_is_local()
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 98fa0d5 commit 2f79c79

File tree

3 files changed

+8
-4
lines changed

3 files changed

+8
-4
lines changed

builtin/repack.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,7 @@ static int write_filtered_pack(const struct write_pack_opts *opts,
147147
FILE *in;
148148
int ret;
149149
const char *caret;
150-
const char *scratch;
151-
int local = skip_prefix(opts->destination, opts->packdir, &scratch);
150+
bool local = write_pack_opts_is_local(opts);
152151
const char *pack_prefix = write_pack_opts_pack_prefix(opts);
153152

154153
prepare_pack_objects(&cmd, opts->po_args, opts->destination);
@@ -232,8 +231,7 @@ static int write_cruft_pack(const struct write_pack_opts *opts,
232231
struct string_list_item *item;
233232
FILE *in;
234233
int ret;
235-
const char *scratch;
236-
int local = skip_prefix(opts->destination, opts->packdir, &scratch);
234+
bool local = write_pack_opts_is_local(opts);
237235
const char *pack_prefix = write_pack_opts_pack_prefix(opts);
238236

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

repack.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,11 @@ const char *write_pack_opts_pack_prefix(const struct write_pack_opts *opts)
7777
return pack_prefix;
7878
}
7979

80+
bool write_pack_opts_is_local(const struct write_pack_opts *opts)
81+
{
82+
return starts_with(opts->destination, opts->packdir);
83+
}
84+
8085
#define DELETE_PACK 1
8186
#define RETAIN_PACK 2
8287

repack.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ struct write_pack_opts {
4040
};
4141

4242
const char *write_pack_opts_pack_prefix(const struct write_pack_opts *opts);
43+
bool write_pack_opts_is_local(const struct write_pack_opts *opts);
4344

4445
struct repository;
4546
struct packed_git;

0 commit comments

Comments
 (0)