Skip to content

Commit 90f838b

Browse files
ttaylorrgitster
authored andcommitted
builtin/repack.c: keep track of existing packs unconditionally
In order to be able to write a multi-pack index during repacking, `git repack` must keep track of which packs it wants to write into the MIDX. This set is the union of existing packs which will not be deleted, new pack(s) generated as a result of the repack, and .keep packs. Prior to this patch, `git repack` populated the list of existing packs only when repacking all-into-one (i.e., with `-A` or `-a`), but we will soon need to know this list when repacking when writing a MIDX without a-i-o. Populate the list of existing packs unconditionally, and guard removing packs from that list only when repacking a-i-o. Additionally, keep track of filenames of kept packs separately, since this, too, will be used in an upcoming patch. Signed-off-by: Taylor Blau <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 08944d1 commit 90f838b

File tree

1 file changed

+31
-25
lines changed

1 file changed

+31
-25
lines changed

builtin/repack.c

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -94,12 +94,14 @@ static void remove_pack_on_signal(int signo)
9494
}
9595

9696
/*
97-
* Adds all packs hex strings to the fname list, which do not
98-
* have a corresponding .keep file. These packs are not to
99-
* be kept if we are going to pack everything into one file.
97+
* Adds all packs hex strings to either fname_list or fname_kept_list
98+
* based on whether each pack has a corresponding .keep file or not.
99+
* Packs without a .keep file are not to be kept if we are going to
100+
* pack everything into one file.
100101
*/
101-
static void get_non_kept_pack_filenames(struct string_list *fname_list,
102-
const struct string_list *extra_keep)
102+
static void collect_pack_filenames(struct string_list *fname_list,
103+
struct string_list *fname_kept_list,
104+
const struct string_list *extra_keep)
103105
{
104106
DIR *dir;
105107
struct dirent *e;
@@ -112,21 +114,20 @@ static void get_non_kept_pack_filenames(struct string_list *fname_list,
112114
size_t len;
113115
int i;
114116

117+
if (!strip_suffix(e->d_name, ".pack", &len))
118+
continue;
119+
115120
for (i = 0; i < extra_keep->nr; i++)
116121
if (!fspathcmp(e->d_name, extra_keep->items[i].string))
117122
break;
118-
if (extra_keep->nr > 0 && i < extra_keep->nr)
119-
continue;
120-
121-
if (!strip_suffix(e->d_name, ".pack", &len))
122-
continue;
123123

124124
fname = xmemdupz(e->d_name, len);
125125

126-
if (!file_exists(mkpath("%s/%s.keep", packdir, fname)))
127-
string_list_append_nodup(fname_list, fname);
126+
if ((extra_keep->nr > 0 && i < extra_keep->nr) ||
127+
(file_exists(mkpath("%s/%s.keep", packdir, fname))))
128+
string_list_append_nodup(fname_kept_list, fname);
128129
else
129-
free(fname);
130+
string_list_append_nodup(fname_list, fname);
130131
}
131132
closedir(dir);
132133
}
@@ -440,6 +441,7 @@ int cmd_repack(int argc, const char **argv, const char *prefix)
440441
struct string_list names = STRING_LIST_INIT_DUP;
441442
struct string_list rollback = STRING_LIST_INIT_NODUP;
442443
struct string_list existing_packs = STRING_LIST_INIT_DUP;
444+
struct string_list existing_kept_packs = STRING_LIST_INIT_DUP;
443445
struct pack_geometry *geometry = NULL;
444446
struct strbuf line = STRBUF_INIT;
445447
int i, ext, ret;
@@ -572,9 +574,10 @@ int cmd_repack(int argc, const char **argv, const char *prefix)
572574
if (use_delta_islands)
573575
strvec_push(&cmd.args, "--delta-islands");
574576

575-
if (pack_everything & ALL_INTO_ONE) {
576-
get_non_kept_pack_filenames(&existing_packs, &keep_pack_list);
577+
collect_pack_filenames(&existing_packs, &existing_kept_packs,
578+
&keep_pack_list);
577579

580+
if (pack_everything & ALL_INTO_ONE) {
578581
repack_promisor_objects(&po_args, &names);
579582

580583
if (existing_packs.nr && delete_redundant) {
@@ -683,17 +686,19 @@ int cmd_repack(int argc, const char **argv, const char *prefix)
683686
reprepare_packed_git(the_repository);
684687

685688
if (delete_redundant) {
686-
const int hexsz = the_hash_algo->hexsz;
687689
int opts = 0;
688-
string_list_sort(&names);
689-
for_each_string_list_item(item, &existing_packs) {
690-
char *sha1;
691-
size_t len = strlen(item->string);
692-
if (len < hexsz)
693-
continue;
694-
sha1 = item->string + len - hexsz;
695-
if (!string_list_has_string(&names, sha1))
696-
remove_redundant_pack(packdir, item->string);
690+
if (pack_everything & ALL_INTO_ONE) {
691+
const int hexsz = the_hash_algo->hexsz;
692+
string_list_sort(&names);
693+
for_each_string_list_item(item, &existing_packs) {
694+
char *sha1;
695+
size_t len = strlen(item->string);
696+
if (len < hexsz)
697+
continue;
698+
sha1 = item->string + len - hexsz;
699+
if (!string_list_has_string(&names, sha1))
700+
remove_redundant_pack(packdir, item->string);
701+
}
697702
}
698703

699704
if (geometry) {
@@ -739,6 +744,7 @@ int cmd_repack(int argc, const char **argv, const char *prefix)
739744
string_list_clear(&names, 0);
740745
string_list_clear(&rollback, 0);
741746
string_list_clear(&existing_packs, 0);
747+
string_list_clear(&existing_kept_packs, 0);
742748
clear_pack_geometry(geometry);
743749
strbuf_release(&line);
744750

0 commit comments

Comments
 (0)