t / t0410-partial-clone.shon commit range-diff: allow to diff files regardless of submodule config (0e573e8)
   1#!/bin/sh
   2
   3test_description='partial clone'
   4
   5. ./test-lib.sh
   6
   7delete_object () {
   8        rm $1/.git/objects/$(echo $2 | sed -e 's|^..|&/|')
   9}
  10
  11pack_as_from_promisor () {
  12        HASH=$(git -C repo pack-objects .git/objects/pack/pack) &&
  13        >repo/.git/objects/pack/pack-$HASH.promisor &&
  14        echo $HASH
  15}
  16
  17promise_and_delete () {
  18        HASH=$(git -C repo rev-parse "$1") &&
  19        git -C repo tag -a -m message my_annotated_tag "$HASH" &&
  20        git -C repo rev-parse my_annotated_tag | pack_as_from_promisor &&
  21        # tag -d prints a message to stdout, so redirect it
  22        git -C repo tag -d my_annotated_tag >/dev/null &&
  23        delete_object repo "$HASH"
  24}
  25
  26test_expect_success 'extensions.partialclone without filter' '
  27        test_create_repo server &&
  28        git clone --filter="blob:none" "file://$(pwd)/server" client &&
  29        git -C client config --unset core.partialclonefilter &&
  30        git -C client fetch origin
  31'
  32
  33test_expect_success 'missing reflog object, but promised by a commit, passes fsck' '
  34        rm -rf repo &&
  35        test_create_repo repo &&
  36        test_commit -C repo my_commit &&
  37
  38        A=$(git -C repo commit-tree -m a HEAD^{tree}) &&
  39        C=$(git -C repo commit-tree -m c -p $A HEAD^{tree}) &&
  40
  41        # Reference $A only from reflog, and delete it
  42        git -C repo branch my_branch "$A" &&
  43        git -C repo branch -f my_branch my_commit &&
  44        delete_object repo "$A" &&
  45
  46        # State that we got $C, which refers to $A, from promisor
  47        printf "$C\n" | pack_as_from_promisor &&
  48
  49        # Normally, it fails
  50        test_must_fail git -C repo fsck &&
  51
  52        # But with the extension, it succeeds
  53        git -C repo config core.repositoryformatversion 1 &&
  54        git -C repo config extensions.partialclone "arbitrary string" &&
  55        git -C repo fsck
  56'
  57
  58test_expect_success 'missing reflog object, but promised by a tag, passes fsck' '
  59        rm -rf repo &&
  60        test_create_repo repo &&
  61        test_commit -C repo my_commit &&
  62
  63        A=$(git -C repo commit-tree -m a HEAD^{tree}) &&
  64        git -C repo tag -a -m d my_tag_name $A &&
  65        T=$(git -C repo rev-parse my_tag_name) &&
  66        git -C repo tag -d my_tag_name &&
  67
  68        # Reference $A only from reflog, and delete it
  69        git -C repo branch my_branch "$A" &&
  70        git -C repo branch -f my_branch my_commit &&
  71        delete_object repo "$A" &&
  72
  73        # State that we got $T, which refers to $A, from promisor
  74        printf "$T\n" | pack_as_from_promisor &&
  75
  76        git -C repo config core.repositoryformatversion 1 &&
  77        git -C repo config extensions.partialclone "arbitrary string" &&
  78        git -C repo fsck
  79'
  80
  81test_expect_success 'missing reflog object alone fails fsck, even with extension set' '
  82        rm -rf repo &&
  83        test_create_repo repo &&
  84        test_commit -C repo my_commit &&
  85
  86        A=$(git -C repo commit-tree -m a HEAD^{tree}) &&
  87        B=$(git -C repo commit-tree -m b HEAD^{tree}) &&
  88
  89        # Reference $A only from reflog, and delete it
  90        git -C repo branch my_branch "$A" &&
  91        git -C repo branch -f my_branch my_commit &&
  92        delete_object repo "$A" &&
  93
  94        git -C repo config core.repositoryformatversion 1 &&
  95        git -C repo config extensions.partialclone "arbitrary string" &&
  96        test_must_fail git -C repo fsck
  97'
  98
  99test_expect_success 'missing ref object, but promised, passes fsck' '
 100        rm -rf repo &&
 101        test_create_repo repo &&
 102        test_commit -C repo my_commit &&
 103
 104        A=$(git -C repo commit-tree -m a HEAD^{tree}) &&
 105
 106        # Reference $A only from ref
 107        git -C repo branch my_branch "$A" &&
 108        promise_and_delete "$A" &&
 109
 110        git -C repo config core.repositoryformatversion 1 &&
 111        git -C repo config extensions.partialclone "arbitrary string" &&
 112        git -C repo fsck
 113'
 114
 115test_expect_success 'missing object, but promised, passes fsck' '
 116        rm -rf repo &&
 117        test_create_repo repo &&
 118        test_commit -C repo 1 &&
 119        test_commit -C repo 2 &&
 120        test_commit -C repo 3 &&
 121        git -C repo tag -a annotated_tag -m "annotated tag" &&
 122
 123        C=$(git -C repo rev-parse 1) &&
 124        T=$(git -C repo rev-parse 2^{tree}) &&
 125        B=$(git hash-object repo/3.t) &&
 126        AT=$(git -C repo rev-parse annotated_tag) &&
 127
 128        promise_and_delete "$C" &&
 129        promise_and_delete "$T" &&
 130        promise_and_delete "$B" &&
 131        promise_and_delete "$AT" &&
 132
 133        git -C repo config core.repositoryformatversion 1 &&
 134        git -C repo config extensions.partialclone "arbitrary string" &&
 135        git -C repo fsck
 136'
 137
 138test_expect_success 'missing CLI object, but promised, passes fsck' '
 139        rm -rf repo &&
 140        test_create_repo repo &&
 141        test_commit -C repo my_commit &&
 142
 143        A=$(git -C repo commit-tree -m a HEAD^{tree}) &&
 144        promise_and_delete "$A" &&
 145
 146        git -C repo config core.repositoryformatversion 1 &&
 147        git -C repo config extensions.partialclone "arbitrary string" &&
 148        git -C repo fsck "$A"
 149'
 150
 151test_expect_success 'fetching of missing objects' '
 152        rm -rf repo &&
 153        test_create_repo server &&
 154        test_commit -C server foo &&
 155        git -C server repack -a -d --write-bitmap-index &&
 156
 157        git clone "file://$(pwd)/server" repo &&
 158        HASH=$(git -C repo rev-parse foo) &&
 159        rm -rf repo/.git/objects/* &&
 160
 161        git -C repo config core.repositoryformatversion 1 &&
 162        git -C repo config extensions.partialclone "origin" &&
 163        git -C repo cat-file -p "$HASH" &&
 164
 165        # Ensure that the .promisor file is written, and check that its
 166        # associated packfile contains the object
 167        ls repo/.git/objects/pack/pack-*.promisor >promisorlist &&
 168        test_line_count = 1 promisorlist &&
 169        IDX=$(cat promisorlist | sed "s/promisor$/idx/") &&
 170        git verify-pack --verbose "$IDX" | grep "$HASH"
 171'
 172
 173test_expect_success 'fetching of missing objects works with ref-in-want enabled' '
 174        # ref-in-want requires protocol version 2
 175        git -C server config protocol.version 2 &&
 176        git -C server config uploadpack.allowrefinwant 1 &&
 177        git -C repo config protocol.version 2 &&
 178
 179        rm -rf repo/.git/objects/* &&
 180        rm -f trace &&
 181        GIT_TRACE_PACKET="$(pwd)/trace" git -C repo cat-file -p "$HASH" &&
 182        grep "git< fetch=.*ref-in-want" trace
 183'
 184
 185test_expect_success 'rev-list stops traversal at missing and promised commit' '
 186        rm -rf repo &&
 187        test_create_repo repo &&
 188        test_commit -C repo foo &&
 189        test_commit -C repo bar &&
 190
 191        FOO=$(git -C repo rev-parse foo) &&
 192        promise_and_delete "$FOO" &&
 193
 194        git -C repo config core.repositoryformatversion 1 &&
 195        git -C repo config extensions.partialclone "arbitrary string" &&
 196        GIT_TEST_COMMIT_GRAPH=0 git -C repo rev-list --exclude-promisor-objects --objects bar >out &&
 197        grep $(git -C repo rev-parse bar) out &&
 198        ! grep $FOO out
 199'
 200
 201test_expect_success 'rev-list stops traversal at missing and promised tree' '
 202        rm -rf repo &&
 203        test_create_repo repo &&
 204        test_commit -C repo foo &&
 205        mkdir repo/a_dir &&
 206        echo something >repo/a_dir/something &&
 207        git -C repo add a_dir/something &&
 208        git -C repo commit -m bar &&
 209
 210        # foo^{tree} (tree referenced from commit)
 211        TREE=$(git -C repo rev-parse foo^{tree}) &&
 212
 213        # a tree referenced by HEAD^{tree} (tree referenced from tree)
 214        TREE2=$(git -C repo ls-tree HEAD^{tree} | grep " tree " | head -1 | cut -b13-52) &&
 215
 216        promise_and_delete "$TREE" &&
 217        promise_and_delete "$TREE2" &&
 218
 219        git -C repo config core.repositoryformatversion 1 &&
 220        git -C repo config extensions.partialclone "arbitrary string" &&
 221        git -C repo rev-list --exclude-promisor-objects --objects HEAD >out &&
 222        grep $(git -C repo rev-parse foo) out &&
 223        ! grep $TREE out &&
 224        grep $(git -C repo rev-parse HEAD) out &&
 225        ! grep $TREE2 out
 226'
 227
 228test_expect_success 'rev-list stops traversal at missing and promised blob' '
 229        rm -rf repo &&
 230        test_create_repo repo &&
 231        echo something >repo/something &&
 232        git -C repo add something &&
 233        git -C repo commit -m foo &&
 234
 235        BLOB=$(git -C repo hash-object -w something) &&
 236        promise_and_delete "$BLOB" &&
 237
 238        git -C repo config core.repositoryformatversion 1 &&
 239        git -C repo config extensions.partialclone "arbitrary string" &&
 240        git -C repo rev-list --exclude-promisor-objects --objects HEAD >out &&
 241        grep $(git -C repo rev-parse HEAD) out &&
 242        ! grep $BLOB out
 243'
 244
 245test_expect_success 'rev-list stops traversal at promisor commit, tree, and blob' '
 246        rm -rf repo &&
 247        test_create_repo repo &&
 248        test_commit -C repo foo &&
 249        test_commit -C repo bar &&
 250        test_commit -C repo baz &&
 251
 252        COMMIT=$(git -C repo rev-parse foo) &&
 253        TREE=$(git -C repo rev-parse bar^{tree}) &&
 254        BLOB=$(git hash-object repo/baz.t) &&
 255        printf "%s\n%s\n%s\n" $COMMIT $TREE $BLOB | pack_as_from_promisor &&
 256
 257        git -C repo config core.repositoryformatversion 1 &&
 258        git -C repo config extensions.partialclone "arbitrary string" &&
 259        git -C repo rev-list --exclude-promisor-objects --objects HEAD >out &&
 260        ! grep $COMMIT out &&
 261        ! grep $TREE out &&
 262        ! grep $BLOB out &&
 263        grep $(git -C repo rev-parse bar) out  # sanity check that some walking was done
 264'
 265
 266test_expect_success 'rev-list accepts missing and promised objects on command line' '
 267        rm -rf repo &&
 268        test_create_repo repo &&
 269        test_commit -C repo foo &&
 270        test_commit -C repo bar &&
 271        test_commit -C repo baz &&
 272
 273        COMMIT=$(git -C repo rev-parse foo) &&
 274        TREE=$(git -C repo rev-parse bar^{tree}) &&
 275        BLOB=$(git hash-object repo/baz.t) &&
 276
 277        promise_and_delete $COMMIT &&
 278        promise_and_delete $TREE &&
 279        promise_and_delete $BLOB &&
 280
 281        git -C repo config core.repositoryformatversion 1 &&
 282        git -C repo config extensions.partialclone "arbitrary string" &&
 283        git -C repo rev-list --exclude-promisor-objects --objects "$COMMIT" "$TREE" "$BLOB"
 284'
 285
 286test_expect_success 'gc repacks promisor objects separately from non-promisor objects' '
 287        rm -rf repo &&
 288        test_create_repo repo &&
 289        test_commit -C repo one &&
 290        test_commit -C repo two &&
 291
 292        TREE_ONE=$(git -C repo rev-parse one^{tree}) &&
 293        printf "$TREE_ONE\n" | pack_as_from_promisor &&
 294        TREE_TWO=$(git -C repo rev-parse two^{tree}) &&
 295        printf "$TREE_TWO\n" | pack_as_from_promisor &&
 296
 297        git -C repo config core.repositoryformatversion 1 &&
 298        git -C repo config extensions.partialclone "arbitrary string" &&
 299        git -C repo gc &&
 300
 301        # Ensure that exactly one promisor packfile exists, and that it
 302        # contains the trees but not the commits
 303        ls repo/.git/objects/pack/pack-*.promisor >promisorlist &&
 304        test_line_count = 1 promisorlist &&
 305        PROMISOR_PACKFILE=$(sed "s/.promisor/.pack/" <promisorlist) &&
 306        git verify-pack $PROMISOR_PACKFILE -v >out &&
 307        grep "$TREE_ONE" out &&
 308        grep "$TREE_TWO" out &&
 309        ! grep "$(git -C repo rev-parse one)" out &&
 310        ! grep "$(git -C repo rev-parse two)" out &&
 311
 312        # Remove the promisor packfile and associated files
 313        rm $(sed "s/.promisor//" <promisorlist).* &&
 314
 315        # Ensure that the single other pack contains the commits, but not the
 316        # trees
 317        ls repo/.git/objects/pack/pack-*.pack >packlist &&
 318        test_line_count = 1 packlist &&
 319        git verify-pack repo/.git/objects/pack/pack-*.pack -v >out &&
 320        grep "$(git -C repo rev-parse one)" out &&
 321        grep "$(git -C repo rev-parse two)" out &&
 322        ! grep "$TREE_ONE" out &&
 323        ! grep "$TREE_TWO" out
 324'
 325
 326test_expect_success 'gc does not repack promisor objects if there are none' '
 327        rm -rf repo &&
 328        test_create_repo repo &&
 329        test_commit -C repo one &&
 330
 331        git -C repo config core.repositoryformatversion 1 &&
 332        git -C repo config extensions.partialclone "arbitrary string" &&
 333        git -C repo gc &&
 334
 335        # Ensure that only one pack exists
 336        ls repo/.git/objects/pack/pack-*.pack >packlist &&
 337        test_line_count = 1 packlist
 338'
 339
 340repack_and_check () {
 341        rm -rf repo2 &&
 342        cp -r repo repo2 &&
 343        git -C repo2 repack $1 -d &&
 344        git -C repo2 fsck &&
 345
 346        git -C repo2 cat-file -e $2 &&
 347        git -C repo2 cat-file -e $3
 348}
 349
 350test_expect_success 'repack -d does not irreversibly delete promisor objects' '
 351        rm -rf repo &&
 352        test_create_repo repo &&
 353        git -C repo config core.repositoryformatversion 1 &&
 354        git -C repo config extensions.partialclone "arbitrary string" &&
 355
 356        git -C repo commit --allow-empty -m one &&
 357        git -C repo commit --allow-empty -m two &&
 358        git -C repo commit --allow-empty -m three &&
 359        git -C repo commit --allow-empty -m four &&
 360        ONE=$(git -C repo rev-parse HEAD^^^) &&
 361        TWO=$(git -C repo rev-parse HEAD^^) &&
 362        THREE=$(git -C repo rev-parse HEAD^) &&
 363
 364        printf "$TWO\n" | pack_as_from_promisor &&
 365        printf "$THREE\n" | pack_as_from_promisor &&
 366        delete_object repo "$ONE" &&
 367
 368        repack_and_check -a "$TWO" "$THREE" &&
 369        repack_and_check -A "$TWO" "$THREE" &&
 370        repack_and_check -l "$TWO" "$THREE"
 371'
 372
 373test_expect_success 'gc stops traversal when a missing but promised object is reached' '
 374        rm -rf repo &&
 375        test_create_repo repo &&
 376        test_commit -C repo my_commit &&
 377
 378        TREE_HASH=$(git -C repo rev-parse HEAD^{tree}) &&
 379        HASH=$(promise_and_delete $TREE_HASH) &&
 380
 381        git -C repo config core.repositoryformatversion 1 &&
 382        git -C repo config extensions.partialclone "arbitrary string" &&
 383        git -C repo gc &&
 384
 385        # Ensure that the promisor packfile still exists, and remove it
 386        test -e repo/.git/objects/pack/pack-$HASH.pack &&
 387        rm repo/.git/objects/pack/pack-$HASH.* &&
 388
 389        # Ensure that the single other pack contains the commit, but not the tree
 390        ls repo/.git/objects/pack/pack-*.pack >packlist &&
 391        test_line_count = 1 packlist &&
 392        git verify-pack repo/.git/objects/pack/pack-*.pack -v >out &&
 393        grep "$(git -C repo rev-parse HEAD)" out &&
 394        ! grep "$TREE_HASH" out
 395'
 396
 397LIB_HTTPD_PORT=12345  # default port, 410, cannot be used as non-root
 398. "$TEST_DIRECTORY"/lib-httpd.sh
 399start_httpd
 400
 401test_expect_success 'fetching of missing objects from an HTTP server' '
 402        rm -rf repo &&
 403        SERVER="$HTTPD_DOCUMENT_ROOT_PATH/server" &&
 404        test_create_repo "$SERVER" &&
 405        test_commit -C "$SERVER" foo &&
 406        git -C "$SERVER" repack -a -d --write-bitmap-index &&
 407
 408        git clone $HTTPD_URL/smart/server repo &&
 409        HASH=$(git -C repo rev-parse foo) &&
 410        rm -rf repo/.git/objects/* &&
 411
 412        git -C repo config core.repositoryformatversion 1 &&
 413        git -C repo config extensions.partialclone "origin" &&
 414        git -C repo cat-file -p "$HASH" &&
 415
 416        # Ensure that the .promisor file is written, and check that its
 417        # associated packfile contains the object
 418        ls repo/.git/objects/pack/pack-*.promisor >promisorlist &&
 419        test_line_count = 1 promisorlist &&
 420        IDX=$(cat promisorlist | sed "s/promisor$/idx/") &&
 421        git verify-pack --verbose "$IDX" | grep "$HASH"
 422'
 423
 424stop_httpd
 425
 426test_done