From: Derrick Stolee <derrickstolee@xxxxxxxxxx> In 73320e49add (builtin/repack.c: only collect fully-formed packs, 2023-06-07), we switched the check for which packs to collect by starting at the .idx files and looking for matching .pack files. This avoids trying to repack pack-files that have not had their pack-indexes installed yet. However, it does cause maintenance to halt if we find the (problematic, but not insurmountable) case of a .idx file without a corresponding .pack file. In an environment where packfile maintenance is a critical function, such a hard stop is costly and requires human intervention to resolve (by deleting the .idx file). This was not the case before. We successfully repacked through this scenario until the recent change to scan for .idx files. Further, if we are actually in a case where objects are missing, we detect this at a different point during the reachability walk. In other cases, Git prepares its list of packfiles by scanning .idx files and then only adds it to the packfile list if the corresponding .pack file exists. It even does so without a warning! (See add_packed_git() in packfile.c for details.) This case is much less likely to occur than the failures seen before 73320e49add. Packfiles are "installed" by writing the .pack file before the .idx and that process can be interrupted. Packfiles _should_ be deleted by deleting the .idx first, followed by the .pack file, but unlink_pack_path() does not do this: it deletes the .pack _first_, allowing a window where this process could be interrupted. We leave the consideration of changing this order as a separate concern. Knowing that this condition is possible from interrupted Git processes and not other tools lends some weight that Git should be more flexible around this scenario. Add a check to see if the .pack file exists before adding it to the list for repacking. This will stop a number of maintenance failures seen in production but fixed by deleting the .idx files. This brings us closer to the case before 73320e49add in that 'git repack' will not fail when there is an orphaned .idx file, at least, not due to the way we scan for packfiles. In the case that the .pack file was erroneously deleted without copies of its objects in other installed packfiles, then 'git repack' will fail due to the reachable object walk. This does resolve the case where automated repacks will no longer be halted on this case. The tests in t7700 show both these successful scenarios and the case of failing if the .pack was truly required. Signed-off-by: Derrick Stolee <derrickstolee@xxxxxxxxxx> Signed-off-by: Taylor Blau <me@xxxxxxxxxxxx> --- builtin/repack.c | 3 +++ t/t7700-repack.sh | 47 ++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 45 insertions(+), 5 deletions(-) diff --git a/builtin/repack.c b/builtin/repack.c index 51698e3c68..724e09536e 100644 --- a/builtin/repack.c +++ b/builtin/repack.c @@ -125,6 +125,9 @@ static void collect_pack_filenames(struct string_list *fname_nonkept_list, strbuf_add(&buf, e->d_name, len); strbuf_addstr(&buf, ".pack"); + if (!file_exists(mkpath("%s/%s", packdir, buf.buf))) + continue; + for (i = 0; i < extra_keep->nr; i++) if (!fspathcmp(buf.buf, extra_keep->items[i].string)) break; diff --git a/t/t7700-repack.sh b/t/t7700-repack.sh index af79266c58..284954791d 100755 --- a/t/t7700-repack.sh +++ b/t/t7700-repack.sh @@ -213,16 +213,16 @@ test_expect_success 'repack --keep-pack' ' test_create_repo keep-pack && ( cd keep-pack && - # avoid producing difference packs to delta/base choices + # avoid producing different packs due to delta/base choices git config pack.window 0 && P1=$(commit_and_pack 1) && P2=$(commit_and_pack 2) && P3=$(commit_and_pack 3) && P4=$(commit_and_pack 4) && - ls .git/objects/pack/*.pack >old-counts && + find .git/objects/pack -name "*.pack" -type f | sort >old-counts && test_line_count = 4 old-counts && git repack -a -d --keep-pack $P1 --keep-pack $P4 && - ls .git/objects/pack/*.pack >new-counts && + find .git/objects/pack -name "*.pack" -type f | sort >new-counts && grep -q $P1 new-counts && grep -q $P4 new-counts && test_line_count = 3 new-counts && @@ -239,14 +239,51 @@ test_expect_success 'repack --keep-pack' ' mv "$from" "$to" || return 1 done && + # A .idx file without a .pack should not stop us from + # repacking what we can. + touch .git/objects/pack/pack-does-not-exist.idx && + + find .git/objects/pack -name "*.pack" -type f | sort >packs.before && git repack --cruft -d --keep-pack $P1 --keep-pack $P4 && + find .git/objects/pack -name "*.pack" -type f | sort >packs.after && - ls .git/objects/pack/*.pack >newer-counts && - test_cmp new-counts newer-counts && + test_cmp packs.before packs.after && git fsck ) ' +test_expect_success 'repacking fails when missing .pack actually means missing objects' ' + test_create_repo idx-without-pack && + ( + cd idx-without-pack && + + # Avoid producing different packs due to delta/base choices + git config pack.window 0 && + P1=$(commit_and_pack 1) && + P2=$(commit_and_pack 2) && + P3=$(commit_and_pack 3) && + P4=$(commit_and_pack 4) && + find .git/objects/pack -name "*.pack" -type f | sort >old-counts && + test_line_count = 4 old-counts && + + # Remove one .pack file + rm .git/objects/pack/$P2 && + + find .git/objects/pack -name "*.pack" -type f | + sort >before-pack-dir && + + test_must_fail git fsck && + test_must_fail git repack --cruft -d 2>err && + grep "bad object" err && + + # Before failing, the repack did not modify the + # pack directory. + find .git/objects/pack -name "*.pack" -type f | + sort >after-pack-dir && + test_cmp before-pack-dir after-pack-dir + ) +' + test_expect_success 'bitmaps are created by default in bare repos' ' git clone --bare .git bare.git && rm -f bare.git/objects/pack/*.bitmap && -- 2.41.0.320.gb3d0d9308ef