3 test_description='git partial clone'
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.
9 test_expect_success 'setup normal src repo' '
10 echo "{print \$1}" >print_1.awk &&
11 echo "{print \$2}" >print_2.awk &&
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
21 awk -f print_2.awk <temp | sort >expect_1.oids &&
22 test_line_count = 4 expect_1.oids
25 # bare clone "src" giving "srv.bare" for use as our server.
26 test_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
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.
35 test_expect_success 'do partial clone 1' '
36 git clone --no-checkout --filter=blob:none "file://$(pwd)/srv.bare" pc1 &&
38 git -C pc1 rev-list --quiet --objects --missing=print HEAD >revs &&
39 awk -f print_1.awk revs |
41 sort >observed.oids &&
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 remote.origin.promisor)" = "true" &&
46 test "$(git -C pc1 config --local remote.origin.partialclonefilter)" = "blob:none"
49 test_expect_success 'verify that .promisor file contains refs fetched' '
50 ls pc1/.git/objects/pack/pack-*.promisor >promisorlist &&
51 test_line_count = 1 promisorlist &&
52 git -C srv.bare rev-list HEAD >headhash &&
53 grep "$(cat headhash) HEAD" $(cat promisorlist) &&
54 grep "$(cat headhash) refs/heads/master" $(cat promisorlist)
57 # checkout master to force dynamic object fetch of blobs at HEAD.
58 test_expect_success 'verify checkout with dynamic object fetch' '
59 git -C pc1 rev-list --quiet --objects --missing=print HEAD >observed &&
60 test_line_count = 4 observed &&
61 git -C pc1 checkout master &&
62 git -C pc1 rev-list --quiet --objects --missing=print HEAD >observed &&
63 test_line_count = 0 observed
66 # create new commits in "src" repo to establish a blame history on file.1.txt
67 # and push to "srv.bare".
68 test_expect_success 'push new commits to server' '
69 git -C src remote add srv "file://$(pwd)/srv.bare" &&
72 echo "Mod file.1.txt $x" >>src/file.1.txt
73 git -C src add file.1.txt
74 git -C src commit -m "mod $x"
76 git -C src blame master -- file.1.txt >expect.blame &&
77 git -C src push -u srv master
80 # (partial) fetch in the partial clone repo from the promisor remote.
81 # verify that fetch inherited the filter-spec from the config and DOES NOT
83 test_expect_success 'partial fetch inherits filter settings' '
84 git -C pc1 fetch origin &&
85 git -C pc1 rev-list --quiet --objects --missing=print \
86 master..origin/master >observed &&
87 test_line_count = 5 observed
90 # force dynamic object fetch using diff.
91 # we should only get 1 new blob (for the file in origin/master).
92 test_expect_success 'verify diff causes dynamic object fetch' '
93 git -C pc1 diff master..origin/master -- file.1.txt &&
94 git -C pc1 rev-list --quiet --objects --missing=print \
95 master..origin/master >observed &&
96 test_line_count = 4 observed
99 # force full dynamic object fetch of the file's history using blame.
100 # we should get the intermediate blobs for the file.
101 test_expect_success 'verify blame causes dynamic object fetch' '
102 git -C pc1 blame origin/master -- file.1.txt >observed.blame &&
103 test_cmp expect.blame observed.blame &&
104 git -C pc1 rev-list --quiet --objects --missing=print \
105 master..origin/master >observed &&
106 test_line_count = 0 observed
109 # create new commits in "src" repo to establish a history on file.2.txt
110 # and push to "srv.bare".
111 test_expect_success 'push new commits to server for file.2.txt' '
114 echo "Mod file.2.txt $x" >>src/file.2.txt
115 git -C src add file.2.txt
116 git -C src commit -m "mod $x"
118 git -C src push -u srv master
121 # Do FULL fetch by disabling inherited filter-spec using --no-filter.
122 # Verify we have all the new blobs.
123 test_expect_success 'override inherited filter-spec using --no-filter' '
124 git -C pc1 fetch --no-filter origin &&
125 git -C pc1 rev-list --quiet --objects --missing=print \
126 master..origin/master >observed &&
127 test_line_count = 0 observed
130 # create new commits in "src" repo to establish a history on file.3.txt
131 # and push to "srv.bare".
132 test_expect_success 'push new commits to server for file.3.txt' '
135 echo "Mod file.3.txt $x" >>src/file.3.txt
136 git -C src add file.3.txt
137 git -C src commit -m "mod $x"
139 git -C src push -u srv master
142 # Do a partial fetch and then try to manually fetch the missing objects.
143 # This can be used as the basis of a pre-command hook to bulk fetch objects
144 # perhaps combined with a command in dry-run mode.
145 test_expect_success 'manual prefetch of missing objects' '
146 git -C pc1 fetch --filter=blob:none origin &&
148 git -C pc1 rev-list --quiet --objects --missing=print \
149 master..origin/master >revs &&
150 awk -f print_1.awk revs |
152 sort >observed.oids &&
154 test_line_count = 6 observed.oids &&
155 git -C pc1 fetch-pack --stdin "file://$(pwd)/srv.bare" <observed.oids &&
157 git -C pc1 rev-list --quiet --objects --missing=print \
158 master..origin/master >revs &&
159 awk -f print_1.awk revs |
161 sort >observed.oids &&
163 test_line_count = 0 observed.oids
166 test_expect_success 'partial clone with transfer.fsckobjects=1 uses index-pack --fsck-objects' '
168 test_commit -C src x &&
169 test_config -C src uploadpack.allowfilter 1 &&
170 test_config -C src uploadpack.allowanysha1inwant 1 &&
172 GIT_TRACE="$(pwd)/trace" git -c transfer.fsckobjects=1 \
173 clone --filter="blob:none" "file://$(pwd)/src" dst &&
174 grep "git index-pack.*--fsck-objects" trace
177 test_expect_success 'use fsck before and after manually fetching a missing subtree' '
178 # push new commit so server has a subtree
180 echo "in dir" >src/dir/file.txt &&
181 git -C src add dir/file.txt &&
182 git -C src commit -m "file in dir" &&
183 git -C src push -u srv master &&
184 SUBTREE=$(git -C src rev-parse HEAD:dir) &&
187 git clone --no-checkout --filter=tree:0 "file://$(pwd)/srv.bare" dst &&
190 # Make sure we only have commits, and all trees and blobs are missing.
191 git -C dst rev-list --missing=allow-any --objects master \
193 awk -f print_1.awk fetched_objects |
194 xargs -n1 git -C dst cat-file -t >fetched_types &&
196 sort -u fetched_types >unique_types.observed &&
197 echo commit >unique_types.expected &&
198 test_cmp unique_types.expected unique_types.observed &&
200 # Auto-fetch a tree with cat-file.
201 git -C dst cat-file -p $SUBTREE >tree_contents &&
202 grep file.txt tree_contents &&
204 # fsck still works after an auto-fetch of a tree.
207 # Auto-fetch all remaining trees and blobs with --missing=error
208 git -C dst rev-list --missing=error --objects master >fetched_objects &&
209 test_line_count = 70 fetched_objects &&
211 awk -f print_1.awk fetched_objects |
212 xargs -n1 git -C dst cat-file -t >fetched_types &&
214 sort -u fetched_types >unique_types.observed &&
215 test_write_lines blob commit tree >unique_types.expected &&
216 test_cmp unique_types.expected unique_types.observed
219 test_expect_success 'implicitly construct combine: filter with repeated flags' '
220 GIT_TRACE=$(pwd)/trace git clone --bare \
221 --filter=blob:none --filter=tree:1 \
222 "file://$(pwd)/srv.bare" pc2 &&
223 grep "trace:.* git pack-objects .*--filter=combine:blob:none+tree:1" \
225 git -C pc2 rev-list --objects --missing=allow-any HEAD >objects &&
227 # We should have gotten some root trees.
229 # Should not have gotten any non-root trees or blobs.
230 ! grep " ." objects &&
232 xargs -n 1 git -C pc2 cat-file -t <objects >types &&
233 sort -u types >unique_types.actual &&
234 test_write_lines commit tree >unique_types.expected &&
235 test_cmp unique_types.expected unique_types.actual
238 test_expect_success 'partial clone fetches blobs pointed to by refs even if normally filtered out' '
241 test_commit -C src x &&
242 test_config -C src uploadpack.allowfilter 1 &&
243 test_config -C src uploadpack.allowanysha1inwant 1 &&
245 # Create a tag pointing to a blob.
246 BLOB=$(echo blob-contents | git -C src hash-object --stdin -w) &&
247 git -C src tag myblob "$BLOB" &&
249 git clone --filter="blob:none" "file://$(pwd)/src" dst 2>err &&
250 ! grep "does not point to a valid object" err &&
254 test_expect_success 'fetch what is specified on CLI even if already promised' '
255 rm -rf src dst.git &&
257 test_commit -C src foo &&
258 test_config -C src uploadpack.allowfilter 1 &&
259 test_config -C src uploadpack.allowanysha1inwant 1 &&
261 git hash-object --stdin <src/foo.t >blob &&
263 git clone --bare --filter=blob:none "file://$(pwd)/src" dst.git &&
264 git -C dst.git rev-list --objects --quiet --missing=print HEAD >missing_before &&
265 grep "?$(cat blob)" missing_before &&
266 git -C dst.git fetch origin $(cat blob) &&
267 git -C dst.git rev-list --objects --quiet --missing=print HEAD >missing_after &&
268 ! grep "?$(cat blob)" missing_after
271 test_expect_success 'setup src repo for sparse filter' '
272 git init sparse-src &&
273 git -C sparse-src config --local uploadpack.allowfilter 1 &&
274 git -C sparse-src config --local uploadpack.allowanysha1inwant 1 &&
275 test_commit -C sparse-src one &&
276 test_commit -C sparse-src two &&
277 echo /one.t >sparse-src/only-one &&
278 git -C sparse-src add . &&
279 git -C sparse-src commit -m "add sparse checkout files"
282 test_expect_success 'partial clone with sparse filter succeeds' '
284 git clone --no-local --bare \
285 --filter=sparse:oid=master:only-one \
286 sparse-src dst.git &&
289 git rev-list --objects --missing=print HEAD >out &&
290 grep "^$(git rev-parse HEAD:one.t)" out &&
291 grep "^?$(git rev-parse HEAD:two.t)" out
295 test_expect_success 'partial clone with unresolvable sparse filter fails cleanly' '
297 test_must_fail git clone --no-local --bare \
298 --filter=sparse:oid=master:no-such-name \
299 sparse-src dst.git 2>err &&
300 test_i18ngrep "unable to access sparse blob in .master:no-such-name" err &&
301 test_must_fail git clone --no-local --bare \
302 --filter=sparse:oid=master \
303 sparse-src dst.git 2>err &&
304 test_i18ngrep "unable to parse sparse filter data in" err
308 rm -rf big-blob.txt server client promisor-remote &&
310 printf "line %d\n" $(test_seq 1 100) >big-blob.txt &&
312 # Create a server with 2 commits: a commit with a big tree and a child
313 # commit with an incremental change. Also, create a partial clone
314 # client that only contains the first commit.
316 git -C server config --local uploadpack.allowfilter 1 &&
317 for i in $(test_seq 1 100)
319 echo "make the tree big" >server/file$i &&
320 git -C server add file$i
322 git -C server commit -m "initial" &&
323 git clone --bare --filter=tree:0 "file://$(pwd)/server" client &&
324 echo another line >>server/file1 &&
325 git -C server commit -am "incremental change" &&
327 # Create a promisor remote that only contains the tree and blob from
329 git init promisor-remote &&
330 git -C server config --local uploadpack.allowanysha1inwant 1 &&
331 TREE_HASH=$(git -C server rev-parse HEAD~1^{tree}) &&
332 git -C promisor-remote fetch --keep "file://$(pwd)/server" "$TREE_HASH" &&
333 git -C promisor-remote count-objects -v >object-count &&
334 test_i18ngrep "count: 0" object-count &&
335 test_i18ngrep "in-pack: 2" object-count &&
337 # Set it as the promisor remote of client. Thus, whenever
338 # the client lazy fetches, the lazy fetch will succeed only if it is
339 # for this tree or blob.
340 test_commit -C promisor-remote one && # so that ref advertisement is not empty
341 git -C promisor-remote config --local uploadpack.allowanysha1inwant 1 &&
342 git -C client remote set-url origin "file://$(pwd)/promisor-remote"
345 # NEEDSWORK: The tests beginning with "fetch lazy-fetches" below only
346 # test that "fetch" avoid fetching trees and blobs, but not commits or
347 # tags. Revisit this if Git is ever taught to support partial clones
348 # with commits and/or tags filtered out.
350 test_expect_success 'fetch lazy-fetches only to resolve deltas' '
353 # Exercise to make sure it works. Git will not fetch anything from the
354 # promisor remote other than for the big tree (because it needs to
355 # resolve the delta).
356 GIT_TRACE_PACKET="$(pwd)/trace" git -C client \
357 fetch "file://$(pwd)/server" master &&
359 # Verify the assumption that the client needed to fetch the delta base
360 # to resolve the delta.
361 git -C server rev-parse HEAD~1^{tree} >hash &&
362 grep "want $(cat hash)" trace
365 test_expect_success 'fetch lazy-fetches only to resolve deltas, protocol v2' '
368 git -C server config --local protocol.version 2 &&
369 git -C client config --local protocol.version 2 &&
370 git -C promisor-remote config --local protocol.version 2 &&
372 # Exercise to make sure it works. Git will not fetch anything from the
373 # promisor remote other than for the big blob (because it needs to
374 # resolve the delta).
375 GIT_TRACE_PACKET="$(pwd)/trace" git -C client \
376 fetch "file://$(pwd)/server" master &&
378 # Verify that protocol version 2 was used.
379 grep "fetch< version 2" trace &&
381 # Verify the assumption that the client needed to fetch the delta base
382 # to resolve the delta.
383 git -C server rev-parse HEAD~1^{tree} >hash &&
384 grep "want $(cat hash)" trace
387 . "$TEST_DIRECTORY"/lib-httpd.sh
390 # Converts bytes into their hexadecimal representation. For example,
391 # "printf 'ab\r\n' | hex_unpack" results in '61620d0a'.
393 perl -e '$/ = undef; $input = <>; print unpack("H2" x length($input), $input)'
396 # Inserts $1 at the start of the string and every 2 characters thereafter.
398 sed 's/\(..\)/'$1'\1/g'
401 # Create a one-time-sed command to replace the existing packfile with $1.
402 replace_packfile () {
403 # The protocol requires that the packfile be sent in sideband 1, hence
404 # the extra \x01 byte at the beginning.
405 printf "1,/packfile/!c %04x\\\\x01%s0000" \
406 "$(($(wc -c <$1) + 5))" \
407 "$(hex_unpack <$1 | intersperse '\\x')" \
408 >"$HTTPD_ROOT_PATH/one-time-sed"
411 test_expect_success 'upon cloning, check that all refs point to objects' '
412 SERVER="$HTTPD_DOCUMENT_ROOT_PATH/server" &&
413 rm -rf "$SERVER" repo &&
414 test_create_repo "$SERVER" &&
415 test_commit -C "$SERVER" foo &&
416 test_config -C "$SERVER" uploadpack.allowfilter 1 &&
417 test_config -C "$SERVER" uploadpack.allowanysha1inwant 1 &&
419 # Create a tag pointing to a blob.
420 BLOB=$(echo blob-contents | git -C "$SERVER" hash-object --stdin -w) &&
421 git -C "$SERVER" tag myblob "$BLOB" &&
423 # Craft a packfile not including that blob.
424 git -C "$SERVER" rev-parse HEAD |
425 git -C "$SERVER" pack-objects --stdout >incomplete.pack &&
427 # Replace the existing packfile with the crafted one. The protocol
428 # requires that the packfile be sent in sideband 1, hence the extra
429 # \x01 byte at the beginning.
430 replace_packfile incomplete.pack &&
432 # Use protocol v2 because the sed command looks for the "packfile"
434 test_config -C "$SERVER" protocol.version 2 &&
435 test_must_fail git -c protocol.version=2 clone \
436 --filter=blob:none $HTTPD_URL/one_time_sed/server repo 2>err &&
438 test_i18ngrep "did not send all necessary objects" err &&
440 # Ensure that the one-time-sed script was used.
441 ! test -e "$HTTPD_ROOT_PATH/one-time-sed"
444 test_expect_success 'when partial cloning, tolerate server not sending target of tag' '
445 SERVER="$HTTPD_DOCUMENT_ROOT_PATH/server" &&
446 rm -rf "$SERVER" repo &&
447 test_create_repo "$SERVER" &&
448 test_commit -C "$SERVER" foo &&
449 test_config -C "$SERVER" uploadpack.allowfilter 1 &&
450 test_config -C "$SERVER" uploadpack.allowanysha1inwant 1 &&
452 # Create an annotated tag pointing to a blob.
453 BLOB=$(echo blob-contents | git -C "$SERVER" hash-object --stdin -w) &&
454 git -C "$SERVER" tag -m message -a myblob "$BLOB" &&
456 # Craft a packfile including the tag, but not the blob it points to.
457 # Also, omit objects referenced from HEAD in order to force a second
458 # fetch (to fetch missing objects) upon the automatic checkout that
459 # happens after a clone.
460 printf "%s\n%s\n--not\n%s\n%s\n" \
461 $(git -C "$SERVER" rev-parse HEAD) \
462 $(git -C "$SERVER" rev-parse myblob) \
463 $(git -C "$SERVER" rev-parse HEAD^{tree}) \
464 $(git -C "$SERVER" rev-parse myblob^{blob}) |
465 git -C "$SERVER" pack-objects --thin --stdout >incomplete.pack &&
467 # Replace the existing packfile with the crafted one. The protocol
468 # requires that the packfile be sent in sideband 1, hence the extra
469 # \x01 byte at the beginning.
470 replace_packfile incomplete.pack &&
472 # Use protocol v2 because the sed command looks for the "packfile"
474 test_config -C "$SERVER" protocol.version 2 &&
476 # Exercise to make sure it works.
477 git -c protocol.version=2 clone \
478 --filter=blob:none $HTTPD_URL/one_time_sed/server repo 2> err &&
479 ! grep "missing object referenced by" err &&
481 # Ensure that the one-time-sed script was used.
482 ! test -e "$HTTPD_ROOT_PATH/one-time-sed"
485 test_expect_success 'tolerate server sending REF_DELTA against missing promisor objects' '
486 SERVER="$HTTPD_DOCUMENT_ROOT_PATH/server" &&
487 rm -rf "$SERVER" repo &&
488 test_create_repo "$SERVER" &&
489 test_config -C "$SERVER" uploadpack.allowfilter 1 &&
490 test_config -C "$SERVER" uploadpack.allowanysha1inwant 1 &&
492 # Create a commit with 2 blobs to be used as delta bases.
493 for i in $(test_seq 10)
495 echo "this is a line" >>"$SERVER/foo.txt" &&
496 echo "this is another line" >>"$SERVER/have.txt"
498 git -C "$SERVER" add foo.txt have.txt &&
499 git -C "$SERVER" commit -m bar &&
500 git -C "$SERVER" rev-parse HEAD:foo.txt >deltabase_missing &&
501 git -C "$SERVER" rev-parse HEAD:have.txt >deltabase_have &&
503 # Clone. The client has deltabase_have but not deltabase_missing.
504 git -c protocol.version=2 clone --no-checkout \
505 --filter=blob:none $HTTPD_URL/one_time_sed/server repo &&
506 git -C repo hash-object -w -- "$SERVER/have.txt" &&
508 # Sanity check to ensure that the client does not have
510 git -C repo rev-list --objects --ignore-missing \
511 -- $(cat deltabase_missing) >objlist &&
512 test_line_count = 0 objlist &&
514 # Another commit. This commit will be fetched by the client.
515 echo "abcdefghijklmnopqrstuvwxyz" >>"$SERVER/foo.txt" &&
516 echo "abcdefghijklmnopqrstuvwxyz" >>"$SERVER/have.txt" &&
517 git -C "$SERVER" add foo.txt have.txt &&
518 git -C "$SERVER" commit -m baz &&
520 # Pack a thin pack containing, among other things, HEAD:foo.txt
521 # delta-ed against HEAD^:foo.txt and HEAD:have.txt delta-ed against
523 printf "%s\n--not\n%s\n" \
524 $(git -C "$SERVER" rev-parse HEAD) \
525 $(git -C "$SERVER" rev-parse HEAD^) |
526 git -C "$SERVER" pack-objects --thin --stdout >thin.pack &&
528 # Ensure that the pack contains one delta against HEAD^:foo.txt. Since
529 # the delta contains at least 26 novel characters, the size cannot be
530 # contained in 4 bits, so the object header will take up 2 bytes. The
531 # most significant nybble of the first byte is 0b1111 (0b1 to indicate
532 # that the header continues, and 0b111 to indicate REF_DELTA), followed
533 # by any 3 nybbles, then the OID of the delta base.
534 printf "f.,..%s" $(intersperse "," <deltabase_missing) >want &&
535 hex_unpack <thin.pack | intersperse "," >have &&
536 grep $(cat want) have &&
538 # Ensure that the pack contains one delta against HEAD^:have.txt,
539 # similar to the above.
540 printf "f.,..%s" $(intersperse "," <deltabase_have) >want &&
541 hex_unpack <thin.pack | intersperse "," >have &&
542 grep $(cat want) have &&
544 replace_packfile thin.pack &&
546 # Use protocol v2 because the sed command looks for the "packfile"
548 test_config -C "$SERVER" protocol.version 2 &&
550 # Fetch the thin pack and ensure that index-pack is able to handle the
551 # REF_DELTA object with a missing promisor delta base.
552 GIT_TRACE_PACKET="$(pwd)/trace" git -C repo -c protocol.version=2 fetch &&
554 # Ensure that the missing delta base was directly fetched, but not the
555 # one that the client has.
556 grep "want $(cat deltabase_missing)" trace &&
557 ! grep "want $(cat deltabase_have)" trace &&
559 # Ensure that the one-time-sed script was used.
560 ! test -e "$HTTPD_ROOT_PATH/one-time-sed"
563 # DO NOT add non-httpd-specific tests here, because the last part of this
564 # test script is only executed when httpd is available and enabled.