Merge branch 'jk/partial-clone-sparse-blob'
[git/git.git] / t / t5616-partial-clone.sh
1 #!/bin/sh
2
3 test_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.
9 test_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.
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
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.
35 test_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 --quiet --objects --missing=print HEAD >revs &&
39 awk -f print_1.awk revs |
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 remote.origin.promisor)" = "true" &&
46 test "$(git -C pc1 config --local remote.origin.partialclonefilter)" = "blob:none"
47 '
48
49 # checkout master to force dynamic object fetch of blobs at HEAD.
50 test_expect_success 'verify checkout with dynamic object fetch' '
51 git -C pc1 rev-list --quiet --objects --missing=print HEAD >observed &&
52 test_line_count = 4 observed &&
53 git -C pc1 checkout master &&
54 git -C pc1 rev-list --quiet --objects --missing=print HEAD >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".
60 test_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.
75 test_expect_success 'partial fetch inherits filter settings' '
76 git -C pc1 fetch origin &&
77 git -C pc1 rev-list --quiet --objects --missing=print \
78 master..origin/master >observed &&
79 test_line_count = 5 observed
80 '
81
82 # force dynamic object fetch using diff.
83 # we should only get 1 new blob (for the file in origin/master).
84 test_expect_success 'verify diff causes dynamic object fetch' '
85 git -C pc1 diff master..origin/master -- file.1.txt &&
86 git -C pc1 rev-list --quiet --objects --missing=print \
87 master..origin/master >observed &&
88 test_line_count = 4 observed
89 '
90
91 # force full dynamic object fetch of the file's history using blame.
92 # we should get the intermediate blobs for the file.
93 test_expect_success 'verify blame causes dynamic object fetch' '
94 git -C pc1 blame origin/master -- file.1.txt >observed.blame &&
95 test_cmp expect.blame observed.blame &&
96 git -C pc1 rev-list --quiet --objects --missing=print \
97 master..origin/master >observed &&
98 test_line_count = 0 observed
99 '
100
101 # create new commits in "src" repo to establish a history on file.2.txt
102 # and push to "srv.bare".
103 test_expect_success 'push new commits to server for file.2.txt' '
104 for x in a b c d e f
105 do
106 echo "Mod file.2.txt $x" >>src/file.2.txt
107 git -C src add file.2.txt
108 git -C src commit -m "mod $x"
109 done &&
110 git -C src push -u srv master
111 '
112
113 # Do FULL fetch by disabling inherited filter-spec using --no-filter.
114 # Verify we have all the new blobs.
115 test_expect_success 'override inherited filter-spec using --no-filter' '
116 git -C pc1 fetch --no-filter origin &&
117 git -C pc1 rev-list --quiet --objects --missing=print \
118 master..origin/master >observed &&
119 test_line_count = 0 observed
120 '
121
122 # create new commits in "src" repo to establish a history on file.3.txt
123 # and push to "srv.bare".
124 test_expect_success 'push new commits to server for file.3.txt' '
125 for x in a b c d e f
126 do
127 echo "Mod file.3.txt $x" >>src/file.3.txt
128 git -C src add file.3.txt
129 git -C src commit -m "mod $x"
130 done &&
131 git -C src push -u srv master
132 '
133
134 # Do a partial fetch and then try to manually fetch the missing objects.
135 # This can be used as the basis of a pre-command hook to bulk fetch objects
136 # perhaps combined with a command in dry-run mode.
137 test_expect_success 'manual prefetch of missing objects' '
138 git -C pc1 fetch --filter=blob:none origin &&
139
140 git -C pc1 rev-list --quiet --objects --missing=print \
141 master..origin/master >revs &&
142 awk -f print_1.awk revs |
143 sed "s/?//" |
144 sort >observed.oids &&
145
146 test_line_count = 6 observed.oids &&
147 git -C pc1 fetch-pack --stdin "file://$(pwd)/srv.bare" <observed.oids &&
148
149 git -C pc1 rev-list --quiet --objects --missing=print \
150 master..origin/master >revs &&
151 awk -f print_1.awk revs |
152 sed "s/?//" |
153 sort >observed.oids &&
154
155 test_line_count = 0 observed.oids
156 '
157
158 test_expect_success 'partial clone with transfer.fsckobjects=1 uses index-pack --fsck-objects' '
159 git init src &&
160 test_commit -C src x &&
161 test_config -C src uploadpack.allowfilter 1 &&
162 test_config -C src uploadpack.allowanysha1inwant 1 &&
163
164 GIT_TRACE="$(pwd)/trace" git -c transfer.fsckobjects=1 \
165 clone --filter="blob:none" "file://$(pwd)/src" dst &&
166 grep "git index-pack.*--fsck-objects" trace
167 '
168
169 test_expect_success 'use fsck before and after manually fetching a missing subtree' '
170 # push new commit so server has a subtree
171 mkdir src/dir &&
172 echo "in dir" >src/dir/file.txt &&
173 git -C src add dir/file.txt &&
174 git -C src commit -m "file in dir" &&
175 git -C src push -u srv master &&
176 SUBTREE=$(git -C src rev-parse HEAD:dir) &&
177
178 rm -rf dst &&
179 git clone --no-checkout --filter=tree:0 "file://$(pwd)/srv.bare" dst &&
180 git -C dst fsck &&
181
182 # Make sure we only have commits, and all trees and blobs are missing.
183 git -C dst rev-list --missing=allow-any --objects master \
184 >fetched_objects &&
185 awk -f print_1.awk fetched_objects |
186 xargs -n1 git -C dst cat-file -t >fetched_types &&
187
188 sort -u fetched_types >unique_types.observed &&
189 echo commit >unique_types.expected &&
190 test_cmp unique_types.expected unique_types.observed &&
191
192 # Auto-fetch a tree with cat-file.
193 git -C dst cat-file -p $SUBTREE >tree_contents &&
194 grep file.txt tree_contents &&
195
196 # fsck still works after an auto-fetch of a tree.
197 git -C dst fsck &&
198
199 # Auto-fetch all remaining trees and blobs with --missing=error
200 git -C dst rev-list --missing=error --objects master >fetched_objects &&
201 test_line_count = 70 fetched_objects &&
202
203 awk -f print_1.awk fetched_objects |
204 xargs -n1 git -C dst cat-file -t >fetched_types &&
205
206 sort -u fetched_types >unique_types.observed &&
207 test_write_lines blob commit tree >unique_types.expected &&
208 test_cmp unique_types.expected unique_types.observed
209 '
210
211 test_expect_success 'implicitly construct combine: filter with repeated flags' '
212 GIT_TRACE=$(pwd)/trace git clone --bare \
213 --filter=blob:none --filter=tree:1 \
214 "file://$(pwd)/srv.bare" pc2 &&
215 grep "trace:.* git pack-objects .*--filter=combine:blob:none+tree:1" \
216 trace &&
217 git -C pc2 rev-list --objects --missing=allow-any HEAD >objects &&
218
219 # We should have gotten some root trees.
220 grep " $" objects &&
221 # Should not have gotten any non-root trees or blobs.
222 ! grep " ." objects &&
223
224 xargs -n 1 git -C pc2 cat-file -t <objects >types &&
225 sort -u types >unique_types.actual &&
226 test_write_lines commit tree >unique_types.expected &&
227 test_cmp unique_types.expected unique_types.actual
228 '
229
230 test_expect_success 'partial clone fetches blobs pointed to by refs even if normally filtered out' '
231 rm -rf src dst &&
232 git init src &&
233 test_commit -C src x &&
234 test_config -C src uploadpack.allowfilter 1 &&
235 test_config -C src uploadpack.allowanysha1inwant 1 &&
236
237 # Create a tag pointing to a blob.
238 BLOB=$(echo blob-contents | git -C src hash-object --stdin -w) &&
239 git -C src tag myblob "$BLOB" &&
240
241 git clone --filter="blob:none" "file://$(pwd)/src" dst 2>err &&
242 ! grep "does not point to a valid object" err &&
243 git -C dst fsck
244 '
245
246 test_expect_success 'fetch what is specified on CLI even if already promised' '
247 rm -rf src dst.git &&
248 git init src &&
249 test_commit -C src foo &&
250 test_config -C src uploadpack.allowfilter 1 &&
251 test_config -C src uploadpack.allowanysha1inwant 1 &&
252
253 git hash-object --stdin <src/foo.t >blob &&
254
255 git clone --bare --filter=blob:none "file://$(pwd)/src" dst.git &&
256 git -C dst.git rev-list --objects --quiet --missing=print HEAD >missing_before &&
257 grep "?$(cat blob)" missing_before &&
258 git -C dst.git fetch origin $(cat blob) &&
259 git -C dst.git rev-list --objects --quiet --missing=print HEAD >missing_after &&
260 ! grep "?$(cat blob)" missing_after
261 '
262
263 test_expect_success 'setup src repo for sparse filter' '
264 git init sparse-src &&
265 git -C sparse-src config --local uploadpack.allowfilter 1 &&
266 git -C sparse-src config --local uploadpack.allowanysha1inwant 1 &&
267 test_commit -C sparse-src one &&
268 test_commit -C sparse-src two &&
269 echo /one.t >sparse-src/only-one &&
270 git -C sparse-src add . &&
271 git -C sparse-src commit -m "add sparse checkout files"
272 '
273
274 test_expect_success 'partial clone with sparse filter succeeds' '
275 rm -rf dst.git &&
276 git clone --no-local --bare \
277 --filter=sparse:oid=master:only-one \
278 sparse-src dst.git &&
279 (
280 cd dst.git &&
281 git rev-list --objects --missing=print HEAD >out &&
282 grep "^$(git rev-parse HEAD:one.t)" out &&
283 grep "^?$(git rev-parse HEAD:two.t)" out
284 )
285 '
286
287 test_expect_success 'partial clone with unresolvable sparse filter fails cleanly' '
288 rm -rf dst.git &&
289 test_must_fail git clone --no-local --bare \
290 --filter=sparse:oid=master:no-such-name \
291 sparse-src dst.git 2>err &&
292 test_i18ngrep "unable to access sparse blob in .master:no-such-name" err &&
293 test_must_fail git clone --no-local --bare \
294 --filter=sparse:oid=master \
295 sparse-src dst.git 2>err &&
296 test_i18ngrep "unable to parse sparse filter data in" err
297 '
298
299 . "$TEST_DIRECTORY"/lib-httpd.sh
300 start_httpd
301
302 # Converts bytes into their hexadecimal representation. For example,
303 # "printf 'ab\r\n' | hex_unpack" results in '61620d0a'.
304 hex_unpack () {
305 perl -e '$/ = undef; $input = <>; print unpack("H2" x length($input), $input)'
306 }
307
308 # Inserts $1 at the start of the string and every 2 characters thereafter.
309 intersperse () {
310 sed 's/\(..\)/'$1'\1/g'
311 }
312
313 # Create a one-time-sed command to replace the existing packfile with $1.
314 replace_packfile () {
315 # The protocol requires that the packfile be sent in sideband 1, hence
316 # the extra \x01 byte at the beginning.
317 printf "1,/packfile/!c %04x\\\\x01%s0000" \
318 "$(($(wc -c <$1) + 5))" \
319 "$(hex_unpack <$1 | intersperse '\\x')" \
320 >"$HTTPD_ROOT_PATH/one-time-sed"
321 }
322
323 test_expect_success 'upon cloning, check that all refs point to objects' '
324 SERVER="$HTTPD_DOCUMENT_ROOT_PATH/server" &&
325 rm -rf "$SERVER" repo &&
326 test_create_repo "$SERVER" &&
327 test_commit -C "$SERVER" foo &&
328 test_config -C "$SERVER" uploadpack.allowfilter 1 &&
329 test_config -C "$SERVER" uploadpack.allowanysha1inwant 1 &&
330
331 # Create a tag pointing to a blob.
332 BLOB=$(echo blob-contents | git -C "$SERVER" hash-object --stdin -w) &&
333 git -C "$SERVER" tag myblob "$BLOB" &&
334
335 # Craft a packfile not including that blob.
336 git -C "$SERVER" rev-parse HEAD |
337 git -C "$SERVER" pack-objects --stdout >incomplete.pack &&
338
339 # Replace the existing packfile with the crafted one. The protocol
340 # requires that the packfile be sent in sideband 1, hence the extra
341 # \x01 byte at the beginning.
342 replace_packfile incomplete.pack &&
343
344 # Use protocol v2 because the sed command looks for the "packfile"
345 # section header.
346 test_config -C "$SERVER" protocol.version 2 &&
347 test_must_fail git -c protocol.version=2 clone \
348 --filter=blob:none $HTTPD_URL/one_time_sed/server repo 2>err &&
349
350 test_i18ngrep "did not send all necessary objects" err &&
351
352 # Ensure that the one-time-sed script was used.
353 ! test -e "$HTTPD_ROOT_PATH/one-time-sed"
354 '
355
356 test_expect_success 'when partial cloning, tolerate server not sending target of tag' '
357 SERVER="$HTTPD_DOCUMENT_ROOT_PATH/server" &&
358 rm -rf "$SERVER" repo &&
359 test_create_repo "$SERVER" &&
360 test_commit -C "$SERVER" foo &&
361 test_config -C "$SERVER" uploadpack.allowfilter 1 &&
362 test_config -C "$SERVER" uploadpack.allowanysha1inwant 1 &&
363
364 # Create an annotated tag pointing to a blob.
365 BLOB=$(echo blob-contents | git -C "$SERVER" hash-object --stdin -w) &&
366 git -C "$SERVER" tag -m message -a myblob "$BLOB" &&
367
368 # Craft a packfile including the tag, but not the blob it points to.
369 # Also, omit objects referenced from HEAD in order to force a second
370 # fetch (to fetch missing objects) upon the automatic checkout that
371 # happens after a clone.
372 printf "%s\n%s\n--not\n%s\n%s\n" \
373 $(git -C "$SERVER" rev-parse HEAD) \
374 $(git -C "$SERVER" rev-parse myblob) \
375 $(git -C "$SERVER" rev-parse HEAD^{tree}) \
376 $(git -C "$SERVER" rev-parse myblob^{blob}) |
377 git -C "$SERVER" pack-objects --thin --stdout >incomplete.pack &&
378
379 # Replace the existing packfile with the crafted one. The protocol
380 # requires that the packfile be sent in sideband 1, hence the extra
381 # \x01 byte at the beginning.
382 replace_packfile incomplete.pack &&
383
384 # Use protocol v2 because the sed command looks for the "packfile"
385 # section header.
386 test_config -C "$SERVER" protocol.version 2 &&
387
388 # Exercise to make sure it works.
389 git -c protocol.version=2 clone \
390 --filter=blob:none $HTTPD_URL/one_time_sed/server repo 2> err &&
391 ! grep "missing object referenced by" err &&
392
393 # Ensure that the one-time-sed script was used.
394 ! test -e "$HTTPD_ROOT_PATH/one-time-sed"
395 '
396
397 test_expect_success 'tolerate server sending REF_DELTA against missing promisor objects' '
398 SERVER="$HTTPD_DOCUMENT_ROOT_PATH/server" &&
399 rm -rf "$SERVER" repo &&
400 test_create_repo "$SERVER" &&
401 test_config -C "$SERVER" uploadpack.allowfilter 1 &&
402 test_config -C "$SERVER" uploadpack.allowanysha1inwant 1 &&
403
404 # Create a commit with 2 blobs to be used as delta bases.
405 for i in $(test_seq 10)
406 do
407 echo "this is a line" >>"$SERVER/foo.txt" &&
408 echo "this is another line" >>"$SERVER/have.txt"
409 done &&
410 git -C "$SERVER" add foo.txt have.txt &&
411 git -C "$SERVER" commit -m bar &&
412 git -C "$SERVER" rev-parse HEAD:foo.txt >deltabase_missing &&
413 git -C "$SERVER" rev-parse HEAD:have.txt >deltabase_have &&
414
415 # Clone. The client has deltabase_have but not deltabase_missing.
416 git -c protocol.version=2 clone --no-checkout \
417 --filter=blob:none $HTTPD_URL/one_time_sed/server repo &&
418 git -C repo hash-object -w -- "$SERVER/have.txt" &&
419
420 # Sanity check to ensure that the client does not have
421 # deltabase_missing.
422 git -C repo rev-list --objects --ignore-missing \
423 -- $(cat deltabase_missing) >objlist &&
424 test_line_count = 0 objlist &&
425
426 # Another commit. This commit will be fetched by the client.
427 echo "abcdefghijklmnopqrstuvwxyz" >>"$SERVER/foo.txt" &&
428 echo "abcdefghijklmnopqrstuvwxyz" >>"$SERVER/have.txt" &&
429 git -C "$SERVER" add foo.txt have.txt &&
430 git -C "$SERVER" commit -m baz &&
431
432 # Pack a thin pack containing, among other things, HEAD:foo.txt
433 # delta-ed against HEAD^:foo.txt and HEAD:have.txt delta-ed against
434 # HEAD^:have.txt.
435 printf "%s\n--not\n%s\n" \
436 $(git -C "$SERVER" rev-parse HEAD) \
437 $(git -C "$SERVER" rev-parse HEAD^) |
438 git -C "$SERVER" pack-objects --thin --stdout >thin.pack &&
439
440 # Ensure that the pack contains one delta against HEAD^:foo.txt. Since
441 # the delta contains at least 26 novel characters, the size cannot be
442 # contained in 4 bits, so the object header will take up 2 bytes. The
443 # most significant nybble of the first byte is 0b1111 (0b1 to indicate
444 # that the header continues, and 0b111 to indicate REF_DELTA), followed
445 # by any 3 nybbles, then the OID of the delta base.
446 printf "f.,..%s" $(intersperse "," <deltabase_missing) >want &&
447 hex_unpack <thin.pack | intersperse "," >have &&
448 grep $(cat want) have &&
449
450 # Ensure that the pack contains one delta against HEAD^:have.txt,
451 # similar to the above.
452 printf "f.,..%s" $(intersperse "," <deltabase_have) >want &&
453 hex_unpack <thin.pack | intersperse "," >have &&
454 grep $(cat want) have &&
455
456 replace_packfile thin.pack &&
457
458 # Use protocol v2 because the sed command looks for the "packfile"
459 # section header.
460 test_config -C "$SERVER" protocol.version 2 &&
461
462 # Fetch the thin pack and ensure that index-pack is able to handle the
463 # REF_DELTA object with a missing promisor delta base.
464 GIT_TRACE_PACKET="$(pwd)/trace" git -C repo -c protocol.version=2 fetch &&
465
466 # Ensure that the missing delta base was directly fetched, but not the
467 # one that the client has.
468 grep "want $(cat deltabase_missing)" trace &&
469 ! grep "want $(cat deltabase_have)" trace &&
470
471 # Ensure that the one-time-sed script was used.
472 ! test -e "$HTTPD_ROOT_PATH/one-time-sed"
473 '
474
475 # DO NOT add non-httpd-specific tests here, because the last part of this
476 # test script is only executed when httpd is available and enabled.
477
478 test_done