t / t5317-pack-objects-filter-objects.shon commit doc: promote "git restore" (80f537f)
   1#!/bin/sh
   2
   3test_description='git pack-objects using object filtering'
   4
   5. ./test-lib.sh
   6
   7# Test blob:none filter.
   8
   9test_expect_success 'setup r1' '
  10        echo "{print \$1}" >print_1.awk &&
  11        echo "{print \$2}" >print_2.awk &&
  12
  13        git init r1 &&
  14        for n in 1 2 3 4 5
  15        do
  16                echo "This is file: $n" > r1/file.$n
  17                git -C r1 add file.$n
  18                git -C r1 commit -m "$n"
  19        done
  20'
  21
  22test_expect_success 'verify blob count in normal packfile' '
  23        git -C r1 ls-files -s file.1 file.2 file.3 file.4 file.5 \
  24                >ls_files_result &&
  25        awk -f print_2.awk ls_files_result |
  26        sort >expected &&
  27
  28        git -C r1 pack-objects --rev --stdout >all.pack <<-EOF &&
  29        HEAD
  30        EOF
  31        git -C r1 index-pack ../all.pack &&
  32
  33        git -C r1 verify-pack -v ../all.pack >verify_result &&
  34        grep blob verify_result |
  35        awk -f print_1.awk |
  36        sort >observed &&
  37
  38        test_cmp expected observed
  39'
  40
  41test_expect_success 'verify blob:none packfile has no blobs' '
  42        git -C r1 pack-objects --rev --stdout --filter=blob:none >filter.pack <<-EOF &&
  43        HEAD
  44        EOF
  45        git -C r1 index-pack ../filter.pack &&
  46
  47        git -C r1 verify-pack -v ../filter.pack >verify_result &&
  48        grep blob verify_result |
  49        awk -f print_1.awk |
  50        sort >observed &&
  51
  52        nr=$(wc -l <observed) &&
  53        test 0 -eq $nr
  54'
  55
  56test_expect_success 'verify normal and blob:none packfiles have same commits/trees' '
  57        git -C r1 verify-pack -v ../all.pack >verify_result &&
  58        grep -E "commit|tree" verify_result |
  59        awk -f print_1.awk |
  60        sort >expected &&
  61
  62        git -C r1 verify-pack -v ../filter.pack >verify_result &&
  63        grep -E "commit|tree" verify_result |
  64        awk -f print_1.awk |
  65        sort >observed &&
  66
  67        test_cmp expected observed
  68'
  69
  70test_expect_success 'get an error for missing tree object' '
  71        git init r5 &&
  72        echo foo >r5/foo &&
  73        git -C r5 add foo &&
  74        git -C r5 commit -m "foo" &&
  75        del=$(git -C r5 rev-parse HEAD^{tree} | sed "s|..|&/|") &&
  76        rm r5/.git/objects/$del &&
  77        test_must_fail git -C r5 pack-objects --rev --stdout 2>bad_tree <<-EOF &&
  78        HEAD
  79        EOF
  80        grep "bad tree object" bad_tree
  81'
  82
  83test_expect_success 'setup for tests of tree:0' '
  84        mkdir r1/subtree &&
  85        echo "This is a file in a subtree" >r1/subtree/file &&
  86        git -C r1 add subtree/file &&
  87        git -C r1 commit -m subtree
  88'
  89
  90test_expect_success 'verify tree:0 packfile has no blobs or trees' '
  91        git -C r1 pack-objects --rev --stdout --filter=tree:0 >commitsonly.pack <<-EOF &&
  92        HEAD
  93        EOF
  94        git -C r1 index-pack ../commitsonly.pack &&
  95        git -C r1 verify-pack -v ../commitsonly.pack >objs &&
  96        ! grep -E "tree|blob" objs
  97'
  98
  99test_expect_success 'grab tree directly when using tree:0' '
 100        # We should get the tree specified directly but not its blobs or subtrees.
 101        git -C r1 pack-objects --rev --stdout --filter=tree:0 >commitsonly.pack <<-EOF &&
 102        HEAD:
 103        EOF
 104        git -C r1 index-pack ../commitsonly.pack &&
 105        git -C r1 verify-pack -v ../commitsonly.pack >objs &&
 106        awk "/tree|blob/{print \$1}" objs >trees_and_blobs &&
 107        git -C r1 rev-parse HEAD: >expected &&
 108        test_cmp expected trees_and_blobs
 109'
 110
 111# Test blob:limit=<n>[kmg] filter.
 112# We boundary test around the size parameter.  The filter is strictly less than
 113# the value, so size 500 and 1000 should have the same results, but 1001 should
 114# filter more.
 115
 116test_expect_success 'setup r2' '
 117        git init r2 &&
 118        for n in 1000 10000
 119        do
 120                printf "%"$n"s" X > r2/large.$n
 121                git -C r2 add large.$n
 122                git -C r2 commit -m "$n"
 123        done
 124'
 125
 126test_expect_success 'verify blob count in normal packfile' '
 127        git -C r2 ls-files -s large.1000 large.10000 >ls_files_result &&
 128        awk -f print_2.awk ls_files_result |
 129        sort >expected &&
 130
 131        git -C r2 pack-objects --rev --stdout >all.pack <<-EOF &&
 132        HEAD
 133        EOF
 134        git -C r2 index-pack ../all.pack &&
 135
 136        git -C r2 verify-pack -v ../all.pack >verify_result &&
 137        grep blob verify_result |
 138        awk -f print_1.awk |
 139        sort >observed &&
 140
 141        test_cmp expected observed
 142'
 143
 144test_expect_success 'verify blob:limit=500 omits all blobs' '
 145        git -C r2 pack-objects --rev --stdout --filter=blob:limit=500 >filter.pack <<-EOF &&
 146        HEAD
 147        EOF
 148        git -C r2 index-pack ../filter.pack &&
 149
 150        git -C r2 verify-pack -v ../filter.pack >verify_result &&
 151        grep blob verify_result |
 152        awk -f print_1.awk |
 153        sort >observed &&
 154
 155        nr=$(wc -l <observed) &&
 156        test 0 -eq $nr
 157'
 158
 159test_expect_success 'verify blob:limit=1000' '
 160        git -C r2 pack-objects --rev --stdout --filter=blob:limit=1000 >filter.pack <<-EOF &&
 161        HEAD
 162        EOF
 163        git -C r2 index-pack ../filter.pack &&
 164
 165        git -C r2 verify-pack -v ../filter.pack >verify_result &&
 166        grep blob verify_result |
 167        awk -f print_1.awk |
 168        sort >observed &&
 169
 170        nr=$(wc -l <observed) &&
 171        test 0 -eq $nr
 172'
 173
 174test_expect_success 'verify blob:limit=1001' '
 175        git -C r2 ls-files -s large.1000 >ls_files_result &&
 176        awk -f print_2.awk ls_files_result |
 177        sort >expected &&
 178
 179        git -C r2 pack-objects --rev --stdout --filter=blob:limit=1001 >filter.pack <<-EOF &&
 180        HEAD
 181        EOF
 182        git -C r2 index-pack ../filter.pack &&
 183
 184        git -C r2 verify-pack -v ../filter.pack >verify_result &&
 185        grep blob verify_result |
 186        awk -f print_1.awk |
 187        sort >observed &&
 188
 189        test_cmp expected observed
 190'
 191
 192test_expect_success 'verify blob:limit=10001' '
 193        git -C r2 ls-files -s large.1000 large.10000 >ls_files_result &&
 194        awk -f print_2.awk ls_files_result |
 195        sort >expected &&
 196
 197        git -C r2 pack-objects --rev --stdout --filter=blob:limit=10001 >filter.pack <<-EOF &&
 198        HEAD
 199        EOF
 200        git -C r2 index-pack ../filter.pack &&
 201
 202        git -C r2 verify-pack -v ../filter.pack >verify_result &&
 203        grep blob verify_result |
 204        awk -f print_1.awk |
 205        sort >observed &&
 206
 207        test_cmp expected observed
 208'
 209
 210test_expect_success 'verify blob:limit=1k' '
 211        git -C r2 ls-files -s large.1000 >ls_files_result &&
 212        awk -f print_2.awk ls_files_result |
 213        sort >expected &&
 214
 215        git -C r2 pack-objects --rev --stdout --filter=blob:limit=1k >filter.pack <<-EOF &&
 216        HEAD
 217        EOF
 218        git -C r2 index-pack ../filter.pack &&
 219
 220        git -C r2 verify-pack -v ../filter.pack >verify_result &&
 221        grep blob verify_result |
 222        awk -f print_1.awk |
 223        sort >observed &&
 224
 225        test_cmp expected observed
 226'
 227
 228test_expect_success 'verify explicitly specifying oversized blob in input' '
 229        git -C r2 ls-files -s large.1000 large.10000 >ls_files_result &&
 230        awk -f print_2.awk ls_files_result |
 231        sort >expected &&
 232
 233        git -C r2 pack-objects --rev --stdout --filter=blob:limit=1k >filter.pack <<-EOF &&
 234        HEAD
 235        $(git -C r2 rev-parse HEAD:large.10000)
 236        EOF
 237        git -C r2 index-pack ../filter.pack &&
 238
 239        git -C r2 verify-pack -v ../filter.pack >verify_result &&
 240        grep blob verify_result |
 241        awk -f print_1.awk |
 242        sort >observed &&
 243
 244        test_cmp expected observed
 245'
 246
 247test_expect_success 'verify blob:limit=1m' '
 248        git -C r2 ls-files -s large.1000 large.10000 >ls_files_result &&
 249        awk -f print_2.awk ls_files_result |
 250        sort >expected &&
 251
 252        git -C r2 pack-objects --rev --stdout --filter=blob:limit=1m >filter.pack <<-EOF &&
 253        HEAD
 254        EOF
 255        git -C r2 index-pack ../filter.pack &&
 256
 257        git -C r2 verify-pack -v ../filter.pack >verify_result &&
 258        grep blob verify_result |
 259        awk -f print_1.awk |
 260        sort >observed &&
 261
 262        test_cmp expected observed
 263'
 264
 265test_expect_success 'verify normal and blob:limit packfiles have same commits/trees' '
 266        git -C r2 verify-pack -v ../all.pack >verify_result &&
 267        grep -E "commit|tree" verify_result |
 268        awk -f print_1.awk |
 269        sort >expected &&
 270
 271        git -C r2 verify-pack -v ../filter.pack >verify_result &&
 272        grep -E "commit|tree" verify_result |
 273        awk -f print_1.awk |
 274        sort >observed &&
 275
 276        test_cmp expected observed
 277'
 278
 279# Test sparse:path=<path> filter.
 280# Use a local file containing a sparse-checkout specification to filter
 281# out blobs not required for the corresponding sparse-checkout.  We do not
 282# require sparse-checkout to actually be enabled.
 283
 284test_expect_success 'setup r3' '
 285        git init r3 &&
 286        mkdir r3/dir1 &&
 287        for n in sparse1 sparse2
 288        do
 289                echo "This is file: $n" > r3/$n
 290                git -C r3 add $n
 291                echo "This is file: dir1/$n" > r3/dir1/$n
 292                git -C r3 add dir1/$n
 293        done &&
 294        git -C r3 commit -m "sparse" &&
 295        echo dir1/ >pattern1 &&
 296        echo sparse1 >pattern2
 297'
 298
 299test_expect_success 'verify blob count in normal packfile' '
 300        git -C r3 ls-files -s sparse1 sparse2 dir1/sparse1 dir1/sparse2 \
 301                >ls_files_result &&
 302        awk -f print_2.awk ls_files_result |
 303        sort >expected &&
 304
 305        git -C r3 pack-objects --rev --stdout >all.pack <<-EOF &&
 306        HEAD
 307        EOF
 308        git -C r3 index-pack ../all.pack &&
 309
 310        git -C r3 verify-pack -v ../all.pack >verify_result &&
 311        grep blob verify_result |
 312        awk -f print_1.awk |
 313        sort >observed &&
 314
 315        test_cmp expected observed
 316'
 317
 318test_expect_success 'verify sparse:path=pattern1' '
 319        git -C r3 ls-files -s dir1/sparse1 dir1/sparse2 >ls_files_result &&
 320        awk -f print_2.awk ls_files_result |
 321        sort >expected &&
 322
 323        git -C r3 pack-objects --rev --stdout --filter=sparse:path=../pattern1 >filter.pack <<-EOF &&
 324        HEAD
 325        EOF
 326        git -C r3 index-pack ../filter.pack &&
 327
 328        git -C r3 verify-pack -v ../filter.pack >verify_result &&
 329        grep blob verify_result |
 330        awk -f print_1.awk |
 331        sort >observed &&
 332
 333        test_cmp expected observed
 334'
 335
 336test_expect_success 'verify normal and sparse:path=pattern1 packfiles have same commits/trees' '
 337        git -C r3 verify-pack -v ../all.pack >verify_result &&
 338        grep -E "commit|tree" verify_result |
 339        awk -f print_1.awk |
 340        sort >expected &&
 341
 342        git -C r3 verify-pack -v ../filter.pack >verify_result &&
 343        grep -E "commit|tree" verify_result |
 344        awk -f print_1.awk |
 345        sort >observed &&
 346
 347        test_cmp expected observed
 348'
 349
 350test_expect_success 'verify sparse:path=pattern2' '
 351        git -C r3 ls-files -s sparse1 dir1/sparse1 >ls_files_result &&
 352        awk -f print_2.awk ls_files_result |
 353        sort >expected &&
 354
 355        git -C r3 pack-objects --rev --stdout --filter=sparse:path=../pattern2 >filter.pack <<-EOF &&
 356        HEAD
 357        EOF
 358        git -C r3 index-pack ../filter.pack &&
 359
 360        git -C r3 verify-pack -v ../filter.pack >verify_result &&
 361        grep blob verify_result |
 362        awk -f print_1.awk |
 363        sort >observed &&
 364
 365        test_cmp expected observed
 366'
 367
 368test_expect_success 'verify normal and sparse:path=pattern2 packfiles have same commits/trees' '
 369        git -C r3 verify-pack -v ../all.pack >verify_result &&
 370        grep -E "commit|tree" verify_result |
 371        awk -f print_1.awk |
 372        sort >expected &&
 373
 374        git -C r3 verify-pack -v ../filter.pack >verify_result &&
 375        grep -E "commit|tree" verify_result |
 376        awk -f print_1.awk |
 377        sort >observed &&
 378
 379        test_cmp expected observed
 380'
 381
 382# Test sparse:oid=<oid-ish> filter.
 383# Like sparse:path, but we get the sparse-checkout specification from
 384# a blob rather than a file on disk.
 385
 386test_expect_success 'setup r4' '
 387        git init r4 &&
 388        mkdir r4/dir1 &&
 389        for n in sparse1 sparse2
 390        do
 391                echo "This is file: $n" > r4/$n
 392                git -C r4 add $n
 393                echo "This is file: dir1/$n" > r4/dir1/$n
 394                git -C r4 add dir1/$n
 395        done &&
 396        echo dir1/ >r4/pattern &&
 397        git -C r4 add pattern &&
 398        git -C r4 commit -m "pattern"
 399'
 400
 401test_expect_success 'verify blob count in normal packfile' '
 402        git -C r4 ls-files -s pattern sparse1 sparse2 dir1/sparse1 dir1/sparse2 \
 403                >ls_files_result &&
 404        awk -f print_2.awk ls_files_result |
 405        sort >expected &&
 406
 407        git -C r4 pack-objects --rev --stdout >all.pack <<-EOF &&
 408        HEAD
 409        EOF
 410        git -C r4 index-pack ../all.pack &&
 411
 412        git -C r4 verify-pack -v ../all.pack >verify_result &&
 413        grep blob verify_result |
 414        awk -f print_1.awk |
 415        sort >observed &&
 416
 417        test_cmp expected observed
 418'
 419
 420test_expect_success 'verify sparse:oid=OID' '
 421        git -C r4 ls-files -s dir1/sparse1 dir1/sparse2 >ls_files_result &&
 422        awk -f print_2.awk ls_files_result |
 423        sort >expected &&
 424
 425        oid=$(git -C r4 ls-files -s pattern | awk -f print_2.awk) &&
 426        git -C r4 pack-objects --rev --stdout --filter=sparse:oid=$oid >filter.pack <<-EOF &&
 427        HEAD
 428        EOF
 429        git -C r4 index-pack ../filter.pack &&
 430
 431        git -C r4 verify-pack -v ../filter.pack >verify_result &&
 432        grep blob verify_result |
 433        awk -f print_1.awk |
 434        sort >observed &&
 435
 436        test_cmp expected observed
 437'
 438
 439test_expect_success 'verify sparse:oid=oid-ish' '
 440        git -C r4 ls-files -s dir1/sparse1 dir1/sparse2 >ls_files_result &&
 441        awk -f print_2.awk ls_files_result |
 442        sort >expected &&
 443
 444        git -C r4 pack-objects --rev --stdout --filter=sparse:oid=master:pattern >filter.pack <<-EOF &&
 445        HEAD
 446        EOF
 447        git -C r4 index-pack ../filter.pack &&
 448
 449        git -C r4 verify-pack -v ../filter.pack >verify_result &&
 450        grep blob verify_result |
 451        awk -f print_1.awk |
 452        sort >observed &&
 453
 454        test_cmp expected observed
 455'
 456
 457# Delete some loose objects and use pack-objects, but WITHOUT any filtering.
 458# This models previously omitted objects that we did not receive.
 459
 460test_expect_success 'setup r1 - delete loose blobs' '
 461        git -C r1 ls-files -s file.1 file.2 file.3 file.4 file.5 \
 462                >ls_files_result &&
 463        awk -f print_2.awk ls_files_result |
 464        sort >expected &&
 465
 466        for id in `cat expected | sed "s|..|&/|"`
 467        do
 468                rm r1/.git/objects/$id
 469        done
 470'
 471
 472test_expect_success 'verify pack-objects fails w/ missing objects' '
 473        test_must_fail git -C r1 pack-objects --rev --stdout >miss.pack <<-EOF
 474        HEAD
 475        EOF
 476'
 477
 478test_expect_success 'verify pack-objects fails w/ --missing=error' '
 479        test_must_fail git -C r1 pack-objects --rev --stdout --missing=error >miss.pack <<-EOF
 480        HEAD
 481        EOF
 482'
 483
 484test_expect_success 'verify pack-objects w/ --missing=allow-any' '
 485        git -C r1 pack-objects --rev --stdout --missing=allow-any >miss.pack <<-EOF
 486        HEAD
 487        EOF
 488'
 489
 490test_done