midx-write: skip writes with no object entries

A MIDX write can find packs without finding any objects, either because
the packs are empty or because an incremental write finds only objects
already indexed by an earlier layer. The existing early exit checks only
the number of packs, so these cases publish a zero-object MIDX without a
reverse index.

Empty MIDXs were already published by 662148c435 (midx: write object
offsets, 2018-07-12); the failure mode appeared in 27afc272c4 (midx:
implement writing incremental MIDX bitmaps, 2025-03-20), when
incremental bitmap writes began loading reverse indexes from prior
layers.

Reproducible on master as of b8242b093d (The 23rd batch, 2026-09-07),
starting with a non-incremental write:

    git init --bare --object-format=sha1 empty.git &&
    (
        cd empty.git &&
        git pack-objects objects/pack/pack </dev/null &&
        git multi-pack-index write &&
        git multi-pack-index write --incremental --bitmap
    )

The first write succeeds but publishes an empty MIDX, which also fails
verification with "the midx contains no oid". The second write exits
with status 255, reporting "could not load reverse index for MIDX".
Starting with an incremental write has the same problem.

Exit before publication whenever the computed entry list is empty, for
both non-incremental and incremental writes, including compaction. Take
the existing cleanup path after pack/drop validation and before
acquiring a lock or creating a temporary MIDX file. This leaves existing
MIDX files untouched and preserves the error when there are no pack
files to index.

Return success silently. Empty-object writes already return 0, including
when --bitmap warns, so preserve that exit status for existing callers
while omitting the warning and empty MIDX.

Test empty packs in a bare repository, empty incremental layers, and
packs containing only objects indexed by an earlier layer. Check silent
success, preservation of existing files, and subsequent writes that add
new objects. Update the existing bitmap test to expect neither an empty
MIDX nor a bitmap.

Signed-off-by: Pia Park <pia@pierre.co>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
jch
Pia Park 2026-09-08 00:10:16 -07:00 committed by Junio C Hamano
parent fa7f9290ef
commit 8d0a01e9d8
4 changed files with 119 additions and 9 deletions

View File

@ -1617,9 +1617,8 @@ static int write_midx_internal(struct write_midx_opts *opts)
}

if (!ctx.entries_nr) {
if (opts->flags & MIDX_WRITE_BITMAP)
warning(_("refusing to write multi-pack .bitmap without any objects"));
opts->flags &= ~(MIDX_WRITE_REV_INDEX | MIDX_WRITE_BITMAP);
result = 0;
goto cleanup;
}

if (ctx.incremental) {

View File

@ -54,6 +54,41 @@ test_expect_success "don't write midx with no packs" '
test_path_is_missing pack/multi-pack-index
'

test_expect_success 'skip non-incremental MIDX with no objects' '
git init --bare empty.git &&
(
cd empty.git &&
git pack-objects objects/pack/pack </dev/null &&
ls objects/pack >files.expect &&

for bitmap in "" --bitmap
do
git multi-pack-index write $bitmap >out 2>&1 &&
test_must_be_empty out &&
test_path_is_missing objects/pack/multi-pack-index &&
ls objects/pack >files.actual &&
test_cmp files.expect files.actual || return 1
done &&

git multi-pack-index write --incremental --bitmap &&
test_dir_is_empty objects/pack/multi-pack-index.d &&

echo blob | git hash-object -w --stdin >in &&
git pack-objects objects/pack/pack <in &&
git multi-pack-index write --incremental --bitmap &&
test_line_count = 1 objects/pack/multi-pack-index.d/multi-pack-index-chain &&
git multi-pack-index verify &&

echo another | git hash-object -w --stdin >in &&
git pack-objects objects/pack/pack <in &&
git multi-pack-index write --bitmap &&
test_path_is_file objects/pack/multi-pack-index &&
midx="$(midx_checksum objects)" &&
test_path_is_file objects/pack/multi-pack-index-$midx.bitmap &&
git multi-pack-index verify
)
'

test_expect_success SHA1 'warn if a midx contains no oid' '
cp "$TEST_DIRECTORY"/t5319/no-objects.midx $objdir/pack/multi-pack-index &&
test_must_fail git multi-pack-index verify &&

View File

@ -305,7 +305,7 @@ test_midx_bitmap_cases () {
)
'

test_expect_success 'no .bitmap is written without any objects' '
test_expect_success 'no MIDX or .bitmap is written without any objects' '
rm -fr repo &&
git init repo &&
test_when_finished "rm -fr repo" &&
@ -318,13 +318,14 @@ test_midx_bitmap_cases () {
pack-$empty.idx
EOF

ls $objdir/pack >files.expect &&
git multi-pack-index write --bitmap --stdin-packs \
<packs 2>err &&
<packs >out 2>&1 &&

test_grep "bitmap without any objects" err &&

test_path_is_file $midx &&
test_path_is_missing $midx-$(midx_checksum $objdir).bitmap
test_must_be_empty out &&
test_path_is_missing $midx &&
ls $objdir/pack >files.actual &&
test_cmp files.expect files.actual
)
'


View File

@ -195,4 +195,79 @@ test_expect_success 'non-incremental write with existing incremental chain' '
)
'

test_expect_success 'skip initial MIDX layer with no objects' '
git init empty &&
(
cd empty &&
git config maintenance.auto false &&
git pack-objects $packdir/pack </dev/null &&

for bitmap in --bitmap --no-bitmap
do
git multi-pack-index write --incremental "$bitmap" >out 2>&1 &&
test_must_be_empty out &&
test_dir_is_empty "$midxdir" || return 1
done &&

write_midx_layer &&
test_line_count = 1 "$midx_chain" &&
git multi-pack-index verify
)
'

test_expect_success 'skip MIDX layer with empty pack' '
git init empty-pack &&
(
cd empty-pack &&
git config maintenance.auto false &&
write_midx_layer &&

git pack-objects $packdir/pack </dev/null &&
cp "$midx_chain" chain.expect &&
ls "$packdir" "$midxdir" >files.expect &&

for bitmap in --bitmap --no-bitmap
do
git multi-pack-index write --incremental "$bitmap" >out 2>&1 &&
test_must_be_empty out &&
test_cmp chain.expect "$midx_chain" &&
ls "$packdir" "$midxdir" >files.actual &&
test_cmp files.expect files.actual || return 1
done &&

write_midx_layer &&
test_line_count = 2 "$midx_chain" &&
git multi-pack-index verify &&
git rev-list --test-bitmap 2.2
)
'

test_expect_success 'skip MIDX layer with duplicate pack' '
git init duplicate-pack &&
(
cd duplicate-pack &&
git config maintenance.auto false &&
write_midx_layer &&

git rev-parse HEAD^{tree} >in &&
git pack-objects $packdir/pack <in &&
cp "$midx_chain" chain.expect &&
ls "$packdir" "$midxdir" >files.expect &&

for bitmap in --bitmap --no-bitmap
do
git multi-pack-index write --incremental "$bitmap" >out 2>&1 &&
test_must_be_empty out &&
test_cmp chain.expect "$midx_chain" &&
ls "$packdir" "$midxdir" >files.actual &&
test_cmp files.expect files.actual || return 1
done &&

write_midx_layer &&
test_line_count = 2 "$midx_chain" &&
git multi-pack-index verify &&
git rev-list --test-bitmap 2.2
)
'

test_done