Files
git-mirror/t/t5335-compact-multi-pack-index.sh
Michael Montalbo 47f79f6198 t: convert grep assertions to test_grep
Replace bare grep with test_grep in test assertions across the
suite, including sourced test helpers (lib-*.sh, *-tests.sh).
test_grep prints the contents of the file being searched on
failure, making debugging easier than a bare grep which fails
silently.

Only assertion-style greps are converted: grep used as a filter
in pipelines, command substitutions, conditionals, or with
redirected I/O is left as-is with a "# lint-ok" annotation.
Existing '! test_grep' calls are rewritten to 'test_grep !' so
that the diagnostic output is preserved on failure.

test_grep requires the file it reads to exist, so '! grep'
assertions that inspect a file whose presence is conditional need
care.  In t5537 the '.git/shallow' file is still present after the
repack (the client remains shallow), so the assertion is
converted like any other.  In t1400 the '.git/packed-refs' file
exists only with the files backend, so its check is guarded with a
REFFILES prerequisite; the backend-agnostic 'git show-ref' check
that follows still runs under every backend.  In t7450 'git~2' is
the NTFS 8.3 short name of a '..git' file and only exists
when 8.3 short-name generation is enabled, so its check is guarded
with a 'test -f' on the path and uses test_grep inside the guard,
the same shape as t1400 (a plain test_grep would BUG when the
short name is absent).

The conversion was generated using a grep-assertion linter
(greplint.pl, added in the following commit) to identify bare
grep calls at command position.  To reproduce, from the t/
directory:

    # Step 1: annotate the two data-filter greps (grep produces
    # data, not a verdict) so the linter skips them.
    sed -i '/grep -vf before commits\.raw/s/$/ # lint-ok: data filter/' \
        t5326-multi-pack-bitmaps.sh
    sed -i '/grep -E "^\[0-9a-f\].*|| :/s/$/ # lint-ok: data filter/' \
        t5702-protocol-v2.sh

    # Step 1b: two '! grep' assertions need more than a mechanical
    # conversion; handle them by hand before the linter-driven steps
    # below so it leaves them alone.
    #
    # t1400: '.git/packed-refs' is absent under reftable, so guard the
    # check with REFFILES (a plain test_grep would BUG on the missing
    # file):
    #
    #      git update-ref -d HEAD $B &&
    #  -   ! grep "$m" .git/packed-refs &&
    #  +   if test_have_prereq REFFILES
    #  +   then
    #  +           test_grep ! "$m" .git/packed-refs
    #  +   fi &&
    #      test_must_fail git show-ref --verify -q $m
    #
    # t7450: git~2 is an NTFS 8.3 short name that exists only when
    # short-name generation is enabled, so guard the check on its
    # presence with 'test -f' and note in a comment why the path can
    # be absent (a plain test_grep would BUG when it is):
    #
    #  -   ! grep gitdir squatting-clone/d/a/git~2
    #  +   if test -f squatting-clone/d/a/git~2
    #  +   then
    #  +           test_grep ! gitdir squatting-clone/d/a/git~2
    #  +   fi

    # Step 2: reorder pre-existing '! test_grep' to 'test_grep !'
    # (must come before steps 3-4 so greplint does not see them)
    sed -i 's/! test_grep/test_grep !/' t0031-lockfile-pid.sh
    sed -i 's/! test_grep/test_grep !/' t5300-pack-object.sh
    sed -i 's/! test_grep/test_grep !/' t5319-multi-pack-index.sh

    # Step 3: convert '! grep' -> 'test_grep !'
    perl greplint.pl *.sh 2>&1 | cut -d: -f1,2 |
    while IFS=: read f l; do
        sed -i "${l}s/! *grep/test_grep !/" "$f"
    done

    # Step 4: convert remaining 'grep' -> 'test_grep'
    perl greplint.pl *.sh 2>&1 | cut -d: -f1,2 |
    while IFS=: read f l; do
        sed -i "${l}s/grep/test_grep/" "$f"
    done

To verify, run: make -C t test-greplint

Signed-off-by: Michael Montalbo <mmontalbo@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2026-07-06 13:27:00 -07:00

407 lines
10 KiB
Bash
Executable File

#!/bin/sh
test_description='multi-pack-index compaction'
. ./test-lib.sh
GIT_TEST_MULTI_PACK_INDEX=0
GIT_TEST_MULTI_PACK_INDEX_WRITE_BITMAP=0
GIT_TEST_MULTI_PACK_INDEX_WRITE_INCREMENTAL=0
objdir=.git/objects
packdir=$objdir/pack
midxdir=$packdir/multi-pack-index.d
midx_chain=$midxdir/multi-pack-index-chain
nth_line() {
local n="$1"
shift
awk "NR==$n" "$@"
}
write_packs () {
for c in "$@"
do
test_commit "$c" &&
git pack-objects --all --unpacked $packdir/pack-$c &&
git prune-packed &&
git multi-pack-index write --incremental --bitmap || return 1
done
}
test_midx_layer_packs () {
local checksum="$1" &&
shift &&
test-tool read-midx $objdir "$checksum" >out &&
printf "%s\n" "$@" >expect &&
# NOTE: do *not* pipe through sort here, we want to ensure the
# order of packs is preserved during compaction.
grep "^pack-" out | cut -d"-" -f2 >actual &&
test_cmp expect actual
}
test_midx_layer_object_uniqueness () {
: >objs.all
while read layer
do
test-tool read-midx --show-objects $objdir "$layer" >out &&
grep "\.pack$" out | cut -d" " -f1 | sort >objs.layer &&
test_stdout_line_count = 0 comm -12 objs.all objs.layer &&
cat objs.all objs.layer | sort >objs.tmp &&
mv objs.tmp objs.all || return 1
done <$midx_chain
}
test_expect_success 'MIDX compaction with lex-ordered pack names' '
git init midx-compact-lex-order &&
(
cd midx-compact-lex-order &&
git config maintenance.auto false &&
write_packs A B C D E &&
test_line_count = 5 $midx_chain &&
git multi-pack-index compact --incremental --bitmap \
"$(nth_line 2 "$midx_chain")" \
"$(nth_line 4 "$midx_chain")" &&
test_line_count = 3 $midx_chain &&
test_midx_layer_packs "$(nth_line 1 "$midx_chain")" A &&
test_midx_layer_packs "$(nth_line 2 "$midx_chain")" B C D &&
test_midx_layer_packs "$(nth_line 3 "$midx_chain")" E &&
test_midx_layer_object_uniqueness
)
'
test_expect_success 'MIDX compaction with non-lex-ordered pack names' '
git init midx-compact-non-lex-order &&
(
cd midx-compact-non-lex-order &&
git config maintenance.auto false &&
write_packs D C A B E &&
test_line_count = 5 $midx_chain &&
git multi-pack-index compact --incremental --bitmap \
"$(nth_line 2 "$midx_chain")" \
"$(nth_line 4 "$midx_chain")" &&
test_line_count = 3 $midx_chain &&
test_midx_layer_packs "$(nth_line 1 "$midx_chain")" D &&
test_midx_layer_packs "$(nth_line 2 "$midx_chain")" C A B &&
test_midx_layer_packs "$(nth_line 3 "$midx_chain")" E &&
test_midx_layer_object_uniqueness
)
'
test_expect_success 'setup for bogus MIDX compaction scenarios' '
git init midx-compact-bogus &&
(
cd midx-compact-bogus &&
git config maintenance.auto false &&
write_packs A B C
)
'
test_expect_success 'MIDX compaction with missing endpoints' '
(
cd midx-compact-bogus &&
test_must_fail git multi-pack-index compact --incremental \
"<missing>" "<missing>" 2>err &&
test_grep "could not find MIDX: <missing>" err &&
test_must_fail git multi-pack-index compact --incremental \
"<missing>" "$(nth_line 2 "$midx_chain")" 2>err &&
test_grep "could not find MIDX: <missing>" err &&
test_must_fail git multi-pack-index compact --incremental \
"$(nth_line 2 "$midx_chain")" "<missing>" 2>err &&
test_grep "could not find MIDX: <missing>" err
)
'
test_expect_success 'MIDX compaction with reversed endpoints' '
(
cd midx-compact-bogus &&
from="$(nth_line 3 "$midx_chain")" &&
to="$(nth_line 1 "$midx_chain")" &&
test_must_fail git multi-pack-index compact --incremental \
"$from" "$to" 2>err &&
test_grep "MIDX $from must be an ancestor of $to" err
)
'
test_expect_success 'MIDX compaction with identical endpoints' '
(
cd midx-compact-bogus &&
from="$(nth_line 3 "$midx_chain")" &&
to="$(nth_line 3 "$midx_chain")" &&
test_must_fail git multi-pack-index compact --incremental \
"$from" "$to" 2>err &&
test_grep "MIDX compaction endpoints must be unique" err
)
'
test_expect_success 'MIDX compaction with midx.version=1' '
(
cd midx-compact-bogus &&
test_must_fail git -c midx.version=1 multi-pack-index compact \
"$(nth_line 1 "$midx_chain")" \
"$(nth_line 2 "$midx_chain")" 2>err &&
test_grep "fatal: cannot perform MIDX compaction with v1 format" err
)
'
midx_objs_by_pack () {
awk '/\.pack$/ { split($3, a, "-"); print a[2], $1 }' | sort
}
tag_objs_from_pack () {
objs="$(git rev-list --objects --no-object-names "$2")" &&
printf "$1 %s\n" $objs | sort
}
test_expect_success 'MIDX compaction preserves pack object selection' '
git init midx-compact-preserve-selection &&
(
cd midx-compact-preserve-selection &&
git config maintenance.auto false &&
test_commit A &&
test_commit B &&
# Create two packs, one containing just the objects from
# A, and another containing all objects from the
# repository.
p1="$(echo A | git pack-objects --revs --delta-base-offset \
$packdir/pack-1)" &&
p0="$(echo B | git pack-objects --revs --delta-base-offset \
$packdir/pack-0)" &&
echo "pack-1-$p1.idx" | git multi-pack-index write \
--incremental --bitmap --stdin-packs &&
echo "pack-0-$p0.idx" | git multi-pack-index write \
--incremental --bitmap --stdin-packs &&
write_packs C &&
git multi-pack-index compact --incremental --bitmap \
"$(nth_line 1 "$midx_chain")" \
"$(nth_line 2 "$midx_chain")" &&
test-tool read-midx --show-objects $objdir \
"$(nth_line 1 "$midx_chain")" >AB.info &&
test-tool read-midx --show-objects $objdir \
"$(nth_line 2 "$midx_chain")" >C.info &&
midx_objs_by_pack <AB.info >AB.actual &&
midx_objs_by_pack <C.info >C.actual &&
{
tag_objs_from_pack 1 A &&
tag_objs_from_pack 0 A..B
} | sort >AB.expect &&
tag_objs_from_pack C B..C >C.expect &&
test_cmp AB.expect AB.actual &&
test_cmp C.expect C.actual
)
'
test_expect_success 'MIDX compaction with bitmaps' '
git init midx-compact-with-bitmaps &&
(
cd midx-compact-with-bitmaps &&
git config maintenance.auto false &&
write_packs foo bar baz quux woot &&
test-tool read-midx --bitmap $objdir >bitmap.expect &&
git multi-pack-index compact --incremental --bitmap \
"$(nth_line 2 "$midx_chain")" \
"$(nth_line 4 "$midx_chain")" &&
test-tool read-midx --bitmap $objdir >bitmap.actual &&
test_cmp bitmap.expect bitmap.actual &&
true
)
'
test_expect_success 'MIDX compaction with bitmaps (non-trivial)' '
git init midx-compact-with-bitmaps-non-trivial &&
(
cd midx-compact-with-bitmaps-non-trivial &&
git config maintenance.auto false &&
git branch -m main &&
# D(4)
# /
# A(1) --- B(2) --- C(3) --- G(7)
# \
# E(5) --- F(6)
write_packs A B C &&
git checkout -b side &&
write_packs D &&
git checkout -b other B &&
write_packs E F &&
git checkout main &&
write_packs G &&
# Compact layers 2-4, leaving us with:
#
# [A, [B, C, D], E, F, G]
git multi-pack-index compact --incremental --bitmap \
"$(nth_line 2 "$midx_chain")" \
"$(nth_line 4 "$midx_chain")" &&
# Then compact the top two layers, condensing the above
# such that the new 4th layer contains F and G.
#
# [A, [B, C, D], E, [F, G]]
git multi-pack-index compact --incremental --bitmap \
"$(nth_line 4 "$midx_chain")" \
"$(nth_line 5 "$midx_chain")"
)
'
test_expect_success 'MIDX compaction with --no-write-chain-file' '
git init midx-compact-with--no-write-chain-file &&
(
cd midx-compact-with--no-write-chain-file &&
git config maintenance.auto false &&
write_packs A B C D &&
test_line_count = 4 $midx_chain &&
cp "$midx_chain" "$midx_chain".bak &&
layer="$(git multi-pack-index compact --incremental \
--no-write-chain-file \
--base="$(nth_line 1 "$midx_chain")" \
"$(nth_line 2 "$midx_chain")" \
"$(nth_line 3 "$midx_chain")")" &&
test_cmp "$midx_chain.bak" "$midx_chain" &&
# After writing the new layer, insert it into the chain
# manually. This is done in order to make $layer visible
# to the read-midx test helper below, and matches what
# the MIDX command would do without --no-write-chain-file.
{
nth_line 1 "$midx_chain.bak" &&
echo $layer &&
nth_line 4 "$midx_chain.bak"
} >$midx_chain &&
test-tool read-midx $objdir $layer >midx.data &&
test_grep "^pack-B-.*\.idx" midx.data &&
test_grep "^pack-C-.*\.idx" midx.data
)
'
test_expect_success 'MIDX compaction with --base' '
git init midx-compact-with--base &&
(
cd midx-compact-with--base &&
git config maintenance.auto false &&
write_packs A B C D &&
test_line_count = 4 "$midx_chain" &&
cp "$midx_chain" "$midx_chain.bak" &&
git multi-pack-index compact --incremental \
--base="$(nth_line 1 "$midx_chain")" \
"$(nth_line 3 "$midx_chain")" \
"$(nth_line 4 "$midx_chain")" &&
test_line_count = 2 $midx_chain &&
nth_line 1 "$midx_chain.bak" >expect &&
nth_line 1 "$midx_chain" >actual &&
test_cmp expect actual
)
'
test_expect_success 'MIDX compaction with --base=none' '
git init midx-compact-base-none &&
(
cd midx-compact-base-none &&
git config maintenance.auto false &&
write_packs A B C D &&
test_line_count = 4 $midx_chain &&
cp "$midx_chain" "$midx_chain".bak &&
# Compact the two bottommost layers (A and B) into a new
# root layer with no parent.
git multi-pack-index compact --incremental \
--base=none \
"$(nth_line 1 "$midx_chain")" \
"$(nth_line 2 "$midx_chain")" &&
test_line_count = 3 $midx_chain &&
# The upper layers (C and D) should be preserved
# unchanged.
nth_line 3 "$midx_chain.bak" >expect &&
nth_line 4 "$midx_chain.bak" >>expect &&
nth_line 2 "$midx_chain" >actual &&
nth_line 3 "$midx_chain" >>actual &&
test_cmp expect actual
)
'
test_expect_success 'MIDX compaction with bogus --base checksum' '
git init midx-compact-bogus-base &&
(
cd midx-compact-bogus-base &&
git config maintenance.auto false &&
write_packs A B C &&
test_must_fail git multi-pack-index compact --incremental \
--base=deadbeef \
"$(nth_line 2 "$midx_chain")" \
"$(nth_line 3 "$midx_chain")" 2>err &&
test_grep "could not find base MIDX" err
)
'
test_done