t5[6-9]*: adjust the references to the default branch name "main"
[git] / t / t5551-http-fetch-smart.sh
1 #!/bin/sh
2
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
6
7 . ./test-lib.sh
8 . "$TEST_DIRECTORY"/lib-httpd.sh
9 start_httpd
10
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
16 '
17
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
25 '
26
27 setup_askpass_helper
28
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         > Pragma: no-cache
35         < HTTP/1.1 200 OK
36         < Pragma: no-cache
37         < Cache-Control: no-cache, max-age=0, must-revalidate
38         < Content-Type: application/x-git-upload-pack-advertisement
39         > POST /smart/repo.git/git-upload-pack HTTP/1.1
40         > Accept-Encoding: ENCODINGS
41         > Content-Type: application/x-git-upload-pack-request
42         > Accept: application/x-git-upload-pack-result
43         > Content-Length: xxx
44         < HTTP/1.1 200 OK
45         < Pragma: no-cache
46         < Cache-Control: no-cache, max-age=0, must-revalidate
47         < Content-Type: application/x-git-upload-pack-result
48         EOF
49         GIT_TRACE_CURL=true GIT_TEST_PROTOCOL_VERSION=0 \
50                 git clone --quiet $HTTPD_URL/smart/repo.git clone 2>err &&
51         test_cmp file clone/file &&
52         tr '\''\015'\'' Q <err |
53         sed -e "
54                 s/Q\$//
55                 /^[*] /d
56                 /^== Info:/d
57                 /^=> Send header, /d
58                 /^=> Send header:$/d
59                 /^<= Recv header, /d
60                 /^<= Recv header:$/d
61                 s/=> Send header: //
62                 s/= Recv header://
63                 /^<= Recv data/d
64                 /^=> Send data/d
65                 /^$/d
66                 /^< $/d
67
68                 /^[^><]/{
69                         s/^/> /
70                 }
71
72                 /^> User-Agent: /d
73                 /^> Host: /d
74                 /^> POST /,$ {
75                         /^> Accept: [*]\\/[*]/d
76                 }
77                 s/^> Content-Length: .*/> Content-Length: xxx/
78                 /^> 00..want /d
79                 /^> 00.*done/d
80
81                 /^< Server: /d
82                 /^< Expires: /d
83                 /^< Date: /d
84                 /^< Content-Length: /d
85                 /^< Transfer-Encoding: /d
86         " >actual &&
87
88         # NEEDSWORK: If the overspecification of the expected result is reduced, we
89         # might be able to run this test in all protocol versions.
90         if test "$GIT_TEST_PROTOCOL_VERSION" = 0
91         then
92                 sed -e "s/^> Accept-Encoding: .*/> Accept-Encoding: ENCODINGS/" \
93                                 actual >actual.smudged &&
94                 test_cmp exp actual.smudged &&
95
96                 grep "Accept-Encoding:.*gzip" actual >actual.gzip &&
97                 test_line_count = 2 actual.gzip
98         fi
99 '
100
101 test_expect_success 'fetch changes via http' '
102         echo content >>file &&
103         git commit -a -m two &&
104         git push public &&
105         (cd clone && git pull) &&
106         test_cmp file clone/file
107 '
108
109 test_expect_success 'used upload-pack service' '
110         cat >exp <<-\EOF &&
111         GET  /smart/repo.git/info/refs?service=git-upload-pack HTTP/1.1 200
112         POST /smart/repo.git/git-upload-pack HTTP/1.1 200
113         GET  /smart/repo.git/info/refs?service=git-upload-pack HTTP/1.1 200
114         POST /smart/repo.git/git-upload-pack HTTP/1.1 200
115         EOF
116
117         # NEEDSWORK: If the overspecification of the expected result is reduced, we
118         # might be able to run this test in all protocol versions.
119         if test "$GIT_TEST_PROTOCOL_VERSION" = 0
120         then
121                 check_access_log exp
122         fi
123 '
124
125 test_expect_success 'follow redirects (301)' '
126         git clone $HTTPD_URL/smart-redir-perm/repo.git --quiet repo-p
127 '
128
129 test_expect_success 'follow redirects (302)' '
130         git clone $HTTPD_URL/smart-redir-temp/repo.git --quiet repo-t
131 '
132
133 test_expect_success 'redirects re-root further requests' '
134         git clone $HTTPD_URL/smart-redir-limited/repo.git repo-redir-limited
135 '
136
137 test_expect_success 're-rooting dies on insane schemes' '
138         test_must_fail git clone $HTTPD_URL/insane-redir/repo.git insane
139 '
140
141 test_expect_success 'clone from password-protected repository' '
142         echo two >expect &&
143         set_askpass user@host pass@host &&
144         git clone --bare "$HTTPD_URL/auth/smart/repo.git" smart-auth &&
145         expect_askpass both user@host &&
146         git --git-dir=smart-auth log -1 --format=%s >actual &&
147         test_cmp expect actual
148 '
149
150 test_expect_success 'clone from auth-only-for-push repository' '
151         echo two >expect &&
152         set_askpass wrong &&
153         git clone --bare "$HTTPD_URL/auth-push/smart/repo.git" smart-noauth &&
154         expect_askpass none &&
155         git --git-dir=smart-noauth log -1 --format=%s >actual &&
156         test_cmp expect actual
157 '
158
159 test_expect_success 'clone from auth-only-for-objects repository' '
160         echo two >expect &&
161         set_askpass user@host pass@host &&
162         git clone --bare "$HTTPD_URL/auth-fetch/smart/repo.git" half-auth &&
163         expect_askpass both user@host &&
164         git --git-dir=half-auth log -1 --format=%s >actual &&
165         test_cmp expect actual
166 '
167
168 test_expect_success 'no-op half-auth fetch does not require a password' '
169         set_askpass wrong &&
170
171         # NEEDSWORK: When using HTTP(S), protocol v0 supports a "half-auth"
172         # configuration with authentication required only when downloading
173         # objects and not refs, by having the HTTP server only require
174         # authentication for the "git-upload-pack" path and not "info/refs".
175         # This is not possible with protocol v2, since both objects and refs
176         # are obtained from the "git-upload-pack" path. A solution to this is
177         # to teach the server and client to be able to inline ls-refs requests
178         # as an Extra Parameter (see pack-protocol.txt), so that "info/refs"
179         # can serve refs, just like it does in protocol v0.
180         GIT_TEST_PROTOCOL_VERSION=0 git --git-dir=half-auth fetch &&
181         expect_askpass none
182 '
183
184 test_expect_success 'redirects send auth to new location' '
185         set_askpass user@host pass@host &&
186         git -c credential.useHttpPath=true \
187           clone $HTTPD_URL/smart-redir-auth/repo.git repo-redir-auth &&
188         expect_askpass both user@host auth/smart/repo.git
189 '
190
191 test_expect_success 'GIT_TRACE_CURL redacts auth details' '
192         rm -rf redact-auth trace &&
193         set_askpass user@host pass@host &&
194         GIT_TRACE_CURL="$(pwd)/trace" git clone --bare "$HTTPD_URL/auth/smart/repo.git" redact-auth &&
195         expect_askpass both user@host &&
196
197         # Ensure that there is no "Basic" followed by a base64 string, but that
198         # the auth details are redacted
199         ! grep "Authorization: Basic [0-9a-zA-Z+/]" trace &&
200         grep "Authorization: Basic <redacted>" trace
201 '
202
203 test_expect_success 'GIT_CURL_VERBOSE redacts auth details' '
204         rm -rf redact-auth trace &&
205         set_askpass user@host pass@host &&
206         GIT_CURL_VERBOSE=1 git clone --bare "$HTTPD_URL/auth/smart/repo.git" redact-auth 2>trace &&
207         expect_askpass both user@host &&
208
209         # Ensure that there is no "Basic" followed by a base64 string, but that
210         # the auth details are redacted
211         ! grep "Authorization: Basic [0-9a-zA-Z+/]" trace &&
212         grep "Authorization: Basic <redacted>" trace
213 '
214
215 test_expect_success 'GIT_TRACE_CURL does not redact auth details if GIT_TRACE_REDACT=0' '
216         rm -rf redact-auth trace &&
217         set_askpass user@host pass@host &&
218         GIT_TRACE_REDACT=0 GIT_TRACE_CURL="$(pwd)/trace" \
219                 git clone --bare "$HTTPD_URL/auth/smart/repo.git" redact-auth &&
220         expect_askpass both user@host &&
221
222         grep "Authorization: Basic [0-9a-zA-Z+/]" trace
223 '
224
225 test_expect_success 'disable dumb http on server' '
226         git --git-dir="$HTTPD_DOCUMENT_ROOT_PATH/repo.git" \
227                 config http.getanyfile false
228 '
229
230 test_expect_success 'GIT_SMART_HTTP can disable smart http' '
231         (GIT_SMART_HTTP=0 &&
232          export GIT_SMART_HTTP &&
233          cd clone &&
234          test_must_fail git fetch)
235 '
236
237 test_expect_success 'invalid Content-Type rejected' '
238         test_must_fail git clone $HTTPD_URL/broken_smart/repo.git 2>actual &&
239         test_i18ngrep "not valid:" actual
240 '
241
242 test_expect_success 'create namespaced refs' '
243         test_commit namespaced &&
244         git push public HEAD:refs/namespaces/ns/refs/heads/main &&
245         git --git-dir="$HTTPD_DOCUMENT_ROOT_PATH/repo.git" \
246                 symbolic-ref refs/namespaces/ns/HEAD refs/namespaces/ns/refs/heads/main
247 '
248
249 test_expect_success 'smart clone respects namespace' '
250         git clone "$HTTPD_URL/smart_namespace/repo.git" ns-smart &&
251         echo namespaced >expect &&
252         git --git-dir=ns-smart/.git log -1 --format=%s >actual &&
253         test_cmp expect actual
254 '
255
256 test_expect_success 'dumb clone via http-backend respects namespace' '
257         git --git-dir="$HTTPD_DOCUMENT_ROOT_PATH/repo.git" \
258                 config http.getanyfile true &&
259         GIT_SMART_HTTP=0 git clone \
260                 "$HTTPD_URL/smart_namespace/repo.git" ns-dumb &&
261         echo namespaced >expect &&
262         git --git-dir=ns-dumb/.git log -1 --format=%s >actual &&
263         test_cmp expect actual
264 '
265
266 test_expect_success 'cookies stored in http.cookiefile when http.savecookies set' '
267         cat >cookies.txt <<-\EOF &&
268         127.0.0.1       FALSE   /smart_cookies/ FALSE   0       othername       othervalue
269         EOF
270         sort >expect_cookies.txt <<-\EOF &&
271
272         127.0.0.1       FALSE   /smart_cookies/ FALSE   0       othername       othervalue
273         127.0.0.1       FALSE   /smart_cookies/repo.git/info/   FALSE   0       name    value
274         EOF
275         git config http.cookiefile cookies.txt &&
276         git config http.savecookies true &&
277         git ls-remote $HTTPD_URL/smart_cookies/repo.git main &&
278
279         # NEEDSWORK: If the overspecification of the expected result is reduced, we
280         # might be able to run this test in all protocol versions.
281         if test "$GIT_TEST_PROTOCOL_VERSION" = 0
282         then
283                 tail -3 cookies.txt | sort >cookies_tail.txt &&
284                 test_cmp expect_cookies.txt cookies_tail.txt
285         fi
286 '
287
288 test_expect_success 'transfer.hiderefs works over smart-http' '
289         test_commit hidden &&
290         test_commit visible &&
291         git push public HEAD^:refs/heads/a HEAD:refs/heads/b &&
292         git --git-dir="$HTTPD_DOCUMENT_ROOT_PATH/repo.git" \
293                 config transfer.hiderefs refs/heads/a &&
294         git clone --bare "$HTTPD_URL/smart/repo.git" hidden.git &&
295         test_must_fail git -C hidden.git rev-parse --verify a &&
296         git -C hidden.git rev-parse --verify b
297 '
298
299 # create an arbitrary number of tags, numbered from tag-$1 to tag-$2
300 create_tags () {
301         rm -f marks &&
302         for i in $(test_seq "$1" "$2")
303         do
304                 # don't use here-doc, because it requires a process
305                 # per loop iteration
306                 echo "commit refs/heads/too-many-refs-$1" &&
307                 echo "mark :$i" &&
308                 echo "committer git <git@example.com> $i +0000" &&
309                 echo "data 0" &&
310                 echo "M 644 inline bla.txt" &&
311                 echo "data 4" &&
312                 echo "bla" &&
313                 # make every commit dangling by always
314                 # rewinding the branch after each commit
315                 echo "reset refs/heads/too-many-refs-$1" &&
316                 echo "from :$1"
317         done | git fast-import --export-marks=marks &&
318
319         # now assign tags to all the dangling commits we created above
320         tag=$(perl -e "print \"bla\" x 30") &&
321         sed -e "s|^:\([^ ]*\) \(.*\)$|\2 refs/tags/$tag-\1|" <marks >>packed-refs
322 }
323
324 test_expect_success 'create 2,000 tags in the repo' '
325         (
326                 cd "$HTTPD_DOCUMENT_ROOT_PATH/repo.git" &&
327                 create_tags 1 2000
328         )
329 '
330
331 test_expect_success CMDLINE_LIMIT \
332         'clone the 2,000 tag repo to check OS command line overflow' '
333         run_with_limited_cmdline git clone $HTTPD_URL/smart/repo.git too-many-refs &&
334         (
335                 cd too-many-refs &&
336                 git for-each-ref refs/tags >actual &&
337                 test_line_count = 2000 actual
338         )
339 '
340
341 test_expect_success 'large fetch-pack requests can be sent using chunked encoding' '
342         GIT_TRACE_CURL=true git -c http.postbuffer=65536 \
343                 clone --bare "$HTTPD_URL/smart/repo.git" split.git 2>err &&
344         grep "^=> Send header: Transfer-Encoding: chunked" err
345 '
346
347 test_expect_success 'test allowreachablesha1inwant' '
348         test_when_finished "rm -rf test_reachable.git" &&
349         server="$HTTPD_DOCUMENT_ROOT_PATH/repo.git" &&
350         main_sha=$(git -C "$server" rev-parse refs/heads/main) &&
351         git -C "$server" config uploadpack.allowreachablesha1inwant 1 &&
352
353         git init --bare test_reachable.git &&
354         git -C test_reachable.git remote add origin "$HTTPD_URL/smart/repo.git" &&
355         git -C test_reachable.git fetch origin "$main_sha"
356 '
357
358 test_expect_success 'test allowreachablesha1inwant with unreachable' '
359         test_when_finished "rm -rf test_reachable.git; git reset --hard $(git rev-parse HEAD)" &&
360
361         #create unreachable sha
362         echo content >file2 &&
363         git add file2 &&
364         git commit -m two &&
365         git push public HEAD:refs/heads/doomed &&
366         git push public :refs/heads/doomed &&
367
368         server="$HTTPD_DOCUMENT_ROOT_PATH/repo.git" &&
369         main_sha=$(git -C "$server" rev-parse refs/heads/main) &&
370         git -C "$server" config uploadpack.allowreachablesha1inwant 1 &&
371
372         git init --bare test_reachable.git &&
373         git -C test_reachable.git remote add origin "$HTTPD_URL/smart/repo.git" &&
374         # Some protocol versions (e.g. 2) support fetching
375         # unadvertised objects, so restrict this test to v0.
376         test_must_fail env GIT_TEST_PROTOCOL_VERSION=0 \
377                 git -C test_reachable.git fetch origin "$(git rev-parse HEAD)"
378 '
379
380 test_expect_success 'test allowanysha1inwant with unreachable' '
381         test_when_finished "rm -rf test_reachable.git; git reset --hard $(git rev-parse HEAD)" &&
382
383         #create unreachable sha
384         echo content >file2 &&
385         git add file2 &&
386         git commit -m two &&
387         git push public HEAD:refs/heads/doomed &&
388         git push public :refs/heads/doomed &&
389
390         server="$HTTPD_DOCUMENT_ROOT_PATH/repo.git" &&
391         main_sha=$(git -C "$server" rev-parse refs/heads/main) &&
392         git -C "$server" config uploadpack.allowreachablesha1inwant 1 &&
393
394         git init --bare test_reachable.git &&
395         git -C test_reachable.git remote add origin "$HTTPD_URL/smart/repo.git" &&
396         # Some protocol versions (e.g. 2) support fetching
397         # unadvertised objects, so restrict this test to v0.
398         test_must_fail env GIT_TEST_PROTOCOL_VERSION=0 \
399                 git -C test_reachable.git fetch origin "$(git rev-parse HEAD)" &&
400
401         git -C "$server" config uploadpack.allowanysha1inwant 1 &&
402         git -C test_reachable.git fetch origin "$(git rev-parse HEAD)"
403 '
404
405 test_expect_success EXPENSIVE 'http can handle enormous ref negotiation' '
406         (
407                 cd "$HTTPD_DOCUMENT_ROOT_PATH/repo.git" &&
408                 create_tags 2001 50000
409         ) &&
410         git -C too-many-refs fetch -q --tags &&
411         (
412                 cd "$HTTPD_DOCUMENT_ROOT_PATH/repo.git" &&
413                 create_tags 50001 100000
414         ) &&
415         git -C too-many-refs fetch -q --tags &&
416         git -C too-many-refs for-each-ref refs/tags >tags &&
417         test_line_count = 100000 tags
418 '
419
420 test_expect_success 'custom http headers' '
421         test_must_fail git -c http.extraheader="x-magic-two: cadabra" \
422                 fetch "$HTTPD_URL/smart_headers/repo.git" &&
423         git -c http.extraheader="x-magic-one: abra" \
424             -c http.extraheader="x-magic-two: cadabra" \
425             fetch "$HTTPD_URL/smart_headers/repo.git" &&
426         git update-index --add --cacheinfo 160000,$(git rev-parse HEAD),sub &&
427         git config -f .gitmodules submodule.sub.path sub &&
428         git config -f .gitmodules submodule.sub.url \
429                 "$HTTPD_URL/smart_headers/repo.git" &&
430         git submodule init sub &&
431         test_must_fail git submodule update sub &&
432         git -c http.extraheader="x-magic-one: abra" \
433             -c http.extraheader="x-magic-two: cadabra" \
434                 submodule update sub
435 '
436
437 test_expect_success 'using fetch command in remote-curl updates refs' '
438         SERVER="$HTTPD_DOCUMENT_ROOT_PATH/twobranch" &&
439         rm -rf "$SERVER" client &&
440
441         git init "$SERVER" &&
442         test_commit -C "$SERVER" foo &&
443         git -C "$SERVER" update-ref refs/heads/anotherbranch foo &&
444
445         git clone $HTTPD_URL/smart/twobranch client &&
446
447         test_commit -C "$SERVER" bar &&
448         git -C client -c protocol.version=0 fetch &&
449
450         git -C "$SERVER" rev-parse main >expect &&
451         git -C client rev-parse origin/main >actual &&
452         test_cmp expect actual
453 '
454
455 test_expect_success 'fetch by SHA-1 without tag following' '
456         SERVER="$HTTPD_DOCUMENT_ROOT_PATH/server" &&
457         rm -rf "$SERVER" client &&
458
459         git init "$SERVER" &&
460         test_commit -C "$SERVER" foo &&
461
462         git clone $HTTPD_URL/smart/server client &&
463
464         test_commit -C "$SERVER" bar &&
465         git -C "$SERVER" rev-parse bar >bar_hash &&
466         git -C client -c protocol.version=0 fetch \
467                 --no-tags origin $(cat bar_hash)
468 '
469
470 test_expect_success 'cookies are redacted by default' '
471         rm -rf clone &&
472         echo "Set-Cookie: Foo=1" >cookies &&
473         echo "Set-Cookie: Bar=2" >>cookies &&
474         GIT_TRACE_CURL=true \
475                 git -c "http.cookieFile=$(pwd)/cookies" clone \
476                 $HTTPD_URL/smart/repo.git clone 2>err &&
477         grep "Cookie:.*Foo=<redacted>" err &&
478         grep "Cookie:.*Bar=<redacted>" err &&
479         ! grep "Cookie:.*Foo=1" err &&
480         ! grep "Cookie:.*Bar=2" err
481 '
482
483 test_expect_success 'empty values of cookies are also redacted' '
484         rm -rf clone &&
485         echo "Set-Cookie: Foo=" >cookies &&
486         GIT_TRACE_CURL=true \
487                 git -c "http.cookieFile=$(pwd)/cookies" clone \
488                 $HTTPD_URL/smart/repo.git clone 2>err &&
489         grep "Cookie:.*Foo=<redacted>" err
490 '
491
492 test_expect_success 'GIT_TRACE_REDACT=0 disables cookie redaction' '
493         rm -rf clone &&
494         echo "Set-Cookie: Foo=1" >cookies &&
495         echo "Set-Cookie: Bar=2" >>cookies &&
496         GIT_TRACE_REDACT=0 GIT_TRACE_CURL=true \
497                 git -c "http.cookieFile=$(pwd)/cookies" clone \
498                 $HTTPD_URL/smart/repo.git clone 2>err &&
499         grep "Cookie:.*Foo=1" err &&
500         grep "Cookie:.*Bar=2" err
501 '
502
503 test_expect_success 'GIT_TRACE_CURL_NO_DATA prevents data from being traced' '
504         rm -rf clone &&
505         GIT_TRACE_CURL=true \
506                 git clone $HTTPD_URL/smart/repo.git clone 2>err &&
507         grep "=> Send data" err &&
508
509         rm -rf clone &&
510         GIT_TRACE_CURL=true GIT_TRACE_CURL_NO_DATA=1 \
511                 git clone $HTTPD_URL/smart/repo.git clone 2>err &&
512         ! grep "=> Send data" err
513 '
514
515 test_expect_success 'server-side error detected' '
516         test_must_fail git clone $HTTPD_URL/error_smart/repo.git 2>actual &&
517         test_i18ngrep "server-side error" actual
518 '
519
520 test_done