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
10 skip_all='skipping test, git built without http support'
11 test_done
12fi
13
14ROOT_PATH="$PWD"
15LIB_HTTPD_PORT=${LIB_HTTPD_PORT-'5541'}
16. "$TEST_DIRECTORY"/lib-httpd.sh
17. "$TEST_DIRECTORY"/lib-terminal.sh
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 &&
33 git config core.logallrefupdates true &&
34 ORIG_HEAD=$(git rev-parse --verify HEAD) &&
35 cd - &&
36 mv test_repo.git "$HTTPD_DOCUMENT_ROOT_PATH"
37'
38
39setup_askpass_helper
40
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
45test_expect_success 'no empty path components' '
46 # In the URL, add a trailing slash, and see if git appends yet another
47 # slash.
48 cd "$ROOT_PATH" &&
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 &&
69 git clone $HTTPD_URL/smart/test_repo.git test_repo_clone
70'
71
72test_expect_success 'push to remote repository (standard)' '
73 cd "$ROOT_PATH"/test_repo_clone &&
74 : >path2 &&
75 git add path2 &&
76 test_tick &&
77 git commit -m path2 &&
78 HEAD=$(git rev-parse --verify HEAD) &&
79 GIT_CURL_VERBOSE=1 git push -v -v 2>err &&
80 ! grep "Expect: 100-continue" err &&
81 grep "POST git-receive-pack ([0-9]* bytes)" err &&
82 (cd "$HTTPD_DOCUMENT_ROOT_PATH"/test_repo.git &&
83 test $HEAD = $(git rev-parse --verify HEAD))
84'
85
86test_expect_success 'push already up-to-date' '
87 git push
88'
89
90test_expect_success 'create and delete remote branch' '
91 cd "$ROOT_PATH"/test_repo_clone &&
92 git checkout -b dev &&
93 : >path3 &&
94 git add path3 &&
95 test_tick &&
96 git commit -m dev &&
97 git push origin dev &&
98 git push origin :dev &&
99 test_must_fail git show-ref --verify refs/remotes/origin/dev
100'
101
102cat >"$HTTPD_DOCUMENT_ROOT_PATH/test_repo.git/hooks/update" <<EOF
103#!/bin/sh
104exit 1
105EOF
106chmod a+x "$HTTPD_DOCUMENT_ROOT_PATH/test_repo.git/hooks/update"
107
108cat >exp <<EOF
109remote: error: hook declined to update refs/heads/dev2
110To http://127.0.0.1:$LIB_HTTPD_PORT/smart/test_repo.git
111 ! [remote rejected] dev2 -> dev2 (hook declined)
112error: failed to push some refs to 'http://127.0.0.1:$LIB_HTTPD_PORT/smart/test_repo.git'
113EOF
114
115test_expect_success 'rejected update prints status' '
116 cd "$ROOT_PATH"/test_repo_clone &&
117 git checkout -b dev2 &&
118 : >path4 &&
119 git add path4 &&
120 test_tick &&
121 git commit -m dev2 &&
122 test_must_fail git push origin dev2 2>act &&
123 sed -e "/^remote: /s/ *$//" <act >cmp &&
124 test_cmp exp cmp
125'
126rm -f "$HTTPD_DOCUMENT_ROOT_PATH/test_repo.git/hooks/update"
127
128cat >exp <<EOF
129
130GET /smart/test_repo.git/info/refs?service=git-upload-pack HTTP/1.1 200
131POST /smart/test_repo.git/git-upload-pack HTTP/1.1 200
132GET /smart/test_repo.git/info/refs?service=git-receive-pack HTTP/1.1 200
133POST /smart/test_repo.git/git-receive-pack HTTP/1.1 200
134GET /smart/test_repo.git/info/refs?service=git-receive-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
138POST /smart/test_repo.git/git-receive-pack HTTP/1.1 200
139GET /smart/test_repo.git/info/refs?service=git-receive-pack HTTP/1.1 200
140POST /smart/test_repo.git/git-receive-pack HTTP/1.1 200
141EOF
142test_expect_success 'used receive-pack service' '
143 sed -e "
144 s/^.* \"//
145 s/\"//
146 s/ [1-9][0-9]*\$//
147 s/^GET /GET /
148 " >act <"$HTTPD_ROOT_PATH"/access.log &&
149 test_cmp exp act
150'
151
152test_http_push_nonff "$HTTPD_DOCUMENT_ROOT_PATH"/test_repo.git \
153 "$ROOT_PATH"/test_repo_clone master
154
155test_expect_success 'push fails for non-fast-forward refs unmatched by remote helper' '
156 # create a dissimilarly-named remote ref so that git is unable to match the
157 # two refs (viz. local, remote) unless an explicit refspec is provided.
158 git push origin master:retsam
159
160 echo "change changed" > path2 &&
161 git commit -a -m path2 --amend &&
162
163 # push master too; this ensures there is at least one '"'push'"' command to
164 # the remote helper and triggers interaction with the helper.
165 test_must_fail git push -v origin +master master:retsam >output 2>&1'
166
167test_expect_success 'push fails for non-fast-forward refs unmatched by remote helper: remote output' '
168 grep "^ + [a-f0-9]*\.\.\.[a-f0-9]* *master -> master (forced update)$" output &&
169 grep "^ ! \[rejected\] *master -> retsam (non-fast-forward)$" output
170'
171
172test_expect_success 'push fails for non-fast-forward refs unmatched by remote helper: our output' '
173 test_i18ngrep "Updates were rejected because" \
174 output
175'
176
177test_expect_success 'push (chunked)' '
178 git checkout master &&
179 test_commit commit path3 &&
180 HEAD=$(git rev-parse --verify HEAD) &&
181 git config http.postbuffer 4 &&
182 test_when_finished "git config --unset http.postbuffer" &&
183 git push -v -v origin $BRANCH 2>err &&
184 grep "POST git-receive-pack (chunked)" err &&
185 (cd "$HTTPD_DOCUMENT_ROOT_PATH"/test_repo.git &&
186 test $HEAD = $(git rev-parse --verify HEAD))
187'
188
189test_expect_success 'push --all can push to empty repo' '
190 d=$HTTPD_DOCUMENT_ROOT_PATH/empty-all.git &&
191 git init --bare "$d" &&
192 git --git-dir="$d" config http.receivepack true &&
193 git push --all "$HTTPD_URL"/smart/empty-all.git
194'
195
196test_expect_success 'push --mirror can push to empty repo' '
197 d=$HTTPD_DOCUMENT_ROOT_PATH/empty-mirror.git &&
198 git init --bare "$d" &&
199 git --git-dir="$d" config http.receivepack true &&
200 git push --mirror "$HTTPD_URL"/smart/empty-mirror.git
201'
202
203test_expect_success 'push --all to repo with alternates' '
204 s=$HTTPD_DOCUMENT_ROOT_PATH/test_repo.git &&
205 d=$HTTPD_DOCUMENT_ROOT_PATH/alternates-all.git &&
206 git clone --bare --shared "$s" "$d" &&
207 git --git-dir="$d" config http.receivepack true &&
208 git --git-dir="$d" repack -adl &&
209 git push --all "$HTTPD_URL"/smart/alternates-all.git
210'
211
212test_expect_success 'push --mirror to repo with alternates' '
213 s=$HTTPD_DOCUMENT_ROOT_PATH/test_repo.git &&
214 d=$HTTPD_DOCUMENT_ROOT_PATH/alternates-mirror.git &&
215 git clone --bare --shared "$s" "$d" &&
216 git --git-dir="$d" config http.receivepack true &&
217 git --git-dir="$d" repack -adl &&
218 git push --mirror "$HTTPD_URL"/smart/alternates-mirror.git
219'
220
221test_expect_success TTY 'push shows progress when stderr is a tty' '
222 cd "$ROOT_PATH"/test_repo_clone &&
223 test_commit noisy &&
224 test_terminal git push >output 2>&1 &&
225 grep "^Writing objects" output
226'
227
228test_expect_success TTY 'push --quiet silences status and progress' '
229 cd "$ROOT_PATH"/test_repo_clone &&
230 test_commit quiet &&
231 test_terminal git push --quiet >output 2>&1 &&
232 test_cmp /dev/null output
233'
234
235test_expect_success TTY 'push --no-progress silences progress but not status' '
236 cd "$ROOT_PATH"/test_repo_clone &&
237 test_commit no-progress &&
238 test_terminal git push --no-progress >output 2>&1 &&
239 grep "^To http" output &&
240 ! grep "^Writing objects"
241'
242
243test_expect_success 'push --progress shows progress to non-tty' '
244 cd "$ROOT_PATH"/test_repo_clone &&
245 test_commit progress &&
246 git push --progress >output 2>&1 &&
247 grep "^To http" output &&
248 grep "^Writing objects" output
249'
250
251test_expect_success 'http push gives sane defaults to reflog' '
252 cd "$ROOT_PATH"/test_repo_clone &&
253 test_commit reflog-test &&
254 git push "$HTTPD_URL"/smart/test_repo.git &&
255 git --git-dir="$HTTPD_DOCUMENT_ROOT_PATH/test_repo.git" \
256 log -g -1 --format="%gn <%ge>" >actual &&
257 echo "anonymous <anonymous@http.127.0.0.1>" >expect &&
258 test_cmp expect actual
259'
260
261test_expect_success 'http push respects GIT_COMMITTER_* in reflog' '
262 cd "$ROOT_PATH"/test_repo_clone &&
263 test_commit custom-reflog-test &&
264 git push "$HTTPD_URL"/smart_custom_env/test_repo.git &&
265 git --git-dir="$HTTPD_DOCUMENT_ROOT_PATH/test_repo.git" \
266 log -g -1 --format="%gn <%ge>" >actual &&
267 echo "Custom User <custom@example.com>" >expect &&
268 test_cmp expect actual
269'
270
271test_expect_success 'push over smart http with auth' '
272 cd "$ROOT_PATH/test_repo_clone" &&
273 echo push-auth-test >expect &&
274 test_commit push-auth-test &&
275 set_askpass user@host &&
276 git push "$HTTPD_URL"/auth/smart/test_repo.git &&
277 git --git-dir="$HTTPD_DOCUMENT_ROOT_PATH/test_repo.git" \
278 log -1 --format=%s >actual &&
279 expect_askpass both user@host &&
280 test_cmp expect actual
281'
282
283test_expect_success 'push to auth-only-for-push repo' '
284 cd "$ROOT_PATH/test_repo_clone" &&
285 echo push-half-auth >expect &&
286 test_commit push-half-auth &&
287 set_askpass user@host &&
288 git push "$HTTPD_URL"/auth-push/smart/test_repo.git &&
289 git --git-dir="$HTTPD_DOCUMENT_ROOT_PATH/test_repo.git" \
290 log -1 --format=%s >actual &&
291 expect_askpass both user@host &&
292 test_cmp expect actual
293'
294
295stop_httpd
296test_done