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