e4d032c9dbc1896cccf2f3006b97b718184ad133
   1#!/bin/sh
   2
   3test_description='git partial clone'
   4
   5. ./test-lib.sh
   6
   7# create a normal "src" repo where we can later create new commits.
   8# expect_1.oids will contain a list of the OIDs of all blobs.
   9test_expect_success 'setup normal src repo' '
  10        echo "{print \$1}" >print_1.awk &&
  11        echo "{print \$2}" >print_2.awk &&
  12
  13        git init src &&
  14        for n in 1 2 3 4
  15        do
  16                echo "This is file: $n" > src/file.$n.txt
  17                git -C src add file.$n.txt
  18                git -C src commit -m "file $n"
  19                git -C src ls-files -s file.$n.txt >>temp
  20        done &&
  21        awk -f print_2.awk <temp | sort >expect_1.oids &&
  22        test_line_count = 4 expect_1.oids
  23'
  24
  25# bare clone "src" giving "srv.bare" for use as our server.
  26test_expect_success 'setup bare clone for server' '
  27        git clone --bare "file://$(pwd)/src" srv.bare &&
  28        git -C srv.bare config --local uploadpack.allowfilter 1 &&
  29        git -C srv.bare config --local uploadpack.allowanysha1inwant 1
  30'
  31
  32# do basic partial clone from "srv.bare"
  33# confirm we are missing all of the known blobs.
  34# confirm partial clone was registered in the local config.
  35test_expect_success 'do partial clone 1' '
  36        git clone --no-checkout --filter=blob:none "file://$(pwd)/srv.bare" pc1 &&
  37
  38        git -C pc1 rev-list HEAD --quiet --objects --missing=print |
  39        awk -f print_1.awk |
  40        sed "s/?//" |
  41        sort >observed.oids &&
  42
  43        test_cmp expect_1.oids observed.oids &&
  44        test "$(git -C pc1 config --local core.repositoryformatversion)" = "1" &&
  45        test "$(git -C pc1 config --local extensions.partialclone)" = "origin" &&
  46        test "$(git -C pc1 config --local core.partialclonefilter)" = "blob:none"
  47'
  48
  49# checkout master to force dynamic object fetch of blobs at HEAD.
  50test_expect_success 'verify checkout with dynamic object fetch' '
  51        git -C pc1 rev-list HEAD --quiet --objects --missing=print >observed &&
  52        test_line_count = 4 observed &&
  53        git -C pc1 checkout master &&
  54        git -C pc1 rev-list HEAD --quiet --objects --missing=print >observed &&
  55        test_line_count = 0 observed
  56'
  57
  58# create new commits in "src" repo to establish a blame history on file.1.txt
  59# and push to "srv.bare".
  60test_expect_success 'push new commits to server' '
  61        git -C src remote add srv "file://$(pwd)/srv.bare" &&
  62        for x in a b c d e
  63        do
  64                echo "Mod file.1.txt $x" >>src/file.1.txt
  65                git -C src add file.1.txt
  66                git -C src commit -m "mod $x"
  67        done &&
  68        git -C src blame master -- file.1.txt >expect.blame &&
  69        git -C src push -u srv master
  70'
  71
  72# (partial) fetch in the partial clone repo from the promisor remote.
  73# verify that fetch inherited the filter-spec from the config and DOES NOT
  74# have the new blobs.
  75test_expect_success 'partial fetch inherits filter settings' '
  76        git -C pc1 fetch origin &&
  77        git -C pc1 rev-list master..origin/master --quiet --objects --missing=print >observed &&
  78        test_line_count = 5 observed
  79'
  80
  81# force dynamic object fetch using diff.
  82# we should only get 1 new blob (for the file in origin/master).
  83test_expect_success 'verify diff causes dynamic object fetch' '
  84        git -C pc1 diff master..origin/master -- file.1.txt &&
  85        git -C pc1 rev-list master..origin/master --quiet --objects --missing=print >observed &&
  86        test_line_count = 4 observed
  87'
  88
  89# force full dynamic object fetch of the file's history using blame.
  90# we should get the intermediate blobs for the file.
  91test_expect_success 'verify blame causes dynamic object fetch' '
  92        git -C pc1 blame origin/master -- file.1.txt >observed.blame &&
  93        test_cmp expect.blame observed.blame &&
  94        git -C pc1 rev-list master..origin/master --quiet --objects --missing=print >observed &&
  95        test_line_count = 0 observed
  96'
  97
  98# create new commits in "src" repo to establish a history on file.2.txt
  99# and push to "srv.bare".
 100test_expect_success 'push new commits to server for file.2.txt' '
 101        for x in a b c d e f
 102        do
 103                echo "Mod file.2.txt $x" >>src/file.2.txt
 104                git -C src add file.2.txt
 105                git -C src commit -m "mod $x"
 106        done &&
 107        git -C src push -u srv master
 108'
 109
 110# Do FULL fetch by disabling inherited filter-spec using --no-filter.
 111# Verify we have all the new blobs.
 112test_expect_success 'override inherited filter-spec using --no-filter' '
 113        git -C pc1 fetch --no-filter origin &&
 114        git -C pc1 rev-list master..origin/master --quiet --objects --missing=print >observed &&
 115        test_line_count = 0 observed
 116'
 117
 118# create new commits in "src" repo to establish a history on file.3.txt
 119# and push to "srv.bare".
 120test_expect_success 'push new commits to server for file.3.txt' '
 121        for x in a b c d e f
 122        do
 123                echo "Mod file.3.txt $x" >>src/file.3.txt
 124                git -C src add file.3.txt
 125                git -C src commit -m "mod $x"
 126        done &&
 127        git -C src push -u srv master
 128'
 129
 130# Do a partial fetch and then try to manually fetch the missing objects.
 131# This can be used as the basis of a pre-command hook to bulk fetch objects
 132# perhaps combined with a command in dry-run mode.
 133test_expect_success 'manual prefetch of missing objects' '
 134        git -C pc1 fetch --filter=blob:none origin &&
 135
 136        git -C pc1 rev-list master..origin/master --quiet --objects --missing=print |
 137        awk -f print_1.awk |
 138        sed "s/?//" |
 139        sort >observed.oids &&
 140
 141        test_line_count = 6 observed.oids &&
 142        git -C pc1 fetch-pack --stdin "file://$(pwd)/srv.bare" <observed.oids &&
 143
 144        git -C pc1 rev-list master..origin/master --quiet --objects --missing=print |
 145        awk -f print_1.awk |
 146        sed "s/?//" |
 147        sort >observed.oids &&
 148
 149        test_line_count = 0 observed.oids
 150'
 151
 152test_expect_success 'partial clone with transfer.fsckobjects=1 uses index-pack --fsck-objects' '
 153        git init src &&
 154        test_commit -C src x &&
 155        test_config -C src uploadpack.allowfilter 1 &&
 156        test_config -C src uploadpack.allowanysha1inwant 1 &&
 157
 158        GIT_TRACE="$(pwd)/trace" git -c transfer.fsckobjects=1 \
 159                clone --filter="blob:none" "file://$(pwd)/src" dst &&
 160        grep "git index-pack.*--fsck-objects" trace
 161'
 162
 163test_expect_success 'partial clone fetches blobs pointed to by refs even if normally filtered out' '
 164        rm -rf src dst &&
 165        git init src &&
 166        test_commit -C src x &&
 167        test_config -C src uploadpack.allowfilter 1 &&
 168        test_config -C src uploadpack.allowanysha1inwant 1 &&
 169
 170        # Create a tag pointing to a blob.
 171        BLOB=$(echo blob-contents | git -C src hash-object --stdin -w) &&
 172        git -C src tag myblob "$BLOB" &&
 173
 174        git clone --filter="blob:none" "file://$(pwd)/src" dst 2>err &&
 175        ! grep "does not point to a valid object" err &&
 176        git -C dst fsck
 177'
 178
 179. "$TEST_DIRECTORY"/lib-httpd.sh
 180start_httpd
 181
 182# Converts bytes into a form suitable for inclusion in a sed command. For
 183# example, "printf 'ab\r\n' | hex_unpack" results in '\x61\x62\x0d\x0a'.
 184sed_escape () {
 185        perl -e '$/ = undef; $input = <>; print unpack("H2" x length($input), $input)' |
 186                sed 's/\(..\)/\\x\1/g'
 187}
 188
 189test_expect_success 'upon cloning, check that all refs point to objects' '
 190        SERVER="$HTTPD_DOCUMENT_ROOT_PATH/server" &&
 191        rm -rf "$SERVER" repo &&
 192        test_create_repo "$SERVER" &&
 193        test_commit -C "$SERVER" foo &&
 194        test_config -C "$SERVER" uploadpack.allowfilter 1 &&
 195        test_config -C "$SERVER" uploadpack.allowanysha1inwant 1 &&
 196
 197        # Create a tag pointing to a blob.
 198        BLOB=$(echo blob-contents | git -C "$SERVER" hash-object --stdin -w) &&
 199        git -C "$SERVER" tag myblob "$BLOB" &&
 200
 201        # Craft a packfile not including that blob.
 202        git -C "$SERVER" rev-parse HEAD |
 203        git -C "$SERVER" pack-objects --stdout >incomplete.pack &&
 204
 205        # Replace the existing packfile with the crafted one. The protocol
 206        # requires that the packfile be sent in sideband 1, hence the extra
 207        # \x01 byte at the beginning.
 208        printf "1,/packfile/!c %04x\\\\x01%s0000" \
 209                "$(($(wc -c <incomplete.pack) + 5))" \
 210                "$(sed_escape <incomplete.pack)" \
 211                >"$HTTPD_ROOT_PATH/one-time-sed" &&
 212
 213        # Use protocol v2 because the sed command looks for the "packfile"
 214        # section header.
 215        test_config -C "$SERVER" protocol.version 2 &&
 216        test_must_fail git -c protocol.version=2 clone \
 217                --filter=blob:none $HTTPD_URL/one_time_sed/server repo 2>err &&
 218
 219        grep "did not send all necessary objects" err &&
 220
 221        # Ensure that the one-time-sed script was used.
 222        ! test -e "$HTTPD_ROOT_PATH/one-time-sed"
 223'
 224
 225test_expect_success 'when partial cloning, tolerate server not sending target of tag' '
 226        SERVER="$HTTPD_DOCUMENT_ROOT_PATH/server" &&
 227        rm -rf "$SERVER" repo &&
 228        test_create_repo "$SERVER" &&
 229        test_commit -C "$SERVER" foo &&
 230        test_config -C "$SERVER" uploadpack.allowfilter 1 &&
 231        test_config -C "$SERVER" uploadpack.allowanysha1inwant 1 &&
 232
 233        # Create an annotated tag pointing to a blob.
 234        BLOB=$(echo blob-contents | git -C "$SERVER" hash-object --stdin -w) &&
 235        git -C "$SERVER" tag -m message -a myblob "$BLOB" &&
 236
 237        # Craft a packfile including the tag, but not the blob it points to.
 238        # Also, omit objects referenced from HEAD in order to force a second
 239        # fetch (to fetch missing objects) upon the automatic checkout that
 240        # happens after a clone.
 241        printf "%s\n%s\n--not\n%s\n%s\n" \
 242                $(git -C "$SERVER" rev-parse HEAD) \
 243                $(git -C "$SERVER" rev-parse myblob) \
 244                $(git -C "$SERVER" rev-parse HEAD^{tree}) \
 245                $(git -C "$SERVER" rev-parse myblob^{blob}) |
 246                git -C "$SERVER" pack-objects --thin --stdout >incomplete.pack &&
 247
 248        # Replace the existing packfile with the crafted one. The protocol
 249        # requires that the packfile be sent in sideband 1, hence the extra
 250        # \x01 byte at the beginning.
 251        printf "1,/packfile/!c %04x\\\\x01%s0000" \
 252                "$(($(wc -c <incomplete.pack) + 5))" \
 253                "$(sed_escape <incomplete.pack)" \
 254                >"$HTTPD_ROOT_PATH/one-time-sed" &&
 255
 256        # Use protocol v2 because the sed command looks for the "packfile"
 257        # section header.
 258        test_config -C "$SERVER" protocol.version 2 &&
 259
 260        # Exercise to make sure it works.
 261        git -c protocol.version=2 clone \
 262                --filter=blob:none $HTTPD_URL/one_time_sed/server repo 2> err &&
 263        ! grep "missing object referenced by" err &&
 264
 265        # Ensure that the one-time-sed script was used.
 266        ! test -e "$HTTPD_ROOT_PATH/one-time-sed"
 267'
 268
 269stop_httpd
 270
 271test_done