signed push: teach smart-HTTP to pass "git push --signed" around
[git/git.git] / t / t5541-http-push-smart.sh
CommitLineData
7da4e228
SP
1#!/bin/sh
2#
3# Copyright (c) 2008 Clemens Buchacher <drizzd@aon.at>
4#
5
6test_description='test smart pushing over http via http-backend'
7. ./test-lib.sh
8
9if test -n "$NO_CURL"; then
fadb5156 10 skip_all='skipping test, git built without http support'
7da4e228
SP
11 test_done
12fi
13
14ROOT_PATH="$PWD"
0ea47f9d 15. "$TEST_DIRECTORY"/lib-gpg.sh
7da4e228 16. "$TEST_DIRECTORY"/lib-httpd.sh
d336572f 17. "$TEST_DIRECTORY"/lib-terminal.sh
7da4e228
SP
18start_httpd
19
20test_expect_success 'setup remote repository' '
21 cd "$ROOT_PATH" &&
22 mkdir test_repo &&
23 cd test_repo &&
24 git init &&
25 : >path1 &&
26 git add path1 &&
27 test_tick &&
28 git commit -m initial &&
29 cd - &&
30 git clone --bare test_repo test_repo.git &&
31 cd test_repo.git &&
32 git config http.receivepack true &&
e32a4581 33 git config core.logallrefupdates true &&
7da4e228
SP
34 ORIG_HEAD=$(git rev-parse --verify HEAD) &&
35 cd - &&
36 mv test_repo.git "$HTTPD_DOCUMENT_ROOT_PATH"
37'
38
6ac2b3ae
JK
39setup_askpass_helper
40
9ee6bcd3
TRC
41cat >exp <<EOF
42GET /smart/test_repo.git/info/refs?service=git-upload-pack HTTP/1.1 200
43POST /smart/test_repo.git/git-upload-pack HTTP/1.1 200
44EOF
d8fab072 45test_expect_success 'no empty path components' '
9ee6bcd3
TRC
46 # In the URL, add a trailing slash, and see if git appends yet another
47 # slash.
7da4e228 48 cd "$ROOT_PATH" &&
9ee6bcd3
TRC
49 git clone $HTTPD_URL/smart/test_repo.git/ test_repo_clone &&
50
51 sed -e "
52 s/^.* \"//
53 s/\"//
54 s/ [1-9][0-9]*\$//
55 s/^GET /GET /
56 " >act <"$HTTPD_ROOT_PATH"/access.log &&
57
58 # Clear the log, so that it does not affect the "used receive-pack
59 # service" test which reads the log too.
60 #
61 # We do this before the actual comparison to ensure the log is cleared.
62 echo > "$HTTPD_ROOT_PATH"/access.log &&
63
64 test_cmp exp act
65'
66
67test_expect_success 'clone remote repository' '
68 rm -rf test_repo_clone &&
f2c2c901
MM
69 git clone $HTTPD_URL/smart/test_repo.git test_repo_clone &&
70 (
71 cd test_repo_clone && git config push.default matching
72 )
7da4e228
SP
73'
74
0cc6330d 75test_expect_success 'push to remote repository (standard)' '
7da4e228
SP
76 cd "$ROOT_PATH"/test_repo_clone &&
77 : >path2 &&
78 git add path2 &&
79 test_tick &&
80 git commit -m path2 &&
81 HEAD=$(git rev-parse --verify HEAD) &&
0cc6330d
TRC
82 GIT_CURL_VERBOSE=1 git push -v -v 2>err &&
83 ! grep "Expect: 100-continue" err &&
84 grep "POST git-receive-pack ([0-9]* bytes)" err &&
7da4e228
SP
85 (cd "$HTTPD_DOCUMENT_ROOT_PATH"/test_repo.git &&
86 test $HEAD = $(git rev-parse --verify HEAD))
87'
88
89test_expect_success 'push already up-to-date' '
90 git push
91'
92
93test_expect_success 'create and delete remote branch' '
94 cd "$ROOT_PATH"/test_repo_clone &&
95 git checkout -b dev &&
96 : >path3 &&
97 git add path3 &&
98 test_tick &&
99 git commit -m dev &&
100 git push origin dev &&
101 git push origin :dev &&
102 test_must_fail git show-ref --verify refs/remotes/origin/dev
103'
104
5238cbf6
SP
105cat >"$HTTPD_DOCUMENT_ROOT_PATH/test_repo.git/hooks/update" <<EOF
106#!/bin/sh
107exit 1
108EOF
109chmod a+x "$HTTPD_DOCUMENT_ROOT_PATH/test_repo.git/hooks/update"
110
111cat >exp <<EOF
112remote: error: hook declined to update refs/heads/dev2
113To http://127.0.0.1:$LIB_HTTPD_PORT/smart/test_repo.git
114 ! [remote rejected] dev2 -> dev2 (hook declined)
d202a513 115error: failed to push some refs to 'http://127.0.0.1:$LIB_HTTPD_PORT/smart/test_repo.git'
5238cbf6
SP
116EOF
117
118test_expect_success 'rejected update prints status' '
119 cd "$ROOT_PATH"/test_repo_clone &&
120 git checkout -b dev2 &&
121 : >path4 &&
122 git add path4 &&
123 test_tick &&
124 git commit -m dev2 &&
125 test_must_fail git push origin dev2 2>act &&
126 sed -e "/^remote: /s/ *$//" <act >cmp &&
127 test_cmp exp cmp
128'
129rm -f "$HTTPD_DOCUMENT_ROOT_PATH/test_repo.git/hooks/update"
130
7da4e228 131cat >exp <<EOF
9ee6bcd3 132
7da4e228
SP
133GET /smart/test_repo.git/info/refs?service=git-upload-pack HTTP/1.1 200
134POST /smart/test_repo.git/git-upload-pack HTTP/1.1 200
135GET /smart/test_repo.git/info/refs?service=git-receive-pack HTTP/1.1 200
136POST /smart/test_repo.git/git-receive-pack HTTP/1.1 200
137GET /smart/test_repo.git/info/refs?service=git-receive-pack HTTP/1.1 200
138GET /smart/test_repo.git/info/refs?service=git-receive-pack HTTP/1.1 200
139POST /smart/test_repo.git/git-receive-pack HTTP/1.1 200
140GET /smart/test_repo.git/info/refs?service=git-receive-pack HTTP/1.1 200
141POST /smart/test_repo.git/git-receive-pack HTTP/1.1 200
5238cbf6
SP
142GET /smart/test_repo.git/info/refs?service=git-receive-pack HTTP/1.1 200
143POST /smart/test_repo.git/git-receive-pack HTTP/1.1 200
7da4e228
SP
144EOF
145test_expect_success 'used receive-pack service' '
146 sed -e "
147 s/^.* \"//
148 s/\"//
149 s/ [1-9][0-9]*\$//
150 s/^GET /GET /
151 " >act <"$HTTPD_ROOT_PATH"/access.log &&
152 test_cmp exp act
153'
154
6cbd6e92 155test_http_push_nonff "$HTTPD_DOCUMENT_ROOT_PATH"/test_repo.git \
05c1eb10 156 "$ROOT_PATH"/test_repo_clone master success
19452374 157
08d63a42 158test_expect_success 'push fails for non-fast-forward refs unmatched by remote helper' '
7b69079b
TRC
159 # create a dissimilarly-named remote ref so that git is unable to match the
160 # two refs (viz. local, remote) unless an explicit refspec is provided.
161 git push origin master:retsam
162
163 echo "change changed" > path2 &&
164 git commit -a -m path2 --amend &&
165
166 # push master too; this ensures there is at least one '"'push'"' command to
167 # the remote helper and triggers interaction with the helper.
b32227e7 168 test_must_fail git push -v origin +master master:retsam >output 2>&1'
7b69079b 169
b32227e7 170test_expect_success 'push fails for non-fast-forward refs unmatched by remote helper: remote output' '
7b69079b 171 grep "^ + [a-f0-9]*\.\.\.[a-f0-9]* *master -> master (forced update)$" output &&
b32227e7
ÆAB
172 grep "^ ! \[rejected\] *master -> retsam (non-fast-forward)$" output
173'
7b69079b 174
b3e1900a 175test_expect_success 'push fails for non-fast-forward refs unmatched by remote helper: our output' '
0aff719f 176 test_i18ngrep "Updates were rejected because" \
9567f082 177 output
7b69079b
TRC
178'
179
0cc6330d
TRC
180test_expect_success 'push (chunked)' '
181 git checkout master &&
182 test_commit commit path3 &&
183 HEAD=$(git rev-parse --verify HEAD) &&
8677b777 184 test_config http.postbuffer 4 &&
0cc6330d
TRC
185 git push -v -v origin $BRANCH 2>err &&
186 grep "POST git-receive-pack (chunked)" err &&
187 (cd "$HTTPD_DOCUMENT_ROOT_PATH"/test_repo.git &&
188 test $HEAD = $(git rev-parse --verify HEAD))
189'
190
02f79147
JK
191test_expect_success 'push --all can push to empty repo' '
192 d=$HTTPD_DOCUMENT_ROOT_PATH/empty-all.git &&
193 git init --bare "$d" &&
194 git --git-dir="$d" config http.receivepack true &&
195 git push --all "$HTTPD_URL"/smart/empty-all.git
196'
197
198test_expect_success 'push --mirror can push to empty repo' '
199 d=$HTTPD_DOCUMENT_ROOT_PATH/empty-mirror.git &&
200 git init --bare "$d" &&
201 git --git-dir="$d" config http.receivepack true &&
202 git push --mirror "$HTTPD_URL"/smart/empty-mirror.git
203'
204
205test_expect_success 'push --all to repo with alternates' '
206 s=$HTTPD_DOCUMENT_ROOT_PATH/test_repo.git &&
207 d=$HTTPD_DOCUMENT_ROOT_PATH/alternates-all.git &&
208 git clone --bare --shared "$s" "$d" &&
209 git --git-dir="$d" config http.receivepack true &&
210 git --git-dir="$d" repack -adl &&
211 git push --all "$HTTPD_URL"/smart/alternates-all.git
212'
213
214test_expect_success 'push --mirror to repo with alternates' '
215 s=$HTTPD_DOCUMENT_ROOT_PATH/test_repo.git &&
216 d=$HTTPD_DOCUMENT_ROOT_PATH/alternates-mirror.git &&
217 git clone --bare --shared "$s" "$d" &&
218 git --git-dir="$d" config http.receivepack true &&
219 git --git-dir="$d" repack -adl &&
220 git push --mirror "$HTTPD_URL"/smart/alternates-mirror.git
221'
222
e304aeba
JK
223test_expect_success TTY 'push shows progress when stderr is a tty' '
224 cd "$ROOT_PATH"/test_repo_clone &&
225 test_commit noisy &&
226 test_terminal git push >output 2>&1 &&
227 grep "^Writing objects" output
228'
229
230test_expect_success TTY 'push --quiet silences status and progress' '
c207e34f
CB
231 cd "$ROOT_PATH"/test_repo_clone &&
232 test_commit quiet &&
e304aeba 233 test_terminal git push --quiet >output 2>&1 &&
c207e34f
CB
234 test_cmp /dev/null output
235'
236
e304aeba
JK
237test_expect_success TTY 'push --no-progress silences progress but not status' '
238 cd "$ROOT_PATH"/test_repo_clone &&
239 test_commit no-progress &&
240 test_terminal git push --no-progress >output 2>&1 &&
241 grep "^To http" output &&
242 ! grep "^Writing objects"
243'
244
245test_expect_success 'push --progress shows progress to non-tty' '
246 cd "$ROOT_PATH"/test_repo_clone &&
247 test_commit progress &&
248 git push --progress >output 2>&1 &&
249 grep "^To http" output &&
250 grep "^Writing objects" output
251'
252
e32a4581
JK
253test_expect_success 'http push gives sane defaults to reflog' '
254 cd "$ROOT_PATH"/test_repo_clone &&
255 test_commit reflog-test &&
256 git push "$HTTPD_URL"/smart/test_repo.git &&
257 git --git-dir="$HTTPD_DOCUMENT_ROOT_PATH/test_repo.git" \
258 log -g -1 --format="%gn <%ge>" >actual &&
259 echo "anonymous <anonymous@http.127.0.0.1>" >expect &&
260 test_cmp expect actual
261'
262
263test_expect_success 'http push respects GIT_COMMITTER_* in reflog' '
264 cd "$ROOT_PATH"/test_repo_clone &&
265 test_commit custom-reflog-test &&
266 git push "$HTTPD_URL"/smart_custom_env/test_repo.git &&
267 git --git-dir="$HTTPD_DOCUMENT_ROOT_PATH/test_repo.git" \
268 log -g -1 --format="%gn <%ge>" >actual &&
269 echo "Custom User <custom@example.com>" >expect &&
270 test_cmp expect actual
271'
272
6ac2b3ae
JK
273test_expect_success 'push over smart http with auth' '
274 cd "$ROOT_PATH/test_repo_clone" &&
275 echo push-auth-test >expect &&
276 test_commit push-auth-test &&
afbf5ca5 277 set_askpass user@host pass@host &&
6ac2b3ae
JK
278 git push "$HTTPD_URL"/auth/smart/test_repo.git &&
279 git --git-dir="$HTTPD_DOCUMENT_ROOT_PATH/test_repo.git" \
280 log -1 --format=%s >actual &&
281 expect_askpass both user@host &&
282 test_cmp expect actual
283'
284
b81401c1 285test_expect_success 'push to auth-only-for-push repo' '
4c71009d
JK
286 cd "$ROOT_PATH/test_repo_clone" &&
287 echo push-half-auth >expect &&
288 test_commit push-half-auth &&
afbf5ca5 289 set_askpass user@host pass@host &&
4c71009d
JK
290 git push "$HTTPD_URL"/auth-push/smart/test_repo.git &&
291 git --git-dir="$HTTPD_DOCUMENT_ROOT_PATH/test_repo.git" \
292 log -1 --format=%s >actual &&
293 expect_askpass both user@host &&
294 test_cmp expect actual
295'
296
b0808819
JK
297test_expect_success 'create repo without http.receivepack set' '
298 cd "$ROOT_PATH" &&
299 git init half-auth &&
300 (
301 cd half-auth &&
302 test_commit one
303 ) &&
304 git clone --bare half-auth "$HTTPD_DOCUMENT_ROOT_PATH/half-auth.git"
305'
306
307test_expect_success 'clone via half-auth-complete does not need password' '
308 cd "$ROOT_PATH" &&
309 set_askpass wrong &&
310 git clone "$HTTPD_URL"/half-auth-complete/smart/half-auth.git \
311 half-auth-clone &&
312 expect_askpass none
313'
314
315test_expect_success 'push into half-auth-complete requires password' '
316 cd "$ROOT_PATH/half-auth-clone" &&
317 echo two >expect &&
318 test_commit two &&
afbf5ca5 319 set_askpass user@host pass@host &&
b0808819
JK
320 git push "$HTTPD_URL/half-auth-complete/smart/half-auth.git" &&
321 git --git-dir="$HTTPD_DOCUMENT_ROOT_PATH/half-auth.git" \
322 log -1 --format=%s >actual &&
323 expect_askpass both user@host &&
324 test_cmp expect actual
325'
326
0ea47f9d
JH
327test_expect_success GPG 'push with post-receive to inspect certificate' '
328 (
329 cd "$HTTPD_DOCUMENT_ROOT_PATH"/test_repo.git &&
330 mkdir -p hooks &&
331 write_script hooks/post-receive <<-\EOF &&
332 # discard the update list
333 cat >/dev/null
334 # record the push certificate
335 if test -n "${GIT_PUSH_CERT-}"
336 then
337 git cat-file blob $GIT_PUSH_CERT >../push-cert
338 fi &&
339 cat >../push-cert-status <<E_O_F
340 SIGNER=${GIT_PUSH_CERT_SIGNER-nobody}
341 KEY=${GIT_PUSH_CERT_KEY-nokey}
342 STATUS=${GIT_PUSH_CERT_STATUS-nostatus}
343 E_O_F
344 EOF
345
346 git config receive.certnonceseed sekrit
347 ) &&
348 cd "$ROOT_PATH/test_repo_clone" &&
349 test_commit cert-test &&
350 git push --signed "$HTTPD_URL/smart/test_repo.git" &&
351 (
352 cd "$HTTPD_DOCUMENT_ROOT_PATH" &&
353 cat <<-\EOF
354 SIGNER=C O Mitter <committer@example.com>
355 KEY=13B6F51ECDDE430D
356 STATUS=G
357 EOF
358 ) >expect &&
359 test_cmp expect "$HTTPD_DOCUMENT_ROOT_PATH/push-cert-status"
360'
361
7da4e228
SP
362stop_httpd
363test_done