t / t5319-multi-pack-index.shon commit Merge branch 'sg/commit-graph-validate' (6ba06b5)
   1#!/bin/sh
   2
   3test_description='multi-pack-indexes'
   4. ./test-lib.sh
   5
   6objdir=.git/objects
   7
   8midx_read_expect () {
   9        NUM_PACKS=$1
  10        NUM_OBJECTS=$2
  11        NUM_CHUNKS=$3
  12        OBJECT_DIR=$4
  13        EXTRA_CHUNKS="$5"
  14        {
  15                cat <<-EOF &&
  16                header: 4d494458 1 $NUM_CHUNKS $NUM_PACKS
  17                chunks: pack-names oid-fanout oid-lookup object-offsets$EXTRA_CHUNKS
  18                num_objects: $NUM_OBJECTS
  19                packs:
  20                EOF
  21                if test $NUM_PACKS -ge 1
  22                then
  23                        ls $OBJECT_DIR/pack/ | grep idx | sort
  24                fi &&
  25                printf "object-dir: $OBJECT_DIR\n"
  26        } >expect &&
  27        test-tool read-midx $OBJECT_DIR >actual &&
  28        test_cmp expect actual
  29}
  30
  31test_expect_success 'write midx with no packs' '
  32        test_when_finished rm -f pack/multi-pack-index &&
  33        git multi-pack-index --object-dir=. write &&
  34        midx_read_expect 0 0 4 .
  35'
  36
  37generate_objects () {
  38        i=$1
  39        iii=$(printf '%03i' $i)
  40        {
  41                test-tool genrandom "bar" 200 &&
  42                test-tool genrandom "baz $iii" 50
  43        } >wide_delta_$iii &&
  44        {
  45                test-tool genrandom "foo"$i 100 &&
  46                test-tool genrandom "foo"$(( $i + 1 )) 100 &&
  47                test-tool genrandom "foo"$(( $i + 2 )) 100
  48        } >deep_delta_$iii &&
  49        {
  50                echo $iii &&
  51                test-tool genrandom "$iii" 8192
  52        } >file_$iii &&
  53        git update-index --add file_$iii deep_delta_$iii wide_delta_$iii
  54}
  55
  56commit_and_list_objects () {
  57        {
  58                echo 101 &&
  59                test-tool genrandom 100 8192;
  60        } >file_101 &&
  61        git update-index --add file_101 &&
  62        tree=$(git write-tree) &&
  63        commit=$(git commit-tree $tree -p HEAD</dev/null) &&
  64        {
  65                echo $tree &&
  66                git ls-tree $tree | sed -e "s/.* \\([0-9a-f]*\\)        .*/\\1/"
  67        } >obj-list &&
  68        git reset --hard $commit
  69}
  70
  71test_expect_success 'create objects' '
  72        test_commit initial &&
  73        for i in $(test_seq 1 5)
  74        do
  75                generate_objects $i
  76        done &&
  77        commit_and_list_objects
  78'
  79
  80test_expect_success 'write midx with one v1 pack' '
  81        pack=$(git pack-objects --index-version=1 $objdir/pack/test <obj-list) &&
  82        test_when_finished rm $objdir/pack/test-$pack.pack \
  83                $objdir/pack/test-$pack.idx $objdir/pack/multi-pack-index &&
  84        git multi-pack-index --object-dir=$objdir write &&
  85        midx_read_expect 1 18 4 $objdir
  86'
  87
  88midx_git_two_modes () {
  89        git -c core.multiPackIndex=false $1 >expect &&
  90        git -c core.multiPackIndex=true $1 >actual &&
  91        if [ "$2" = "sorted" ]
  92        then
  93                sort <expect >expect.sorted &&
  94                mv expect.sorted expect &&
  95                sort <actual >actual.sorted &&
  96                mv actual.sorted actual
  97        fi &&
  98        test_cmp expect actual
  99}
 100
 101compare_results_with_midx () {
 102        MSG=$1
 103        test_expect_success "check normal git operations: $MSG" '
 104                midx_git_two_modes "rev-list --objects --all" &&
 105                midx_git_two_modes "log --raw" &&
 106                midx_git_two_modes "count-objects --verbose" &&
 107                midx_git_two_modes "cat-file --batch-all-objects --batch-check" &&
 108                midx_git_two_modes "cat-file --batch-all-objects --batch-check --unordered" sorted
 109        '
 110}
 111
 112test_expect_success 'write midx with one v2 pack' '
 113        git pack-objects --index-version=2,0x40 $objdir/pack/test <obj-list &&
 114        git multi-pack-index --object-dir=$objdir write &&
 115        midx_read_expect 1 18 4 $objdir
 116'
 117
 118compare_results_with_midx "one v2 pack"
 119
 120test_expect_success 'corrupt idx not opened' '
 121        idx=$(test-tool read-midx $objdir | grep "\.idx\$") &&
 122        mv $objdir/pack/$idx backup-$idx &&
 123        test_when_finished "mv backup-\$idx \$objdir/pack/\$idx" &&
 124
 125        # This is the minimum size for a sha-1 based .idx; this lets
 126        # us pass perfunctory tests, but anything that actually opens and reads
 127        # the idx file will complain.
 128        test_copy_bytes 1064 <backup-$idx >$objdir/pack/$idx &&
 129
 130        git -c core.multiPackIndex=true rev-list --objects --all 2>err &&
 131        test_must_be_empty err
 132'
 133
 134test_expect_success 'add more objects' '
 135        for i in $(test_seq 6 10)
 136        do
 137                generate_objects $i
 138        done &&
 139        commit_and_list_objects
 140'
 141
 142test_expect_success 'write midx with two packs' '
 143        git pack-objects --index-version=1 $objdir/pack/test-2 <obj-list &&
 144        git multi-pack-index --object-dir=$objdir write &&
 145        midx_read_expect 2 34 4 $objdir
 146'
 147
 148compare_results_with_midx "two packs"
 149
 150test_expect_success 'add more packs' '
 151        for j in $(test_seq 11 20)
 152        do
 153                generate_objects $j &&
 154                commit_and_list_objects &&
 155                git pack-objects --index-version=2 $objdir/pack/test-pack <obj-list
 156        done
 157'
 158
 159compare_results_with_midx "mixed mode (two packs + extra)"
 160
 161test_expect_success 'write midx with twelve packs' '
 162        git multi-pack-index --object-dir=$objdir write &&
 163        midx_read_expect 12 74 4 $objdir
 164'
 165
 166compare_results_with_midx "twelve packs"
 167
 168test_expect_success 'verify multi-pack-index success' '
 169        git multi-pack-index verify --object-dir=$objdir
 170'
 171
 172# usage: corrupt_midx_and_verify <pos> <data> <objdir> <string>
 173corrupt_midx_and_verify() {
 174        POS=$1 &&
 175        DATA="${2:-\0}" &&
 176        OBJDIR=$3 &&
 177        GREPSTR="$4" &&
 178        COMMAND="$5" &&
 179        if test -z "$COMMAND"
 180        then
 181                COMMAND="git multi-pack-index verify --object-dir=$OBJDIR"
 182        fi &&
 183        FILE=$OBJDIR/pack/multi-pack-index &&
 184        chmod a+w $FILE &&
 185        test_when_finished mv midx-backup $FILE &&
 186        cp $FILE midx-backup &&
 187        printf "$DATA" | dd of="$FILE" bs=1 seek="$POS" conv=notrunc &&
 188        test_must_fail $COMMAND 2>test_err &&
 189        grep -v "^+" test_err >err &&
 190        test_i18ngrep "$GREPSTR" err
 191}
 192
 193test_expect_success 'verify bad signature' '
 194        corrupt_midx_and_verify 0 "\00" $objdir \
 195                "multi-pack-index signature"
 196'
 197
 198HASH_LEN=20
 199NUM_OBJECTS=74
 200MIDX_BYTE_VERSION=4
 201MIDX_BYTE_OID_VERSION=5
 202MIDX_BYTE_CHUNK_COUNT=6
 203MIDX_HEADER_SIZE=12
 204MIDX_BYTE_CHUNK_ID=$MIDX_HEADER_SIZE
 205MIDX_BYTE_CHUNK_OFFSET=$(($MIDX_HEADER_SIZE + 4))
 206MIDX_NUM_CHUNKS=5
 207MIDX_CHUNK_LOOKUP_WIDTH=12
 208MIDX_OFFSET_PACKNAMES=$(($MIDX_HEADER_SIZE + \
 209                         $MIDX_NUM_CHUNKS * $MIDX_CHUNK_LOOKUP_WIDTH))
 210MIDX_BYTE_PACKNAME_ORDER=$(($MIDX_OFFSET_PACKNAMES + 2))
 211MIDX_OFFSET_OID_FANOUT=$(($MIDX_OFFSET_PACKNAMES + 652))
 212MIDX_OID_FANOUT_WIDTH=4
 213MIDX_BYTE_OID_FANOUT_ORDER=$((MIDX_OFFSET_OID_FANOUT + 250 * $MIDX_OID_FANOUT_WIDTH + 1))
 214MIDX_OFFSET_OID_LOOKUP=$(($MIDX_OFFSET_OID_FANOUT + 256 * $MIDX_OID_FANOUT_WIDTH))
 215MIDX_BYTE_OID_LOOKUP=$(($MIDX_OFFSET_OID_LOOKUP + 16 * $HASH_LEN))
 216MIDX_OFFSET_OBJECT_OFFSETS=$(($MIDX_OFFSET_OID_LOOKUP + $NUM_OBJECTS * $HASH_LEN))
 217MIDX_OFFSET_WIDTH=8
 218MIDX_BYTE_PACK_INT_ID=$(($MIDX_OFFSET_OBJECT_OFFSETS + 16 * $MIDX_OFFSET_WIDTH + 2))
 219MIDX_BYTE_OFFSET=$(($MIDX_OFFSET_OBJECT_OFFSETS + 16 * $MIDX_OFFSET_WIDTH + 6))
 220
 221test_expect_success 'verify bad version' '
 222        corrupt_midx_and_verify $MIDX_BYTE_VERSION "\00" $objdir \
 223                "multi-pack-index version"
 224'
 225
 226test_expect_success 'verify bad OID version' '
 227        corrupt_midx_and_verify $MIDX_BYTE_OID_VERSION "\02" $objdir \
 228                "hash version"
 229'
 230
 231test_expect_success 'verify truncated chunk count' '
 232        corrupt_midx_and_verify $MIDX_BYTE_CHUNK_COUNT "\01" $objdir \
 233                "missing required"
 234'
 235
 236test_expect_success 'verify extended chunk count' '
 237        corrupt_midx_and_verify $MIDX_BYTE_CHUNK_COUNT "\07" $objdir \
 238                "terminating multi-pack-index chunk id appears earlier than expected"
 239'
 240
 241test_expect_success 'verify missing required chunk' '
 242        corrupt_midx_and_verify $MIDX_BYTE_CHUNK_ID "\01" $objdir \
 243                "missing required"
 244'
 245
 246test_expect_success 'verify invalid chunk offset' '
 247        corrupt_midx_and_verify $MIDX_BYTE_CHUNK_OFFSET "\01" $objdir \
 248                "invalid chunk offset (too large)"
 249'
 250
 251test_expect_success 'verify packnames out of order' '
 252        corrupt_midx_and_verify $MIDX_BYTE_PACKNAME_ORDER "z" $objdir \
 253                "pack names out of order"
 254'
 255
 256test_expect_success 'verify packnames out of order' '
 257        corrupt_midx_and_verify $MIDX_BYTE_PACKNAME_ORDER "a" $objdir \
 258                "failed to load pack"
 259'
 260
 261test_expect_success 'verify oid fanout out of order' '
 262        corrupt_midx_and_verify $MIDX_BYTE_OID_FANOUT_ORDER "\01" $objdir \
 263                "oid fanout out of order"
 264'
 265
 266test_expect_success 'verify oid lookup out of order' '
 267        corrupt_midx_and_verify $MIDX_BYTE_OID_LOOKUP "\00" $objdir \
 268                "oid lookup out of order"
 269'
 270
 271test_expect_success 'verify incorrect pack-int-id' '
 272        corrupt_midx_and_verify $MIDX_BYTE_PACK_INT_ID "\07" $objdir \
 273                "bad pack-int-id"
 274'
 275
 276test_expect_success 'verify incorrect offset' '
 277        corrupt_midx_and_verify $MIDX_BYTE_OFFSET "\07" $objdir \
 278                "incorrect object offset"
 279'
 280
 281test_expect_success 'git-fsck incorrect offset' '
 282        corrupt_midx_and_verify $MIDX_BYTE_OFFSET "\07" $objdir \
 283                "incorrect object offset" \
 284                "git -c core.multipackindex=true fsck"
 285'
 286
 287test_expect_success 'repack removes multi-pack-index' '
 288        test_path_is_file $objdir/pack/multi-pack-index &&
 289        GIT_TEST_MULTI_PACK_INDEX=0 git repack -adf &&
 290        test_path_is_missing $objdir/pack/multi-pack-index
 291'
 292
 293compare_results_with_midx "after repack"
 294
 295test_expect_success 'multi-pack-index and pack-bitmap' '
 296        git -c repack.writeBitmaps=true repack -ad &&
 297        git multi-pack-index write &&
 298        git rev-list --test-bitmap HEAD
 299'
 300
 301test_expect_success 'multi-pack-index and alternates' '
 302        git init --bare alt.git &&
 303        echo $(pwd)/alt.git/objects >.git/objects/info/alternates &&
 304        echo content1 >file1 &&
 305        altblob=$(GIT_DIR=alt.git git hash-object -w file1) &&
 306        git cat-file blob $altblob &&
 307        git rev-list --all
 308'
 309
 310compare_results_with_midx "with alternate (local midx)"
 311
 312test_expect_success 'multi-pack-index in an alternate' '
 313        mv .git/objects/pack/* alt.git/objects/pack &&
 314        test_commit add_local_objects &&
 315        git repack --local &&
 316        git multi-pack-index write &&
 317        midx_read_expect 1 3 4 $objdir &&
 318        git reset --hard HEAD~1 &&
 319        rm -f .git/objects/pack/*
 320'
 321
 322compare_results_with_midx "with alternate (remote midx)"
 323
 324# usage: corrupt_data <file> <pos> [<data>]
 325corrupt_data () {
 326        file=$1
 327        pos=$2
 328        data="${3:-\0}"
 329        printf "$data" | dd of="$file" bs=1 seek="$pos" conv=notrunc
 330}
 331
 332# Force 64-bit offsets by manipulating the idx file.
 333# This makes the IDX file _incorrect_ so be careful to clean up after!
 334test_expect_success 'force some 64-bit offsets with pack-objects' '
 335        mkdir objects64 &&
 336        mkdir objects64/pack &&
 337        for i in $(test_seq 1 11)
 338        do
 339                generate_objects 11
 340        done &&
 341        commit_and_list_objects &&
 342        pack64=$(git pack-objects --index-version=2,0x40 objects64/pack/test-64 <obj-list) &&
 343        idx64=objects64/pack/test-64-$pack64.idx &&
 344        chmod u+w $idx64 &&
 345        corrupt_data $idx64 2999 "\02" &&
 346        midx64=$(git multi-pack-index --object-dir=objects64 write) &&
 347        midx_read_expect 1 63 5 objects64 " large-offsets"
 348'
 349
 350test_expect_success 'verify multi-pack-index with 64-bit offsets' '
 351        git multi-pack-index verify --object-dir=objects64
 352'
 353
 354NUM_OBJECTS=63
 355MIDX_OFFSET_OID_FANOUT=$((MIDX_OFFSET_PACKNAMES + 54))
 356MIDX_OFFSET_OID_LOOKUP=$((MIDX_OFFSET_OID_FANOUT + 256 * $MIDX_OID_FANOUT_WIDTH))
 357MIDX_OFFSET_OBJECT_OFFSETS=$(($MIDX_OFFSET_OID_LOOKUP + $NUM_OBJECTS * $HASH_LEN))
 358MIDX_OFFSET_LARGE_OFFSETS=$(($MIDX_OFFSET_OBJECT_OFFSETS + $NUM_OBJECTS * $MIDX_OFFSET_WIDTH))
 359MIDX_BYTE_LARGE_OFFSET=$(($MIDX_OFFSET_LARGE_OFFSETS + 3))
 360
 361test_expect_success 'verify incorrect 64-bit offset' '
 362        corrupt_midx_and_verify $MIDX_BYTE_LARGE_OFFSET "\07" objects64 \
 363                "incorrect object offset"
 364'
 365
 366test_expect_success 'setup expire tests' '
 367        mkdir dup &&
 368        (
 369                cd dup &&
 370                git init &&
 371                test-tool genrandom "data" 4096 >large_file.txt &&
 372                git update-index --add large_file.txt &&
 373                for i in $(test_seq 1 20)
 374                do
 375                        test_commit $i
 376                done &&
 377                git branch A HEAD &&
 378                git branch B HEAD~8 &&
 379                git branch C HEAD~13 &&
 380                git branch D HEAD~16 &&
 381                git branch E HEAD~18 &&
 382                git pack-objects --revs .git/objects/pack/pack-A <<-EOF &&
 383                refs/heads/A
 384                ^refs/heads/B
 385                EOF
 386                git pack-objects --revs .git/objects/pack/pack-B <<-EOF &&
 387                refs/heads/B
 388                ^refs/heads/C
 389                EOF
 390                git pack-objects --revs .git/objects/pack/pack-C <<-EOF &&
 391                refs/heads/C
 392                ^refs/heads/D
 393                EOF
 394                git pack-objects --revs .git/objects/pack/pack-D <<-EOF &&
 395                refs/heads/D
 396                ^refs/heads/E
 397                EOF
 398                git pack-objects --revs .git/objects/pack/pack-E <<-EOF &&
 399                refs/heads/E
 400                EOF
 401                git multi-pack-index write &&
 402                cp -r .git/objects/pack .git/objects/pack-backup
 403        )
 404'
 405
 406test_expect_success 'expire does not remove any packs' '
 407        (
 408                cd dup &&
 409                ls .git/objects/pack >expect &&
 410                git multi-pack-index expire &&
 411                ls .git/objects/pack >actual &&
 412                test_cmp expect actual
 413        )
 414'
 415
 416test_expect_success 'expire removes unreferenced packs' '
 417        (
 418                cd dup &&
 419                git pack-objects --revs .git/objects/pack/pack-combined <<-EOF &&
 420                refs/heads/A
 421                ^refs/heads/C
 422                EOF
 423                git multi-pack-index write &&
 424                ls .git/objects/pack | grep -v -e pack-[AB] >expect &&
 425                git multi-pack-index expire &&
 426                ls .git/objects/pack >actual &&
 427                test_cmp expect actual &&
 428                ls .git/objects/pack/ | grep idx >expect-idx &&
 429                test-tool read-midx .git/objects | grep idx >actual-midx &&
 430                test_cmp expect-idx actual-midx &&
 431                git multi-pack-index verify &&
 432                git fsck
 433        )
 434'
 435
 436test_expect_success 'repack with minimum size does not alter existing packs' '
 437        (
 438                cd dup &&
 439                rm -rf .git/objects/pack &&
 440                mv .git/objects/pack-backup .git/objects/pack &&
 441                touch -m -t 201901010000 .git/objects/pack/pack-D* &&
 442                touch -m -t 201901010001 .git/objects/pack/pack-C* &&
 443                touch -m -t 201901010002 .git/objects/pack/pack-B* &&
 444                touch -m -t 201901010003 .git/objects/pack/pack-A* &&
 445                ls .git/objects/pack >expect &&
 446                MINSIZE=$(test-tool path-utils file-size .git/objects/pack/*pack | sort -n | head -n 1) &&
 447                git multi-pack-index repack --batch-size=$MINSIZE &&
 448                ls .git/objects/pack >actual &&
 449                test_cmp expect actual
 450        )
 451'
 452
 453test_expect_success 'repack creates a new pack' '
 454        (
 455                cd dup &&
 456                ls .git/objects/pack/*idx >idx-list &&
 457                test_line_count = 5 idx-list &&
 458                THIRD_SMALLEST_SIZE=$(test-tool path-utils file-size .git/objects/pack/*pack | sort -n | head -n 3 | tail -n 1) &&
 459                BATCH_SIZE=$(($THIRD_SMALLEST_SIZE + 1)) &&
 460                git multi-pack-index repack --batch-size=$BATCH_SIZE &&
 461                ls .git/objects/pack/*idx >idx-list &&
 462                test_line_count = 6 idx-list &&
 463                test-tool read-midx .git/objects | grep idx >midx-list &&
 464                test_line_count = 6 midx-list
 465        )
 466'
 467
 468test_expect_success 'expire removes repacked packs' '
 469        (
 470                cd dup &&
 471                ls -al .git/objects/pack/*pack &&
 472                ls -S .git/objects/pack/*pack | head -n 4 >expect &&
 473                git multi-pack-index expire &&
 474                ls -S .git/objects/pack/*pack >actual &&
 475                test_cmp expect actual &&
 476                test-tool read-midx .git/objects | grep idx >midx-list &&
 477                test_line_count = 4 midx-list
 478        )
 479'
 480
 481test_expect_success 'expire works when adding new packs' '
 482        (
 483                cd dup &&
 484                git pack-objects --revs .git/objects/pack/pack-combined <<-EOF &&
 485                refs/heads/A
 486                ^refs/heads/B
 487                EOF
 488                git pack-objects --revs .git/objects/pack/pack-combined <<-EOF &&
 489                refs/heads/B
 490                ^refs/heads/C
 491                EOF
 492                git pack-objects --revs .git/objects/pack/pack-combined <<-EOF &&
 493                refs/heads/C
 494                ^refs/heads/D
 495                EOF
 496                git multi-pack-index write &&
 497                git pack-objects --revs .git/objects/pack/a-pack <<-EOF &&
 498                refs/heads/D
 499                ^refs/heads/E
 500                EOF
 501                git multi-pack-index write &&
 502                git pack-objects --revs .git/objects/pack/z-pack <<-EOF &&
 503                refs/heads/E
 504                EOF
 505                git multi-pack-index expire &&
 506                ls .git/objects/pack/ | grep idx >expect &&
 507                test-tool read-midx .git/objects | grep idx >actual &&
 508                test_cmp expect actual &&
 509                git multi-pack-index verify
 510        )
 511'
 512
 513test_expect_success 'expire respects .keep files' '
 514        (
 515                cd dup &&
 516                git pack-objects --revs .git/objects/pack/pack-all <<-EOF &&
 517                refs/heads/A
 518                EOF
 519                git multi-pack-index write &&
 520                PACKA=$(ls .git/objects/pack/a-pack*\.pack | sed s/\.pack\$//) &&
 521                touch $PACKA.keep &&
 522                git multi-pack-index expire &&
 523                ls -S .git/objects/pack/a-pack* | grep $PACKA >a-pack-files &&
 524                test_line_count = 3 a-pack-files &&
 525                test-tool read-midx .git/objects | grep idx >midx-list &&
 526                test_line_count = 2 midx-list
 527        )
 528'
 529
 530test_expect_success 'repack --batch-size=0 repacks everything' '
 531        (
 532                cd dup &&
 533                rm .git/objects/pack/*.keep &&
 534                ls .git/objects/pack/*idx >idx-list &&
 535                test_line_count = 2 idx-list &&
 536                git multi-pack-index repack --batch-size=0 &&
 537                ls .git/objects/pack/*idx >idx-list &&
 538                test_line_count = 3 idx-list &&
 539                test-tool read-midx .git/objects | grep idx >midx-list &&
 540                test_line_count = 3 midx-list &&
 541                git multi-pack-index expire &&
 542                ls -al .git/objects/pack/*idx >idx-list &&
 543                test_line_count = 1 idx-list &&
 544                git multi-pack-index repack --batch-size=0 &&
 545                ls -al .git/objects/pack/*idx >new-idx-list &&
 546                test_cmp idx-list new-idx-list
 547        )
 548'
 549
 550test_done