Skip to content

Commit e2b4383

Browse files
ttaylorrgitster
authored andcommitted
builtin/repack.c: extract structure to store existing packs
The repack machinery needs to keep track of which packfiles were present in the repository at the beginning of a repack, segmented by whether or not each pack is marked as kept. The names of these packs are stored in two `string_list`s, corresponding to kept- and non-kept packs, respectively. As a consequence, many functions within the repack code need to take both `string_list`s as arguments, leading to code like this: ret = write_cruft_pack(&cruft_po_args, packtmp, pack_prefix, cruft_expiration, &names, &existing_nonkept_packs, /* <- */ &existing_kept_packs); /* <- */ Wrap up this pair of `string_list`s into a single structure that stores both. This saves us from having to pass both string lists separately, and prepares for adding additional fields to this structure. Signed-off-by: Taylor Blau <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 1fc548b commit e2b4383

File tree

1 file changed

+49
-41
lines changed

1 file changed

+49
-41
lines changed

builtin/repack.c

Lines changed: 49 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -95,14 +95,29 @@ static int repack_config(const char *var, const char *value,
9595
return git_default_config(var, value, ctx, cb);
9696
}
9797

98+
struct existing_packs {
99+
struct string_list kept_packs;
100+
struct string_list non_kept_packs;
101+
};
102+
103+
#define EXISTING_PACKS_INIT { \
104+
.kept_packs = STRING_LIST_INIT_DUP, \
105+
.non_kept_packs = STRING_LIST_INIT_DUP, \
106+
}
107+
108+
static void existing_packs_release(struct existing_packs *existing)
109+
{
110+
string_list_clear(&existing->kept_packs, 0);
111+
string_list_clear(&existing->non_kept_packs, 0);
112+
}
113+
98114
/*
99-
* Adds all packs hex strings (pack-$HASH) to either fname_nonkept_list
100-
* or fname_kept_list based on whether each pack has a corresponding
115+
* Adds all packs hex strings (pack-$HASH) to either packs->non_kept
116+
* or packs->kept based on whether each pack has a corresponding
101117
* .keep file or not. Packs without a .keep file are not to be kept
102118
* if we are going to pack everything into one file.
103119
*/
104-
static void collect_pack_filenames(struct string_list *fname_nonkept_list,
105-
struct string_list *fname_kept_list,
120+
static void collect_pack_filenames(struct existing_packs *existing,
106121
const struct string_list *extra_keep)
107122
{
108123
struct packed_git *p;
@@ -126,16 +141,16 @@ static void collect_pack_filenames(struct string_list *fname_nonkept_list,
126141
strbuf_strip_suffix(&buf, ".pack");
127142

128143
if ((extra_keep->nr > 0 && i < extra_keep->nr) || p->pack_keep)
129-
string_list_append(fname_kept_list, buf.buf);
144+
string_list_append(&existing->kept_packs, buf.buf);
130145
else {
131146
struct string_list_item *item;
132-
item = string_list_append(fname_nonkept_list, buf.buf);
147+
item = string_list_append(&existing->non_kept_packs, buf.buf);
133148
if (p->is_cruft)
134149
item->util = (void*)(uintptr_t)CRUFT_PACK;
135150
}
136151
}
137152

138-
string_list_sort(fname_kept_list);
153+
string_list_sort(&existing->kept_packs);
139154
strbuf_release(&buf);
140155
}
141156

@@ -327,7 +342,7 @@ static int geometry_cmp(const void *va, const void *vb)
327342
}
328343

329344
static void init_pack_geometry(struct pack_geometry *geometry,
330-
struct string_list *existing_kept_packs,
345+
struct existing_packs *existing,
331346
const struct pack_objects_args *args)
332347
{
333348
struct packed_git *p;
@@ -344,23 +359,24 @@ static void init_pack_geometry(struct pack_geometry *geometry,
344359

345360
if (!pack_kept_objects) {
346361
/*
347-
* Any pack that has its pack_keep bit set will appear
348-
* in existing_kept_packs below, but this saves us from
349-
* doing a more expensive check.
362+
* Any pack that has its pack_keep bit set will
363+
* appear in existing->kept_packs below, but
364+
* this saves us from doing a more expensive
365+
* check.
350366
*/
351367
if (p->pack_keep)
352368
continue;
353369

354370
/*
355-
* The pack may be kept via the --keep-pack option;
356-
* check 'existing_kept_packs' to determine whether to
357-
* ignore it.
371+
* The pack may be kept via the --keep-pack
372+
* option; check 'existing->kept_packs' to
373+
* determine whether to ignore it.
358374
*/
359375
strbuf_reset(&buf);
360376
strbuf_addstr(&buf, pack_basename(p));
361377
strbuf_strip_suffix(&buf, ".pack");
362378

363-
if (string_list_has_string(existing_kept_packs, buf.buf))
379+
if (string_list_has_string(&existing->kept_packs, buf.buf))
364380
continue;
365381
}
366382
if (p->is_cruft)
@@ -565,14 +581,13 @@ static void midx_snapshot_refs(struct tempfile *f)
565581
}
566582

567583
static void midx_included_packs(struct string_list *include,
568-
struct string_list *existing_nonkept_packs,
569-
struct string_list *existing_kept_packs,
584+
struct existing_packs *existing,
570585
struct string_list *names,
571586
struct pack_geometry *geometry)
572587
{
573588
struct string_list_item *item;
574589

575-
for_each_string_list_item(item, existing_kept_packs)
590+
for_each_string_list_item(item, &existing->kept_packs)
576591
string_list_insert(include, xstrfmt("%s.idx", item->string));
577592
for_each_string_list_item(item, names)
578593
string_list_insert(include, xstrfmt("pack-%s.idx", item->string));
@@ -600,7 +615,7 @@ static void midx_included_packs(struct string_list *include,
600615
string_list_insert(include, strbuf_detach(&buf, NULL));
601616
}
602617

603-
for_each_string_list_item(item, existing_nonkept_packs) {
618+
for_each_string_list_item(item, &existing->non_kept_packs) {
604619
if (!((uintptr_t)item->util & CRUFT_PACK)) {
605620
/*
606621
* no need to check DELETE_PACK, since we're not
@@ -611,7 +626,7 @@ static void midx_included_packs(struct string_list *include,
611626
string_list_insert(include, xstrfmt("%s.idx", item->string));
612627
}
613628
} else {
614-
for_each_string_list_item(item, existing_nonkept_packs) {
629+
for_each_string_list_item(item, &existing->non_kept_packs) {
615630
if ((uintptr_t)item->util & DELETE_PACK)
616631
continue;
617632
string_list_insert(include, xstrfmt("%s.idx", item->string));
@@ -700,8 +715,7 @@ static int write_cruft_pack(const struct pack_objects_args *args,
700715
const char *pack_prefix,
701716
const char *cruft_expiration,
702717
struct string_list *names,
703-
struct string_list *existing_packs,
704-
struct string_list *existing_kept_packs)
718+
struct existing_packs *existing)
705719
{
706720
struct child_process cmd = CHILD_PROCESS_INIT;
707721
struct strbuf line = STRBUF_INIT;
@@ -744,9 +758,9 @@ static int write_cruft_pack(const struct pack_objects_args *args,
744758
in = xfdopen(cmd.in, "w");
745759
for_each_string_list_item(item, names)
746760
fprintf(in, "%s-%s.pack\n", pack_prefix, item->string);
747-
for_each_string_list_item(item, existing_packs)
761+
for_each_string_list_item(item, &existing->non_kept_packs)
748762
fprintf(in, "-%s.pack\n", item->string);
749-
for_each_string_list_item(item, existing_kept_packs)
763+
for_each_string_list_item(item, &existing->kept_packs)
750764
fprintf(in, "%s.pack\n", item->string);
751765
fclose(in);
752766

@@ -778,8 +792,7 @@ int cmd_repack(int argc, const char **argv, const char *prefix)
778792
struct child_process cmd = CHILD_PROCESS_INIT;
779793
struct string_list_item *item;
780794
struct string_list names = STRING_LIST_INIT_DUP;
781-
struct string_list existing_nonkept_packs = STRING_LIST_INIT_DUP;
782-
struct string_list existing_kept_packs = STRING_LIST_INIT_DUP;
795+
struct existing_packs existing = EXISTING_PACKS_INIT;
783796
struct pack_geometry geometry = { 0 };
784797
struct strbuf line = STRBUF_INIT;
785798
struct tempfile *refs_snapshot = NULL;
@@ -915,13 +928,12 @@ int cmd_repack(int argc, const char **argv, const char *prefix)
915928
packtmp_name = xstrfmt(".tmp-%d-pack", (int)getpid());
916929
packtmp = mkpathdup("%s/%s", packdir, packtmp_name);
917930

918-
collect_pack_filenames(&existing_nonkept_packs, &existing_kept_packs,
919-
&keep_pack_list);
931+
collect_pack_filenames(&existing, &keep_pack_list);
920932

921933
if (geometry.split_factor) {
922934
if (pack_everything)
923935
die(_("options '%s' and '%s' cannot be used together"), "--geometric", "-A/-a");
924-
init_pack_geometry(&geometry, &existing_kept_packs, &po_args);
936+
init_pack_geometry(&geometry, &existing, &po_args);
925937
split_pack_geometry(&geometry);
926938
}
927939

@@ -965,7 +977,7 @@ int cmd_repack(int argc, const char **argv, const char *prefix)
965977
if (pack_everything & ALL_INTO_ONE) {
966978
repack_promisor_objects(&po_args, &names);
967979

968-
if (existing_nonkept_packs.nr && delete_redundant &&
980+
if (existing.non_kept_packs.nr && delete_redundant &&
969981
!(pack_everything & PACK_CRUFT)) {
970982
for_each_string_list_item(item, &names) {
971983
strvec_pushf(&cmd.args, "--keep-pack=%s-%s.pack",
@@ -1054,8 +1066,7 @@ int cmd_repack(int argc, const char **argv, const char *prefix)
10541066

10551067
ret = write_cruft_pack(&cruft_po_args, packtmp, pack_prefix,
10561068
cruft_expiration, &names,
1057-
&existing_nonkept_packs,
1058-
&existing_kept_packs);
1069+
&existing);
10591070
if (ret)
10601071
goto cleanup;
10611072

@@ -1086,8 +1097,7 @@ int cmd_repack(int argc, const char **argv, const char *prefix)
10861097
pack_prefix,
10871098
NULL,
10881099
&names,
1089-
&existing_nonkept_packs,
1090-
&existing_kept_packs);
1100+
&existing);
10911101
if (ret)
10921102
goto cleanup;
10931103
}
@@ -1133,7 +1143,7 @@ int cmd_repack(int argc, const char **argv, const char *prefix)
11331143

11341144
if (delete_redundant && pack_everything & ALL_INTO_ONE) {
11351145
const int hexsz = the_hash_algo->hexsz;
1136-
for_each_string_list_item(item, &existing_nonkept_packs) {
1146+
for_each_string_list_item(item, &existing.non_kept_packs) {
11371147
char *sha1;
11381148
size_t len = strlen(item->string);
11391149
if (len < hexsz)
@@ -1152,8 +1162,7 @@ int cmd_repack(int argc, const char **argv, const char *prefix)
11521162

11531163
if (write_midx) {
11541164
struct string_list include = STRING_LIST_INIT_NODUP;
1155-
midx_included_packs(&include, &existing_nonkept_packs,
1156-
&existing_kept_packs, &names, &geometry);
1165+
midx_included_packs(&include, &existing, &names, &geometry);
11571166

11581167
ret = write_midx_included_packs(&include, &geometry,
11591168
refs_snapshot ? get_tempfile_path(refs_snapshot) : NULL,
@@ -1172,7 +1181,7 @@ int cmd_repack(int argc, const char **argv, const char *prefix)
11721181

11731182
if (delete_redundant) {
11741183
int opts = 0;
1175-
for_each_string_list_item(item, &existing_nonkept_packs) {
1184+
for_each_string_list_item(item, &existing.non_kept_packs) {
11761185
if (!((uintptr_t)item->util & DELETE_PACK))
11771186
continue;
11781187
remove_redundant_pack(packdir, item->string);
@@ -1193,7 +1202,7 @@ int cmd_repack(int argc, const char **argv, const char *prefix)
11931202
strbuf_strip_suffix(&buf, ".pack");
11941203

11951204
if ((p->pack_keep) ||
1196-
(string_list_has_string(&existing_kept_packs,
1205+
(string_list_has_string(&existing.kept_packs,
11971206
buf.buf)))
11981207
continue;
11991208

@@ -1224,8 +1233,7 @@ int cmd_repack(int argc, const char **argv, const char *prefix)
12241233

12251234
cleanup:
12261235
string_list_clear(&names, 1);
1227-
string_list_clear(&existing_nonkept_packs, 0);
1228-
string_list_clear(&existing_kept_packs, 0);
1236+
existing_packs_release(&existing);
12291237
free_pack_geometry(&geometry);
12301238

12311239
return ret;

0 commit comments

Comments
 (0)