Skip to content

Commit 426c00e

Browse files
ttaylorrgitster
authored andcommitted
midx: fix *.rev cleanups with --object-dir
If using --object-dir to point into an object directory which belongs to a different repository than the one in the current working directory, such as: git init repo git -C repo ... # add some objects cd alternate git multi-pack-index --object-dir ../repo/.git/objects write the binary will segfault trying to access the object-dir via the repo it found, but that's not fully initialized. Worse, if we later call clear_midx_files_ext(), we will use `the_repository` and remove files out of the wrong object directory. Fix this by using the given object_dir (or the object directory of `the_repository` if `--object-dir` wasn't given) to properly to clean up the *.rev files, avoiding the crash. Original-patch-by: Johannes Berg <[email protected]> Signed-off-by: Taylor Blau <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 73ff4ad commit 426c00e

File tree

2 files changed

+33
-5
lines changed

2 files changed

+33
-5
lines changed

midx.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -882,7 +882,7 @@ static void write_midx_reverse_index(char *midx_name, unsigned char *midx_hash,
882882
strbuf_release(&buf);
883883
}
884884

885-
static void clear_midx_files_ext(struct repository *r, const char *ext,
885+
static void clear_midx_files_ext(const char *object_dir, const char *ext,
886886
unsigned char *keep_hash);
887887

888888
static int midx_checksum_valid(struct multi_pack_index *m)
@@ -1086,7 +1086,7 @@ static int write_midx_internal(const char *object_dir, struct multi_pack_index *
10861086

10871087
if (flags & MIDX_WRITE_REV_INDEX)
10881088
write_midx_reverse_index(midx_name, midx_hash, &ctx);
1089-
clear_midx_files_ext(the_repository, ".rev", midx_hash);
1089+
clear_midx_files_ext(object_dir, ".rev", midx_hash);
10901090

10911091
commit_lock_file(&lk);
10921092

@@ -1135,7 +1135,7 @@ static void clear_midx_file_ext(const char *full_path, size_t full_path_len,
11351135
die_errno(_("failed to remove %s"), full_path);
11361136
}
11371137

1138-
static void clear_midx_files_ext(struct repository *r, const char *ext,
1138+
static void clear_midx_files_ext(const char *object_dir, const char *ext,
11391139
unsigned char *keep_hash)
11401140
{
11411141
struct clear_midx_data data;
@@ -1146,7 +1146,7 @@ static void clear_midx_files_ext(struct repository *r, const char *ext,
11461146
hash_to_hex(keep_hash), ext);
11471147
data.ext = ext;
11481148

1149-
for_each_file_in_pack_dir(r->objects->odb->path,
1149+
for_each_file_in_pack_dir(object_dir,
11501150
clear_midx_file_ext,
11511151
&data);
11521152

@@ -1165,7 +1165,7 @@ void clear_midx_file(struct repository *r)
11651165
if (remove_path(midx))
11661166
die(_("failed to clear multi-pack-index at %s"), midx);
11671167

1168-
clear_midx_files_ext(r, ".rev", NULL);
1168+
clear_midx_files_ext(r->objects->odb->path, ".rev", NULL);
11691169

11701170
free(midx);
11711171
}

t/t5319-multi-pack-index.sh

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,34 @@ test_expect_success 'write midx with twelve packs' '
201201

202202
compare_results_with_midx "twelve packs"
203203

204+
test_expect_success 'multi-pack-index *.rev cleanup with --object-dir' '
205+
git init repo &&
206+
git clone -s repo alternate &&
207+
208+
test_when_finished "rm -rf repo alternate" &&
209+
210+
(
211+
cd repo &&
212+
test_commit base &&
213+
git repack -d
214+
) &&
215+
216+
ours="alternate/.git/objects/pack/multi-pack-index-123.rev" &&
217+
theirs="repo/.git/objects/pack/multi-pack-index-abc.rev" &&
218+
touch "$ours" "$theirs" &&
219+
220+
(
221+
cd alternate &&
222+
git multi-pack-index --object-dir ../repo/.git/objects write
223+
) &&
224+
225+
# writing a midx in "repo" should not remove the .rev file in the
226+
# alternate
227+
test_path_is_file repo/.git/objects/pack/multi-pack-index &&
228+
test_path_is_file $ours &&
229+
test_path_is_missing $theirs
230+
'
231+
204232
test_expect_success 'warn on improper hash version' '
205233
git init --object-format=sha1 sha1 &&
206234
(

0 commit comments

Comments
 (0)