Skip to content

Commit f3f9363

Browse files
committed
midx: clear midx on repack
If a 'git repack' command replaces existing packfiles, then we must clear the existing multi-pack-index before moving the packfiles it references. Signed-off-by: Derrick Stolee <[email protected]>
1 parent 79c1ce2 commit f3f9363

File tree

4 files changed

+31
-0
lines changed

4 files changed

+31
-0
lines changed

builtin/repack.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "strbuf.h"
99
#include "string-list.h"
1010
#include "argv-array.h"
11+
#include "midx.h"
1112

1213
static int delta_base_offset = 1;
1314
static int pack_kept_objects = -1;
@@ -174,6 +175,7 @@ int cmd_repack(int argc, const char **argv, const char *prefix)
174175
int no_update_server_info = 0;
175176
int quiet = 0;
176177
int local = 0;
178+
int midx_cleared = 0;
177179

178180
struct option builtin_repack_options[] = {
179181
OPT_BIT('a', NULL, &pack_everything,
@@ -333,6 +335,13 @@ int cmd_repack(int argc, const char **argv, const char *prefix)
333335
for_each_string_list_item(item, &names) {
334336
for (ext = 0; ext < ARRAY_SIZE(exts); ext++) {
335337
char *fname, *fname_old;
338+
339+
if (!midx_cleared) {
340+
/* if we move a packfile, it will invalidated the midx */
341+
clear_midx_file(get_object_directory());
342+
midx_cleared = 1;
343+
}
344+
336345
fname = mkpathdup("%s/pack-%s%s", packdir,
337346
item->string, exts[ext].name);
338347
if (!file_exists(fname)) {

midx.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -904,3 +904,15 @@ int write_midx_file(const char *object_dir)
904904
free(midx_name);
905905
return 0;
906906
}
907+
908+
void clear_midx_file(const char *object_dir)
909+
{
910+
char *midx = get_midx_filename(object_dir);
911+
912+
if (remove_path(midx)) {
913+
UNLEAK(midx);
914+
die(_("failed to clear multi-pack-index at %s"), midx);
915+
}
916+
917+
free(midx);
918+
}

midx.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,6 @@ int midx_contains_pack(struct multi_pack_index *m, const char *idx_name);
3939
int prepare_multi_pack_index_one(struct repository *r, const char *object_dir);
4040

4141
int write_midx_file(const char *object_dir);
42+
void clear_midx_file(const char *object_dir);
4243

4344
#endif

t/t5319-multi-pack-index.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,15 @@ test_expect_success 'write midx with twelve packs' '
141141

142142
compare_results_with_midx "twelve packs"
143143

144+
test_expect_success 'repack removes multi-pack-index' '
145+
test_path_is_file $objdir/pack/multi-pack-index &&
146+
git repack -adf &&
147+
test_path_is_missing $objdir/pack/multi-pack-index
148+
'
149+
150+
compare_results_with_midx "after repack"
151+
152+
144153
# usage: corrupt_data <file> <pos> [<data>]
145154
corrupt_data () {
146155
file=$1

0 commit comments

Comments
 (0)