828c240389f2a00dcf0f736f39e6c2dcfd1034ed
   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        if [ "$2" = "sorted" ]
  90        then
  91                git -c core.multiPackIndex=false $1 | sort >expect &&
  92                git -c core.multiPackIndex=true $1 | sort >actual
  93        else
  94                git -c core.multiPackIndex=false $1 >expect &&
  95                git -c core.multiPackIndex=true $1 >actual
  96        fi &&
  97        test_cmp expect actual
  98}
  99
 100compare_results_with_midx () {
 101        MSG=$1
 102        test_expect_success "check normal git operations: $MSG" '
 103                midx_git_two_modes "rev-list --objects --all" &&
 104                midx_git_two_modes "log --raw" &&
 105                midx_git_two_modes "count-objects --verbose" &&
 106                midx_git_two_modes "cat-file --batch-all-objects --buffer --batch-check" &&
 107                midx_git_two_modes "cat-file --batch-all-objects --buffer --batch-check --unsorted" sorted
 108        '
 109}
 110
 111test_expect_success 'write midx with one v2 pack' '
 112        git pack-objects --index-version=2,0x40 $objdir/pack/test <obj-list &&
 113        git multi-pack-index --object-dir=$objdir write &&
 114        midx_read_expect 1 18 4 $objdir
 115'
 116
 117compare_results_with_midx "one v2 pack"
 118
 119test_expect_success 'add more objects' '
 120        for i in $(test_seq 6 10)
 121        do
 122                generate_objects $i
 123        done &&
 124        commit_and_list_objects
 125'
 126
 127test_expect_success 'write midx with two packs' '
 128        git pack-objects --index-version=1 $objdir/pack/test-2 <obj-list &&
 129        git multi-pack-index --object-dir=$objdir write &&
 130        midx_read_expect 2 34 4 $objdir
 131'
 132
 133compare_results_with_midx "two packs"
 134
 135test_expect_success 'add more packs' '
 136        for j in $(test_seq 11 20)
 137        do
 138                generate_objects $j &&
 139                commit_and_list_objects &&
 140                git pack-objects --index-version=2 $objdir/pack/test-pack <obj-list
 141        done
 142'
 143
 144compare_results_with_midx "mixed mode (two packs + extra)"
 145
 146test_expect_success 'write midx with twelve packs' '
 147        git multi-pack-index --object-dir=$objdir write &&
 148        midx_read_expect 12 74 4 $objdir
 149'
 150
 151compare_results_with_midx "twelve packs"
 152
 153test_expect_success 'verify multi-pack-index success' '
 154        git multi-pack-index verify --object-dir=$objdir
 155'
 156
 157# usage: corrupt_midx_and_verify <pos> <data> <objdir> <string>
 158corrupt_midx_and_verify() {
 159        POS=$1 &&
 160        DATA="${2:-\0}" &&
 161        OBJDIR=$3 &&
 162        GREPSTR="$4" &&
 163        FILE=$OBJDIR/pack/multi-pack-index &&
 164        chmod a+w $FILE &&
 165        test_when_finished mv midx-backup $FILE &&
 166        cp $FILE midx-backup &&
 167        printf "$DATA" | dd of="$FILE" bs=1 seek="$POS" conv=notrunc &&
 168        test_must_fail git multi-pack-index verify --object-dir=$OBJDIR 2>test_err &&
 169        grep -v "^+" test_err >err &&
 170        test_i18ngrep "$GREPSTR" err
 171}
 172
 173test_expect_success 'verify bad signature' '
 174        corrupt_midx_and_verify 0 "\00" $objdir \
 175                "multi-pack-index signature"
 176'
 177
 178HASH_LEN=20
 179NUM_OBJECTS=74
 180MIDX_BYTE_VERSION=4
 181MIDX_BYTE_OID_VERSION=5
 182MIDX_BYTE_CHUNK_COUNT=6
 183MIDX_HEADER_SIZE=12
 184MIDX_BYTE_CHUNK_ID=$MIDX_HEADER_SIZE
 185MIDX_BYTE_CHUNK_OFFSET=$(($MIDX_HEADER_SIZE + 4))
 186MIDX_NUM_CHUNKS=5
 187MIDX_CHUNK_LOOKUP_WIDTH=12
 188MIDX_OFFSET_PACKNAMES=$(($MIDX_HEADER_SIZE + \
 189                         $MIDX_NUM_CHUNKS * $MIDX_CHUNK_LOOKUP_WIDTH))
 190MIDX_BYTE_PACKNAME_ORDER=$(($MIDX_OFFSET_PACKNAMES + 2))
 191MIDX_OFFSET_OID_FANOUT=$(($MIDX_OFFSET_PACKNAMES + 652))
 192MIDX_OID_FANOUT_WIDTH=4
 193MIDX_BYTE_OID_FANOUT_ORDER=$((MIDX_OFFSET_OID_FANOUT + 250 * $MIDX_OID_FANOUT_WIDTH + 1))
 194MIDX_OFFSET_OID_LOOKUP=$(($MIDX_OFFSET_OID_FANOUT + 256 * $MIDX_OID_FANOUT_WIDTH))
 195MIDX_BYTE_OID_LOOKUP=$(($MIDX_OFFSET_OID_LOOKUP + 16 * $HASH_LEN))
 196MIDX_OFFSET_OBJECT_OFFSETS=$(($MIDX_OFFSET_OID_LOOKUP + $NUM_OBJECTS * $HASH_LEN))
 197MIDX_OFFSET_WIDTH=8
 198MIDX_BYTE_PACK_INT_ID=$(($MIDX_OFFSET_OBJECT_OFFSETS + 16 * $MIDX_OFFSET_WIDTH + 2))
 199MIDX_BYTE_OFFSET=$(($MIDX_OFFSET_OBJECT_OFFSETS + 16 * $MIDX_OFFSET_WIDTH + 6))
 200
 201test_expect_success 'verify bad version' '
 202        corrupt_midx_and_verify $MIDX_BYTE_VERSION "\00" $objdir \
 203                "multi-pack-index version"
 204'
 205
 206test_expect_success 'verify bad OID version' '
 207        corrupt_midx_and_verify $MIDX_BYTE_OID_VERSION "\02" $objdir \
 208                "hash version"
 209'
 210
 211test_expect_success 'verify truncated chunk count' '
 212        corrupt_midx_and_verify $MIDX_BYTE_CHUNK_COUNT "\01" $objdir \
 213                "missing required"
 214'
 215
 216test_expect_success 'verify extended chunk count' '
 217        corrupt_midx_and_verify $MIDX_BYTE_CHUNK_COUNT "\07" $objdir \
 218                "terminating multi-pack-index chunk id appears earlier than expected"
 219'
 220
 221test_expect_success 'verify missing required chunk' '
 222        corrupt_midx_and_verify $MIDX_BYTE_CHUNK_ID "\01" $objdir \
 223                "missing required"
 224'
 225
 226test_expect_success 'verify invalid chunk offset' '
 227        corrupt_midx_and_verify $MIDX_BYTE_CHUNK_OFFSET "\01" $objdir \
 228                "invalid chunk offset (too large)"
 229'
 230
 231test_expect_success 'verify packnames out of order' '
 232        corrupt_midx_and_verify $MIDX_BYTE_PACKNAME_ORDER "z" $objdir \
 233                "pack names out of order"
 234'
 235
 236test_expect_success 'verify packnames out of order' '
 237        corrupt_midx_and_verify $MIDX_BYTE_PACKNAME_ORDER "a" $objdir \
 238                "failed to load pack"
 239'
 240
 241test_expect_success 'verify oid fanout out of order' '
 242        corrupt_midx_and_verify $MIDX_BYTE_OID_FANOUT_ORDER "\01" $objdir \
 243                "oid fanout out of order"
 244'
 245
 246test_expect_success 'verify oid lookup out of order' '
 247        corrupt_midx_and_verify $MIDX_BYTE_OID_LOOKUP "\00" $objdir \
 248                "oid lookup out of order"
 249'
 250
 251test_expect_success 'verify incorrect pack-int-id' '
 252        corrupt_midx_and_verify $MIDX_BYTE_PACK_INT_ID "\07" $objdir \
 253                "bad pack-int-id"
 254'
 255
 256test_expect_success 'verify incorrect offset' '
 257        corrupt_midx_and_verify $MIDX_BYTE_OFFSET "\07" $objdir \
 258                "incorrect object offset"
 259'
 260
 261test_expect_success 'repack removes multi-pack-index' '
 262        test_path_is_file $objdir/pack/multi-pack-index &&
 263        git repack -adf &&
 264        test_path_is_missing $objdir/pack/multi-pack-index
 265'
 266
 267compare_results_with_midx "after repack"
 268
 269test_expect_success 'multi-pack-index and pack-bitmap' '
 270        git -c repack.writeBitmaps=true repack -ad &&
 271        git multi-pack-index write &&
 272        git rev-list --test-bitmap HEAD
 273'
 274
 275test_expect_success 'multi-pack-index and alternates' '
 276        git init --bare alt.git &&
 277        echo $(pwd)/alt.git/objects >.git/objects/info/alternates &&
 278        echo content1 >file1 &&
 279        altblob=$(GIT_DIR=alt.git git hash-object -w file1) &&
 280        git cat-file blob $altblob &&
 281        git rev-list --all
 282'
 283
 284compare_results_with_midx "with alternate (local midx)"
 285
 286test_expect_success 'multi-pack-index in an alternate' '
 287        mv .git/objects/pack/* alt.git/objects/pack &&
 288        test_commit add_local_objects &&
 289        git repack --local &&
 290        git multi-pack-index write &&
 291        midx_read_expect 1 3 4 $objdir &&
 292        git reset --hard HEAD~1 &&
 293        rm -f .git/objects/pack/*
 294'
 295
 296compare_results_with_midx "with alternate (remote midx)"
 297
 298# usage: corrupt_data <file> <pos> [<data>]
 299corrupt_data () {
 300        file=$1
 301        pos=$2
 302        data="${3:-\0}"
 303        printf "$data" | dd of="$file" bs=1 seek="$pos" conv=notrunc
 304}
 305
 306# Force 64-bit offsets by manipulating the idx file.
 307# This makes the IDX file _incorrect_ so be careful to clean up after!
 308test_expect_success 'force some 64-bit offsets with pack-objects' '
 309        mkdir objects64 &&
 310        mkdir objects64/pack &&
 311        for i in $(test_seq 1 11)
 312        do
 313                generate_objects 11
 314        done &&
 315        commit_and_list_objects &&
 316        pack64=$(git pack-objects --index-version=2,0x40 objects64/pack/test-64 <obj-list) &&
 317        idx64=objects64/pack/test-64-$pack64.idx &&
 318        chmod u+w $idx64 &&
 319        corrupt_data $idx64 2999 "\02" &&
 320        midx64=$(git multi-pack-index --object-dir=objects64 write) &&
 321        midx_read_expect 1 63 5 objects64 " large-offsets"
 322'
 323
 324test_expect_success 'verify multi-pack-index with 64-bit offsets' '
 325        git multi-pack-index verify --object-dir=objects64
 326'
 327
 328NUM_OBJECTS=63
 329MIDX_OFFSET_OID_FANOUT=$((MIDX_OFFSET_PACKNAMES + 54))
 330MIDX_OFFSET_OID_LOOKUP=$((MIDX_OFFSET_OID_FANOUT + 256 * $MIDX_OID_FANOUT_WIDTH))
 331MIDX_OFFSET_OBJECT_OFFSETS=$(($MIDX_OFFSET_OID_LOOKUP + $NUM_OBJECTS * $HASH_LEN))
 332MIDX_OFFSET_LARGE_OFFSETS=$(($MIDX_OFFSET_OBJECT_OFFSETS + $NUM_OBJECTS * $MIDX_OFFSET_WIDTH))
 333MIDX_BYTE_LARGE_OFFSET=$(($MIDX_OFFSET_LARGE_OFFSETS + 3))
 334
 335test_expect_success 'verify incorrect 64-bit offset' '
 336        corrupt_midx_and_verify $MIDX_BYTE_LARGE_OFFSET "\07" objects64 \
 337                "incorrect object offset"
 338'
 339
 340test_done