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
13 .
"$TEST_DIRECTORY"/lib-gpg.sh
14 .
"$TEST_DIRECTORY"/lib-httpd.sh
15 .
"$TEST_DIRECTORY"/lib-terminal.sh
18 test_expect_success
'setup remote repository' '
26 git commit -m initial &&
28 git clone --bare test_repo test_repo.git &&
30 git config http.receivepack true &&
31 git config core.logallrefupdates true &&
32 ORIG_HEAD=$(git rev-parse --verify HEAD) &&
34 mv test_repo.git "$HTTPD_DOCUMENT_ROOT_PATH"
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
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" &&
48 # In the URL, add a trailing slash, and see if git appends yet another
51 git clone $HTTPD_URL/smart/test_repo.git/ test_repo_clone &&
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
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 &&
65 cd test_repo_clone && git config push.default matching
69 test_expect_success
'push to remote repository (standard)' '
70 cd "$ROOT_PATH"/test_repo_clone &&
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))
83 test_expect_success
'push to remote repository (standard) with sending Accept-Language' '
85 => Send header: Accept-Language: ko-KR, *;q=0.9
86 => Send header: Accept-Language: ko-KR, *;q=0.9
89 cd "$ROOT_PATH"/test_repo_clone &&
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 &&
98 grep "=> Send header: Accept-Language:" err >err.language &&
99 test_cmp exp err.language
102 test_expect_success
'push already up-to-date' '
106 test_expect_success
'create and delete remote branch' '
107 cd "$ROOT_PATH"/test_repo_clone &&
108 git checkout -b dev &&
113 git push origin dev &&
114 git push origin :dev &&
115 test_must_fail git show-ref --verify refs/remotes/origin/dev
118 test_expect_success
'setup rejected update hook' '
119 test_hook --setup -C "$HTTPD_DOCUMENT_ROOT_PATH/test_repo.git" update <<-\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'\''
131 test_expect_success
'rejected update prints status' '
132 cd "$ROOT_PATH"/test_repo_clone &&
133 git checkout -b dev2 &&
137 git commit -m dev2 &&
138 test_must_fail git push origin dev2 2>act &&
139 sed -e "/^remote: /s/ *$//" <act >cmp &&
142 rm -f "$HTTPD_DOCUMENT_ROOT_PATH/test_repo.git/hooks/update"
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
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
166 test_http_push_nonff
"$HTTPD_DOCUMENT_ROOT_PATH"/test_repo.git \
167 "$ROOT_PATH"/test_repo_clone main success
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 &&
174 echo "change changed" > path2 &&
175 git commit -a -m path2 --amend &&
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'
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
186 test_expect_success
'push fails for non-fast-forward refs unmatched by remote helper: our output' '
187 test_i18ngrep "Updates were rejected because" \
191 test_expect_success
'push (chunked)' '
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))
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 &&
212 # Tell "$up" about three branches for now
213 test_commit atomic1 &&
214 test_commit atomic2 &&
215 git branch collateral &&
217 git push "$up" atomic1 main collateral other &&
218 git tag -d atomic1 &&
220 # collateral is a valid push, but should be failed by atomic push
221 git checkout collateral &&
222 test_commit collateral1 &&
224 # Make main incompatible with upstream to provoke atomic
226 git reset --hard HEAD^ &&
228 # Add a new branch which should be failed by atomic push. This is a
232 # --atomic should cause entire push to be rejected
233 test_must_fail git push --atomic "$up" main atomic collateral 2>output &&
235 # the new branch should not have been created upstream
236 test_must_fail git -C "$d" show-ref --verify refs/heads/atomic &&
238 # upstream should still reflect atomic2, the last thing we pushed
240 git rev-parse atomic2 >expected &&
242 git -C "$d" rev-parse refs/heads/main >actual &&
243 test_cmp expected actual &&
245 git -C "$d" rev-parse refs/heads/collateral >actual &&
246 test_cmp expected actual &&
248 # the failed refs should be indicated to the user
249 grep "^ ! .*rejected.* main -> main" output &&
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 &&
255 # never report what we do not push
256 ! grep "^ ! .*rejected.* atomic1 " output &&
257 ! grep "^ ! .*rejected.* other " output
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 &&
266 # break ref updates for other on the remote site
267 mkdir "$d/refs/heads/other.lock" &&
269 # add the new commit to other
270 git branch -f other collateral &&
272 # --atomic should cause entire push to be rejected
273 test_must_fail git push --atomic "$up" atomic other 2>output &&
275 # the new branch should not have been created upstream
276 test_must_fail git -C "$d" show-ref --verify refs/heads/atomic &&
278 # upstream should still reflect atomic2, the last thing we pushed
280 git rev-parse atomic2 >expected &&
282 git -C "$d" rev-parse refs/heads/other >actual &&
283 test_cmp expected actual &&
285 # the new branch should not have been created upstream
286 test_must_fail git -C "$d" show-ref --verify refs/heads/atomic &&
288 # the failed refs should be indicated to the user
289 grep "^ ! .*rejected.* other -> other .*atomic transaction failed" output &&
291 # the collateral failure refs should be indicated to the user
292 grep "^ ! .*rejected.* atomic -> atomic .*atomic transaction failed" output
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
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
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
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
327 test_expect_success TTY
'push shows progress when stderr is a tty' '
328 cd "$ROOT_PATH"/test_repo_clone &&
330 test_terminal git push >output 2>&1 &&
331 test_i18ngrep "^Writing objects" output
334 test_expect_success TTY
'push --quiet silences status and progress' '
335 cd "$ROOT_PATH"/test_repo_clone &&
337 test_terminal git push --quiet >output 2>&1 &&
338 test_must_be_empty output
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
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
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
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
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
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
401 test_expect_success
'create repo without http.receivepack set' '
403 git init half-auth &&
408 git clone --bare half-auth "$HTTPD_DOCUMENT_ROOT_PATH/half-auth.git"
411 test_expect_success
'clone via half-auth-complete does not need password' '
414 git clone "$HTTPD_URL"/half-auth-complete/smart/half-auth.git \
419 test_expect_success
'push into half-auth-complete requires password' '
420 cd "$ROOT_PATH/half-auth-clone" &&
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
431 test_expect_success CMDLINE_LIMIT
'push 2000 tags over http' '
432 sha1=$(git rev-parse HEAD) &&
435 sed "s|.*|$sha1 refs/tags/really-long-tag-name-&|" \
437 run_with_limited_cmdline git push --mirror
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
444 # record the push certificate
445 if test -n "${GIT_PUSH_CERT-}"
447 git cat-file blob $GIT_PUSH_CERT >../push-cert
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}
458 cd "$HTTPD_DOCUMENT_ROOT_PATH"/test_repo.git &&
459 git config receive.certnonceseed sekrit &&
460 git config receive.certnonceslop 30
462 cd "$ROOT_PATH/test_repo_clone" &&
463 test_commit cert-test &&
464 git push --signed "$HTTPD_URL/smart/test_repo.git" &&
466 cd "$HTTPD_DOCUMENT_ROOT_PATH" &&
468 SIGNER=C O Mitter <committer@example.com>
473 sed -n -e "s/^nonce /NONCE=/p" -e "/^$/q" push-cert
475 test_cmp expect "$HTTPD_DOCUMENT_ROOT_PATH/push-cert-status"
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
487 test_expect_success
'clone/fetch scrubs password from reflogs' '
489 git clone "$HTTPD_URL_USER_PASS/smart/test_repo.git" \
492 test_commit prepare-for-force-fetch &&
493 git switch -c away &&
494 git fetch "$HTTPD_URL_USER_PASS/smart/test_repo.git" \
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
502 test_expect_success
'Non-ASCII branch name can be used with --force-with-lease' '
504 git clone "$HTTPD_URL_USER_PASS/smart/test_repo.git" non-ascii &&
506 git checkout -b rama-de-árbol &&
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
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
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)
542 test_cmp expect actual