scalar: fix command documentation section header
[git/gitster.git] / t / t5551-http-fetch-smart.sh
blob6a38294a47671dccdc81849e1385a8c6e0310488
1 #!/bin/sh
3 test_description='test smart fetching over http via http-backend'
4 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
5 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
7 . ./test-lib.sh
8 . "$TEST_DIRECTORY"/lib-httpd.sh
9 start_httpd
11 test_expect_success 'setup repository' '
12 git config push.default matching &&
13 echo content >file &&
14 git add file &&
15 git commit -m one
18 test_expect_success 'create http-accessible bare repository' '
19 mkdir "$HTTPD_DOCUMENT_ROOT_PATH/repo.git" &&
20 (cd "$HTTPD_DOCUMENT_ROOT_PATH/repo.git" &&
21 git --bare init
22 ) &&
23 git remote add public "$HTTPD_DOCUMENT_ROOT_PATH/repo.git" &&
24 git push public main:main
27 setup_askpass_helper
29 test_expect_success 'clone http repository' '
30 cat >exp <<-\EOF &&
31 > GET /smart/repo.git/info/refs?service=git-upload-pack HTTP/1.1
32 > Accept: */*
33 > Accept-Encoding: ENCODINGS
34 > Accept-Language: ko-KR, *;q=0.9
35 > Pragma: no-cache
36 < HTTP/1.1 200 OK
37 < Pragma: no-cache
38 < Cache-Control: no-cache, max-age=0, must-revalidate
39 < Content-Type: application/x-git-upload-pack-advertisement
40 > POST /smart/repo.git/git-upload-pack HTTP/1.1
41 > Accept-Encoding: ENCODINGS
42 > Content-Type: application/x-git-upload-pack-request
43 > Accept: application/x-git-upload-pack-result
44 > Accept-Language: ko-KR, *;q=0.9
45 > Content-Length: xxx
46 < HTTP/1.1 200 OK
47 < Pragma: no-cache
48 < Cache-Control: no-cache, max-age=0, must-revalidate
49 < Content-Type: application/x-git-upload-pack-result
50 EOF
52 GIT_TRACE_CURL=true GIT_TEST_PROTOCOL_VERSION=0 LANGUAGE="ko_KR.UTF-8" \
53 git clone --quiet $HTTPD_URL/smart/repo.git clone 2>err &&
54 test_cmp file clone/file &&
55 tr '\''\015'\'' Q <err |
56 sed -e "
57 s/Q\$//
58 /^[*] /d
59 /^== Info:/d
60 /^=> Send header, /d
61 /^=> Send header:$/d
62 /^<= Recv header, /d
63 /^<= Recv header:$/d
64 s/=> Send header: //
65 s/= Recv header://
66 /^<= Recv data/d
67 /^=> Send data/d
68 /^$/d
69 /^< $/d
71 /^[^><]/{
72 s/^/> /
75 /^> User-Agent: /d
76 /^> Host: /d
77 /^> POST /,$ {
78 /^> Accept: [*]\\/[*]/d
80 s/^> Content-Length: .*/> Content-Length: xxx/
81 /^> 00..want /d
82 /^> 00.*done/d
84 /^< Server: /d
85 /^< Expires: /d
86 /^< Date: /d
87 /^< Content-Length: /d
88 /^< Transfer-Encoding: /d
89 " >actual &&
91 # NEEDSWORK: If the overspecification of the expected result is reduced, we
92 # might be able to run this test in all protocol versions.
93 if test "$GIT_TEST_PROTOCOL_VERSION" = 0
94 then
95 sed -e "s/^> Accept-Encoding: .*/> Accept-Encoding: ENCODINGS/" \
96 actual >actual.smudged &&
97 test_cmp exp actual.smudged &&
99 grep "Accept-Encoding:.*gzip" actual >actual.gzip &&
100 test_line_count = 2 actual.gzip &&
102 grep "Accept-Language: ko-KR, *" actual >actual.language &&
103 test_line_count = 2 actual.language
107 test_expect_success 'fetch changes via http' '
108 echo content >>file &&
109 git commit -a -m two &&
110 git push public &&
111 (cd clone && git pull) &&
112 test_cmp file clone/file
115 test_expect_success 'used upload-pack service' '
116 cat >exp <<-\EOF &&
117 GET /smart/repo.git/info/refs?service=git-upload-pack HTTP/1.1 200
118 POST /smart/repo.git/git-upload-pack HTTP/1.1 200
119 GET /smart/repo.git/info/refs?service=git-upload-pack HTTP/1.1 200
120 POST /smart/repo.git/git-upload-pack HTTP/1.1 200
123 # NEEDSWORK: If the overspecification of the expected result is reduced, we
124 # might be able to run this test in all protocol versions.
125 if test "$GIT_TEST_PROTOCOL_VERSION" = 0
126 then
127 check_access_log exp
131 test_expect_success 'follow redirects (301)' '
132 git clone $HTTPD_URL/smart-redir-perm/repo.git --quiet repo-p
135 test_expect_success 'follow redirects (302)' '
136 git clone $HTTPD_URL/smart-redir-temp/repo.git --quiet repo-t
139 test_expect_success 'redirects re-root further requests' '
140 git clone $HTTPD_URL/smart-redir-limited/repo.git repo-redir-limited
143 test_expect_success 're-rooting dies on insane schemes' '
144 test_must_fail git clone $HTTPD_URL/insane-redir/repo.git insane
147 test_expect_success 'clone from password-protected repository' '
148 echo two >expect &&
149 set_askpass user@host pass@host &&
150 git clone --bare "$HTTPD_URL/auth/smart/repo.git" smart-auth &&
151 expect_askpass both user@host &&
152 git --git-dir=smart-auth log -1 --format=%s >actual &&
153 test_cmp expect actual
156 test_expect_success 'clone from auth-only-for-push repository' '
157 echo two >expect &&
158 set_askpass wrong &&
159 git clone --bare "$HTTPD_URL/auth-push/smart/repo.git" smart-noauth &&
160 expect_askpass none &&
161 git --git-dir=smart-noauth log -1 --format=%s >actual &&
162 test_cmp expect actual
165 test_expect_success 'clone from auth-only-for-objects repository' '
166 echo two >expect &&
167 set_askpass user@host pass@host &&
168 git clone --bare "$HTTPD_URL/auth-fetch/smart/repo.git" half-auth &&
169 expect_askpass both user@host &&
170 git --git-dir=half-auth log -1 --format=%s >actual &&
171 test_cmp expect actual
174 test_expect_success 'no-op half-auth fetch does not require a password' '
175 set_askpass wrong &&
177 # NEEDSWORK: When using HTTP(S), protocol v0 supports a "half-auth"
178 # configuration with authentication required only when downloading
179 # objects and not refs, by having the HTTP server only require
180 # authentication for the "git-upload-pack" path and not "info/refs".
181 # This is not possible with protocol v2, since both objects and refs
182 # are obtained from the "git-upload-pack" path. A solution to this is
183 # to teach the server and client to be able to inline ls-refs requests
184 # as an Extra Parameter (see "git help gitformat-pack-protocol"), so that
185 # "info/refs" can serve refs, just like it does in protocol v0.
186 GIT_TEST_PROTOCOL_VERSION=0 git --git-dir=half-auth fetch &&
187 expect_askpass none
190 test_expect_success 'redirects send auth to new location' '
191 set_askpass user@host pass@host &&
192 git -c credential.useHttpPath=true \
193 clone $HTTPD_URL/smart-redir-auth/repo.git repo-redir-auth &&
194 expect_askpass both user@host auth/smart/repo.git
197 test_expect_success 'GIT_TRACE_CURL redacts auth details' '
198 rm -rf redact-auth trace &&
199 set_askpass user@host pass@host &&
200 GIT_TRACE_CURL="$(pwd)/trace" git clone --bare "$HTTPD_URL/auth/smart/repo.git" redact-auth &&
201 expect_askpass both user@host &&
203 # Ensure that there is no "Basic" followed by a base64 string, but that
204 # the auth details are redacted
205 ! grep -i "Authorization: Basic [0-9a-zA-Z+/]" trace &&
206 grep -i "Authorization: Basic <redacted>" trace
209 test_expect_success 'GIT_CURL_VERBOSE redacts auth details' '
210 rm -rf redact-auth trace &&
211 set_askpass user@host pass@host &&
212 GIT_CURL_VERBOSE=1 git clone --bare "$HTTPD_URL/auth/smart/repo.git" redact-auth 2>trace &&
213 expect_askpass both user@host &&
215 # Ensure that there is no "Basic" followed by a base64 string, but that
216 # the auth details are redacted
217 ! grep -i "Authorization: Basic [0-9a-zA-Z+/]" trace &&
218 grep -i "Authorization: Basic <redacted>" trace
221 test_expect_success 'GIT_TRACE_CURL does not redact auth details if GIT_TRACE_REDACT=0' '
222 rm -rf redact-auth trace &&
223 set_askpass user@host pass@host &&
224 GIT_TRACE_REDACT=0 GIT_TRACE_CURL="$(pwd)/trace" \
225 git clone --bare "$HTTPD_URL/auth/smart/repo.git" redact-auth &&
226 expect_askpass both user@host &&
228 grep -i "Authorization: Basic [0-9a-zA-Z+/]" trace
231 test_expect_success 'disable dumb http on server' '
232 git --git-dir="$HTTPD_DOCUMENT_ROOT_PATH/repo.git" \
233 config http.getanyfile false
236 test_expect_success 'GIT_SMART_HTTP can disable smart http' '
237 (GIT_SMART_HTTP=0 &&
238 export GIT_SMART_HTTP &&
239 cd clone &&
240 test_must_fail git fetch)
243 test_expect_success 'invalid Content-Type rejected' '
244 test_must_fail git clone $HTTPD_URL/broken_smart/repo.git 2>actual &&
245 test_i18ngrep "not valid:" actual
248 test_expect_success 'create namespaced refs' '
249 test_commit namespaced &&
250 git push public HEAD:refs/namespaces/ns/refs/heads/main &&
251 git --git-dir="$HTTPD_DOCUMENT_ROOT_PATH/repo.git" \
252 symbolic-ref refs/namespaces/ns/HEAD refs/namespaces/ns/refs/heads/main
255 test_expect_success 'smart clone respects namespace' '
256 git clone "$HTTPD_URL/smart_namespace/repo.git" ns-smart &&
257 echo namespaced >expect &&
258 git --git-dir=ns-smart/.git log -1 --format=%s >actual &&
259 test_cmp expect actual
262 test_expect_success 'dumb clone via http-backend respects namespace' '
263 git --git-dir="$HTTPD_DOCUMENT_ROOT_PATH/repo.git" \
264 config http.getanyfile true &&
265 GIT_SMART_HTTP=0 git clone \
266 "$HTTPD_URL/smart_namespace/repo.git" ns-dumb &&
267 echo namespaced >expect &&
268 git --git-dir=ns-dumb/.git log -1 --format=%s >actual &&
269 test_cmp expect actual
272 test_expect_success 'cookies stored in http.cookiefile when http.savecookies set' '
273 cat >cookies.txt <<-\EOF &&
274 127.0.0.1 FALSE /smart_cookies/ FALSE 0 othername othervalue
276 sort >expect_cookies.txt <<-\EOF &&
278 127.0.0.1 FALSE /smart_cookies/ FALSE 0 othername othervalue
279 127.0.0.1 FALSE /smart_cookies/repo.git/info/ FALSE 0 name value
281 git config http.cookiefile cookies.txt &&
282 git config http.savecookies true &&
283 git ls-remote $HTTPD_URL/smart_cookies/repo.git main &&
285 # NEEDSWORK: If the overspecification of the expected result is reduced, we
286 # might be able to run this test in all protocol versions.
287 if test "$GIT_TEST_PROTOCOL_VERSION" = 0
288 then
289 tail -3 cookies.txt | sort >cookies_tail.txt &&
290 test_cmp expect_cookies.txt cookies_tail.txt
294 test_expect_success 'transfer.hiderefs works over smart-http' '
295 test_commit hidden &&
296 test_commit visible &&
297 git push public HEAD^:refs/heads/a HEAD:refs/heads/b &&
298 git --git-dir="$HTTPD_DOCUMENT_ROOT_PATH/repo.git" \
299 config transfer.hiderefs refs/heads/a &&
300 git clone --bare "$HTTPD_URL/smart/repo.git" hidden.git &&
301 test_must_fail git -C hidden.git rev-parse --verify a &&
302 git -C hidden.git rev-parse --verify b
305 # create an arbitrary number of tags, numbered from tag-$1 to tag-$2
306 create_tags () {
307 rm -f marks &&
308 for i in $(test_seq "$1" "$2")
310 # don't use here-doc, because it requires a process
311 # per loop iteration
312 echo "commit refs/heads/too-many-refs-$1" &&
313 echo "mark :$i" &&
314 echo "committer git <git@example.com> $i +0000" &&
315 echo "data 0" &&
316 echo "M 644 inline bla.txt" &&
317 echo "data 4" &&
318 echo "bla" &&
319 # make every commit dangling by always
320 # rewinding the branch after each commit
321 echo "reset refs/heads/too-many-refs-$1" &&
322 echo "from :$1"
323 done | git fast-import --export-marks=marks &&
325 # now assign tags to all the dangling commits we created above
326 tag=$(perl -e "print \"bla\" x 30") &&
327 sed -e "s|^:\([^ ]*\) \(.*\)$|\2 refs/tags/$tag-\1|" <marks >>packed-refs
330 test_expect_success 'create 2,000 tags in the repo' '
332 cd "$HTTPD_DOCUMENT_ROOT_PATH/repo.git" &&
333 create_tags 1 2000
337 test_expect_success CMDLINE_LIMIT \
338 'clone the 2,000 tag repo to check OS command line overflow' '
339 run_with_limited_cmdline git clone $HTTPD_URL/smart/repo.git too-many-refs &&
341 cd too-many-refs &&
342 git for-each-ref refs/tags >actual &&
343 test_line_count = 2000 actual
347 test_expect_success 'large fetch-pack requests can be sent using chunked encoding' '
348 GIT_TRACE_CURL=true git -c http.postbuffer=65536 \
349 clone --bare "$HTTPD_URL/smart/repo.git" split.git 2>err &&
350 grep "^=> Send header: Transfer-Encoding: chunked" err
353 test_expect_success 'test allowreachablesha1inwant' '
354 test_when_finished "rm -rf test_reachable.git" &&
355 server="$HTTPD_DOCUMENT_ROOT_PATH/repo.git" &&
356 main_sha=$(git -C "$server" rev-parse refs/heads/main) &&
357 git -C "$server" config uploadpack.allowreachablesha1inwant 1 &&
359 git init --bare test_reachable.git &&
360 git -C test_reachable.git remote add origin "$HTTPD_URL/smart/repo.git" &&
361 git -C test_reachable.git fetch origin "$main_sha"
364 test_expect_success 'test allowreachablesha1inwant with unreachable' '
365 test_when_finished "rm -rf test_reachable.git; git reset --hard $(git rev-parse HEAD)" &&
367 #create unreachable sha
368 echo content >file2 &&
369 git add file2 &&
370 git commit -m two &&
371 git push public HEAD:refs/heads/doomed &&
372 git push public :refs/heads/doomed &&
374 server="$HTTPD_DOCUMENT_ROOT_PATH/repo.git" &&
375 main_sha=$(git -C "$server" rev-parse refs/heads/main) &&
376 git -C "$server" config uploadpack.allowreachablesha1inwant 1 &&
378 git init --bare test_reachable.git &&
379 git -C test_reachable.git remote add origin "$HTTPD_URL/smart/repo.git" &&
380 # Some protocol versions (e.g. 2) support fetching
381 # unadvertised objects, so restrict this test to v0.
382 test_must_fail env GIT_TEST_PROTOCOL_VERSION=0 \
383 git -C test_reachable.git fetch origin "$(git rev-parse HEAD)"
386 test_expect_success 'test allowanysha1inwant with unreachable' '
387 test_when_finished "rm -rf test_reachable.git; git reset --hard $(git rev-parse HEAD)" &&
389 #create unreachable sha
390 echo content >file2 &&
391 git add file2 &&
392 git commit -m two &&
393 git push public HEAD:refs/heads/doomed &&
394 git push public :refs/heads/doomed &&
396 server="$HTTPD_DOCUMENT_ROOT_PATH/repo.git" &&
397 main_sha=$(git -C "$server" rev-parse refs/heads/main) &&
398 git -C "$server" config uploadpack.allowreachablesha1inwant 1 &&
400 git init --bare test_reachable.git &&
401 git -C test_reachable.git remote add origin "$HTTPD_URL/smart/repo.git" &&
402 # Some protocol versions (e.g. 2) support fetching
403 # unadvertised objects, so restrict this test to v0.
404 test_must_fail env GIT_TEST_PROTOCOL_VERSION=0 \
405 git -C test_reachable.git fetch origin "$(git rev-parse HEAD)" &&
407 git -C "$server" config uploadpack.allowanysha1inwant 1 &&
408 git -C test_reachable.git fetch origin "$(git rev-parse HEAD)"
411 test_expect_success EXPENSIVE 'http can handle enormous ref negotiation' '
413 cd "$HTTPD_DOCUMENT_ROOT_PATH/repo.git" &&
414 create_tags 2001 50000
415 ) &&
416 git -C too-many-refs fetch -q --tags &&
418 cd "$HTTPD_DOCUMENT_ROOT_PATH/repo.git" &&
419 create_tags 50001 100000
420 ) &&
421 git -C too-many-refs fetch -q --tags &&
422 git -C too-many-refs for-each-ref refs/tags >tags &&
423 test_line_count = 100000 tags
426 test_expect_success 'custom http headers' '
427 test_must_fail git -c http.extraheader="x-magic-two: cadabra" \
428 fetch "$HTTPD_URL/smart_headers/repo.git" &&
429 git -c http.extraheader="x-magic-one: abra" \
430 -c http.extraheader="x-magic-two: cadabra" \
431 fetch "$HTTPD_URL/smart_headers/repo.git" &&
432 git update-index --add --cacheinfo 160000,$(git rev-parse HEAD),sub &&
433 git config -f .gitmodules submodule.sub.path sub &&
434 git config -f .gitmodules submodule.sub.url \
435 "$HTTPD_URL/smart_headers/repo.git" &&
436 git submodule init sub &&
437 test_must_fail git submodule update sub &&
438 git -c http.extraheader="x-magic-one: abra" \
439 -c http.extraheader="x-magic-two: cadabra" \
440 submodule update sub
443 test_expect_success 'using fetch command in remote-curl updates refs' '
444 SERVER="$HTTPD_DOCUMENT_ROOT_PATH/twobranch" &&
445 rm -rf "$SERVER" client &&
447 git init "$SERVER" &&
448 test_commit -C "$SERVER" foo &&
449 git -C "$SERVER" update-ref refs/heads/anotherbranch foo &&
451 git clone $HTTPD_URL/smart/twobranch client &&
453 test_commit -C "$SERVER" bar &&
454 git -C client -c protocol.version=0 fetch &&
456 git -C "$SERVER" rev-parse main >expect &&
457 git -C client rev-parse origin/main >actual &&
458 test_cmp expect actual
461 test_expect_success 'fetch by SHA-1 without tag following' '
462 SERVER="$HTTPD_DOCUMENT_ROOT_PATH/server" &&
463 rm -rf "$SERVER" client &&
465 git init "$SERVER" &&
466 test_commit -C "$SERVER" foo &&
468 git clone $HTTPD_URL/smart/server client &&
470 test_commit -C "$SERVER" bar &&
471 git -C "$SERVER" rev-parse bar >bar_hash &&
472 git -C client -c protocol.version=0 fetch \
473 --no-tags origin $(cat bar_hash)
476 test_expect_success 'cookies are redacted by default' '
477 rm -rf clone &&
478 echo "Set-Cookie: Foo=1" >cookies &&
479 echo "Set-Cookie: Bar=2" >>cookies &&
480 GIT_TRACE_CURL=true \
481 git -c "http.cookieFile=$(pwd)/cookies" clone \
482 $HTTPD_URL/smart/repo.git clone 2>err &&
483 grep -i "Cookie:.*Foo=<redacted>" err &&
484 grep -i "Cookie:.*Bar=<redacted>" err &&
485 ! grep -i "Cookie:.*Foo=1" err &&
486 ! grep -i "Cookie:.*Bar=2" err
489 test_expect_success 'empty values of cookies are also redacted' '
490 rm -rf clone &&
491 echo "Set-Cookie: Foo=" >cookies &&
492 GIT_TRACE_CURL=true \
493 git -c "http.cookieFile=$(pwd)/cookies" clone \
494 $HTTPD_URL/smart/repo.git clone 2>err &&
495 grep -i "Cookie:.*Foo=<redacted>" err
498 test_expect_success 'GIT_TRACE_REDACT=0 disables cookie redaction' '
499 rm -rf clone &&
500 echo "Set-Cookie: Foo=1" >cookies &&
501 echo "Set-Cookie: Bar=2" >>cookies &&
502 GIT_TRACE_REDACT=0 GIT_TRACE_CURL=true \
503 git -c "http.cookieFile=$(pwd)/cookies" clone \
504 $HTTPD_URL/smart/repo.git clone 2>err &&
505 grep -i "Cookie:.*Foo=1" err &&
506 grep -i "Cookie:.*Bar=2" err
509 test_expect_success 'GIT_TRACE_CURL_NO_DATA prevents data from being traced' '
510 rm -rf clone &&
511 GIT_TRACE_CURL=true \
512 git clone $HTTPD_URL/smart/repo.git clone 2>err &&
513 grep "=> Send data" err &&
515 rm -rf clone &&
516 GIT_TRACE_CURL=true GIT_TRACE_CURL_NO_DATA=1 \
517 git clone $HTTPD_URL/smart/repo.git clone 2>err &&
518 ! grep "=> Send data" err
521 test_expect_success 'server-side error detected' '
522 test_must_fail git clone $HTTPD_URL/error_smart/repo.git 2>actual &&
523 test_i18ngrep "server-side error" actual
526 test_expect_success 'http auth remembers successful credentials' '
527 rm -f .git-credentials &&
528 test_config credential.helper store &&
530 # the first request prompts the user...
531 set_askpass user@host pass@host &&
532 git ls-remote "$HTTPD_URL/auth/smart/repo.git" >/dev/null &&
533 expect_askpass both user@host &&
535 # ...and the second one uses the stored value rather than
536 # prompting the user.
537 set_askpass bogus-user bogus-pass &&
538 git ls-remote "$HTTPD_URL/auth/smart/repo.git" >/dev/null &&
539 expect_askpass none
542 test_expect_success 'http auth forgets bogus credentials' '
543 # seed credential store with bogus values. In real life,
544 # this would probably come from a password which worked
545 # for a previous request.
546 rm -f .git-credentials &&
547 test_config credential.helper store &&
549 echo "url=$HTTPD_URL" &&
550 echo "username=bogus" &&
551 echo "password=bogus"
552 } | git credential approve &&
554 # we expect this to use the bogus values and fail, never even
555 # prompting the user...
556 set_askpass user@host pass@host &&
557 test_must_fail git ls-remote "$HTTPD_URL/auth/smart/repo.git" >/dev/null &&
558 expect_askpass none &&
560 # ...but now we should have forgotten the bad value, causing
561 # us to prompt the user again.
562 set_askpass user@host pass@host &&
563 git ls-remote "$HTTPD_URL/auth/smart/repo.git" >/dev/null &&
564 expect_askpass both user@host
567 test_expect_success 'client falls back from v2 to v0 to match server' '
568 GIT_TRACE_PACKET=$PWD/trace \
569 GIT_TEST_PROTOCOL_VERSION=2 \
570 git clone $HTTPD_URL/smart_v0/repo.git repo-v0 &&
571 # check for v0; there the HEAD symref is communicated in the capability
572 # line; v2 uses a different syntax on each ref advertisement line
573 grep symref=HEAD:refs/heads/ trace
576 test_expect_success 'passing hostname resolution information works' '
577 BOGUS_HOST=gitbogusexamplehost.invalid &&
578 BOGUS_HTTPD_URL=$HTTPD_PROTO://$BOGUS_HOST:$LIB_HTTPD_PORT &&
579 test_must_fail git ls-remote "$BOGUS_HTTPD_URL/smart/repo.git" >/dev/null &&
580 git -c "http.curloptResolve=$BOGUS_HOST:$LIB_HTTPD_PORT:127.0.0.1" ls-remote "$BOGUS_HTTPD_URL/smart/repo.git" >/dev/null
583 test_done