]> git.ipfire.org Git - thirdparty/git.git/blob - t/t5541-http-push-smart.sh
Merge branch 'jk/clone-allow-bare-and-o-together'
[thirdparty/git.git] / t / t5541-http-push-smart.sh
1 #!/bin/sh
2 #
3 # Copyright (c) 2008 Clemens Buchacher <drizzd@aon.at>
4 #
5
6 test_description='test smart pushing over http via http-backend'
7 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
8 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
9
10 . ./test-lib.sh
11
12 ROOT_PATH="$PWD"
13 . "$TEST_DIRECTORY"/lib-gpg.sh
14 . "$TEST_DIRECTORY"/lib-httpd.sh
15 . "$TEST_DIRECTORY"/lib-terminal.sh
16 start_httpd
17
18 test_expect_success 'setup remote repository' '
19 cd "$ROOT_PATH" &&
20 mkdir test_repo &&
21 cd test_repo &&
22 git init &&
23 : >path1 &&
24 git add path1 &&
25 test_tick &&
26 git commit -m initial &&
27 cd - &&
28 git clone --bare test_repo test_repo.git &&
29 cd test_repo.git &&
30 git config http.receivepack true &&
31 git config core.logallrefupdates true &&
32 ORIG_HEAD=$(git rev-parse --verify HEAD) &&
33 cd - &&
34 mv test_repo.git "$HTTPD_DOCUMENT_ROOT_PATH"
35 '
36
37 setup_askpass_helper
38
39 cat >exp <<EOF
40 GET /smart/test_repo.git/info/refs?service=git-upload-pack HTTP/1.1 200
41 POST /smart/test_repo.git/git-upload-pack HTTP/1.1 200
42 EOF
43 test_expect_success 'no empty path components' '
44 # Clear the log, so that it does not affect the "used receive-pack
45 # service" test which reads the log too.
46 test_when_finished ">\"\$HTTPD_ROOT_PATH\"/access.log" &&
47
48 # In the URL, add a trailing slash, and see if git appends yet another
49 # slash.
50 cd "$ROOT_PATH" &&
51 git clone $HTTPD_URL/smart/test_repo.git/ test_repo_clone &&
52
53 # NEEDSWORK: If the overspecification of the expected result is reduced, we
54 # might be able to run this test in all protocol versions.
55 if test "$GIT_TEST_PROTOCOL_VERSION" = 0
56 then
57 check_access_log exp
58 fi
59 '
60
61 test_expect_success 'clone remote repository' '
62 rm -rf test_repo_clone &&
63 git clone $HTTPD_URL/smart/test_repo.git test_repo_clone &&
64 (
65 cd test_repo_clone && git config push.default matching
66 )
67 '
68
69 test_expect_success 'push to remote repository (standard)' '
70 cd "$ROOT_PATH"/test_repo_clone &&
71 : >path2 &&
72 git add path2 &&
73 test_tick &&
74 git commit -m path2 &&
75 HEAD=$(git rev-parse --verify HEAD) &&
76 GIT_TRACE_CURL=true git push -v -v 2>err &&
77 ! grep "Expect: 100-continue" err &&
78 grep "POST git-receive-pack ([0-9]* bytes)" err &&
79 (cd "$HTTPD_DOCUMENT_ROOT_PATH"/test_repo.git &&
80 test $HEAD = $(git rev-parse --verify HEAD))
81 '
82
83 test_expect_success 'push to remote repository (standard) with sending Accept-Language' '
84 cat >exp <<-\EOF &&
85 => Send header: Accept-Language: ko-KR, *;q=0.9
86 => Send header: Accept-Language: ko-KR, *;q=0.9
87 EOF
88
89 cd "$ROOT_PATH"/test_repo_clone &&
90 : >path_lang &&
91 git add path_lang &&
92 test_tick &&
93 git commit -m path_lang &&
94 HEAD=$(git rev-parse --verify HEAD) &&
95 GIT_TRACE_CURL=true LANGUAGE="ko_KR.UTF-8" git push -v -v 2>err &&
96 ! grep "Expect: 100-continue" err &&
97
98 grep "=> Send header: Accept-Language:" err >err.language &&
99 test_cmp exp err.language
100 '
101
102 test_expect_success 'push already up-to-date' '
103 git push
104 '
105
106 test_expect_success 'create and delete remote branch' '
107 cd "$ROOT_PATH"/test_repo_clone &&
108 git checkout -b dev &&
109 : >path3 &&
110 git add path3 &&
111 test_tick &&
112 git commit -m dev &&
113 git push origin dev &&
114 git push origin :dev &&
115 test_must_fail git show-ref --verify refs/remotes/origin/dev
116 '
117
118 test_expect_success 'setup rejected update hook' '
119 test_hook --setup -C "$HTTPD_DOCUMENT_ROOT_PATH/test_repo.git" update <<-\EOF &&
120 exit 1
121 EOF
122
123 cat >exp <<-EOF
124 remote: error: hook declined to update refs/heads/dev2
125 To http://127.0.0.1:$LIB_HTTPD_PORT/smart/test_repo.git
126 ! [remote rejected] dev2 -> dev2 (hook declined)
127 error: failed to push some refs to '\''http://127.0.0.1:$LIB_HTTPD_PORT/smart/test_repo.git'\''
128 EOF
129 '
130
131 test_expect_success 'rejected update prints status' '
132 cd "$ROOT_PATH"/test_repo_clone &&
133 git checkout -b dev2 &&
134 : >path4 &&
135 git add path4 &&
136 test_tick &&
137 git commit -m dev2 &&
138 test_must_fail git push origin dev2 2>act &&
139 sed -e "/^remote: /s/ *$//" <act >cmp &&
140 test_cmp exp cmp
141 '
142 rm -f "$HTTPD_DOCUMENT_ROOT_PATH/test_repo.git/hooks/update"
143
144 cat >exp <<EOF
145 GET /smart/test_repo.git/info/refs?service=git-upload-pack HTTP/1.1 200
146 POST /smart/test_repo.git/git-upload-pack HTTP/1.1 200
147 GET /smart/test_repo.git/info/refs?service=git-receive-pack HTTP/1.1 200
148 POST /smart/test_repo.git/git-receive-pack HTTP/1.1 200
149 GET /smart/test_repo.git/info/refs?service=git-receive-pack HTTP/1.1 200
150 GET /smart/test_repo.git/info/refs?service=git-receive-pack HTTP/1.1 200
151 POST /smart/test_repo.git/git-receive-pack HTTP/1.1 200
152 GET /smart/test_repo.git/info/refs?service=git-receive-pack HTTP/1.1 200
153 POST /smart/test_repo.git/git-receive-pack HTTP/1.1 200
154 GET /smart/test_repo.git/info/refs?service=git-receive-pack HTTP/1.1 200
155 POST /smart/test_repo.git/git-receive-pack HTTP/1.1 200
156 EOF
157 test_expect_success 'used receive-pack service' '
158 # NEEDSWORK: If the overspecification of the expected result is reduced, we
159 # might be able to run this test in all protocol versions.
160 if test "$GIT_TEST_PROTOCOL_VERSION" = 0
161 then
162 check_access_log exp
163 fi
164 '
165
166 test_http_push_nonff "$HTTPD_DOCUMENT_ROOT_PATH"/test_repo.git \
167 "$ROOT_PATH"/test_repo_clone main success
168
169 test_expect_success 'push fails for non-fast-forward refs unmatched by remote helper' '
170 # create a dissimilarly-named remote ref so that git is unable to match the
171 # two refs (viz. local, remote) unless an explicit refspec is provided.
172 git push origin main:niam &&
173
174 echo "change changed" > path2 &&
175 git commit -a -m path2 --amend &&
176
177 # push main too; this ensures there is at least one '"'push'"' command to
178 # the remote helper and triggers interaction with the helper.
179 test_must_fail git push -v origin +main main:niam >output 2>&1'
180
181 test_expect_success 'push fails for non-fast-forward refs unmatched by remote helper: remote output' '
182 grep "^ + [a-f0-9]*\.\.\.[a-f0-9]* *main -> main (forced update)$" output &&
183 grep "^ ! \[rejected\] *main -> niam (non-fast-forward)$" output
184 '
185
186 test_expect_success 'push fails for non-fast-forward refs unmatched by remote helper: our output' '
187 test_i18ngrep "Updates were rejected because" \
188 output
189 '
190
191 test_expect_success 'push (chunked)' '
192 git checkout main &&
193 test_commit commit path3 &&
194 HEAD=$(git rev-parse --verify HEAD) &&
195 test_config http.postbuffer 4 &&
196 git push -v -v origin $BRANCH 2>err &&
197 grep "POST git-receive-pack (chunked)" err &&
198 (cd "$HTTPD_DOCUMENT_ROOT_PATH"/test_repo.git &&
199 test $HEAD = $(git rev-parse --verify HEAD))
200 '
201
202 ## References of remote: atomic1(1) main(2) collateral(2) other(2)
203 ## References of local : atomic2(2) main(1) collateral(3) other(2) collateral1(3) atomic(1)
204 ## Atomic push : main(1) collateral(3) atomic(1)
205 test_expect_success 'push --atomic also prevents branch creation, reports collateral' '
206 # Setup upstream repo - empty for now
207 d=$HTTPD_DOCUMENT_ROOT_PATH/atomic-branches.git &&
208 git init --bare "$d" &&
209 test_config -C "$d" http.receivepack true &&
210 up="$HTTPD_URL"/smart/atomic-branches.git &&
211
212 # Tell "$up" about three branches for now
213 test_commit atomic1 &&
214 test_commit atomic2 &&
215 git branch collateral &&
216 git branch other &&
217 git push "$up" atomic1 main collateral other &&
218 git tag -d atomic1 &&
219
220 # collateral is a valid push, but should be failed by atomic push
221 git checkout collateral &&
222 test_commit collateral1 &&
223
224 # Make main incompatible with upstream to provoke atomic
225 git checkout main &&
226 git reset --hard HEAD^ &&
227
228 # Add a new branch which should be failed by atomic push. This is a
229 # regression case.
230 git branch atomic &&
231
232 # --atomic should cause entire push to be rejected
233 test_must_fail git push --atomic "$up" main atomic collateral 2>output &&
234
235 # the new branch should not have been created upstream
236 test_must_fail git -C "$d" show-ref --verify refs/heads/atomic &&
237
238 # upstream should still reflect atomic2, the last thing we pushed
239 # successfully
240 git rev-parse atomic2 >expected &&
241 # on main...
242 git -C "$d" rev-parse refs/heads/main >actual &&
243 test_cmp expected actual &&
244 # ...and collateral.
245 git -C "$d" rev-parse refs/heads/collateral >actual &&
246 test_cmp expected actual &&
247
248 # the failed refs should be indicated to the user
249 grep "^ ! .*rejected.* main -> main" output &&
250
251 # the collateral failure refs should be indicated to the user
252 grep "^ ! .*rejected.* atomic -> atomic .*atomic push failed" output &&
253 grep "^ ! .*rejected.* collateral -> collateral .*atomic push failed" output &&
254
255 # never report what we do not push
256 ! grep "^ ! .*rejected.* atomic1 " output &&
257 ! grep "^ ! .*rejected.* other " output
258 '
259
260 test_expect_success 'push --atomic fails on server-side errors' '
261 # Use previously set up repository
262 d=$HTTPD_DOCUMENT_ROOT_PATH/atomic-branches.git &&
263 test_config -C "$d" http.receivepack true &&
264 up="$HTTPD_URL"/smart/atomic-branches.git &&
265
266 # break ref updates for other on the remote site
267 mkdir "$d/refs/heads/other.lock" &&
268
269 # add the new commit to other
270 git branch -f other collateral &&
271
272 # --atomic should cause entire push to be rejected
273 test_must_fail git push --atomic "$up" atomic other 2>output &&
274
275 # the new branch should not have been created upstream
276 test_must_fail git -C "$d" show-ref --verify refs/heads/atomic &&
277
278 # upstream should still reflect atomic2, the last thing we pushed
279 # successfully
280 git rev-parse atomic2 >expected &&
281 # ...to other.
282 git -C "$d" rev-parse refs/heads/other >actual &&
283 test_cmp expected actual &&
284
285 # the new branch should not have been created upstream
286 test_must_fail git -C "$d" show-ref --verify refs/heads/atomic &&
287
288 # the failed refs should be indicated to the user
289 grep "^ ! .*rejected.* other -> other .*atomic transaction failed" output &&
290
291 # the collateral failure refs should be indicated to the user
292 grep "^ ! .*rejected.* atomic -> atomic .*atomic transaction failed" output
293 '
294
295 test_expect_success 'push --all can push to empty repo' '
296 d=$HTTPD_DOCUMENT_ROOT_PATH/empty-all.git &&
297 git init --bare "$d" &&
298 git --git-dir="$d" config http.receivepack true &&
299 git push --all "$HTTPD_URL"/smart/empty-all.git
300 '
301
302 test_expect_success 'push --mirror can push to empty repo' '
303 d=$HTTPD_DOCUMENT_ROOT_PATH/empty-mirror.git &&
304 git init --bare "$d" &&
305 git --git-dir="$d" config http.receivepack true &&
306 git push --mirror "$HTTPD_URL"/smart/empty-mirror.git
307 '
308
309 test_expect_success 'push --all to repo with alternates' '
310 s=$HTTPD_DOCUMENT_ROOT_PATH/test_repo.git &&
311 d=$HTTPD_DOCUMENT_ROOT_PATH/alternates-all.git &&
312 git clone --bare --shared "$s" "$d" &&
313 git --git-dir="$d" config http.receivepack true &&
314 git --git-dir="$d" repack -adl &&
315 git push --all "$HTTPD_URL"/smart/alternates-all.git
316 '
317
318 test_expect_success 'push --mirror to repo with alternates' '
319 s=$HTTPD_DOCUMENT_ROOT_PATH/test_repo.git &&
320 d=$HTTPD_DOCUMENT_ROOT_PATH/alternates-mirror.git &&
321 git clone --bare --shared "$s" "$d" &&
322 git --git-dir="$d" config http.receivepack true &&
323 git --git-dir="$d" repack -adl &&
324 git push --mirror "$HTTPD_URL"/smart/alternates-mirror.git
325 '
326
327 test_expect_success TTY 'push shows progress when stderr is a tty' '
328 cd "$ROOT_PATH"/test_repo_clone &&
329 test_commit noisy &&
330 test_terminal git push >output 2>&1 &&
331 test_i18ngrep "^Writing objects" output
332 '
333
334 test_expect_success TTY 'push --quiet silences status and progress' '
335 cd "$ROOT_PATH"/test_repo_clone &&
336 test_commit quiet &&
337 test_terminal git push --quiet >output 2>&1 &&
338 test_must_be_empty output
339 '
340
341 test_expect_success TTY 'push --no-progress silences progress but not status' '
342 cd "$ROOT_PATH"/test_repo_clone &&
343 test_commit no-progress &&
344 test_terminal git push --no-progress >output 2>&1 &&
345 test_i18ngrep "^To http" output &&
346 test_i18ngrep ! "^Writing objects" output
347 '
348
349 test_expect_success 'push --progress shows progress to non-tty' '
350 cd "$ROOT_PATH"/test_repo_clone &&
351 test_commit progress &&
352 git push --progress >output 2>&1 &&
353 test_i18ngrep "^To http" output &&
354 test_i18ngrep "^Writing objects" output
355 '
356
357 test_expect_success 'http push gives sane defaults to reflog' '
358 cd "$ROOT_PATH"/test_repo_clone &&
359 test_commit reflog-test &&
360 git push "$HTTPD_URL"/smart/test_repo.git &&
361 git --git-dir="$HTTPD_DOCUMENT_ROOT_PATH/test_repo.git" \
362 log -g -1 --format="%gn <%ge>" >actual &&
363 echo "anonymous <anonymous@http.127.0.0.1>" >expect &&
364 test_cmp expect actual
365 '
366
367 test_expect_success 'http push respects GIT_COMMITTER_* in reflog' '
368 cd "$ROOT_PATH"/test_repo_clone &&
369 test_commit custom-reflog-test &&
370 git push "$HTTPD_URL"/smart_custom_env/test_repo.git &&
371 git --git-dir="$HTTPD_DOCUMENT_ROOT_PATH/test_repo.git" \
372 log -g -1 --format="%gn <%ge>" >actual &&
373 echo "Custom User <custom@example.com>" >expect &&
374 test_cmp expect actual
375 '
376
377 test_expect_success 'push over smart http with auth' '
378 cd "$ROOT_PATH/test_repo_clone" &&
379 echo push-auth-test >expect &&
380 test_commit push-auth-test &&
381 set_askpass user@host pass@host &&
382 git push "$HTTPD_URL"/auth/smart/test_repo.git &&
383 git --git-dir="$HTTPD_DOCUMENT_ROOT_PATH/test_repo.git" \
384 log -1 --format=%s >actual &&
385 expect_askpass both user@host &&
386 test_cmp expect actual
387 '
388
389 test_expect_success 'push to auth-only-for-push repo' '
390 cd "$ROOT_PATH/test_repo_clone" &&
391 echo push-half-auth >expect &&
392 test_commit push-half-auth &&
393 set_askpass user@host pass@host &&
394 git push "$HTTPD_URL"/auth-push/smart/test_repo.git &&
395 git --git-dir="$HTTPD_DOCUMENT_ROOT_PATH/test_repo.git" \
396 log -1 --format=%s >actual &&
397 expect_askpass both user@host &&
398 test_cmp expect actual
399 '
400
401 test_expect_success 'create repo without http.receivepack set' '
402 cd "$ROOT_PATH" &&
403 git init half-auth &&
404 (
405 cd half-auth &&
406 test_commit one
407 ) &&
408 git clone --bare half-auth "$HTTPD_DOCUMENT_ROOT_PATH/half-auth.git"
409 '
410
411 test_expect_success 'clone via half-auth-complete does not need password' '
412 cd "$ROOT_PATH" &&
413 set_askpass wrong &&
414 git clone "$HTTPD_URL"/half-auth-complete/smart/half-auth.git \
415 half-auth-clone &&
416 expect_askpass none
417 '
418
419 test_expect_success 'push into half-auth-complete requires password' '
420 cd "$ROOT_PATH/half-auth-clone" &&
421 echo two >expect &&
422 test_commit two &&
423 set_askpass user@host pass@host &&
424 git push "$HTTPD_URL/half-auth-complete/smart/half-auth.git" &&
425 git --git-dir="$HTTPD_DOCUMENT_ROOT_PATH/half-auth.git" \
426 log -1 --format=%s >actual &&
427 expect_askpass both user@host &&
428 test_cmp expect actual
429 '
430
431 test_expect_success CMDLINE_LIMIT 'push 2000 tags over http' '
432 sha1=$(git rev-parse HEAD) &&
433 test_seq 2000 |
434 sort |
435 sed "s|.*|$sha1 refs/tags/really-long-tag-name-&|" \
436 >.git/packed-refs &&
437 run_with_limited_cmdline git push --mirror
438 '
439
440 test_expect_success GPG 'push with post-receive to inspect certificate' '
441 test_hook -C "$HTTPD_DOCUMENT_ROOT_PATH"/test_repo.git post-receive <<-\EOF &&
442 # discard the update list
443 cat >/dev/null
444 # record the push certificate
445 if test -n "${GIT_PUSH_CERT-}"
446 then
447 git cat-file blob $GIT_PUSH_CERT >../push-cert
448 fi &&
449 cat >../push-cert-status <<E_O_F
450 SIGNER=${GIT_PUSH_CERT_SIGNER-nobody}
451 KEY=${GIT_PUSH_CERT_KEY-nokey}
452 STATUS=${GIT_PUSH_CERT_STATUS-nostatus}
453 NONCE_STATUS=${GIT_PUSH_CERT_NONCE_STATUS-nononcestatus}
454 NONCE=${GIT_PUSH_CERT_NONCE-nononce}
455 E_O_F
456 EOF
457 (
458 cd "$HTTPD_DOCUMENT_ROOT_PATH"/test_repo.git &&
459 git config receive.certnonceseed sekrit &&
460 git config receive.certnonceslop 30
461 ) &&
462 cd "$ROOT_PATH/test_repo_clone" &&
463 test_commit cert-test &&
464 git push --signed "$HTTPD_URL/smart/test_repo.git" &&
465 (
466 cd "$HTTPD_DOCUMENT_ROOT_PATH" &&
467 cat <<-\EOF &&
468 SIGNER=C O Mitter <committer@example.com>
469 KEY=13B6F51ECDDE430D
470 STATUS=G
471 NONCE_STATUS=OK
472 EOF
473 sed -n -e "s/^nonce /NONCE=/p" -e "/^$/q" push-cert
474 ) >expect &&
475 test_cmp expect "$HTTPD_DOCUMENT_ROOT_PATH/push-cert-status"
476 '
477
478 test_expect_success 'push status output scrubs password' '
479 cd "$ROOT_PATH/test_repo_clone" &&
480 git push --porcelain \
481 "$HTTPD_URL_USER_PASS/smart/test_repo.git" \
482 +HEAD:scrub >status &&
483 # should have been scrubbed down to vanilla URL
484 grep "^To $HTTPD_URL/smart/test_repo.git" status
485 '
486
487 test_expect_success 'clone/fetch scrubs password from reflogs' '
488 cd "$ROOT_PATH" &&
489 git clone "$HTTPD_URL_USER_PASS/smart/test_repo.git" \
490 reflog-test &&
491 cd reflog-test &&
492 test_commit prepare-for-force-fetch &&
493 git switch -c away &&
494 git fetch "$HTTPD_URL_USER_PASS/smart/test_repo.git" \
495 +main:main &&
496 # should have been scrubbed down to vanilla URL
497 git log -g main >reflog &&
498 grep "$HTTPD_URL" reflog &&
499 ! grep "$HTTPD_URL_USER_PASS" reflog
500 '
501
502 test_expect_success 'Non-ASCII branch name can be used with --force-with-lease' '
503 cd "$ROOT_PATH" &&
504 git clone "$HTTPD_URL_USER_PASS/smart/test_repo.git" non-ascii &&
505 cd non-ascii &&
506 git checkout -b rama-de-árbol &&
507 test_commit F &&
508 git push --force-with-lease origin rama-de-árbol &&
509 git ls-remote origin refs/heads/rama-de-árbol >actual &&
510 git ls-remote . refs/heads/rama-de-árbol >expect &&
511 test_cmp expect actual &&
512 git push --delete --force-with-lease origin rama-de-árbol &&
513 git ls-remote origin refs/heads/rama-de-árbol >actual &&
514 test_must_be_empty actual
515 '
516
517 test_expect_success 'colorize errors/hints' '
518 cd "$ROOT_PATH"/test_repo_clone &&
519 test_must_fail git -c color.transport=always -c color.advice=always \
520 -c color.push=always \
521 push origin origin/main^:main 2>act &&
522 test_decode_color <act >decoded &&
523 test_i18ngrep "<RED>.*rejected.*<RESET>" decoded &&
524 test_i18ngrep "<RED>error: failed to push some refs" decoded &&
525 test_i18ngrep "<YELLOW>hint: " decoded &&
526 test_i18ngrep ! "^hint: " decoded
527 '
528
529 test_expect_success 'report error server does not provide ref status' '
530 git init "$HTTPD_DOCUMENT_ROOT_PATH/no_report" &&
531 git -C "$HTTPD_DOCUMENT_ROOT_PATH/no_report" config http.receivepack true &&
532 test_must_fail git push --porcelain \
533 $HTTPD_URL_USER_PASS/smart/no_report \
534 HEAD:refs/tags/will-fail >actual &&
535 test_must_fail git -C "$HTTPD_DOCUMENT_ROOT_PATH/no_report" \
536 rev-parse --verify refs/tags/will-fail &&
537 cat >expect <<-EOF &&
538 To $HTTPD_URL/smart/no_report
539 ! HEAD:refs/tags/will-fail [remote failure] (remote failed to report status)
540 Done
541 EOF
542 test_cmp expect actual
543 '
544
545 test_done