Merge branch 'jt/cache-tree-avoid-lazy-fetch-during-merge'
[git] / t / t0410-partial-clone.sh
1 #!/bin/sh
2
3 test_description='partial clone'
4
5 . ./test-lib.sh
6
7 delete_object () {
8         rm $1/.git/objects/$(echo $2 | sed -e 's|^..|&/|')
9 }
10
11 pack_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
17 promise_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
26 test_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 remote.origin.partialclonefilter &&
30         git -C client fetch origin
31 '
32
33 test_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
58 test_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
81 test_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
99 test_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
115 test_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
138 test_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
151 test_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=$(sed "s/promisor$/idx/" promisorlist) &&
170         git verify-pack --verbose "$IDX" >out &&
171         grep "$HASH" out
172 '
173
174 test_expect_success 'fetching of missing objects works with ref-in-want enabled' '
175         # ref-in-want requires protocol version 2
176         git -C server config protocol.version 2 &&
177         git -C server config uploadpack.allowrefinwant 1 &&
178         git -C repo config protocol.version 2 &&
179
180         rm -rf repo/.git/objects/* &&
181         rm -f trace &&
182         GIT_TRACE_PACKET="$(pwd)/trace" git -C repo cat-file -p "$HASH" &&
183         grep "git< fetch=.*ref-in-want" trace
184 '
185
186 test_expect_success 'fetching of missing objects from another promisor remote' '
187         git clone "file://$(pwd)/server" server2 &&
188         test_commit -C server2 bar &&
189         git -C server2 repack -a -d --write-bitmap-index &&
190         HASH2=$(git -C server2 rev-parse bar) &&
191
192         git -C repo remote add server2 "file://$(pwd)/server2" &&
193         git -C repo config remote.server2.promisor true &&
194         git -C repo cat-file -p "$HASH2" &&
195
196         git -C repo fetch server2 &&
197         rm -rf repo/.git/objects/* &&
198         git -C repo cat-file -p "$HASH2" &&
199
200         # Ensure that the .promisor file is written, and check that its
201         # associated packfile contains the object
202         ls repo/.git/objects/pack/pack-*.promisor >promisorlist &&
203         test_line_count = 1 promisorlist &&
204         IDX=$(sed "s/promisor$/idx/" promisorlist) &&
205         git verify-pack --verbose "$IDX" >out &&
206         grep "$HASH2" out
207 '
208
209 test_expect_success 'fetching of missing objects configures a promisor remote' '
210         git clone "file://$(pwd)/server" server3 &&
211         test_commit -C server3 baz &&
212         git -C server3 repack -a -d --write-bitmap-index &&
213         HASH3=$(git -C server3 rev-parse baz) &&
214         git -C server3 config uploadpack.allowfilter 1 &&
215
216         rm repo/.git/objects/pack/pack-*.promisor &&
217
218         git -C repo remote add server3 "file://$(pwd)/server3" &&
219         git -C repo fetch --filter="blob:none" server3 $HASH3 &&
220
221         test_cmp_config -C repo true remote.server3.promisor &&
222
223         # Ensure that the .promisor file is written, and check that its
224         # associated packfile contains the object
225         ls repo/.git/objects/pack/pack-*.promisor >promisorlist &&
226         test_line_count = 1 promisorlist &&
227         IDX=$(sed "s/promisor$/idx/" promisorlist) &&
228         git verify-pack --verbose "$IDX" >out &&
229         grep "$HASH3" out
230 '
231
232 test_expect_success 'fetching of missing blobs works' '
233         rm -rf server server2 repo &&
234         rm -rf server server3 repo &&
235         test_create_repo server &&
236         test_commit -C server foo &&
237         git -C server repack -a -d --write-bitmap-index &&
238
239         git clone "file://$(pwd)/server" repo &&
240         git hash-object repo/foo.t >blobhash &&
241         rm -rf repo/.git/objects/* &&
242
243         git -C server config uploadpack.allowanysha1inwant 1 &&
244         git -C server config uploadpack.allowfilter 1 &&
245         git -C repo config core.repositoryformatversion 1 &&
246         git -C repo config extensions.partialclone "origin" &&
247
248         git -C repo cat-file -p $(cat blobhash)
249 '
250
251 test_expect_success 'fetching of missing trees does not fetch blobs' '
252         rm -rf server repo &&
253         test_create_repo server &&
254         test_commit -C server foo &&
255         git -C server repack -a -d --write-bitmap-index &&
256
257         git clone "file://$(pwd)/server" repo &&
258         git -C repo rev-parse foo^{tree} >treehash &&
259         git hash-object repo/foo.t >blobhash &&
260         rm -rf repo/.git/objects/* &&
261
262         git -C server config uploadpack.allowanysha1inwant 1 &&
263         git -C server config uploadpack.allowfilter 1 &&
264         git -C repo config core.repositoryformatversion 1 &&
265         git -C repo config extensions.partialclone "origin" &&
266         git -C repo cat-file -p $(cat treehash) &&
267
268         # Ensure that the tree, but not the blob, is fetched
269         git -C repo rev-list --objects --missing=print $(cat treehash) >objects &&
270         grep "^$(cat treehash)" objects &&
271         grep "^[?]$(cat blobhash)" objects
272 '
273
274 test_expect_success 'rev-list stops traversal at missing and promised commit' '
275         rm -rf repo &&
276         test_create_repo repo &&
277         test_commit -C repo foo &&
278         test_commit -C repo bar &&
279
280         FOO=$(git -C repo rev-parse foo) &&
281         promise_and_delete "$FOO" &&
282
283         git -C repo config core.repositoryformatversion 1 &&
284         git -C repo config extensions.partialclone "arbitrary string" &&
285         GIT_TEST_COMMIT_GRAPH=0 git -C repo -c core.commitGraph=false rev-list --exclude-promisor-objects --objects bar >out &&
286         grep $(git -C repo rev-parse bar) out &&
287         ! grep $FOO out
288 '
289
290 test_expect_success 'missing tree objects with --missing=allow-promisor and --exclude-promisor-objects' '
291         rm -rf repo &&
292         test_create_repo repo &&
293         test_commit -C repo foo &&
294         test_commit -C repo bar &&
295         test_commit -C repo baz &&
296
297         promise_and_delete $(git -C repo rev-parse bar^{tree}) &&
298         promise_and_delete $(git -C repo rev-parse foo^{tree}) &&
299
300         git -C repo config core.repositoryformatversion 1 &&
301         git -C repo config extensions.partialclone "arbitrary string" &&
302
303         git -C repo rev-list --missing=allow-promisor --objects HEAD >objs 2>rev_list_err &&
304         test_must_be_empty rev_list_err &&
305         # 3 commits, 3 blobs, and 1 tree
306         test_line_count = 7 objs &&
307
308         # Do the same for --exclude-promisor-objects, but with all trees gone.
309         promise_and_delete $(git -C repo rev-parse baz^{tree}) &&
310         git -C repo rev-list --exclude-promisor-objects --objects HEAD >objs 2>rev_list_err &&
311         test_must_be_empty rev_list_err &&
312         # 3 commits, no blobs or trees
313         test_line_count = 3 objs
314 '
315
316 test_expect_success 'missing non-root tree object and rev-list' '
317         rm -rf repo &&
318         test_create_repo repo &&
319         mkdir repo/dir &&
320         echo foo >repo/dir/foo &&
321         git -C repo add dir/foo &&
322         git -C repo commit -m "commit dir/foo" &&
323
324         promise_and_delete $(git -C repo rev-parse HEAD:dir) &&
325
326         git -C repo config core.repositoryformatversion 1 &&
327         git -C repo config extensions.partialclone "arbitrary string" &&
328
329         git -C repo rev-list --missing=allow-any --objects HEAD >objs 2>rev_list_err &&
330         test_must_be_empty rev_list_err &&
331         # 1 commit and 1 tree
332         test_line_count = 2 objs
333 '
334
335 test_expect_success 'rev-list stops traversal at missing and promised tree' '
336         rm -rf repo &&
337         test_create_repo repo &&
338         test_commit -C repo foo &&
339         mkdir repo/a_dir &&
340         echo something >repo/a_dir/something &&
341         git -C repo add a_dir/something &&
342         git -C repo commit -m bar &&
343
344         # foo^{tree} (tree referenced from commit)
345         TREE=$(git -C repo rev-parse foo^{tree}) &&
346
347         # a tree referenced by HEAD^{tree} (tree referenced from tree)
348         TREE2=$(git -C repo ls-tree HEAD^{tree} | grep " tree " | head -1 | cut -b13-52) &&
349
350         promise_and_delete "$TREE" &&
351         promise_and_delete "$TREE2" &&
352
353         git -C repo config core.repositoryformatversion 1 &&
354         git -C repo config extensions.partialclone "arbitrary string" &&
355         git -C repo rev-list --exclude-promisor-objects --objects HEAD >out &&
356         grep $(git -C repo rev-parse foo) out &&
357         ! grep $TREE out &&
358         grep $(git -C repo rev-parse HEAD) out &&
359         ! grep $TREE2 out
360 '
361
362 test_expect_success 'rev-list stops traversal at missing and promised blob' '
363         rm -rf repo &&
364         test_create_repo repo &&
365         echo something >repo/something &&
366         git -C repo add something &&
367         git -C repo commit -m foo &&
368
369         BLOB=$(git -C repo hash-object -w something) &&
370         promise_and_delete "$BLOB" &&
371
372         git -C repo config core.repositoryformatversion 1 &&
373         git -C repo config extensions.partialclone "arbitrary string" &&
374         git -C repo rev-list --exclude-promisor-objects --objects HEAD >out &&
375         grep $(git -C repo rev-parse HEAD) out &&
376         ! grep $BLOB out
377 '
378
379 test_expect_success 'rev-list stops traversal at promisor commit, tree, and blob' '
380         rm -rf repo &&
381         test_create_repo repo &&
382         test_commit -C repo foo &&
383         test_commit -C repo bar &&
384         test_commit -C repo baz &&
385
386         COMMIT=$(git -C repo rev-parse foo) &&
387         TREE=$(git -C repo rev-parse bar^{tree}) &&
388         BLOB=$(git hash-object repo/baz.t) &&
389         printf "%s\n%s\n%s\n" $COMMIT $TREE $BLOB | pack_as_from_promisor &&
390
391         git -C repo config core.repositoryformatversion 1 &&
392         git -C repo config extensions.partialclone "arbitrary string" &&
393         git -C repo rev-list --exclude-promisor-objects --objects HEAD >out &&
394         ! grep $COMMIT out &&
395         ! grep $TREE out &&
396         ! grep $BLOB out &&
397         grep $(git -C repo rev-parse bar) out  # sanity check that some walking was done
398 '
399
400 test_expect_success 'rev-list dies for missing objects on cmd line' '
401         rm -rf repo &&
402         test_create_repo repo &&
403         test_commit -C repo foo &&
404         test_commit -C repo bar &&
405         test_commit -C repo baz &&
406
407         COMMIT=$(git -C repo rev-parse foo) &&
408         TREE=$(git -C repo rev-parse bar^{tree}) &&
409         BLOB=$(git hash-object repo/baz.t) &&
410
411         promise_and_delete $COMMIT &&
412         promise_and_delete $TREE &&
413         promise_and_delete $BLOB &&
414
415         git -C repo config core.repositoryformatversion 1 &&
416         git -C repo config extensions.partialclone "arbitrary string" &&
417
418         for OBJ in "$COMMIT" "$TREE" "$BLOB"; do
419                 test_must_fail git -C repo rev-list --objects \
420                         --exclude-promisor-objects "$OBJ" &&
421                 test_must_fail git -C repo rev-list --objects-edge-aggressive \
422                         --exclude-promisor-objects "$OBJ" &&
423
424                 # Do not die or crash when --ignore-missing is passed.
425                 git -C repo rev-list --ignore-missing --objects \
426                         --exclude-promisor-objects "$OBJ" &&
427                 git -C repo rev-list --ignore-missing --objects-edge-aggressive \
428                         --exclude-promisor-objects "$OBJ"
429         done
430 '
431
432 test_expect_success 'gc repacks promisor objects separately from non-promisor objects' '
433         rm -rf repo &&
434         test_create_repo repo &&
435         test_commit -C repo one &&
436         test_commit -C repo two &&
437
438         TREE_ONE=$(git -C repo rev-parse one^{tree}) &&
439         printf "$TREE_ONE\n" | pack_as_from_promisor &&
440         TREE_TWO=$(git -C repo rev-parse two^{tree}) &&
441         printf "$TREE_TWO\n" | pack_as_from_promisor &&
442
443         git -C repo config core.repositoryformatversion 1 &&
444         git -C repo config extensions.partialclone "arbitrary string" &&
445         git -C repo gc &&
446
447         # Ensure that exactly one promisor packfile exists, and that it
448         # contains the trees but not the commits
449         ls repo/.git/objects/pack/pack-*.promisor >promisorlist &&
450         test_line_count = 1 promisorlist &&
451         PROMISOR_PACKFILE=$(sed "s/.promisor/.pack/" <promisorlist) &&
452         git verify-pack $PROMISOR_PACKFILE -v >out &&
453         grep "$TREE_ONE" out &&
454         grep "$TREE_TWO" out &&
455         ! grep "$(git -C repo rev-parse one)" out &&
456         ! grep "$(git -C repo rev-parse two)" out &&
457
458         # Remove the promisor packfile and associated files
459         rm $(sed "s/.promisor//" <promisorlist).* &&
460
461         # Ensure that the single other pack contains the commits, but not the
462         # trees
463         ls repo/.git/objects/pack/pack-*.pack >packlist &&
464         test_line_count = 1 packlist &&
465         git verify-pack repo/.git/objects/pack/pack-*.pack -v >out &&
466         grep "$(git -C repo rev-parse one)" out &&
467         grep "$(git -C repo rev-parse two)" out &&
468         ! grep "$TREE_ONE" out &&
469         ! grep "$TREE_TWO" out
470 '
471
472 test_expect_success 'gc does not repack promisor objects if there are none' '
473         rm -rf repo &&
474         test_create_repo repo &&
475         test_commit -C repo one &&
476
477         git -C repo config core.repositoryformatversion 1 &&
478         git -C repo config extensions.partialclone "arbitrary string" &&
479         git -C repo gc &&
480
481         # Ensure that only one pack exists
482         ls repo/.git/objects/pack/pack-*.pack >packlist &&
483         test_line_count = 1 packlist
484 '
485
486 repack_and_check () {
487         rm -rf repo2 &&
488         cp -r repo repo2 &&
489         git -C repo2 repack $1 -d &&
490         git -C repo2 fsck &&
491
492         git -C repo2 cat-file -e $2 &&
493         git -C repo2 cat-file -e $3
494 }
495
496 test_expect_success 'repack -d does not irreversibly delete promisor objects' '
497         rm -rf repo &&
498         test_create_repo repo &&
499         git -C repo config core.repositoryformatversion 1 &&
500         git -C repo config extensions.partialclone "arbitrary string" &&
501
502         git -C repo commit --allow-empty -m one &&
503         git -C repo commit --allow-empty -m two &&
504         git -C repo commit --allow-empty -m three &&
505         git -C repo commit --allow-empty -m four &&
506         ONE=$(git -C repo rev-parse HEAD^^^) &&
507         TWO=$(git -C repo rev-parse HEAD^^) &&
508         THREE=$(git -C repo rev-parse HEAD^) &&
509
510         printf "$TWO\n" | pack_as_from_promisor &&
511         printf "$THREE\n" | pack_as_from_promisor &&
512         delete_object repo "$ONE" &&
513
514         repack_and_check -a "$TWO" "$THREE" &&
515         repack_and_check -A "$TWO" "$THREE" &&
516         repack_and_check -l "$TWO" "$THREE"
517 '
518
519 test_expect_success 'gc stops traversal when a missing but promised object is reached' '
520         rm -rf repo &&
521         test_create_repo repo &&
522         test_commit -C repo my_commit &&
523
524         TREE_HASH=$(git -C repo rev-parse HEAD^{tree}) &&
525         HASH=$(promise_and_delete $TREE_HASH) &&
526
527         git -C repo config core.repositoryformatversion 1 &&
528         git -C repo config extensions.partialclone "arbitrary string" &&
529         git -C repo gc &&
530
531         # Ensure that the promisor packfile still exists, and remove it
532         test -e repo/.git/objects/pack/pack-$HASH.pack &&
533         rm repo/.git/objects/pack/pack-$HASH.* &&
534
535         # Ensure that the single other pack contains the commit, but not the tree
536         ls repo/.git/objects/pack/pack-*.pack >packlist &&
537         test_line_count = 1 packlist &&
538         git verify-pack repo/.git/objects/pack/pack-*.pack -v >out &&
539         grep "$(git -C repo rev-parse HEAD)" out &&
540         ! grep "$TREE_HASH" out
541 '
542
543 test_expect_success 'do not fetch when checking existence of tree we construct ourselves' '
544         rm -rf repo &&
545         test_create_repo repo &&
546         test_commit -C repo base &&
547         test_commit -C repo side1 &&
548         git -C repo checkout base &&
549         test_commit -C repo side2 &&
550
551         git -C repo config core.repositoryformatversion 1 &&
552         git -C repo config extensions.partialclone "arbitrary string" &&
553
554         git -C repo cherry-pick side1
555 '
556
557 . "$TEST_DIRECTORY"/lib-httpd.sh
558 start_httpd
559
560 test_expect_success 'fetching of missing objects from an HTTP server' '
561         rm -rf repo &&
562         SERVER="$HTTPD_DOCUMENT_ROOT_PATH/server" &&
563         test_create_repo "$SERVER" &&
564         test_commit -C "$SERVER" foo &&
565         git -C "$SERVER" repack -a -d --write-bitmap-index &&
566
567         git clone $HTTPD_URL/smart/server repo &&
568         HASH=$(git -C repo rev-parse foo) &&
569         rm -rf repo/.git/objects/* &&
570
571         git -C repo config core.repositoryformatversion 1 &&
572         git -C repo config extensions.partialclone "origin" &&
573         git -C repo cat-file -p "$HASH" &&
574
575         # Ensure that the .promisor file is written, and check that its
576         # associated packfile contains the object
577         ls repo/.git/objects/pack/pack-*.promisor >promisorlist &&
578         test_line_count = 1 promisorlist &&
579         IDX=$(sed "s/promisor$/idx/" promisorlist) &&
580         git verify-pack --verbose "$IDX" >out &&
581         grep "$HASH" out
582 '
583
584 # DO NOT add non-httpd-specific tests here, because the last part of this
585 # test script is only executed when httpd is available and enabled.
586
587 test_done