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