Git 2.19
[git/git.git] / t / t5551-http-fetch-smart.sh
CommitLineData
7da4e228
SP
1#!/bin/sh
2
3test_description='test smart fetching over http via http-backend'
4. ./test-lib.sh
7da4e228
SP
5. "$TEST_DIRECTORY"/lib-httpd.sh
6start_httpd
7
8test_expect_success 'setup repository' '
bd7ac599 9 git config push.default matching &&
7da4e228
SP
10 echo content >file &&
11 git add file &&
12 git commit -m one
13'
14
15test_expect_success 'create http-accessible bare repository' '
16 mkdir "$HTTPD_DOCUMENT_ROOT_PATH/repo.git" &&
17 (cd "$HTTPD_DOCUMENT_ROOT_PATH/repo.git" &&
18 git --bare init
19 ) &&
20 git remote add public "$HTTPD_DOCUMENT_ROOT_PATH/repo.git" &&
21 git push public master:master
22'
23
6ac2b3ae
JK
24setup_askpass_helper
25
7da4e228
SP
26cat >exp <<EOF
27> GET /smart/repo.git/info/refs?service=git-upload-pack HTTP/1.1
28> Accept: */*
1a53e692 29> Accept-Encoding: ENCODINGS
7da4e228 30> Pragma: no-cache
7da4e228
SP
31< HTTP/1.1 200 OK
32< Pragma: no-cache
33< Cache-Control: no-cache, max-age=0, must-revalidate
34< Content-Type: application/x-git-upload-pack-advertisement
7da4e228 35> POST /smart/repo.git/git-upload-pack HTTP/1.1
1a53e692 36> Accept-Encoding: ENCODINGS
7da4e228 37> Content-Type: application/x-git-upload-pack-request
8efa5f62 38> Accept: application/x-git-upload-pack-result
7da4e228 39> Content-Length: xxx
7da4e228
SP
40< HTTP/1.1 200 OK
41< Pragma: no-cache
42< Cache-Control: no-cache, max-age=0, must-revalidate
43< Content-Type: application/x-git-upload-pack-result
7da4e228
SP
44EOF
45test_expect_success 'clone http repository' '
14e24114 46 GIT_TRACE_CURL=true git clone --quiet $HTTPD_URL/smart/repo.git clone 2>err &&
7da4e228
SP
47 test_cmp file clone/file &&
48 tr '\''\015'\'' Q <err |
49 sed -e "
50 s/Q\$//
51 /^[*] /d
14e24114
EP
52 /^== Info:/d
53 /^=> Send header, /d
54 /^=> Send header:$/d
55 /^<= Recv header, /d
56 /^<= Recv header:$/d
57 s/=> Send header: //
58 s/= Recv header://
59 /^<= Recv data/d
60 /^=> Send data/d
0a8fcbdc
SP
61 /^$/d
62 /^< $/d
7da4e228
SP
63
64 /^[^><]/{
65 s/^/> /
66 }
67
68 /^> User-Agent: /d
69 /^> Host: /d
20366635
SP
70 /^> POST /,$ {
71 /^> Accept: [*]\\/[*]/d
72 }
7da4e228 73 s/^> Content-Length: .*/> Content-Length: xxx/
0a8fcbdc
SP
74 /^> 00..want /d
75 /^> 00.*done/d
7da4e228
SP
76
77 /^< Server: /d
78 /^< Expires: /d
79 /^< Date: /d
80 /^< Content-Length: /d
81 /^< Transfer-Encoding: /d
1a53e692
BW
82 " >actual &&
83 sed -e "s/^> Accept-Encoding: .*/> Accept-Encoding: ENCODINGS/" \
84 actual >actual.smudged &&
85 test_cmp exp actual.smudged &&
86
87 grep "Accept-Encoding:.*gzip" actual >actual.gzip &&
88 test_line_count = 2 actual.gzip
7da4e228
SP
89'
90
91test_expect_success 'fetch changes via http' '
92 echo content >>file &&
93 git commit -a -m two &&
99094a7a 94 git push public &&
7da4e228
SP
95 (cd clone && git pull) &&
96 test_cmp file clone/file
97'
98
99cat >exp <<EOF
100GET /smart/repo.git/info/refs?service=git-upload-pack HTTP/1.1 200
101POST /smart/repo.git/git-upload-pack HTTP/1.1 200
102GET /smart/repo.git/info/refs?service=git-upload-pack HTTP/1.1 200
103POST /smart/repo.git/git-upload-pack HTTP/1.1 200
104EOF
105test_expect_success 'used upload-pack service' '
e8b3b2e2 106 check_access_log exp
7da4e228
SP
107'
108
311e2ea0
TRC
109test_expect_success 'follow redirects (301)' '
110 git clone $HTTPD_URL/smart-redir-perm/repo.git --quiet repo-p
111'
112
113test_expect_success 'follow redirects (302)' '
114 git clone $HTTPD_URL/smart-redir-temp/repo.git --quiet repo-t
115'
116
050ef365
JK
117test_expect_success 'redirects re-root further requests' '
118 git clone $HTTPD_URL/smart-redir-limited/repo.git repo-redir-limited
119'
120
6628eb41
JK
121test_expect_success 're-rooting dies on insane schemes' '
122 test_must_fail git clone $HTTPD_URL/insane-redir/repo.git insane
123'
124
6ac2b3ae
JK
125test_expect_success 'clone from password-protected repository' '
126 echo two >expect &&
afbf5ca5 127 set_askpass user@host pass@host &&
6ac2b3ae
JK
128 git clone --bare "$HTTPD_URL/auth/smart/repo.git" smart-auth &&
129 expect_askpass both user@host &&
130 git --git-dir=smart-auth log -1 --format=%s >actual &&
131 test_cmp expect actual
132'
133
4c71009d
JK
134test_expect_success 'clone from auth-only-for-push repository' '
135 echo two >expect &&
136 set_askpass wrong &&
137 git clone --bare "$HTTPD_URL/auth-push/smart/repo.git" smart-noauth &&
138 expect_askpass none &&
139 git --git-dir=smart-noauth log -1 --format=%s >actual &&
140 test_cmp expect actual
141'
142
2e736fd5
JK
143test_expect_success 'clone from auth-only-for-objects repository' '
144 echo two >expect &&
afbf5ca5 145 set_askpass user@host pass@host &&
2e736fd5
JK
146 git clone --bare "$HTTPD_URL/auth-fetch/smart/repo.git" half-auth &&
147 expect_askpass both user@host &&
148 git --git-dir=half-auth log -1 --format=%s >actual &&
149 test_cmp expect actual
150'
151
152test_expect_success 'no-op half-auth fetch does not require a password' '
153 set_askpass wrong &&
154 git --git-dir=half-auth fetch &&
155 expect_askpass none
156'
157
050ef365 158test_expect_success 'redirects send auth to new location' '
afbf5ca5 159 set_askpass user@host pass@host &&
050ef365
JK
160 git -c credential.useHttpPath=true \
161 clone $HTTPD_URL/smart-redir-auth/repo.git repo-redir-auth &&
162 expect_askpass both user@host auth/smart/repo.git
163'
164
02572c2e
JK
165test_expect_success 'disable dumb http on server' '
166 git --git-dir="$HTTPD_DOCUMENT_ROOT_PATH/repo.git" \
167 config http.getanyfile false
168'
169
170test_expect_success 'GIT_SMART_HTTP can disable smart http' '
171 (GIT_SMART_HTTP=0 &&
172 export GIT_SMART_HTTP &&
173 cd clone &&
174 test_must_fail git fetch)
175'
176
4656bf47 177test_expect_success 'invalid Content-Type rejected' '
8fb26872 178 test_must_fail git clone $HTTPD_URL/broken_smart/repo.git 2>actual &&
af3aec44 179 grep "not valid:" actual
4656bf47
SP
180'
181
6130f86d
JK
182test_expect_success 'create namespaced refs' '
183 test_commit namespaced &&
184 git push public HEAD:refs/namespaces/ns/refs/heads/master &&
185 git --git-dir="$HTTPD_DOCUMENT_ROOT_PATH/repo.git" \
186 symbolic-ref refs/namespaces/ns/HEAD refs/namespaces/ns/refs/heads/master
187'
188
189test_expect_success 'smart clone respects namespace' '
190 git clone "$HTTPD_URL/smart_namespace/repo.git" ns-smart &&
191 echo namespaced >expect &&
192 git --git-dir=ns-smart/.git log -1 --format=%s >actual &&
193 test_cmp expect actual
194'
195
196test_expect_success 'dumb clone via http-backend respects namespace' '
197 git --git-dir="$HTTPD_DOCUMENT_ROOT_PATH/repo.git" \
198 config http.getanyfile true &&
199 GIT_SMART_HTTP=0 git clone \
200 "$HTTPD_URL/smart_namespace/repo.git" ns-dumb &&
201 echo namespaced >expect &&
202 git --git-dir=ns-dumb/.git log -1 --format=%s >actual &&
203 test_cmp expect actual
204'
205
912b2acf
DB
206cat >cookies.txt <<EOF
207127.0.0.1 FALSE /smart_cookies/ FALSE 0 othername othervalue
208EOF
209cat >expect_cookies.txt <<EOF
912b2acf
DB
210
211127.0.0.1 FALSE /smart_cookies/ FALSE 0 othername othervalue
212127.0.0.1 FALSE /smart_cookies/repo.git/info/ FALSE 0 name value
213EOF
214test_expect_success 'cookies stored in http.cookiefile when http.savecookies set' '
215 git config http.cookiefile cookies.txt &&
216 git config http.savecookies true &&
217 git ls-remote $HTTPD_URL/smart_cookies/repo.git master &&
60687de5 218 tail -3 cookies.txt >cookies_tail.txt &&
580cf0a0 219 test_cmp expect_cookies.txt cookies_tail.txt
912b2acf
DB
220'
221
e172755b
JK
222test_expect_success 'transfer.hiderefs works over smart-http' '
223 test_commit hidden &&
224 test_commit visible &&
225 git push public HEAD^:refs/heads/a HEAD:refs/heads/b &&
226 git --git-dir="$HTTPD_DOCUMENT_ROOT_PATH/repo.git" \
227 config transfer.hiderefs refs/heads/a &&
228 git clone --bare "$HTTPD_URL/smart/repo.git" hidden.git &&
229 test_must_fail git -C hidden.git rev-parse --verify a &&
230 git -C hidden.git rev-parse --verify b
231'
232
cc969c8d
JK
233# create an arbitrary number of tags, numbered from tag-$1 to tag-$2
234create_tags () {
235 rm -f marks &&
236 for i in $(test_seq "$1" "$2")
7103d254 237 do
cc969c8d
JK
238 # don't use here-doc, because it requires a process
239 # per loop iteration
240 echo "commit refs/heads/too-many-refs-$1" &&
241 echo "mark :$i" &&
242 echo "committer git <git@example.com> $i +0000" &&
243 echo "data 0" &&
244 echo "M 644 inline bla.txt" &&
245 echo "data 4" &&
246 echo "bla" &&
7103d254
IT
247 # make every commit dangling by always
248 # rewinding the branch after each commit
cc969c8d
JK
249 echo "reset refs/heads/too-many-refs-$1" &&
250 echo "from :$1"
7103d254
IT
251 done | git fast-import --export-marks=marks &&
252
253 # now assign tags to all the dangling commits we created above
94221d22 254 tag=$(perl -e "print \"bla\" x 30") &&
5e2c7cd2 255 sed -e "s|^:\([^ ]*\) \(.*\)$|\2 refs/tags/$tag-\1|" <marks >>packed-refs
cc969c8d
JK
256}
257
7419a03f 258test_expect_success 'create 2,000 tags in the repo' '
cc969c8d
JK
259 (
260 cd "$HTTPD_DOCUMENT_ROOT_PATH/repo.git" &&
7419a03f 261 create_tags 1 2000
7103d254
IT
262 )
263'
264
376e4b39
JK
265test_expect_success CMDLINE_LIMIT \
266 'clone the 2,000 tag repo to check OS command line overflow' '
267 run_with_limited_cmdline git clone $HTTPD_URL/smart/repo.git too-many-refs &&
5e2c7cd2
JH
268 (
269 cd too-many-refs &&
376e4b39
JK
270 git for-each-ref refs/tags >actual &&
271 test_line_count = 2000 actual
5e2c7cd2 272 )
7103d254
IT
273'
274
376e4b39 275test_expect_success 'large fetch-pack requests can be split across POSTs' '
14e24114 276 GIT_TRACE_CURL=true git -c http.postbuffer=65536 \
376e4b39 277 clone --bare "$HTTPD_URL/smart/repo.git" split.git 2>err &&
14e24114 278 grep "^=> Send header: POST" err >posts &&
376e4b39
JK
279 test_line_count = 2 posts
280'
281
296b847c
DT
282test_expect_success 'test allowreachablesha1inwant' '
283 test_when_finished "rm -rf test_reachable.git" &&
284 server="$HTTPD_DOCUMENT_ROOT_PATH/repo.git" &&
285 master_sha=$(git -C "$server" rev-parse refs/heads/master) &&
286 git -C "$server" config uploadpack.allowreachablesha1inwant 1 &&
287
288 git init --bare test_reachable.git &&
289 git -C test_reachable.git remote add origin "$HTTPD_URL/smart/repo.git" &&
290 git -C test_reachable.git fetch origin "$master_sha"
291'
292
293test_expect_success 'test allowreachablesha1inwant with unreachable' '
294 test_when_finished "rm -rf test_reachable.git; git reset --hard $(git rev-parse HEAD)" &&
295
296 #create unreachable sha
297 echo content >file2 &&
298 git add file2 &&
299 git commit -m two &&
300 git push public HEAD:refs/heads/doomed &&
301 git push public :refs/heads/doomed &&
302
303 server="$HTTPD_DOCUMENT_ROOT_PATH/repo.git" &&
304 master_sha=$(git -C "$server" rev-parse refs/heads/master) &&
305 git -C "$server" config uploadpack.allowreachablesha1inwant 1 &&
306
307 git init --bare test_reachable.git &&
308 git -C test_reachable.git remote add origin "$HTTPD_URL/smart/repo.git" &&
309 test_must_fail git -C test_reachable.git fetch origin "$(git rev-parse HEAD)"
310'
311
f8edeaa0
DT
312test_expect_success 'test allowanysha1inwant with unreachable' '
313 test_when_finished "rm -rf test_reachable.git; git reset --hard $(git rev-parse HEAD)" &&
314
315 #create unreachable sha
316 echo content >file2 &&
317 git add file2 &&
318 git commit -m two &&
319 git push public HEAD:refs/heads/doomed &&
320 git push public :refs/heads/doomed &&
321
322 server="$HTTPD_DOCUMENT_ROOT_PATH/repo.git" &&
323 master_sha=$(git -C "$server" rev-parse refs/heads/master) &&
324 git -C "$server" config uploadpack.allowreachablesha1inwant 1 &&
325
326 git init --bare test_reachable.git &&
327 git -C test_reachable.git remote add origin "$HTTPD_URL/smart/repo.git" &&
328 test_must_fail git -C test_reachable.git fetch origin "$(git rev-parse HEAD)" &&
329
330 git -C "$server" config uploadpack.allowanysha1inwant 1 &&
331 git -C test_reachable.git fetch origin "$(git rev-parse HEAD)"
332'
333
6bc0cb51 334test_expect_success EXPENSIVE 'http can handle enormous ref negotiation' '
7419a03f
JH
335 (
336 cd "$HTTPD_DOCUMENT_ROOT_PATH/repo.git" &&
337 create_tags 2001 50000
338 ) &&
6bc0cb51
JK
339 git -C too-many-refs fetch -q --tags &&
340 (
341 cd "$HTTPD_DOCUMENT_ROOT_PATH/repo.git" &&
342 create_tags 50001 100000
343 ) &&
344 git -C too-many-refs fetch -q --tags &&
345 git -C too-many-refs for-each-ref refs/tags >tags &&
346 test_line_count = 100000 tags
347'
348
8cb01e2f 349test_expect_success 'custom http headers' '
e31165ce
JS
350 test_must_fail git -c http.extraheader="x-magic-two: cadabra" \
351 fetch "$HTTPD_URL/smart_headers/repo.git" &&
8cb01e2f
JS
352 git -c http.extraheader="x-magic-one: abra" \
353 -c http.extraheader="x-magic-two: cadabra" \
0bbe7317
JS
354 fetch "$HTTPD_URL/smart_headers/repo.git" &&
355 git update-index --add --cacheinfo 160000,$(git rev-parse HEAD),sub &&
356 git config -f .gitmodules submodule.sub.path sub &&
357 git config -f .gitmodules submodule.sub.url \
358 "$HTTPD_URL/smart_headers/repo.git" &&
359 git submodule init sub &&
360 test_must_fail git submodule update sub &&
361 git -c http.extraheader="x-magic-one: abra" \
362 -c http.extraheader="x-magic-two: cadabra" \
363 submodule update sub
8cb01e2f
JS
364'
365
e2842b39
JT
366test_expect_success 'using fetch command in remote-curl updates refs' '
367 SERVER="$HTTPD_DOCUMENT_ROOT_PATH/twobranch" &&
368 rm -rf "$SERVER" client &&
369
370 git init "$SERVER" &&
371 test_commit -C "$SERVER" foo &&
372 git -C "$SERVER" update-ref refs/heads/anotherbranch foo &&
373
374 git clone $HTTPD_URL/smart/twobranch client &&
375
376 test_commit -C "$SERVER" bar &&
377 git -C client -c protocol.version=0 fetch &&
378
379 git -C "$SERVER" rev-parse master >expect &&
380 git -C client rev-parse origin/master >actual &&
381 test_cmp expect actual
382'
383
83411783
JT
384test_expect_success 'GIT_REDACT_COOKIES redacts cookies' '
385 rm -rf clone &&
386 echo "Set-Cookie: Foo=1" >cookies &&
387 echo "Set-Cookie: Bar=2" >>cookies &&
388 GIT_TRACE_CURL=true GIT_REDACT_COOKIES=Bar,Baz \
389 git -c "http.cookieFile=$(pwd)/cookies" clone \
390 $HTTPD_URL/smart/repo.git clone 2>err &&
391 grep "Cookie:.*Foo=1" err &&
392 grep "Cookie:.*Bar=<redacted>" err &&
393 ! grep "Cookie:.*Bar=2" err
394'
395
396test_expect_success 'GIT_REDACT_COOKIES handles empty values' '
397 rm -rf clone &&
398 echo "Set-Cookie: Foo=" >cookies &&
399 GIT_TRACE_CURL=true GIT_REDACT_COOKIES=Foo \
400 git -c "http.cookieFile=$(pwd)/cookies" clone \
401 $HTTPD_URL/smart/repo.git clone 2>err &&
402 grep "Cookie:.*Foo=<redacted>" err
403'
404
8ba18e6f
JT
405test_expect_success 'GIT_TRACE_CURL_NO_DATA prevents data from being traced' '
406 rm -rf clone &&
407 GIT_TRACE_CURL=true \
408 git clone $HTTPD_URL/smart/repo.git clone 2>err &&
409 grep "=> Send data" err &&
410
411 rm -rf clone &&
412 GIT_TRACE_CURL=true GIT_TRACE_CURL_NO_DATA=1 \
413 git clone $HTTPD_URL/smart/repo.git clone 2>err &&
414 ! grep "=> Send data" err
415'
416
7da4e228
SP
417stop_httpd
418test_done