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 already up-to-date' '
87 test_expect_success
'create and delete remote branch' '
88 cd "$ROOT_PATH"/test_repo_clone &&
89 git checkout -b dev &&
94 git push origin dev &&
95 git push origin :dev &&
96 test_must_fail git show-ref --verify refs/remotes/origin/dev
99 cat >"$HTTPD_DOCUMENT_ROOT_PATH/test_repo.git/hooks/update" <<EOF
103 chmod a
+x
"$HTTPD_DOCUMENT_ROOT_PATH/test_repo.git/hooks/update"
106 remote: error: hook declined to update refs/heads/dev2
107 To http://127.0.0.1:$LIB_HTTPD_PORT/smart/test_repo.git
108 ! [remote rejected] dev2 -> dev2 (hook declined)
109 error: failed to push some refs to 'http://127.0.0.1:$LIB_HTTPD_PORT/smart/test_repo.git'
112 test_expect_success
'rejected update prints status' '
113 cd "$ROOT_PATH"/test_repo_clone &&
114 git checkout -b dev2 &&
118 git commit -m dev2 &&
119 test_must_fail git push origin dev2 2>act &&
120 sed -e "/^remote: /s/ *$//" <act >cmp &&
123 rm -f "$HTTPD_DOCUMENT_ROOT_PATH/test_repo.git/hooks/update"
126 GET /smart/test_repo.git/info/refs?service=git-upload-pack HTTP/1.1 200
127 POST /smart/test_repo.git/git-upload-pack HTTP/1.1 200
128 GET /smart/test_repo.git/info/refs?service=git-receive-pack HTTP/1.1 200
129 POST /smart/test_repo.git/git-receive-pack HTTP/1.1 200
130 GET /smart/test_repo.git/info/refs?service=git-receive-pack HTTP/1.1 200
131 GET /smart/test_repo.git/info/refs?service=git-receive-pack HTTP/1.1 200
132 POST /smart/test_repo.git/git-receive-pack HTTP/1.1 200
133 GET /smart/test_repo.git/info/refs?service=git-receive-pack HTTP/1.1 200
134 POST /smart/test_repo.git/git-receive-pack HTTP/1.1 200
135 GET /smart/test_repo.git/info/refs?service=git-receive-pack HTTP/1.1 200
136 POST /smart/test_repo.git/git-receive-pack HTTP/1.1 200
138 test_expect_success
'used receive-pack service' '
139 # NEEDSWORK: If the overspecification of the expected result is reduced, we
140 # might be able to run this test in all protocol versions.
141 if test "$GIT_TEST_PROTOCOL_VERSION" = 0
147 test_http_push_nonff
"$HTTPD_DOCUMENT_ROOT_PATH"/test_repo.git \
148 "$ROOT_PATH"/test_repo_clone main success
150 test_expect_success
'push fails for non-fast-forward refs unmatched by remote helper' '
151 # create a dissimilarly-named remote ref so that git is unable to match the
152 # two refs (viz. local, remote) unless an explicit refspec is provided.
153 git push origin main:niam &&
155 echo "change changed" > path2 &&
156 git commit -a -m path2 --amend &&
158 # push main too; this ensures there is at least one '"'push'"' command to
159 # the remote helper and triggers interaction with the helper.
160 test_must_fail git push -v origin +main main:niam >output 2>&1'
162 test_expect_success
'push fails for non-fast-forward refs unmatched by remote helper: remote output' '
163 grep "^ + [a-f0-9]*\.\.\.[a-f0-9]* *main -> main (forced update)$" output &&
164 grep "^ ! \[rejected\] *main -> niam (non-fast-forward)$" output
167 test_expect_success
'push fails for non-fast-forward refs unmatched by remote helper: our output' '
168 test_i18ngrep "Updates were rejected because" \
172 test_expect_success
'push (chunked)' '
174 test_commit commit path3 &&
175 HEAD=$(git rev-parse --verify HEAD) &&
176 test_config http.postbuffer 4 &&
177 git push -v -v origin $BRANCH 2>err &&
178 grep "POST git-receive-pack (chunked)" err &&
179 (cd "$HTTPD_DOCUMENT_ROOT_PATH"/test_repo.git &&
180 test $HEAD = $(git rev-parse --verify HEAD))
183 ## References of remote: atomic1(1) main(2) collateral(2) other(2)
184 ## References of local : atomic2(2) main(1) collateral(3) other(2) collateral1(3) atomic(1)
185 ## Atomic push : main(1) collateral(3) atomic(1)
186 test_expect_success
'push --atomic also prevents branch creation, reports collateral' '
187 # Setup upstream repo - empty for now
188 d=$HTTPD_DOCUMENT_ROOT_PATH/atomic-branches.git &&
189 git init --bare "$d" &&
190 test_config -C "$d" http.receivepack true &&
191 up="$HTTPD_URL"/smart/atomic-branches.git &&
193 # Tell "$up" about three branches for now
194 test_commit atomic1 &&
195 test_commit atomic2 &&
196 git branch collateral &&
198 git push "$up" atomic1 main collateral other &&
199 git tag -d atomic1 &&
201 # collateral is a valid push, but should be failed by atomic push
202 git checkout collateral &&
203 test_commit collateral1 &&
205 # Make main incompatible with upstream to provoke atomic
207 git reset --hard HEAD^ &&
209 # Add a new branch which should be failed by atomic push. This is a
213 # --atomic should cause entire push to be rejected
214 test_must_fail git push --atomic "$up" main atomic collateral 2>output &&
216 # the new branch should not have been created upstream
217 test_must_fail git -C "$d" show-ref --verify refs/heads/atomic &&
219 # upstream should still reflect atomic2, the last thing we pushed
221 git rev-parse atomic2 >expected &&
223 git -C "$d" rev-parse refs/heads/main >actual &&
224 test_cmp expected actual &&
226 git -C "$d" rev-parse refs/heads/collateral >actual &&
227 test_cmp expected actual &&
229 # the failed refs should be indicated to the user
230 grep "^ ! .*rejected.* main -> main" output &&
232 # the collateral failure refs should be indicated to the user
233 grep "^ ! .*rejected.* atomic -> atomic .*atomic push failed" output &&
234 grep "^ ! .*rejected.* collateral -> collateral .*atomic push failed" output &&
236 # never report what we do not push
237 ! grep "^ ! .*rejected.* atomic1 " output &&
238 ! grep "^ ! .*rejected.* other " output
241 test_expect_success
'push --atomic fails on server-side errors' '
242 # Use previously set up repository
243 d=$HTTPD_DOCUMENT_ROOT_PATH/atomic-branches.git &&
244 test_config -C "$d" http.receivepack true &&
245 up="$HTTPD_URL"/smart/atomic-branches.git &&
247 # break ref updates for other on the remote site
248 mkdir "$d/refs/heads/other.lock" &&
250 # add the new commit to other
251 git branch -f other collateral &&
253 # --atomic should cause entire push to be rejected
254 test_must_fail git push --atomic "$up" atomic other 2>output &&
256 # the new branch should not have been created upstream
257 test_must_fail git -C "$d" show-ref --verify refs/heads/atomic &&
259 # upstream should still reflect atomic2, the last thing we pushed
261 git rev-parse atomic2 >expected &&
263 git -C "$d" rev-parse refs/heads/other >actual &&
264 test_cmp expected actual &&
266 # the new branch should not have been created upstream
267 test_must_fail git -C "$d" show-ref --verify refs/heads/atomic &&
269 # the failed refs should be indicated to the user
270 grep "^ ! .*rejected.* other -> other .*atomic transaction failed" output &&
272 # the collateral failure refs should be indicated to the user
273 grep "^ ! .*rejected.* atomic -> atomic .*atomic transaction failed" output
276 test_expect_success
'push --all can push to empty repo' '
277 d=$HTTPD_DOCUMENT_ROOT_PATH/empty-all.git &&
278 git init --bare "$d" &&
279 git --git-dir="$d" config http.receivepack true &&
280 git push --all "$HTTPD_URL"/smart/empty-all.git
283 test_expect_success
'push --mirror can push to empty repo' '
284 d=$HTTPD_DOCUMENT_ROOT_PATH/empty-mirror.git &&
285 git init --bare "$d" &&
286 git --git-dir="$d" config http.receivepack true &&
287 git push --mirror "$HTTPD_URL"/smart/empty-mirror.git
290 test_expect_success
'push --all to repo with alternates' '
291 s=$HTTPD_DOCUMENT_ROOT_PATH/test_repo.git &&
292 d=$HTTPD_DOCUMENT_ROOT_PATH/alternates-all.git &&
293 git clone --bare --shared "$s" "$d" &&
294 git --git-dir="$d" config http.receivepack true &&
295 git --git-dir="$d" repack -adl &&
296 git push --all "$HTTPD_URL"/smart/alternates-all.git
299 test_expect_success
'push --mirror to repo with alternates' '
300 s=$HTTPD_DOCUMENT_ROOT_PATH/test_repo.git &&
301 d=$HTTPD_DOCUMENT_ROOT_PATH/alternates-mirror.git &&
302 git clone --bare --shared "$s" "$d" &&
303 git --git-dir="$d" config http.receivepack true &&
304 git --git-dir="$d" repack -adl &&
305 git push --mirror "$HTTPD_URL"/smart/alternates-mirror.git
308 test_expect_success TTY
'push shows progress when stderr is a tty' '
309 cd "$ROOT_PATH"/test_repo_clone &&
311 test_terminal git push >output 2>&1 &&
312 test_i18ngrep "^Writing objects" output
315 test_expect_success TTY
'push --quiet silences status and progress' '
316 cd "$ROOT_PATH"/test_repo_clone &&
318 test_terminal git push --quiet >output 2>&1 &&
319 test_must_be_empty output
322 test_expect_success TTY
'push --no-progress silences progress but not status' '
323 cd "$ROOT_PATH"/test_repo_clone &&
324 test_commit no-progress &&
325 test_terminal git push --no-progress >output 2>&1 &&
326 test_i18ngrep "^To http" output &&
327 test_i18ngrep ! "^Writing objects" output
330 test_expect_success
'push --progress shows progress to non-tty' '
331 cd "$ROOT_PATH"/test_repo_clone &&
332 test_commit progress &&
333 git push --progress >output 2>&1 &&
334 test_i18ngrep "^To http" output &&
335 test_i18ngrep "^Writing objects" output
338 test_expect_success
'http push gives sane defaults to reflog' '
339 cd "$ROOT_PATH"/test_repo_clone &&
340 test_commit reflog-test &&
341 git push "$HTTPD_URL"/smart/test_repo.git &&
342 git --git-dir="$HTTPD_DOCUMENT_ROOT_PATH/test_repo.git" \
343 log -g -1 --format="%gn <%ge>" >actual &&
344 echo "anonymous <anonymous@http.127.0.0.1>" >expect &&
345 test_cmp expect actual
348 test_expect_success
'http push respects GIT_COMMITTER_* in reflog' '
349 cd "$ROOT_PATH"/test_repo_clone &&
350 test_commit custom-reflog-test &&
351 git push "$HTTPD_URL"/smart_custom_env/test_repo.git &&
352 git --git-dir="$HTTPD_DOCUMENT_ROOT_PATH/test_repo.git" \
353 log -g -1 --format="%gn <%ge>" >actual &&
354 echo "Custom User <custom@example.com>" >expect &&
355 test_cmp expect actual
358 test_expect_success
'push over smart http with auth' '
359 cd "$ROOT_PATH/test_repo_clone" &&
360 echo push-auth-test >expect &&
361 test_commit push-auth-test &&
362 set_askpass user@host pass@host &&
363 git push "$HTTPD_URL"/auth/smart/test_repo.git &&
364 git --git-dir="$HTTPD_DOCUMENT_ROOT_PATH/test_repo.git" \
365 log -1 --format=%s >actual &&
366 expect_askpass both user@host &&
367 test_cmp expect actual
370 test_expect_success
'push to auth-only-for-push repo' '
371 cd "$ROOT_PATH/test_repo_clone" &&
372 echo push-half-auth >expect &&
373 test_commit push-half-auth &&
374 set_askpass user@host pass@host &&
375 git push "$HTTPD_URL"/auth-push/smart/test_repo.git &&
376 git --git-dir="$HTTPD_DOCUMENT_ROOT_PATH/test_repo.git" \
377 log -1 --format=%s >actual &&
378 expect_askpass both user@host &&
379 test_cmp expect actual
382 test_expect_success
'create repo without http.receivepack set' '
384 git init half-auth &&
389 git clone --bare half-auth "$HTTPD_DOCUMENT_ROOT_PATH/half-auth.git"
392 test_expect_success
'clone via half-auth-complete does not need password' '
395 git clone "$HTTPD_URL"/half-auth-complete/smart/half-auth.git \
400 test_expect_success
'push into half-auth-complete requires password' '
401 cd "$ROOT_PATH/half-auth-clone" &&
404 set_askpass user@host pass@host &&
405 git push "$HTTPD_URL/half-auth-complete/smart/half-auth.git" &&
406 git --git-dir="$HTTPD_DOCUMENT_ROOT_PATH/half-auth.git" \
407 log -1 --format=%s >actual &&
408 expect_askpass both user@host &&
409 test_cmp expect actual
412 test_expect_success CMDLINE_LIMIT
'push 2000 tags over http' '
413 sha1=$(git rev-parse HEAD) &&
416 sed "s|.*|$sha1 refs/tags/really-long-tag-name-&|" \
418 run_with_limited_cmdline git push --mirror
421 test_expect_success GPG
'push with post-receive to inspect certificate' '
423 cd "$HTTPD_DOCUMENT_ROOT_PATH"/test_repo.git &&
425 write_script hooks/post-receive <<-\EOF &&
426 # discard the update list
428 # record the push certificate
429 if test -n "${GIT_PUSH_CERT-}"
431 git cat-file blob $GIT_PUSH_CERT >../push-cert
433 cat >../push-cert-status <<E_O_F
434 SIGNER=${GIT_PUSH_CERT_SIGNER-nobody}
435 KEY=${GIT_PUSH_CERT_KEY-nokey}
436 STATUS=${GIT_PUSH_CERT_STATUS-nostatus}
437 NONCE_STATUS=${GIT_PUSH_CERT_NONCE_STATUS-nononcestatus}
438 NONCE=${GIT_PUSH_CERT_NONCE-nononce}
442 git config receive.certnonceseed sekrit &&
443 git config receive.certnonceslop 30
445 cd "$ROOT_PATH/test_repo_clone" &&
446 test_commit cert-test &&
447 git push --signed "$HTTPD_URL/smart/test_repo.git" &&
449 cd "$HTTPD_DOCUMENT_ROOT_PATH" &&
451 SIGNER=C O Mitter <committer@example.com>
456 sed -n -e "s/^nonce /NONCE=/p" -e "/^$/q" push-cert
458 test_cmp expect "$HTTPD_DOCUMENT_ROOT_PATH/push-cert-status"
461 test_expect_success
'push status output scrubs password' '
462 cd "$ROOT_PATH/test_repo_clone" &&
463 git push --porcelain \
464 "$HTTPD_URL_USER_PASS/smart/test_repo.git" \
465 +HEAD:scrub >status &&
466 # should have been scrubbed down to vanilla URL
467 grep "^To $HTTPD_URL/smart/test_repo.git" status
470 test_expect_success
'clone/fetch scrubs password from reflogs' '
472 git clone "$HTTPD_URL_USER_PASS/smart/test_repo.git" \
475 test_commit prepare-for-force-fetch &&
476 git switch -c away &&
477 git fetch "$HTTPD_URL_USER_PASS/smart/test_repo.git" \
479 # should have been scrubbed down to vanilla URL
480 git log -g main >reflog &&
481 grep "$HTTPD_URL" reflog &&
482 ! grep "$HTTPD_URL_USER_PASS" reflog
485 test_expect_success
'Non-ASCII branch name can be used with --force-with-lease' '
487 git clone "$HTTPD_URL_USER_PASS/smart/test_repo.git" non-ascii &&
489 git checkout -b rama-de-árbol &&
491 git push --force-with-lease origin rama-de-árbol &&
492 git ls-remote origin refs/heads/rama-de-árbol >actual &&
493 git ls-remote . refs/heads/rama-de-árbol >expect &&
494 test_cmp expect actual &&
495 git push --delete --force-with-lease origin rama-de-árbol &&
496 git ls-remote origin refs/heads/rama-de-árbol >actual &&
497 test_must_be_empty actual
500 test_expect_success
'colorize errors/hints' '
501 cd "$ROOT_PATH"/test_repo_clone &&
502 test_must_fail git -c color.transport=always -c color.advice=always \
503 -c color.push=always \
504 push origin origin/main^:main 2>act &&
505 test_decode_color <act >decoded &&
506 test_i18ngrep "<RED>.*rejected.*<RESET>" decoded &&
507 test_i18ngrep "<RED>error: failed to push some refs" decoded &&
508 test_i18ngrep "<YELLOW>hint: " decoded &&
509 test_i18ngrep ! "^hint: " decoded
512 test_expect_success
'report error server does not provide ref status' '
513 git init "$HTTPD_DOCUMENT_ROOT_PATH/no_report" &&
514 git -C "$HTTPD_DOCUMENT_ROOT_PATH/no_report" config http.receivepack true &&
515 test_must_fail git push --porcelain \
516 $HTTPD_URL_USER_PASS/smart/no_report \
517 HEAD:refs/tags/will-fail >actual &&
518 test_must_fail git -C "$HTTPD_DOCUMENT_ROOT_PATH/no_report" \
519 rev-parse --verify refs/tags/will-fail &&
520 cat >expect <<-EOF &&
521 To $HTTPD_URL/smart/no_report
522 ! HEAD:refs/tags/will-fail [remote failure] (remote failed to report status)
525 test_cmp expect actual