t / t5310-pack-bitmaps.shon commit t1450: make hash size independent (8de6b38)
   1#!/bin/sh
   2
   3test_description='exercise basic bitmap functionality'
   4. ./test-lib.sh
   5
   6objpath () {
   7        echo ".git/objects/$(echo "$1" | sed -e 's|\(..\)|\1/|')"
   8}
   9
  10# show objects present in pack ($1 should be associated *.idx)
  11list_packed_objects () {
  12        git show-index <"$1" >object-list &&
  13        cut -d' ' -f2 object-list
  14}
  15
  16# has_any pattern-file content-file
  17# tests whether content-file has any entry from pattern-file with entries being
  18# whole lines.
  19has_any () {
  20        grep -Ff "$1" "$2"
  21}
  22
  23test_expect_success 'setup repo with moderate-sized history' '
  24        for i in $(test_seq 1 10)
  25        do
  26                test_commit $i
  27        done &&
  28        git checkout -b other HEAD~5 &&
  29        for i in $(test_seq 1 10)
  30        do
  31                test_commit side-$i
  32        done &&
  33        git checkout master &&
  34        bitmaptip=$(git rev-parse master) &&
  35        blob=$(echo tagged-blob | git hash-object -w --stdin) &&
  36        git tag tagged-blob $blob &&
  37        git config repack.writebitmaps true
  38'
  39
  40test_expect_success 'full repack creates bitmaps' '
  41        git repack -ad &&
  42        ls .git/objects/pack/ | grep bitmap >output &&
  43        test_line_count = 1 output
  44'
  45
  46test_expect_success 'rev-list --test-bitmap verifies bitmaps' '
  47        git rev-list --test-bitmap HEAD
  48'
  49
  50rev_list_tests() {
  51        state=$1
  52
  53        test_expect_success "counting commits via bitmap ($state)" '
  54                git rev-list --count HEAD >expect &&
  55                git rev-list --use-bitmap-index --count HEAD >actual &&
  56                test_cmp expect actual
  57        '
  58
  59        test_expect_success "counting partial commits via bitmap ($state)" '
  60                git rev-list --count HEAD~5..HEAD >expect &&
  61                git rev-list --use-bitmap-index --count HEAD~5..HEAD >actual &&
  62                test_cmp expect actual
  63        '
  64
  65        test_expect_success "counting commits with limit ($state)" '
  66                git rev-list --count -n 1 HEAD >expect &&
  67                git rev-list --use-bitmap-index --count -n 1 HEAD >actual &&
  68                test_cmp expect actual
  69        '
  70
  71        test_expect_success "counting non-linear history ($state)" '
  72                git rev-list --count other...master >expect &&
  73                git rev-list --use-bitmap-index --count other...master >actual &&
  74                test_cmp expect actual
  75        '
  76
  77        test_expect_success "counting commits with limiting ($state)" '
  78                git rev-list --count HEAD -- 1.t >expect &&
  79                git rev-list --use-bitmap-index --count HEAD -- 1.t >actual &&
  80                test_cmp expect actual
  81        '
  82
  83        test_expect_success "enumerate --objects ($state)" '
  84                git rev-list --objects --use-bitmap-index HEAD >tmp &&
  85                cut -d" " -f1 <tmp >tmp2 &&
  86                sort <tmp2 >actual &&
  87                git rev-list --objects HEAD >tmp &&
  88                cut -d" " -f1 <tmp >tmp2 &&
  89                sort <tmp2 >expect &&
  90                test_cmp expect actual
  91        '
  92
  93        test_expect_success "bitmap --objects handles non-commit objects ($state)" '
  94                git rev-list --objects --use-bitmap-index HEAD tagged-blob >actual &&
  95                grep $blob actual
  96        '
  97}
  98
  99rev_list_tests 'full bitmap'
 100
 101test_expect_success 'clone from bitmapped repository' '
 102        git clone --no-local --bare . clone.git &&
 103        git rev-parse HEAD >expect &&
 104        git --git-dir=clone.git rev-parse HEAD >actual &&
 105        test_cmp expect actual
 106'
 107
 108test_expect_success 'setup further non-bitmapped commits' '
 109        for i in $(test_seq 1 10)
 110        do
 111                test_commit further-$i
 112        done
 113'
 114
 115rev_list_tests 'partial bitmap'
 116
 117test_expect_success 'fetch (partial bitmap)' '
 118        git --git-dir=clone.git fetch origin master:master &&
 119        git rev-parse HEAD >expect &&
 120        git --git-dir=clone.git rev-parse HEAD >actual &&
 121        test_cmp expect actual
 122'
 123
 124test_expect_success 'incremental repack fails when bitmaps are requested' '
 125        test_commit more-1 &&
 126        test_must_fail git repack -d 2>err &&
 127        test_i18ngrep "Incremental repacks are incompatible with bitmap" err
 128'
 129
 130test_expect_success 'incremental repack can disable bitmaps' '
 131        test_commit more-2 &&
 132        git repack -d --no-write-bitmap-index
 133'
 134
 135test_expect_success 'pack-objects respects --local (non-local loose)' '
 136        git init --bare alt.git &&
 137        echo $(pwd)/alt.git/objects >.git/objects/info/alternates &&
 138        echo content1 >file1 &&
 139        # non-local loose object which is not present in bitmapped pack
 140        altblob=$(GIT_DIR=alt.git git hash-object -w file1) &&
 141        # non-local loose object which is also present in bitmapped pack
 142        git cat-file blob $blob | GIT_DIR=alt.git git hash-object -w --stdin &&
 143        git add file1 &&
 144        test_tick &&
 145        git commit -m commit_file1 &&
 146        echo HEAD | git pack-objects --local --stdout --revs >1.pack &&
 147        git index-pack 1.pack &&
 148        list_packed_objects 1.idx >1.objects &&
 149        printf "%s\n" "$altblob" "$blob" >nonlocal-loose &&
 150        ! has_any nonlocal-loose 1.objects
 151'
 152
 153test_expect_success 'pack-objects respects --honor-pack-keep (local non-bitmapped pack)' '
 154        echo content2 >file2 &&
 155        blob2=$(git hash-object -w file2) &&
 156        git add file2 &&
 157        test_tick &&
 158        git commit -m commit_file2 &&
 159        printf "%s\n" "$blob2" "$bitmaptip" >keepobjects &&
 160        pack2=$(git pack-objects pack2 <keepobjects) &&
 161        mv pack2-$pack2.* .git/objects/pack/ &&
 162        >.git/objects/pack/pack2-$pack2.keep &&
 163        rm $(objpath $blob2) &&
 164        echo HEAD | git pack-objects --honor-pack-keep --stdout --revs >2a.pack &&
 165        git index-pack 2a.pack &&
 166        list_packed_objects 2a.idx >2a.objects &&
 167        ! has_any keepobjects 2a.objects
 168'
 169
 170test_expect_success 'pack-objects respects --local (non-local pack)' '
 171        mv .git/objects/pack/pack2-$pack2.* alt.git/objects/pack/ &&
 172        echo HEAD | git pack-objects --local --stdout --revs >2b.pack &&
 173        git index-pack 2b.pack &&
 174        list_packed_objects 2b.idx >2b.objects &&
 175        ! has_any keepobjects 2b.objects
 176'
 177
 178test_expect_success 'pack-objects respects --honor-pack-keep (local bitmapped pack)' '
 179        ls .git/objects/pack/ | grep bitmap >output &&
 180        test_line_count = 1 output &&
 181        packbitmap=$(basename $(cat output) .bitmap) &&
 182        list_packed_objects .git/objects/pack/$packbitmap.idx >packbitmap.objects &&
 183        test_when_finished "rm -f .git/objects/pack/$packbitmap.keep" &&
 184        >.git/objects/pack/$packbitmap.keep &&
 185        echo HEAD | git pack-objects --honor-pack-keep --stdout --revs >3a.pack &&
 186        git index-pack 3a.pack &&
 187        list_packed_objects 3a.idx >3a.objects &&
 188        ! has_any packbitmap.objects 3a.objects
 189'
 190
 191test_expect_success 'pack-objects respects --local (non-local bitmapped pack)' '
 192        mv .git/objects/pack/$packbitmap.* alt.git/objects/pack/ &&
 193        rm -f .git/objects/pack/multi-pack-index &&
 194        test_when_finished "mv alt.git/objects/pack/$packbitmap.* .git/objects/pack/" &&
 195        echo HEAD | git pack-objects --local --stdout --revs >3b.pack &&
 196        git index-pack 3b.pack &&
 197        list_packed_objects 3b.idx >3b.objects &&
 198        ! has_any packbitmap.objects 3b.objects
 199'
 200
 201test_expect_success 'pack-objects to file can use bitmap' '
 202        # make sure we still have 1 bitmap index from previous tests
 203        ls .git/objects/pack/ | grep bitmap >output &&
 204        test_line_count = 1 output &&
 205        # verify equivalent packs are generated with/without using bitmap index
 206        packasha1=$(git pack-objects --no-use-bitmap-index --all packa </dev/null) &&
 207        packbsha1=$(git pack-objects --use-bitmap-index --all packb </dev/null) &&
 208        list_packed_objects packa-$packasha1.idx >packa.objects &&
 209        list_packed_objects packb-$packbsha1.idx >packb.objects &&
 210        test_cmp packa.objects packb.objects
 211'
 212
 213test_expect_success 'full repack, reusing previous bitmaps' '
 214        git repack -ad &&
 215        ls .git/objects/pack/ | grep bitmap >output &&
 216        test_line_count = 1 output
 217'
 218
 219test_expect_success 'fetch (full bitmap)' '
 220        git --git-dir=clone.git fetch origin master:master &&
 221        git rev-parse HEAD >expect &&
 222        git --git-dir=clone.git rev-parse HEAD >actual &&
 223        test_cmp expect actual
 224'
 225
 226test_expect_success 'create objects for missing-HAVE tests' '
 227        blob=$(echo "missing have" | git hash-object -w --stdin) &&
 228        tree=$(printf "100644 blob $blob\tfile\n" | git mktree) &&
 229        parent=$(echo parent | git commit-tree $tree) &&
 230        commit=$(echo commit | git commit-tree $tree -p $parent) &&
 231        cat >revs <<-EOF
 232        HEAD
 233        ^HEAD^
 234        ^$commit
 235        EOF
 236'
 237
 238test_expect_success 'pack-objects respects --incremental' '
 239        cat >revs2 <<-EOF &&
 240        HEAD
 241        $commit
 242        EOF
 243        git pack-objects --incremental --stdout --revs <revs2 >4.pack &&
 244        git index-pack 4.pack &&
 245        list_packed_objects 4.idx >4.objects &&
 246        test_line_count = 4 4.objects &&
 247        git rev-list --objects $commit >revlist &&
 248        cut -d" " -f1 revlist |sort >objects &&
 249        test_cmp 4.objects objects
 250'
 251
 252test_expect_success 'pack with missing blob' '
 253        rm $(objpath $blob) &&
 254        git pack-objects --stdout --revs <revs >/dev/null
 255'
 256
 257test_expect_success 'pack with missing tree' '
 258        rm $(objpath $tree) &&
 259        git pack-objects --stdout --revs <revs >/dev/null
 260'
 261
 262test_expect_success 'pack with missing parent' '
 263        rm $(objpath $parent) &&
 264        git pack-objects --stdout --revs <revs >/dev/null
 265'
 266
 267test_expect_success JGIT 'we can read jgit bitmaps' '
 268        git clone --bare . compat-jgit.git &&
 269        (
 270                cd compat-jgit.git &&
 271                rm -f objects/pack/*.bitmap &&
 272                jgit gc &&
 273                git rev-list --test-bitmap HEAD
 274        )
 275'
 276
 277test_expect_success JGIT 'jgit can read our bitmaps' '
 278        git clone --bare . compat-us.git &&
 279        (
 280                cd compat-us.git &&
 281                git repack -adb &&
 282                # jgit gc will barf if it does not like our bitmaps
 283                jgit gc
 284        )
 285'
 286
 287test_expect_success 'splitting packs does not generate bogus bitmaps' '
 288        test-tool genrandom foo $((1024 * 1024)) >rand &&
 289        git add rand &&
 290        git commit -m "commit with big file" &&
 291        git -c pack.packSizeLimit=500k repack -adb &&
 292        git init --bare no-bitmaps.git &&
 293        git -C no-bitmaps.git fetch .. HEAD
 294'
 295
 296test_expect_success 'set up reusable pack' '
 297        rm -f .git/objects/pack/*.keep &&
 298        git repack -adb &&
 299        reusable_pack () {
 300                git for-each-ref --format="%(objectname)" |
 301                git pack-objects --delta-base-offset --revs --stdout "$@"
 302        }
 303'
 304
 305test_expect_success 'pack reuse respects --honor-pack-keep' '
 306        test_when_finished "rm -f .git/objects/pack/*.keep" &&
 307        for i in .git/objects/pack/*.pack
 308        do
 309                >${i%.pack}.keep
 310        done &&
 311        reusable_pack --honor-pack-keep >empty.pack &&
 312        git index-pack empty.pack &&
 313        git show-index <empty.idx >actual &&
 314        test_must_be_empty actual
 315'
 316
 317test_expect_success 'pack reuse respects --local' '
 318        mv .git/objects/pack/* alt.git/objects/pack/ &&
 319        test_when_finished "mv alt.git/objects/pack/* .git/objects/pack/" &&
 320        reusable_pack --local >empty.pack &&
 321        git index-pack empty.pack &&
 322        git show-index <empty.idx >actual &&
 323        test_must_be_empty actual
 324'
 325
 326test_expect_success 'pack reuse respects --incremental' '
 327        reusable_pack --incremental >empty.pack &&
 328        git index-pack empty.pack &&
 329        git show-index <empty.idx >actual &&
 330        test_must_be_empty actual
 331'
 332
 333test_expect_success 'truncated bitmap fails gracefully' '
 334        git repack -ad &&
 335        git rev-list --use-bitmap-index --count --all >expect &&
 336        bitmap=$(ls .git/objects/pack/*.bitmap) &&
 337        test_when_finished "rm -f $bitmap" &&
 338        test_copy_bytes 512 <$bitmap >$bitmap.tmp &&
 339        mv -f $bitmap.tmp $bitmap &&
 340        git rev-list --use-bitmap-index --count --all >actual 2>stderr &&
 341        test_cmp expect actual &&
 342        test_i18ngrep corrupt stderr
 343'
 344
 345# have_delta <obj> <expected_base>
 346#
 347# Note that because this relies on cat-file, it might find _any_ copy of an
 348# object in the repository. The caller is responsible for making sure
 349# there's only one (e.g., via "repack -ad", or having just fetched a copy).
 350have_delta () {
 351        echo $2 >expect &&
 352        echo $1 | git cat-file --batch-check="%(deltabase)" >actual &&
 353        test_cmp expect actual
 354}
 355
 356# Create a state of history with these properties:
 357#
 358#  - refs that allow a client to fetch some new history, while sharing some old
 359#    history with the server; we use branches delta-reuse-old and
 360#    delta-reuse-new here
 361#
 362#  - the new history contains an object that is stored on the server as a delta
 363#    against a base that is in the old history
 364#
 365#  - the base object is not immediately reachable from the tip of the old
 366#    history; finding it would involve digging down through history we know the
 367#    other side has
 368#
 369# This should result in a state where fetching from old->new would not
 370# traditionally reuse the on-disk delta (because we'd have to dig to realize
 371# that the client has it), but we will do so if bitmaps can tell us cheaply
 372# that the other side has it.
 373test_expect_success 'set up thin delta-reuse parent' '
 374        # This first commit contains the buried base object.
 375        test-tool genrandom delta 16384 >file &&
 376        git add file &&
 377        git commit -m "delta base" &&
 378        base=$(git rev-parse --verify HEAD:file) &&
 379
 380        # These intermediate commits bury the base back in history.
 381        # This becomes the "old" state.
 382        for i in 1 2 3 4 5
 383        do
 384                echo $i >file &&
 385                git commit -am "intermediate $i" || return 1
 386        done &&
 387        git branch delta-reuse-old &&
 388
 389        # And now our new history has a delta against the buried base. Note
 390        # that this must be smaller than the original file, since pack-objects
 391        # prefers to create deltas from smaller objects to larger.
 392        test-tool genrandom delta 16300 >file &&
 393        git commit -am "delta result" &&
 394        delta=$(git rev-parse --verify HEAD:file) &&
 395        git branch delta-reuse-new &&
 396
 397        # Repack with bitmaps and double check that we have the expected delta
 398        # relationship.
 399        git repack -adb &&
 400        have_delta $delta $base
 401'
 402
 403# Now we can sanity-check the non-bitmap behavior (that the server is not able
 404# to reuse the delta). This isn't strictly something we care about, so this
 405# test could be scrapped in the future. But it makes sure that the next test is
 406# actually triggering the feature we want.
 407#
 408# Note that our tools for working with on-the-wire "thin" packs are limited. So
 409# we actually perform the fetch, retain the resulting pack, and inspect the
 410# result.
 411test_expect_success 'fetch without bitmaps ignores delta against old base' '
 412        test_config pack.usebitmaps false &&
 413        test_when_finished "rm -rf client.git" &&
 414        git init --bare client.git &&
 415        (
 416                cd client.git &&
 417                git config transfer.unpackLimit 1 &&
 418                git fetch .. delta-reuse-old:delta-reuse-old &&
 419                git fetch .. delta-reuse-new:delta-reuse-new &&
 420                have_delta $delta $ZERO_OID
 421        )
 422'
 423
 424# And do the same for the bitmap case, where we do expect to find the delta.
 425test_expect_success 'fetch with bitmaps can reuse old base' '
 426        test_config pack.usebitmaps true &&
 427        test_when_finished "rm -rf client.git" &&
 428        git init --bare client.git &&
 429        (
 430                cd client.git &&
 431                git config transfer.unpackLimit 1 &&
 432                git fetch .. delta-reuse-old:delta-reuse-old &&
 433                git fetch .. delta-reuse-new:delta-reuse-new &&
 434                have_delta $delta $base
 435        )
 436'
 437
 438test_done