Merge branch 'jc/diffcore-rotate'
[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 already up-to-date' '
84         git push
85 '
86
87 test_expect_success 'create and delete remote branch' '
88         cd "$ROOT_PATH"/test_repo_clone &&
89         git checkout -b dev &&
90         : >path3 &&
91         git add path3 &&
92         test_tick &&
93         git commit -m dev &&
94         git push origin dev &&
95         git push origin :dev &&
96         test_must_fail git show-ref --verify refs/remotes/origin/dev
97 '
98
99 cat >"$HTTPD_DOCUMENT_ROOT_PATH/test_repo.git/hooks/update" <<EOF
100 #!/bin/sh
101 exit 1
102 EOF
103 chmod a+x "$HTTPD_DOCUMENT_ROOT_PATH/test_repo.git/hooks/update"
104
105 cat >exp <<EOF
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'
110 EOF
111
112 test_expect_success 'rejected update prints status' '
113         cd "$ROOT_PATH"/test_repo_clone &&
114         git checkout -b dev2 &&
115         : >path4 &&
116         git add path4 &&
117         test_tick &&
118         git commit -m dev2 &&
119         test_must_fail git push origin dev2 2>act &&
120         sed -e "/^remote: /s/ *$//" <act >cmp &&
121         test_cmp exp cmp
122 '
123 rm -f "$HTTPD_DOCUMENT_ROOT_PATH/test_repo.git/hooks/update"
124
125 cat >exp <<EOF
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
137 EOF
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
142         then
143                 check_access_log exp
144         fi
145 '
146
147 test_http_push_nonff "$HTTPD_DOCUMENT_ROOT_PATH"/test_repo.git \
148         "$ROOT_PATH"/test_repo_clone main               success
149
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 &&
154
155         echo "change changed" > path2 &&
156         git commit -a -m path2 --amend &&
157
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'
161
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
165 '
166
167 test_expect_success 'push fails for non-fast-forward refs unmatched by remote helper: our output' '
168         test_i18ngrep "Updates were rejected because" \
169                 output
170 '
171
172 test_expect_success 'push (chunked)' '
173         git checkout main &&
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))
181 '
182
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 &&
192
193         # Tell "$up" about three branches for now
194         test_commit atomic1 &&
195         test_commit atomic2 &&
196         git branch collateral &&
197         git branch other &&
198         git push "$up" atomic1 main collateral other &&
199         git tag -d atomic1 &&
200
201         # collateral is a valid push, but should be failed by atomic push
202         git checkout collateral &&
203         test_commit collateral1 &&
204
205         # Make main incompatible with upstream to provoke atomic
206         git checkout main &&
207         git reset --hard HEAD^ &&
208
209         # Add a new branch which should be failed by atomic push. This is a
210         # regression case.
211         git branch atomic &&
212
213         # --atomic should cause entire push to be rejected
214         test_must_fail git push --atomic "$up" main atomic collateral 2>output &&
215
216         # the new branch should not have been created upstream
217         test_must_fail git -C "$d" show-ref --verify refs/heads/atomic &&
218
219         # upstream should still reflect atomic2, the last thing we pushed
220         # successfully
221         git rev-parse atomic2 >expected &&
222         # on main...
223         git -C "$d" rev-parse refs/heads/main >actual &&
224         test_cmp expected actual &&
225         # ...and collateral.
226         git -C "$d" rev-parse refs/heads/collateral >actual &&
227         test_cmp expected actual &&
228
229         # the failed refs should be indicated to the user
230         grep "^ ! .*rejected.* main -> main" output &&
231
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 &&
235
236         # never report what we do not push
237         ! grep "^ ! .*rejected.* atomic1 " output &&
238         ! grep "^ ! .*rejected.* other " output
239 '
240
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 &&
246
247         # break ref updates for other on the remote site
248         mkdir "$d/refs/heads/other.lock" &&
249
250         # add the new commit to other
251         git branch -f other collateral &&
252
253         # --atomic should cause entire push to be rejected
254         test_must_fail git push --atomic "$up" atomic other 2>output  &&
255
256         # the new branch should not have been created upstream
257         test_must_fail git -C "$d" show-ref --verify refs/heads/atomic &&
258
259         # upstream should still reflect atomic2, the last thing we pushed
260         # successfully
261         git rev-parse atomic2 >expected &&
262         # ...to other.
263         git -C "$d" rev-parse refs/heads/other >actual &&
264         test_cmp expected actual &&
265
266         # the new branch should not have been created upstream
267         test_must_fail git -C "$d" show-ref --verify refs/heads/atomic &&
268
269         # the failed refs should be indicated to the user
270         grep "^ ! .*rejected.* other -> other .*atomic transaction failed" output &&
271
272         # the collateral failure refs should be indicated to the user
273         grep "^ ! .*rejected.* atomic -> atomic .*atomic transaction failed" output
274 '
275
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
281 '
282
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
288 '
289
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
297 '
298
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
306 '
307
308 test_expect_success TTY 'push shows progress when stderr is a tty' '
309         cd "$ROOT_PATH"/test_repo_clone &&
310         test_commit noisy &&
311         test_terminal git push >output 2>&1 &&
312         test_i18ngrep "^Writing objects" output
313 '
314
315 test_expect_success TTY 'push --quiet silences status and progress' '
316         cd "$ROOT_PATH"/test_repo_clone &&
317         test_commit quiet &&
318         test_terminal git push --quiet >output 2>&1 &&
319         test_must_be_empty output
320 '
321
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
328 '
329
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
336 '
337
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
346 '
347
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
356 '
357
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
368 '
369
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
380 '
381
382 test_expect_success 'create repo without http.receivepack set' '
383         cd "$ROOT_PATH" &&
384         git init half-auth &&
385         (
386                 cd half-auth &&
387                 test_commit one
388         ) &&
389         git clone --bare half-auth "$HTTPD_DOCUMENT_ROOT_PATH/half-auth.git"
390 '
391
392 test_expect_success 'clone via half-auth-complete does not need password' '
393         cd "$ROOT_PATH" &&
394         set_askpass wrong &&
395         git clone "$HTTPD_URL"/half-auth-complete/smart/half-auth.git \
396                 half-auth-clone &&
397         expect_askpass none
398 '
399
400 test_expect_success 'push into half-auth-complete requires password' '
401         cd "$ROOT_PATH/half-auth-clone" &&
402         echo two >expect &&
403         test_commit two &&
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
410 '
411
412 test_expect_success CMDLINE_LIMIT 'push 2000 tags over http' '
413         sha1=$(git rev-parse HEAD) &&
414         test_seq 2000 |
415           sort |
416           sed "s|.*|$sha1 refs/tags/really-long-tag-name-&|" \
417           >.git/packed-refs &&
418         run_with_limited_cmdline git push --mirror
419 '
420
421 test_expect_success GPG 'push with post-receive to inspect certificate' '
422         (
423                 cd "$HTTPD_DOCUMENT_ROOT_PATH"/test_repo.git &&
424                 mkdir -p hooks &&
425                 write_script hooks/post-receive <<-\EOF &&
426                 # discard the update list
427                 cat >/dev/null
428                 # record the push certificate
429                 if test -n "${GIT_PUSH_CERT-}"
430                 then
431                         git cat-file blob $GIT_PUSH_CERT >../push-cert
432                 fi &&
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}
439                 E_O_F
440                 EOF
441
442                 git config receive.certnonceseed sekrit &&
443                 git config receive.certnonceslop 30
444         ) &&
445         cd "$ROOT_PATH/test_repo_clone" &&
446         test_commit cert-test &&
447         git push --signed "$HTTPD_URL/smart/test_repo.git" &&
448         (
449                 cd "$HTTPD_DOCUMENT_ROOT_PATH" &&
450                 cat <<-\EOF &&
451                 SIGNER=C O Mitter <committer@example.com>
452                 KEY=13B6F51ECDDE430D
453                 STATUS=G
454                 NONCE_STATUS=OK
455                 EOF
456                 sed -n -e "s/^nonce /NONCE=/p" -e "/^$/q" push-cert
457         ) >expect &&
458         test_cmp expect "$HTTPD_DOCUMENT_ROOT_PATH/push-cert-status"
459 '
460
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
468 '
469
470 test_expect_success 'clone/fetch scrubs password from reflogs' '
471         cd "$ROOT_PATH" &&
472         git clone "$HTTPD_URL_USER_PASS/smart/test_repo.git" \
473                 reflog-test &&
474         cd reflog-test &&
475         test_commit prepare-for-force-fetch &&
476         git switch -c away &&
477         git fetch "$HTTPD_URL_USER_PASS/smart/test_repo.git" \
478                 +main:main &&
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
483 '
484
485 test_expect_success 'Non-ASCII branch name can be used with --force-with-lease' '
486         cd "$ROOT_PATH" &&
487         git clone "$HTTPD_URL_USER_PASS/smart/test_repo.git" non-ascii &&
488         cd non-ascii &&
489         git checkout -b rama-de-árbol &&
490         test_commit F &&
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
498 '
499
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
510 '
511
512 test_done