Merge branch 'ab/config-based-hooks-base' into seen
[git] / t / t5601-clone.sh
1 #!/bin/sh
2
3 test_description=clone
4
5 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
6 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
7
8 . ./test-lib.sh
9
10 X=
11 test_have_prereq !MINGW || X=.exe
12
13 test_expect_success setup '
14
15         rm -fr .git &&
16         test_create_repo src &&
17         (
18                 cd src &&
19                 >file &&
20                 git add file &&
21                 git commit -m initial &&
22                 echo 1 >file &&
23                 git add file &&
24                 git commit -m updated
25         )
26
27 '
28
29 test_expect_success 'clone with excess parameters (1)' '
30
31         rm -fr dst &&
32         test_must_fail git clone -n src dst junk
33
34 '
35
36 test_expect_success 'clone with excess parameters (2)' '
37
38         rm -fr dst &&
39         test_must_fail git clone -n "file://$(pwd)/src" dst junk
40
41 '
42
43 test_expect_success 'output from clone' '
44         rm -fr dst &&
45         git clone -n "file://$(pwd)/src" dst >output 2>&1 &&
46         test $(grep Clon output | wc -l) = 1
47 '
48
49 test_expect_success 'clone does not keep pack' '
50
51         rm -fr dst &&
52         git clone -n "file://$(pwd)/src" dst &&
53         ! test -f dst/file &&
54         ! (echo dst/.git/objects/pack/pack-* | grep "\.keep")
55
56 '
57
58 test_expect_success 'clone checks out files' '
59
60         rm -fr dst &&
61         git clone src dst &&
62         test -f dst/file
63
64 '
65
66 test_expect_success 'clone respects GIT_WORK_TREE' '
67
68         GIT_WORK_TREE=worktree git clone src bare &&
69         test -f bare/config &&
70         test -f worktree/file
71
72 '
73
74 test_expect_success 'clone from hooks' '
75
76         test_create_repo r0 &&
77         cd r0 &&
78         test_commit initial &&
79         cd .. &&
80         git init r1 &&
81         cd r1 &&
82         cat >.git/hooks/pre-commit <<-\EOF &&
83         #!/bin/sh
84         git clone ../r0 ../r2
85         exit 1
86         EOF
87         chmod u+x .git/hooks/pre-commit &&
88         : >file &&
89         git add file &&
90         test_must_fail git commit -m invoke-hook &&
91         cd .. &&
92         test_cmp r0/.git/HEAD r2/.git/HEAD &&
93         test_cmp r0/initial.t r2/initial.t
94
95 '
96
97 test_expect_success 'clone creates intermediate directories' '
98
99         git clone src long/path/to/dst &&
100         test -f long/path/to/dst/file
101
102 '
103
104 test_expect_success 'clone creates intermediate directories for bare repo' '
105
106         git clone --bare src long/path/to/bare/dst &&
107         test -f long/path/to/bare/dst/config
108
109 '
110
111 test_expect_success 'clone --mirror' '
112
113         git clone --mirror src mirror &&
114         test -f mirror/HEAD &&
115         test ! -f mirror/file &&
116         FETCH="$(cd mirror && git config remote.origin.fetch)" &&
117         test "+refs/*:refs/*" = "$FETCH" &&
118         MIRROR="$(cd mirror && git config --bool remote.origin.mirror)" &&
119         test "$MIRROR" = true
120
121 '
122
123 test_expect_success 'clone --mirror with detached HEAD' '
124
125         ( cd src && git checkout HEAD^ && git rev-parse HEAD >../expected ) &&
126         git clone --mirror src mirror.detached &&
127         ( cd src && git checkout - ) &&
128         GIT_DIR=mirror.detached git rev-parse HEAD >actual &&
129         test_cmp expected actual
130
131 '
132
133 test_expect_success 'clone --bare with detached HEAD' '
134
135         ( cd src && git checkout HEAD^ && git rev-parse HEAD >../expected ) &&
136         git clone --bare src bare.detached &&
137         ( cd src && git checkout - ) &&
138         GIT_DIR=bare.detached git rev-parse HEAD >actual &&
139         test_cmp expected actual
140
141 '
142
143 test_expect_success 'clone --bare names the local repository <name>.git' '
144
145         git clone --bare src &&
146         test -d src.git
147
148 '
149
150 test_expect_success 'clone --mirror does not repeat tags' '
151
152         (cd src &&
153          git tag some-tag HEAD) &&
154         git clone --mirror src mirror2 &&
155         (cd mirror2 &&
156          git show-ref 2> clone.err > clone.out) &&
157         ! grep Duplicate mirror2/clone.err &&
158         grep some-tag mirror2/clone.out
159
160 '
161
162 test_expect_success 'clone to destination with trailing /' '
163
164         git clone src target-1/ &&
165         T=$( cd target-1 && git rev-parse HEAD ) &&
166         S=$( cd src && git rev-parse HEAD ) &&
167         test "$T" = "$S"
168
169 '
170
171 test_expect_success 'clone to destination with extra trailing /' '
172
173         git clone src target-2/// &&
174         T=$( cd target-2 && git rev-parse HEAD ) &&
175         S=$( cd src && git rev-parse HEAD ) &&
176         test "$T" = "$S"
177
178 '
179
180 test_expect_success 'clone to an existing empty directory' '
181         mkdir target-3 &&
182         git clone src target-3 &&
183         T=$( cd target-3 && git rev-parse HEAD ) &&
184         S=$( cd src && git rev-parse HEAD ) &&
185         test "$T" = "$S"
186 '
187
188 test_expect_success 'clone to an existing non-empty directory' '
189         mkdir target-4 &&
190         >target-4/Fakefile &&
191         test_must_fail git clone src target-4
192 '
193
194 test_expect_success 'clone to an existing path' '
195         >target-5 &&
196         test_must_fail git clone src target-5
197 '
198
199 test_expect_success 'clone a void' '
200         mkdir src-0 &&
201         (
202                 cd src-0 && git init
203         ) &&
204         git clone "file://$(pwd)/src-0" target-6 2>err-6 &&
205         ! grep "fatal:" err-6 &&
206         (
207                 cd src-0 && test_commit A
208         ) &&
209         git clone "file://$(pwd)/src-0" target-7 2>err-7 &&
210         ! grep "fatal:" err-7 &&
211         # There is no reason to insist they are bit-for-bit
212         # identical, but this test should suffice for now.
213         test_cmp target-6/.git/config target-7/.git/config
214 '
215
216 test_expect_success 'clone respects global branch.autosetuprebase' '
217         (
218                 test_config="$HOME/.gitconfig" &&
219                 git config -f "$test_config" branch.autosetuprebase remote &&
220                 rm -fr dst &&
221                 git clone src dst &&
222                 cd dst &&
223                 actual="z$(git config branch.main.rebase)" &&
224                 test ztrue = $actual
225         )
226 '
227
228 test_expect_success 'respect url-encoding of file://' '
229         git init x+y &&
230         git clone "file://$PWD/x+y" xy-url-1 &&
231         git clone "file://$PWD/x%2By" xy-url-2
232 '
233
234 test_expect_success 'do not query-string-decode + in URLs' '
235         rm -rf x+y &&
236         git init "x y" &&
237         test_must_fail git clone "file://$PWD/x+y" xy-no-plus
238 '
239
240 test_expect_success 'do not respect url-encoding of non-url path' '
241         git init x+y &&
242         test_must_fail git clone x%2By xy-regular &&
243         git clone x+y xy-regular
244 '
245
246 test_expect_success 'clone separate gitdir' '
247         rm -rf dst &&
248         git clone --separate-git-dir realgitdir src dst &&
249         test -d realgitdir/refs
250 '
251
252 test_expect_success 'clone separate gitdir: output' '
253         echo "gitdir: $(pwd)/realgitdir" >expected &&
254         test_cmp expected dst/.git
255 '
256
257 test_expect_success 'clone from .git file' '
258         git clone dst/.git dst2
259 '
260
261 test_expect_success 'fetch from .git gitfile' '
262         (
263                 cd dst2 &&
264                 git fetch ../dst/.git
265         )
266 '
267
268 test_expect_success 'fetch from gitfile parent' '
269         (
270                 cd dst2 &&
271                 git fetch ../dst
272         )
273 '
274
275 test_expect_success 'clone separate gitdir where target already exists' '
276         rm -rf dst &&
277         echo foo=bar >>realgitdir/config &&
278         test_must_fail git clone --separate-git-dir realgitdir src dst &&
279         grep foo=bar realgitdir/config
280 '
281
282 test_expect_success 'clone --reference from original' '
283         git clone --shared --bare src src-1 &&
284         git clone --bare src src-2 &&
285         git clone --reference=src-2 --bare src-1 target-8 &&
286         grep /src-2/ target-8/objects/info/alternates
287 '
288
289 test_expect_success 'clone with more than one --reference' '
290         git clone --bare src src-3 &&
291         git clone --bare src src-4 &&
292         git clone --reference=src-3 --reference=src-4 src target-9 &&
293         grep /src-3/ target-9/.git/objects/info/alternates &&
294         grep /src-4/ target-9/.git/objects/info/alternates
295 '
296
297 test_expect_success 'clone from original with relative alternate' '
298         mkdir nest &&
299         git clone --bare src nest/src-5 &&
300         echo ../../../src/.git/objects >nest/src-5/objects/info/alternates &&
301         git clone --bare nest/src-5 target-10 &&
302         grep /src/\\.git/objects target-10/objects/info/alternates
303 '
304
305 test_expect_success 'clone checking out a tag' '
306         git clone --branch=some-tag src dst.tag &&
307         GIT_DIR=src/.git git rev-parse some-tag >expected &&
308         GIT_DIR=dst.tag/.git git rev-parse HEAD >actual &&
309         test_cmp expected actual &&
310         GIT_DIR=dst.tag/.git git config remote.origin.fetch >fetch.actual &&
311         echo "+refs/heads/*:refs/remotes/origin/*" >fetch.expected &&
312         test_cmp fetch.expected fetch.actual
313 '
314
315 test_expect_success 'set up ssh wrapper' '
316         cp "$GIT_BUILD_DIR/t/helper/test-fake-ssh$X" \
317                 "$TRASH_DIRECTORY/ssh$X" &&
318         GIT_SSH="$TRASH_DIRECTORY/ssh$X" &&
319         export GIT_SSH &&
320         export TRASH_DIRECTORY &&
321         >"$TRASH_DIRECTORY"/ssh-output
322 '
323
324 copy_ssh_wrapper_as () {
325         rm -f "${1%$X}$X" &&
326         cp "$TRASH_DIRECTORY/ssh$X" "${1%$X}$X" &&
327         test_when_finished "rm $(git rev-parse --sq-quote "${1%$X}$X")" &&
328         GIT_SSH="${1%$X}$X" &&
329         test_when_finished "GIT_SSH=\"\$TRASH_DIRECTORY/ssh\$X\""
330 }
331
332 expect_ssh () {
333         test_when_finished '
334                 (cd "$TRASH_DIRECTORY" && rm -f ssh-expect && >ssh-output)
335         ' &&
336         {
337                 case "$#" in
338                 1)
339                         ;;
340                 2)
341                         echo "ssh: $1 git-upload-pack '$2'"
342                         ;;
343                 3)
344                         echo "ssh: $1 $2 git-upload-pack '$3'"
345                         ;;
346                 *)
347                         echo "ssh: $1 $2 git-upload-pack '$3' $4"
348                 esac
349         } >"$TRASH_DIRECTORY/ssh-expect" &&
350         (cd "$TRASH_DIRECTORY" && test_cmp ssh-expect ssh-output)
351 }
352
353 test_expect_success 'clone myhost:src uses ssh' '
354         GIT_TEST_PROTOCOL_VERSION=0 git clone myhost:src ssh-clone &&
355         expect_ssh myhost src
356 '
357
358 test_expect_success !MINGW,!CYGWIN 'clone local path foo:bar' '
359         cp -R src "foo:bar" &&
360         git clone "foo:bar" foobar &&
361         expect_ssh none
362 '
363
364 test_expect_success 'bracketed hostnames are still ssh' '
365         GIT_TEST_PROTOCOL_VERSION=0 git clone "[myhost:123]:src" ssh-bracket-clone &&
366         expect_ssh "-p 123" myhost src
367 '
368
369 test_expect_success 'OpenSSH variant passes -4' '
370         GIT_TEST_PROTOCOL_VERSION=0 git clone -4 "[myhost:123]:src" ssh-ipv4-clone &&
371         expect_ssh "-4 -p 123" myhost src
372 '
373
374 test_expect_success 'variant can be overridden' '
375         copy_ssh_wrapper_as "$TRASH_DIRECTORY/putty" &&
376         git -c ssh.variant=putty clone -4 "[myhost:123]:src" ssh-putty-clone &&
377         expect_ssh "-4 -P 123" myhost src
378 '
379
380 test_expect_success 'variant=auto picks based on basename' '
381         copy_ssh_wrapper_as "$TRASH_DIRECTORY/plink" &&
382         git -c ssh.variant=auto clone -4 "[myhost:123]:src" ssh-auto-clone &&
383         expect_ssh "-4 -P 123" myhost src
384 '
385
386 test_expect_success 'simple does not support -4/-6' '
387         copy_ssh_wrapper_as "$TRASH_DIRECTORY/simple" &&
388         test_must_fail git clone -4 "myhost:src" ssh-4-clone-simple
389 '
390
391 test_expect_success 'simple does not support port' '
392         copy_ssh_wrapper_as "$TRASH_DIRECTORY/simple" &&
393         test_must_fail git clone "[myhost:123]:src" ssh-bracket-clone-simple
394 '
395
396 test_expect_success 'uplink is treated as simple' '
397         copy_ssh_wrapper_as "$TRASH_DIRECTORY/uplink" &&
398         test_must_fail git clone "[myhost:123]:src" ssh-bracket-clone-uplink &&
399         git clone "myhost:src" ssh-clone-uplink &&
400         expect_ssh myhost src
401 '
402
403 test_expect_success 'OpenSSH-like uplink is treated as ssh' '
404         write_script "$TRASH_DIRECTORY/uplink" <<-EOF &&
405         if test "\$1" = "-G"
406         then
407                 exit 0
408         fi &&
409         exec "\$TRASH_DIRECTORY/ssh$X" "\$@"
410         EOF
411         test_when_finished "rm -f \"\$TRASH_DIRECTORY/uplink\"" &&
412         GIT_SSH="$TRASH_DIRECTORY/uplink" &&
413         test_when_finished "GIT_SSH=\"\$TRASH_DIRECTORY/ssh\$X\"" &&
414         GIT_TEST_PROTOCOL_VERSION=0 git clone "[myhost:123]:src" ssh-bracket-clone-sshlike-uplink &&
415         expect_ssh "-p 123" myhost src
416 '
417
418 test_expect_success 'plink is treated specially (as putty)' '
419         copy_ssh_wrapper_as "$TRASH_DIRECTORY/plink" &&
420         git clone "[myhost:123]:src" ssh-bracket-clone-plink-0 &&
421         expect_ssh "-P 123" myhost src
422 '
423
424 test_expect_success 'plink.exe is treated specially (as putty)' '
425         copy_ssh_wrapper_as "$TRASH_DIRECTORY/plink.exe" &&
426         git clone "[myhost:123]:src" ssh-bracket-clone-plink-1 &&
427         expect_ssh "-P 123" myhost src
428 '
429
430 test_expect_success 'tortoiseplink is like putty, with extra arguments' '
431         copy_ssh_wrapper_as "$TRASH_DIRECTORY/tortoiseplink" &&
432         git clone "[myhost:123]:src" ssh-bracket-clone-plink-2 &&
433         expect_ssh "-batch -P 123" myhost src
434 '
435
436 test_expect_success 'double quoted plink.exe in GIT_SSH_COMMAND' '
437         copy_ssh_wrapper_as "$TRASH_DIRECTORY/plink.exe" &&
438         GIT_SSH_COMMAND="\"$TRASH_DIRECTORY/plink.exe\" -v" \
439                 git clone "[myhost:123]:src" ssh-bracket-clone-plink-3 &&
440         expect_ssh "-v -P 123" myhost src
441 '
442
443 test_expect_success 'single quoted plink.exe in GIT_SSH_COMMAND' '
444         copy_ssh_wrapper_as "$TRASH_DIRECTORY/plink.exe" &&
445         GIT_SSH_COMMAND="$SQ$TRASH_DIRECTORY/plink.exe$SQ -v" \
446                 git clone "[myhost:123]:src" ssh-bracket-clone-plink-4 &&
447         expect_ssh "-v -P 123" myhost src
448 '
449
450 test_expect_success 'GIT_SSH_VARIANT overrides plink detection' '
451         copy_ssh_wrapper_as "$TRASH_DIRECTORY/plink" &&
452         GIT_TEST_PROTOCOL_VERSION=0 GIT_SSH_VARIANT=ssh \
453                 git clone "[myhost:123]:src" ssh-bracket-clone-variant-1 &&
454         expect_ssh "-p 123" myhost src
455 '
456
457 test_expect_success 'ssh.variant overrides plink detection' '
458         copy_ssh_wrapper_as "$TRASH_DIRECTORY/plink" &&
459         GIT_TEST_PROTOCOL_VERSION=0 git -c ssh.variant=ssh \
460                 clone "[myhost:123]:src" ssh-bracket-clone-variant-2 &&
461         expect_ssh "-p 123" myhost src
462 '
463
464 test_expect_success 'GIT_SSH_VARIANT overrides plink detection to plink' '
465         copy_ssh_wrapper_as "$TRASH_DIRECTORY/plink" &&
466         GIT_SSH_VARIANT=plink \
467         git clone "[myhost:123]:src" ssh-bracket-clone-variant-3 &&
468         expect_ssh "-P 123" myhost src
469 '
470
471 test_expect_success 'GIT_SSH_VARIANT overrides plink to tortoiseplink' '
472         copy_ssh_wrapper_as "$TRASH_DIRECTORY/plink" &&
473         GIT_SSH_VARIANT=tortoiseplink \
474         git clone "[myhost:123]:src" ssh-bracket-clone-variant-4 &&
475         expect_ssh "-batch -P 123" myhost src
476 '
477
478 test_expect_success 'clean failure on broken quoting' '
479         test_must_fail \
480                 env GIT_SSH_COMMAND="${SQ}plink.exe -v" \
481                 git clone "[myhost:123]:src" sq-failure
482 '
483
484 counter=0
485 # $1 url
486 # $2 none|host
487 # $3 path
488 test_clone_url () {
489         counter=$(($counter + 1))
490         test_might_fail env GIT_TEST_PROTOCOL_VERSION=0 git clone "$1" tmp$counter &&
491         shift &&
492         expect_ssh "$@"
493 }
494
495 test_expect_success !MINGW,!CYGWIN 'clone c:temp is ssl' '
496         test_clone_url c:temp c temp
497 '
498
499 test_expect_success MINGW 'clone c:temp is dos drive' '
500         test_clone_url c:temp none
501 '
502
503 #ip v4
504 for repo in rep rep/home/project 123
505 do
506         test_expect_success "clone host:$repo" '
507                 test_clone_url host:$repo host $repo
508         '
509 done
510
511 #ipv6
512 for repo in rep rep/home/project 123
513 do
514         test_expect_success "clone [::1]:$repo" '
515                 test_clone_url [::1]:$repo ::1 "$repo"
516         '
517 done
518 #home directory
519 test_expect_success "clone host:/~repo" '
520         test_clone_url host:/~repo host "~repo"
521 '
522
523 test_expect_success "clone [::1]:/~repo" '
524         test_clone_url [::1]:/~repo ::1 "~repo"
525 '
526
527 # Corner cases
528 for url in foo/bar:baz [foo]bar/baz:qux [foo/bar]:baz
529 do
530         test_expect_success "clone $url is not ssh" '
531                 test_clone_url $url none
532         '
533 done
534
535 #with ssh:// scheme
536 #ignore trailing colon
537 for tcol in "" :
538 do
539         test_expect_success "clone ssh://host.xz$tcol/home/user/repo" '
540                 test_clone_url "ssh://host.xz$tcol/home/user/repo" host.xz /home/user/repo
541         '
542         # from home directory
543         test_expect_success "clone ssh://host.xz$tcol/~repo" '
544         test_clone_url "ssh://host.xz$tcol/~repo" host.xz "~repo"
545 '
546 done
547
548 # with port number
549 test_expect_success 'clone ssh://host.xz:22/home/user/repo' '
550         test_clone_url "ssh://host.xz:22/home/user/repo" "-p 22 host.xz" "/home/user/repo"
551 '
552
553 # from home directory with port number
554 test_expect_success 'clone ssh://host.xz:22/~repo' '
555         test_clone_url "ssh://host.xz:22/~repo" "-p 22 host.xz" "~repo"
556 '
557
558 #IPv6
559 for tuah in ::1 [::1] [::1]: user@::1 user@[::1] user@[::1]: [user@::1] [user@::1]:
560 do
561         ehost=$(echo $tuah | sed -e "s/1]:/1]/" | tr -d "[]")
562         test_expect_success "clone ssh://$tuah/home/user/repo" "
563           test_clone_url ssh://$tuah/home/user/repo $ehost /home/user/repo
564         "
565 done
566
567 #IPv6 from home directory
568 for tuah in ::1 [::1] user@::1 user@[::1] [user@::1]
569 do
570         euah=$(echo $tuah | tr -d "[]")
571         test_expect_success "clone ssh://$tuah/~repo" "
572           test_clone_url ssh://$tuah/~repo $euah '~repo'
573         "
574 done
575
576 #IPv6 with port number
577 for tuah in [::1] user@[::1] [user@::1]
578 do
579         euah=$(echo $tuah | tr -d "[]")
580         test_expect_success "clone ssh://$tuah:22/home/user/repo" "
581           test_clone_url ssh://$tuah:22/home/user/repo '-p 22' $euah /home/user/repo
582         "
583 done
584
585 #IPv6 from home directory with port number
586 for tuah in [::1] user@[::1] [user@::1]
587 do
588         euah=$(echo $tuah | tr -d "[]")
589         test_expect_success "clone ssh://$tuah:22/~repo" "
590           test_clone_url ssh://$tuah:22/~repo '-p 22' $euah '~repo'
591         "
592 done
593
594 test_expect_success 'clone from a repository with two identical branches' '
595
596         (
597                 cd src &&
598                 git checkout -b another main
599         ) &&
600         git clone src target-11 &&
601         test "z$( cd target-11 && git symbolic-ref HEAD )" = zrefs/heads/another
602
603 '
604
605 test_expect_success 'shallow clone locally' '
606         git clone --depth=1 --no-local src ssrrcc &&
607         git clone ssrrcc ddsstt &&
608         test_cmp ssrrcc/.git/shallow ddsstt/.git/shallow &&
609         ( cd ddsstt && git fsck )
610 '
611
612 test_expect_success 'GIT_TRACE_PACKFILE produces a usable pack' '
613         rm -rf dst.git &&
614         GIT_TRACE_PACKFILE=$PWD/tmp.pack git clone --no-local --bare src dst.git &&
615         git init --bare replay.git &&
616         git -C replay.git index-pack -v --stdin <tmp.pack
617 '
618
619 test_expect_success 'clone on case-insensitive fs' '
620         git init icasefs &&
621         (
622                 cd icasefs &&
623                 o=$(git hash-object -w --stdin </dev/null | hex2oct) &&
624                 t=$(printf "100644 X\0${o}100644 x\0${o}" |
625                         git hash-object -w -t tree --stdin) &&
626                 c=$(git commit-tree -m bogus $t) &&
627                 git update-ref refs/heads/bogus $c &&
628                 git clone -b bogus . bogus 2>warning
629         )
630 '
631
632 test_expect_success CASE_INSENSITIVE_FS 'colliding file detection' '
633         grep X icasefs/warning &&
634         grep x icasefs/warning &&
635         test_i18ngrep "the following paths have collided" icasefs/warning
636 '
637
638 test_expect_success 'clone with GIT_DEFAULT_HASH' '
639         (
640                 sane_unset GIT_DEFAULT_HASH &&
641                 git init --object-format=sha1 test-sha1 &&
642                 git init --object-format=sha256 test-sha256
643         ) &&
644         test_commit -C test-sha1 foo &&
645         test_commit -C test-sha256 foo &&
646         GIT_DEFAULT_HASH=sha1 git clone test-sha256 test-clone-sha256 &&
647         GIT_DEFAULT_HASH=sha256 git clone test-sha1 test-clone-sha1 &&
648         git -C test-clone-sha1 status &&
649         git -C test-clone-sha256 status
650 '
651
652 partial_clone_server () {
653                SERVER="$1" &&
654
655         rm -rf "$SERVER" client &&
656         test_create_repo "$SERVER" &&
657         test_commit -C "$SERVER" one &&
658         HASH1=$(git -C "$SERVER" hash-object one.t) &&
659         git -C "$SERVER" revert HEAD &&
660         test_commit -C "$SERVER" two &&
661         HASH2=$(git -C "$SERVER" hash-object two.t) &&
662         test_config -C "$SERVER" uploadpack.allowfilter 1 &&
663         test_config -C "$SERVER" uploadpack.allowanysha1inwant 1
664 }
665
666 partial_clone () {
667                SERVER="$1" &&
668                URL="$2" &&
669
670         partial_clone_server "${SERVER}" &&
671         git clone --filter=blob:limit=0 "$URL" client &&
672
673         git -C client fsck &&
674
675         # Ensure that unneeded blobs are not inadvertently fetched.
676         test_config -C client remote.origin.promisor "false" &&
677         git -C client config --unset remote.origin.partialclonefilter &&
678         test_must_fail git -C client cat-file -e "$HASH1" &&
679
680         # But this blob was fetched, because clone performs an initial checkout
681         git -C client cat-file -e "$HASH2"
682 }
683
684 test_expect_success 'partial clone' '
685         partial_clone server "file://$(pwd)/server"
686 '
687
688 test_expect_success 'partial clone with -o' '
689         partial_clone_server server &&
690         git clone -o blah --filter=blob:limit=0 "file://$(pwd)/server" client &&
691         test_cmp_config -C client "blob:limit=0" --get-all remote.blah.partialclonefilter
692 '
693
694 test_expect_success 'partial clone: warn if server does not support object filtering' '
695         rm -rf server client &&
696         test_create_repo server &&
697         test_commit -C server one &&
698
699         git clone --filter=blob:limit=0 "file://$(pwd)/server" client 2> err &&
700
701         test_i18ngrep "filtering not recognized by server" err
702 '
703
704 test_expect_success 'batch missing blob request during checkout' '
705         rm -rf server client &&
706
707         test_create_repo server &&
708         echo a >server/a &&
709         echo b >server/b &&
710         git -C server add a b &&
711
712         git -C server commit -m x &&
713         echo aa >server/a &&
714         echo bb >server/b &&
715         git -C server add a b &&
716         git -C server commit -m x &&
717
718         test_config -C server uploadpack.allowfilter 1 &&
719         test_config -C server uploadpack.allowanysha1inwant 1 &&
720
721         git clone --filter=blob:limit=0 "file://$(pwd)/server" client &&
722
723         # Ensure that there is only one negotiation by checking that there is
724         # only "done" line sent. ("done" marks the end of negotiation.)
725         GIT_TRACE_PACKET="$(pwd)/trace" git -C client checkout HEAD^ &&
726         grep "fetch> done" trace >done_lines &&
727         test_line_count = 1 done_lines
728 '
729
730 test_expect_success 'batch missing blob request does not inadvertently try to fetch gitlinks' '
731         rm -rf server client &&
732
733         test_create_repo repo_for_submodule &&
734         test_commit -C repo_for_submodule x &&
735
736         test_create_repo server &&
737         echo a >server/a &&
738         echo b >server/b &&
739         git -C server add a b &&
740         git -C server commit -m x &&
741
742         echo aa >server/a &&
743         echo bb >server/b &&
744         # Also add a gitlink pointing to an arbitrary repository
745         git -C server submodule add "$(pwd)/repo_for_submodule" c &&
746         git -C server add a b c &&
747         git -C server commit -m x &&
748
749         test_config -C server uploadpack.allowfilter 1 &&
750         test_config -C server uploadpack.allowanysha1inwant 1 &&
751
752         # Make sure that it succeeds
753         git clone --filter=blob:limit=0 "file://$(pwd)/server" client
754 '
755
756 . "$TEST_DIRECTORY"/lib-httpd.sh
757 start_httpd
758
759 test_expect_success 'partial clone using HTTP' '
760         partial_clone "$HTTPD_DOCUMENT_ROOT_PATH/server" "$HTTPD_URL/smart/server"
761 '
762
763 test_expect_success 'reject cloning shallow repository using HTTP' '
764         test_when_finished "rm -rf repo" &&
765         git clone --bare --no-local --depth=1 src "$HTTPD_DOCUMENT_ROOT_PATH/repo.git" &&
766         test_must_fail git -c protocol.version=2 clone --reject-shallow $HTTPD_URL/smart/repo.git repo 2>err &&
767         test_i18ngrep -e "source repository is shallow, reject to clone." err &&
768
769         git clone --no-reject-shallow $HTTPD_URL/smart/repo.git repo
770 '
771
772 # DO NOT add non-httpd-specific tests here, because the last part of this
773 # test script is only executed when httpd is available and enabled.
774
775 test_done