tests: skip dav http-push tests under NO_EXPAT=NoThanks
[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-httpd.sh
11 . "$TEST_DIRECTORY"/lib-terminal.sh
12 start_httpd
13
14 test_expect_success 'setup remote repository' '
15         cd "$ROOT_PATH" &&
16         mkdir test_repo &&
17         cd test_repo &&
18         git init &&
19         : >path1 &&
20         git add path1 &&
21         test_tick &&
22         git commit -m initial &&
23         cd - &&
24         git clone --bare test_repo test_repo.git &&
25         cd test_repo.git &&
26         git config http.receivepack true &&
27         git config core.logallrefupdates true &&
28         ORIG_HEAD=$(git rev-parse --verify HEAD) &&
29         cd - &&
30         mv test_repo.git "$HTTPD_DOCUMENT_ROOT_PATH"
31 '
32
33 setup_askpass_helper
34
35 cat >exp <<EOF
36 GET  /smart/test_repo.git/info/refs?service=git-upload-pack HTTP/1.1 200
37 POST /smart/test_repo.git/git-upload-pack HTTP/1.1 200
38 EOF
39 test_expect_success 'no empty path components' '
40         # In the URL, add a trailing slash, and see if git appends yet another
41         # slash.
42         cd "$ROOT_PATH" &&
43         git clone $HTTPD_URL/smart/test_repo.git/ test_repo_clone &&
44
45         sed -e "
46                 s/^.* \"//
47                 s/\"//
48                 s/ [1-9][0-9]*\$//
49                 s/^GET /GET  /
50         " >act <"$HTTPD_ROOT_PATH"/access.log &&
51
52         # Clear the log, so that it does not affect the "used receive-pack
53         # service" test which reads the log too.
54         #
55         # We do this before the actual comparison to ensure the log is cleared.
56         echo > "$HTTPD_ROOT_PATH"/access.log &&
57
58         test_cmp exp act
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_CURL_VERBOSE=1 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
127 GET  /smart/test_repo.git/info/refs?service=git-upload-pack HTTP/1.1 200
128 POST /smart/test_repo.git/git-upload-pack HTTP/1.1 200
129 GET  /smart/test_repo.git/info/refs?service=git-receive-pack HTTP/1.1 200
130 POST /smart/test_repo.git/git-receive-pack HTTP/1.1 200
131 GET  /smart/test_repo.git/info/refs?service=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 GET  /smart/test_repo.git/info/refs?service=git-receive-pack HTTP/1.1 200
135 POST /smart/test_repo.git/git-receive-pack HTTP/1.1 200
136 GET  /smart/test_repo.git/info/refs?service=git-receive-pack HTTP/1.1 200
137 POST /smart/test_repo.git/git-receive-pack HTTP/1.1 200
138 EOF
139 test_expect_success 'used receive-pack service' '
140         sed -e "
141                 s/^.* \"//
142                 s/\"//
143                 s/ [1-9][0-9]*\$//
144                 s/^GET /GET  /
145         " >act <"$HTTPD_ROOT_PATH"/access.log &&
146         test_cmp exp act
147 '
148
149 test_http_push_nonff "$HTTPD_DOCUMENT_ROOT_PATH"/test_repo.git \
150         "$ROOT_PATH"/test_repo_clone master             success
151
152 test_expect_success 'push fails for non-fast-forward refs unmatched by remote helper' '
153         # create a dissimilarly-named remote ref so that git is unable to match the
154         # two refs (viz. local, remote) unless an explicit refspec is provided.
155         git push origin master:retsam
156
157         echo "change changed" > path2 &&
158         git commit -a -m path2 --amend &&
159
160         # push master too; this ensures there is at least one '"'push'"' command to
161         # the remote helper and triggers interaction with the helper.
162         test_must_fail git push -v origin +master master:retsam >output 2>&1'
163
164 test_expect_success 'push fails for non-fast-forward refs unmatched by remote helper: remote output' '
165         grep "^ + [a-f0-9]*\.\.\.[a-f0-9]* *master -> master (forced update)$" output &&
166         grep "^ ! \[rejected\] *master -> retsam (non-fast-forward)$" output
167 '
168
169 test_expect_success 'push fails for non-fast-forward refs unmatched by remote helper: our output' '
170         test_i18ngrep "Updates were rejected because" \
171                 output
172 '
173
174 test_expect_success 'push (chunked)' '
175         git checkout master &&
176         test_commit commit path3 &&
177         HEAD=$(git rev-parse --verify HEAD) &&
178         test_config http.postbuffer 4 &&
179         git push -v -v origin $BRANCH 2>err &&
180         grep "POST git-receive-pack (chunked)" err &&
181         (cd "$HTTPD_DOCUMENT_ROOT_PATH"/test_repo.git &&
182          test $HEAD = $(git rev-parse --verify HEAD))
183 '
184
185 test_expect_success 'push --all can push to empty repo' '
186         d=$HTTPD_DOCUMENT_ROOT_PATH/empty-all.git &&
187         git init --bare "$d" &&
188         git --git-dir="$d" config http.receivepack true &&
189         git push --all "$HTTPD_URL"/smart/empty-all.git
190 '
191
192 test_expect_success 'push --mirror can push to empty repo' '
193         d=$HTTPD_DOCUMENT_ROOT_PATH/empty-mirror.git &&
194         git init --bare "$d" &&
195         git --git-dir="$d" config http.receivepack true &&
196         git push --mirror "$HTTPD_URL"/smart/empty-mirror.git
197 '
198
199 test_expect_success 'push --all to repo with alternates' '
200         s=$HTTPD_DOCUMENT_ROOT_PATH/test_repo.git &&
201         d=$HTTPD_DOCUMENT_ROOT_PATH/alternates-all.git &&
202         git clone --bare --shared "$s" "$d" &&
203         git --git-dir="$d" config http.receivepack true &&
204         git --git-dir="$d" repack -adl &&
205         git push --all "$HTTPD_URL"/smart/alternates-all.git
206 '
207
208 test_expect_success 'push --mirror to repo with alternates' '
209         s=$HTTPD_DOCUMENT_ROOT_PATH/test_repo.git &&
210         d=$HTTPD_DOCUMENT_ROOT_PATH/alternates-mirror.git &&
211         git clone --bare --shared "$s" "$d" &&
212         git --git-dir="$d" config http.receivepack true &&
213         git --git-dir="$d" repack -adl &&
214         git push --mirror "$HTTPD_URL"/smart/alternates-mirror.git
215 '
216
217 test_expect_success TTY 'push shows progress when stderr is a tty' '
218         cd "$ROOT_PATH"/test_repo_clone &&
219         test_commit noisy &&
220         test_terminal git push >output 2>&1 &&
221         grep "^Writing objects" output
222 '
223
224 test_expect_success TTY 'push --quiet silences status and progress' '
225         cd "$ROOT_PATH"/test_repo_clone &&
226         test_commit quiet &&
227         test_terminal git push --quiet >output 2>&1 &&
228         test_cmp /dev/null output
229 '
230
231 test_expect_success TTY 'push --no-progress silences progress but not status' '
232         cd "$ROOT_PATH"/test_repo_clone &&
233         test_commit no-progress &&
234         test_terminal git push --no-progress >output 2>&1 &&
235         grep "^To http" output &&
236         ! grep "^Writing objects"
237 '
238
239 test_expect_success 'push --progress shows progress to non-tty' '
240         cd "$ROOT_PATH"/test_repo_clone &&
241         test_commit progress &&
242         git push --progress >output 2>&1 &&
243         grep "^To http" output &&
244         grep "^Writing objects" output
245 '
246
247 test_expect_success 'http push gives sane defaults to reflog' '
248         cd "$ROOT_PATH"/test_repo_clone &&
249         test_commit reflog-test &&
250         git push "$HTTPD_URL"/smart/test_repo.git &&
251         git --git-dir="$HTTPD_DOCUMENT_ROOT_PATH/test_repo.git" \
252                 log -g -1 --format="%gn <%ge>" >actual &&
253         echo "anonymous <anonymous@http.127.0.0.1>" >expect &&
254         test_cmp expect actual
255 '
256
257 test_expect_success 'http push respects GIT_COMMITTER_* in reflog' '
258         cd "$ROOT_PATH"/test_repo_clone &&
259         test_commit custom-reflog-test &&
260         git push "$HTTPD_URL"/smart_custom_env/test_repo.git &&
261         git --git-dir="$HTTPD_DOCUMENT_ROOT_PATH/test_repo.git" \
262                 log -g -1 --format="%gn <%ge>" >actual &&
263         echo "Custom User <custom@example.com>" >expect &&
264         test_cmp expect actual
265 '
266
267 test_expect_success 'push over smart http with auth' '
268         cd "$ROOT_PATH/test_repo_clone" &&
269         echo push-auth-test >expect &&
270         test_commit push-auth-test &&
271         set_askpass user@host pass@host &&
272         git push "$HTTPD_URL"/auth/smart/test_repo.git &&
273         git --git-dir="$HTTPD_DOCUMENT_ROOT_PATH/test_repo.git" \
274                 log -1 --format=%s >actual &&
275         expect_askpass both user@host &&
276         test_cmp expect actual
277 '
278
279 test_expect_success 'push to auth-only-for-push repo' '
280         cd "$ROOT_PATH/test_repo_clone" &&
281         echo push-half-auth >expect &&
282         test_commit push-half-auth &&
283         set_askpass user@host pass@host &&
284         git push "$HTTPD_URL"/auth-push/smart/test_repo.git &&
285         git --git-dir="$HTTPD_DOCUMENT_ROOT_PATH/test_repo.git" \
286                 log -1 --format=%s >actual &&
287         expect_askpass both user@host &&
288         test_cmp expect actual
289 '
290
291 test_expect_success 'create repo without http.receivepack set' '
292         cd "$ROOT_PATH" &&
293         git init half-auth &&
294         (
295                 cd half-auth &&
296                 test_commit one
297         ) &&
298         git clone --bare half-auth "$HTTPD_DOCUMENT_ROOT_PATH/half-auth.git"
299 '
300
301 test_expect_success 'clone via half-auth-complete does not need password' '
302         cd "$ROOT_PATH" &&
303         set_askpass wrong &&
304         git clone "$HTTPD_URL"/half-auth-complete/smart/half-auth.git \
305                 half-auth-clone &&
306         expect_askpass none
307 '
308
309 test_expect_success 'push into half-auth-complete requires password' '
310         cd "$ROOT_PATH/half-auth-clone" &&
311         echo two >expect &&
312         test_commit two &&
313         set_askpass user@host pass@host &&
314         git push "$HTTPD_URL/half-auth-complete/smart/half-auth.git" &&
315         git --git-dir="$HTTPD_DOCUMENT_ROOT_PATH/half-auth.git" \
316                 log -1 --format=%s >actual &&
317         expect_askpass both user@host &&
318         test_cmp expect actual
319 '
320
321 stop_httpd
322 test_done