blob: c81ca360ac4ac9edccf86132aa63e44812906980 [file] [log] [blame]
Junio C Hamanobcdb34f2007-06-08 00:43:22 -07001#!/bin/sh
2
Ramkumar Ramachandra2ead7a62013-04-02 13:10:30 +05303test_description='Basic fetch/push functionality.
4
5This test checks the following functionality:
6
7* command-line syntax
8* refspecs
9* fast-forward detection, and overriding it
10* configuration
11* hooks
12* --porcelain output format
13* hiderefs
Jeff King72549df2014-11-04 08:11:19 -050014* reflogs
Ramkumar Ramachandra2ead7a62013-04-02 13:10:30 +053015'
Junio C Hamanobcdb34f2007-06-08 00:43:22 -070016
17. ./test-lib.sh
18
Elia Pintobf452422015-12-23 14:45:57 +010019D=$(pwd)
Junio C Hamanobcdb34f2007-06-08 00:43:22 -070020
21mk_empty () {
Jeff King2e433b72013-04-02 13:10:31 +053022 repo_name="$1"
23 rm -fr "$repo_name" &&
24 mkdir "$repo_name" &&
Junio C Hamanobcdb34f2007-06-08 00:43:22 -070025 (
Jeff King2e433b72013-04-02 13:10:31 +053026 cd "$repo_name" &&
Alex Riesen586e4ce2007-07-04 14:06:04 +020027 git init &&
Junio C Hamanoacd2a452009-02-11 02:28:03 -080028 git config receive.denyCurrentBranch warn &&
Alex Riesen586e4ce2007-07-04 14:06:04 +020029 mv .git/hooks .git/hooks-disabled
Junio C Hamanobcdb34f2007-06-08 00:43:22 -070030 )
31}
32
Junio C Hamano61257962007-06-09 01:23:53 -070033mk_test () {
Jeff King2e433b72013-04-02 13:10:31 +053034 repo_name="$1"
35 shift
36
37 mk_empty "$repo_name" &&
Junio C Hamano61257962007-06-09 01:23:53 -070038 (
39 for ref in "$@"
40 do
Jeff King2e433b72013-04-02 13:10:31 +053041 git push "$repo_name" $the_first_commit:refs/$ref ||
Jonathan Nieder5bd81c72013-03-18 16:14:26 -070042 exit
Junio C Hamano61257962007-06-09 01:23:53 -070043 done &&
Jeff King2e433b72013-04-02 13:10:31 +053044 cd "$repo_name" &&
Junio C Hamano61257962007-06-09 01:23:53 -070045 for ref in "$@"
46 do
Jonathan Nieder848575d2013-03-18 16:13:41 -070047 echo "$the_first_commit" >expect &&
48 git show-ref -s --verify refs/$ref >actual &&
49 test_cmp expect actual ||
50 exit
Junio C Hamano61257962007-06-09 01:23:53 -070051 done &&
52 git fsck --full
53 )
54}
55
Pang Yan Han160b81e2011-09-28 23:39:35 +080056mk_test_with_hooks() {
Jeff King2e433b72013-04-02 13:10:31 +053057 repo_name=$1
Pang Yan Han160b81e2011-09-28 23:39:35 +080058 mk_test "$@" &&
59 (
Jeff King2e433b72013-04-02 13:10:31 +053060 cd "$repo_name" &&
Pang Yan Han160b81e2011-09-28 23:39:35 +080061 mkdir .git/hooks &&
62 cd .git/hooks &&
63
64 cat >pre-receive <<-'EOF' &&
65 #!/bin/sh
66 cat - >>pre-receive.actual
67 EOF
68
69 cat >update <<-'EOF' &&
70 #!/bin/sh
71 printf "%s %s %s\n" "$@" >>update.actual
72 EOF
73
74 cat >post-receive <<-'EOF' &&
75 #!/bin/sh
76 cat - >>post-receive.actual
77 EOF
78
79 cat >post-update <<-'EOF' &&
80 #!/bin/sh
81 for ref in "$@"
82 do
83 printf "%s\n" "$ref" >>post-update.actual
84 done
85 EOF
86
87 chmod +x pre-receive update post-receive post-update
88 )
89}
90
Jeff Kingb2dc9682008-11-07 17:20:33 -050091mk_child() {
Jeff King2e433b72013-04-02 13:10:31 +053092 rm -rf "$2" &&
93 git clone "$1" "$2"
Jeff Kingb2dc9682008-11-07 17:20:33 -050094}
95
Junio C Hamano61257962007-06-09 01:23:53 -070096check_push_result () {
SZEDER Gáborcfb482b2018-05-10 16:01:53 +020097 test $# -ge 3 ||
SZEDER Gábor165293a2018-11-19 14:13:26 +010098 BUG "check_push_result requires at least 3 parameters"
SZEDER Gáborcfb482b2018-05-10 16:01:53 +020099
Jeff King2e433b72013-04-02 13:10:31 +0530100 repo_name="$1"
101 shift
102
Junio C Hamano61257962007-06-09 01:23:53 -0700103 (
Jeff King2e433b72013-04-02 13:10:31 +0530104 cd "$repo_name" &&
Jonathan Nieder848575d2013-03-18 16:13:41 -0700105 echo "$1" >expect &&
106 shift &&
Junio C Hamano61257962007-06-09 01:23:53 -0700107 for ref in "$@"
108 do
Jonathan Nieder848575d2013-03-18 16:13:41 -0700109 git show-ref -s --verify refs/$ref >actual &&
110 test_cmp expect actual ||
111 exit
Junio C Hamano61257962007-06-09 01:23:53 -0700112 done &&
113 git fsck --full
114 )
115}
116
Junio C Hamanobcdb34f2007-06-08 00:43:22 -0700117test_expect_success setup '
118
Jay Soffiand4785cd2010-04-19 18:08:31 -0400119 >path1 &&
Junio C Hamanobcdb34f2007-06-08 00:43:22 -0700120 git add path1 &&
121 test_tick &&
122 git commit -a -m repo &&
Junio C Hamano61257962007-06-09 01:23:53 -0700123 the_first_commit=$(git show-ref -s --verify refs/heads/master) &&
124
Jay Soffiand4785cd2010-04-19 18:08:31 -0400125 >path2 &&
Junio C Hamano61257962007-06-09 01:23:53 -0700126 git add path2 &&
127 test_tick &&
128 git commit -a -m second &&
Junio C Hamanobcdb34f2007-06-08 00:43:22 -0700129 the_commit=$(git show-ref -s --verify refs/heads/master)
130
131'
132
133test_expect_success 'fetch without wildcard' '
Jeff King2e433b72013-04-02 13:10:31 +0530134 mk_empty testrepo &&
Junio C Hamanobcdb34f2007-06-08 00:43:22 -0700135 (
136 cd testrepo &&
137 git fetch .. refs/heads/master:refs/remotes/origin/master &&
138
Jonathan Nieder848575d2013-03-18 16:13:41 -0700139 echo "$the_commit commit refs/remotes/origin/master" >expect &&
140 git for-each-ref refs/remotes/origin >actual &&
141 test_cmp expect actual
Junio C Hamanobcdb34f2007-06-08 00:43:22 -0700142 )
143'
144
145test_expect_success 'fetch with wildcard' '
Jeff King2e433b72013-04-02 13:10:31 +0530146 mk_empty testrepo &&
Junio C Hamanobcdb34f2007-06-08 00:43:22 -0700147 (
148 cd testrepo &&
149 git config remote.up.url .. &&
150 git config remote.up.fetch "refs/heads/*:refs/remotes/origin/*" &&
151 git fetch up &&
152
Jonathan Nieder848575d2013-03-18 16:13:41 -0700153 echo "$the_commit commit refs/remotes/origin/master" >expect &&
154 git for-each-ref refs/remotes/origin >actual &&
155 test_cmp expect actual
Junio C Hamanobcdb34f2007-06-08 00:43:22 -0700156 )
157'
158
Daniel Barkalow55029ae2008-02-20 13:43:53 -0500159test_expect_success 'fetch with insteadOf' '
Jeff King2e433b72013-04-02 13:10:31 +0530160 mk_empty testrepo &&
Daniel Barkalow55029ae2008-02-20 13:43:53 -0500161 (
Daniel Barkalow60e3aba2008-04-12 15:32:00 -0400162 TRASH=$(pwd)/ &&
Daniel Barkalow55029ae2008-02-20 13:43:53 -0500163 cd testrepo &&
Bryan Donlanf69e8362008-05-04 01:37:59 -0400164 git config "url.$TRASH.insteadOf" trash/ &&
Daniel Barkalow55029ae2008-02-20 13:43:53 -0500165 git config remote.up.url trash/. &&
166 git config remote.up.fetch "refs/heads/*:refs/remotes/origin/*" &&
167 git fetch up &&
168
Jonathan Nieder848575d2013-03-18 16:13:41 -0700169 echo "$the_commit commit refs/remotes/origin/master" >expect &&
170 git for-each-ref refs/remotes/origin >actual &&
171 test_cmp expect actual
Daniel Barkalow55029ae2008-02-20 13:43:53 -0500172 )
173'
174
Josh Triplett1c2eafb2009-09-07 01:56:33 -0700175test_expect_success 'fetch with pushInsteadOf (should not rewrite)' '
Jeff King2e433b72013-04-02 13:10:31 +0530176 mk_empty testrepo &&
Josh Triplett1c2eafb2009-09-07 01:56:33 -0700177 (
178 TRASH=$(pwd)/ &&
179 cd testrepo &&
180 git config "url.trash/.pushInsteadOf" "$TRASH" &&
181 git config remote.up.url "$TRASH." &&
182 git config remote.up.fetch "refs/heads/*:refs/remotes/origin/*" &&
183 git fetch up &&
184
Jonathan Nieder848575d2013-03-18 16:13:41 -0700185 echo "$the_commit commit refs/remotes/origin/master" >expect &&
186 git for-each-ref refs/remotes/origin >actual &&
187 test_cmp expect actual
Josh Triplett1c2eafb2009-09-07 01:56:33 -0700188 )
189'
190
Junio C Hamanobcdb34f2007-06-08 00:43:22 -0700191test_expect_success 'push without wildcard' '
Jeff King2e433b72013-04-02 13:10:31 +0530192 mk_empty testrepo &&
Junio C Hamanobcdb34f2007-06-08 00:43:22 -0700193
194 git push testrepo refs/heads/master:refs/remotes/origin/master &&
195 (
196 cd testrepo &&
Jonathan Nieder848575d2013-03-18 16:13:41 -0700197 echo "$the_commit commit refs/remotes/origin/master" >expect &&
198 git for-each-ref refs/remotes/origin >actual &&
199 test_cmp expect actual
Junio C Hamanobcdb34f2007-06-08 00:43:22 -0700200 )
201'
202
203test_expect_success 'push with wildcard' '
Jeff King2e433b72013-04-02 13:10:31 +0530204 mk_empty testrepo &&
Junio C Hamanobcdb34f2007-06-08 00:43:22 -0700205
206 git push testrepo "refs/heads/*:refs/remotes/origin/*" &&
207 (
208 cd testrepo &&
Jonathan Nieder848575d2013-03-18 16:13:41 -0700209 echo "$the_commit commit refs/remotes/origin/master" >expect &&
210 git for-each-ref refs/remotes/origin >actual &&
211 test_cmp expect actual
Junio C Hamanobcdb34f2007-06-08 00:43:22 -0700212 )
213'
214
Daniel Barkalow55029ae2008-02-20 13:43:53 -0500215test_expect_success 'push with insteadOf' '
Jeff King2e433b72013-04-02 13:10:31 +0530216 mk_empty testrepo &&
Bryan Donlanf69e8362008-05-04 01:37:59 -0400217 TRASH="$(pwd)/" &&
Jonathan Nieder3c695522013-03-18 16:12:11 -0700218 test_config "url.$TRASH.insteadOf" trash/ &&
Daniel Barkalow55029ae2008-02-20 13:43:53 -0500219 git push trash/testrepo refs/heads/master:refs/remotes/origin/master &&
220 (
221 cd testrepo &&
Jonathan Nieder848575d2013-03-18 16:13:41 -0700222 echo "$the_commit commit refs/remotes/origin/master" >expect &&
223 git for-each-ref refs/remotes/origin >actual &&
224 test_cmp expect actual
Daniel Barkalow55029ae2008-02-20 13:43:53 -0500225 )
226'
227
Josh Triplett1c2eafb2009-09-07 01:56:33 -0700228test_expect_success 'push with pushInsteadOf' '
Jeff King2e433b72013-04-02 13:10:31 +0530229 mk_empty testrepo &&
Josh Triplett1c2eafb2009-09-07 01:56:33 -0700230 TRASH="$(pwd)/" &&
Jonathan Nieder3c695522013-03-18 16:12:11 -0700231 test_config "url.$TRASH.pushInsteadOf" trash/ &&
Josh Triplett1c2eafb2009-09-07 01:56:33 -0700232 git push trash/testrepo refs/heads/master:refs/remotes/origin/master &&
233 (
234 cd testrepo &&
Jonathan Nieder848575d2013-03-18 16:13:41 -0700235 echo "$the_commit commit refs/remotes/origin/master" >expect &&
236 git for-each-ref refs/remotes/origin >actual &&
237 test_cmp expect actual
Josh Triplett1c2eafb2009-09-07 01:56:33 -0700238 )
239'
240
241test_expect_success 'push with pushInsteadOf and explicit pushurl (pushInsteadOf should not rewrite)' '
Jeff King2e433b72013-04-02 13:10:31 +0530242 mk_empty testrepo &&
Junio C Hamano41ae34d2013-04-03 09:34:48 -0700243 test_config "url.trash2/.pushInsteadOf" testrepo/ &&
Anders Kaseorgeb32c662015-02-28 23:18:14 -0500244 test_config "url.trash3/.pushInsteadOf" trash/wrong &&
Jonathan Nieder3c695522013-03-18 16:12:11 -0700245 test_config remote.r.url trash/wrong &&
Junio C Hamano41ae34d2013-04-03 09:34:48 -0700246 test_config remote.r.pushurl "testrepo/" &&
Josh Triplett1c2eafb2009-09-07 01:56:33 -0700247 git push r refs/heads/master:refs/remotes/origin/master &&
248 (
249 cd testrepo &&
Jonathan Nieder848575d2013-03-18 16:13:41 -0700250 echo "$the_commit commit refs/remotes/origin/master" >expect &&
251 git for-each-ref refs/remotes/origin >actual &&
252 test_cmp expect actual
Josh Triplett1c2eafb2009-09-07 01:56:33 -0700253 )
254'
255
Junio C Hamano61257962007-06-09 01:23:53 -0700256test_expect_success 'push with matching heads' '
257
Jeff King2e433b72013-04-02 13:10:31 +0530258 mk_test testrepo heads/master &&
Junio C Hamano0a42ac02013-01-04 16:08:26 -0800259 git push testrepo : &&
Jeff King2e433b72013-04-02 13:10:31 +0530260 check_push_result testrepo $the_commit heads/master
Junio C Hamano61257962007-06-09 01:23:53 -0700261
262'
263
Paolo Bonzinia83619d2008-04-28 11:32:12 -0400264test_expect_success 'push with matching heads on the command line' '
265
Jeff King2e433b72013-04-02 13:10:31 +0530266 mk_test testrepo heads/master &&
Paolo Bonzinia83619d2008-04-28 11:32:12 -0400267 git push testrepo : &&
Jeff King2e433b72013-04-02 13:10:31 +0530268 check_push_result testrepo $the_commit heads/master
Paolo Bonzinia83619d2008-04-28 11:32:12 -0400269
270'
271
272test_expect_success 'failed (non-fast-forward) push with matching heads' '
273
Jeff King2e433b72013-04-02 13:10:31 +0530274 mk_test testrepo heads/master &&
Paolo Bonzinia83619d2008-04-28 11:32:12 -0400275 git push testrepo : &&
276 git commit --amend -massaged &&
Stephan Beyerd492b312008-07-12 17:47:52 +0200277 test_must_fail git push testrepo &&
Jeff King2e433b72013-04-02 13:10:31 +0530278 check_push_result testrepo $the_commit heads/master &&
Paolo Bonzinia83619d2008-04-28 11:32:12 -0400279 git reset --hard $the_commit
280
281'
282
283test_expect_success 'push --force with matching heads' '
284
Jeff King2e433b72013-04-02 13:10:31 +0530285 mk_test testrepo heads/master &&
Paolo Bonzinia83619d2008-04-28 11:32:12 -0400286 git push testrepo : &&
287 git commit --amend -massaged &&
Junio C Hamano0a42ac02013-01-04 16:08:26 -0800288 git push --force testrepo : &&
Jeff King2e433b72013-04-02 13:10:31 +0530289 ! check_push_result testrepo $the_commit heads/master &&
Paolo Bonzinia83619d2008-04-28 11:32:12 -0400290 git reset --hard $the_commit
291
292'
293
294test_expect_success 'push with matching heads and forced update' '
295
Jeff King2e433b72013-04-02 13:10:31 +0530296 mk_test testrepo heads/master &&
Paolo Bonzinia83619d2008-04-28 11:32:12 -0400297 git push testrepo : &&
298 git commit --amend -massaged &&
299 git push testrepo +: &&
Jeff King2e433b72013-04-02 13:10:31 +0530300 ! check_push_result testrepo $the_commit heads/master &&
Paolo Bonzinia83619d2008-04-28 11:32:12 -0400301 git reset --hard $the_commit
302
303'
304
Junio C Hamano61257962007-06-09 01:23:53 -0700305test_expect_success 'push with no ambiguity (1)' '
306
Jeff King2e433b72013-04-02 13:10:31 +0530307 mk_test testrepo heads/master &&
Junio C Hamano61257962007-06-09 01:23:53 -0700308 git push testrepo master:master &&
Jeff King2e433b72013-04-02 13:10:31 +0530309 check_push_result testrepo $the_commit heads/master
Junio C Hamano61257962007-06-09 01:23:53 -0700310
311'
312
313test_expect_success 'push with no ambiguity (2)' '
314
Jeff King2e433b72013-04-02 13:10:31 +0530315 mk_test testrepo remotes/origin/master &&
Steffen Prohaskaae36bdc2007-11-11 15:01:47 +0100316 git push testrepo master:origin/master &&
Jeff King2e433b72013-04-02 13:10:31 +0530317 check_push_result testrepo $the_commit remotes/origin/master
Junio C Hamano61257962007-06-09 01:23:53 -0700318
319'
320
Steffen Prohaskaae36bdc2007-11-11 15:01:47 +0100321test_expect_success 'push with colon-less refspec, no ambiguity' '
322
Jeff King2e433b72013-04-02 13:10:31 +0530323 mk_test testrepo heads/master heads/t/master &&
Steffen Prohaskaae36bdc2007-11-11 15:01:47 +0100324 git branch -f t/master master &&
325 git push testrepo master &&
Jeff King2e433b72013-04-02 13:10:31 +0530326 check_push_result testrepo $the_commit heads/master &&
327 check_push_result testrepo $the_first_commit heads/t/master
Steffen Prohaskaae36bdc2007-11-11 15:01:47 +0100328
329'
330
Junio C Hamano61257962007-06-09 01:23:53 -0700331test_expect_success 'push with weak ambiguity (1)' '
332
Jeff King2e433b72013-04-02 13:10:31 +0530333 mk_test testrepo heads/master remotes/origin/master &&
Junio C Hamano61257962007-06-09 01:23:53 -0700334 git push testrepo master:master &&
Jeff King2e433b72013-04-02 13:10:31 +0530335 check_push_result testrepo $the_commit heads/master &&
336 check_push_result testrepo $the_first_commit remotes/origin/master
Junio C Hamano61257962007-06-09 01:23:53 -0700337
338'
339
340test_expect_success 'push with weak ambiguity (2)' '
341
Jeff King2e433b72013-04-02 13:10:31 +0530342 mk_test testrepo heads/master remotes/origin/master remotes/another/master &&
Junio C Hamano61257962007-06-09 01:23:53 -0700343 git push testrepo master:master &&
Jeff King2e433b72013-04-02 13:10:31 +0530344 check_push_result testrepo $the_commit heads/master &&
345 check_push_result testrepo $the_first_commit remotes/origin/master remotes/another/master
Junio C Hamano61257962007-06-09 01:23:53 -0700346
347'
348
Jeff King3ef6a1f2008-04-23 05:21:45 -0400349test_expect_success 'push with ambiguity' '
Junio C Hamano61257962007-06-09 01:23:53 -0700350
Jeff King2e433b72013-04-02 13:10:31 +0530351 mk_test testrepo heads/frotz tags/frotz &&
Jonathan Nieder5bd81c72013-03-18 16:14:26 -0700352 test_must_fail git push testrepo master:frotz &&
Jeff King2e433b72013-04-02 13:10:31 +0530353 check_push_result testrepo $the_first_commit heads/frotz tags/frotz
Junio C Hamano1ed10b82007-06-09 01:37:14 -0700354
355'
356
357test_expect_success 'push with colon-less refspec (1)' '
358
Jeff King2e433b72013-04-02 13:10:31 +0530359 mk_test testrepo heads/frotz tags/frotz &&
Junio C Hamano1ed10b82007-06-09 01:37:14 -0700360 git branch -f frotz master &&
361 git push testrepo frotz &&
Jeff King2e433b72013-04-02 13:10:31 +0530362 check_push_result testrepo $the_commit heads/frotz &&
363 check_push_result testrepo $the_first_commit tags/frotz
Junio C Hamano1ed10b82007-06-09 01:37:14 -0700364
365'
366
367test_expect_success 'push with colon-less refspec (2)' '
368
Jeff King2e433b72013-04-02 13:10:31 +0530369 mk_test testrepo heads/frotz tags/frotz &&
Junio C Hamano1ed10b82007-06-09 01:37:14 -0700370 if git show-ref --verify -q refs/heads/frotz
371 then
372 git branch -D frotz
373 fi &&
374 git tag -f frotz &&
Chris Rorvickdbfeddb2012-11-29 19:41:37 -0600375 git push -f testrepo frotz &&
Jeff King2e433b72013-04-02 13:10:31 +0530376 check_push_result testrepo $the_commit tags/frotz &&
377 check_push_result testrepo $the_first_commit heads/frotz
Junio C Hamano1ed10b82007-06-09 01:37:14 -0700378
379'
380
381test_expect_success 'push with colon-less refspec (3)' '
382
Jeff King2e433b72013-04-02 13:10:31 +0530383 mk_test testrepo &&
Junio C Hamano1ed10b82007-06-09 01:37:14 -0700384 if git show-ref --verify -q refs/tags/frotz
385 then
386 git tag -d frotz
387 fi &&
388 git branch -f frotz master &&
389 git push testrepo frotz &&
Jeff King2e433b72013-04-02 13:10:31 +0530390 check_push_result testrepo $the_commit heads/frotz &&
Brian Gernhardt9a3c6f72007-07-01 11:48:54 -0400391 test 1 = $( cd testrepo && git show-ref | wc -l )
Junio C Hamano1ed10b82007-06-09 01:37:14 -0700392'
393
394test_expect_success 'push with colon-less refspec (4)' '
395
Jeff King2e433b72013-04-02 13:10:31 +0530396 mk_test testrepo &&
Junio C Hamano1ed10b82007-06-09 01:37:14 -0700397 if git show-ref --verify -q refs/heads/frotz
398 then
399 git branch -D frotz
400 fi &&
401 git tag -f frotz &&
402 git push testrepo frotz &&
Jeff King2e433b72013-04-02 13:10:31 +0530403 check_push_result testrepo $the_commit tags/frotz &&
Brian Gernhardt9a3c6f72007-07-01 11:48:54 -0400404 test 1 = $( cd testrepo && git show-ref | wc -l )
Junio C Hamano1ed10b82007-06-09 01:37:14 -0700405
Junio C Hamano61257962007-06-09 01:23:53 -0700406'
407
Dmitry Ivankov7be8b3b2011-06-16 19:42:48 +0600408test_expect_success 'push head with non-existent, incomplete dest' '
Jeff Kingf8aae122008-04-23 05:16:06 -0400409
Jeff King2e433b72013-04-02 13:10:31 +0530410 mk_test testrepo &&
Jeff Kingf8aae122008-04-23 05:16:06 -0400411 git push testrepo master:branch &&
Jeff King2e433b72013-04-02 13:10:31 +0530412 check_push_result testrepo $the_commit heads/branch
Jeff Kingf8aae122008-04-23 05:16:06 -0400413
414'
415
Dmitry Ivankov7be8b3b2011-06-16 19:42:48 +0600416test_expect_success 'push tag with non-existent, incomplete dest' '
Jeff Kingf8aae122008-04-23 05:16:06 -0400417
Jeff King2e433b72013-04-02 13:10:31 +0530418 mk_test testrepo &&
Jeff Kingf8aae122008-04-23 05:16:06 -0400419 git tag -f v1.0 &&
420 git push testrepo v1.0:tag &&
Jeff King2e433b72013-04-02 13:10:31 +0530421 check_push_result testrepo $the_commit tags/tag
Jeff Kingf8aae122008-04-23 05:16:06 -0400422
423'
424
Dmitry Ivankov7be8b3b2011-06-16 19:42:48 +0600425test_expect_success 'push sha1 with non-existent, incomplete dest' '
Jeff Kingf8aae122008-04-23 05:16:06 -0400426
Jeff King2e433b72013-04-02 13:10:31 +0530427 mk_test testrepo &&
Elia Pintobf452422015-12-23 14:45:57 +0100428 test_must_fail git push testrepo $(git rev-parse master):foo
Jeff Kingf8aae122008-04-23 05:16:06 -0400429
430'
431
Dmitry Ivankov7be8b3b2011-06-16 19:42:48 +0600432test_expect_success 'push ref expression with non-existent, incomplete dest' '
Jeff Kingf8aae122008-04-23 05:16:06 -0400433
Jeff King2e433b72013-04-02 13:10:31 +0530434 mk_test testrepo &&
Jeff Kingf8aae122008-04-23 05:16:06 -0400435 test_must_fail git push testrepo master^:branch
436
437'
438
Steffen Prohaska47d996a2007-11-11 15:35:07 +0100439test_expect_success 'push with HEAD' '
440
Jeff King2e433b72013-04-02 13:10:31 +0530441 mk_test testrepo heads/master &&
Steffen Prohaska47d996a2007-11-11 15:35:07 +0100442 git checkout master &&
443 git push testrepo HEAD &&
Jeff King2e433b72013-04-02 13:10:31 +0530444 check_push_result testrepo $the_commit heads/master
Steffen Prohaska47d996a2007-11-11 15:35:07 +0100445
446'
447
448test_expect_success 'push with HEAD nonexisting at remote' '
449
Jeff King2e433b72013-04-02 13:10:31 +0530450 mk_test testrepo heads/master &&
Steffen Prohaska47d996a2007-11-11 15:35:07 +0100451 git checkout -b local master &&
452 git push testrepo HEAD &&
Jeff King2e433b72013-04-02 13:10:31 +0530453 check_push_result testrepo $the_commit heads/local
Steffen Prohaska47d996a2007-11-11 15:35:07 +0100454'
455
Daniel Barkalow9f0ea7e2008-02-20 12:54:05 -0500456test_expect_success 'push with +HEAD' '
457
Jeff King2e433b72013-04-02 13:10:31 +0530458 mk_test testrepo heads/master &&
Daniel Barkalow9f0ea7e2008-02-20 12:54:05 -0500459 git checkout master &&
460 git branch -D local &&
461 git checkout -b local &&
462 git push testrepo master local &&
Jeff King2e433b72013-04-02 13:10:31 +0530463 check_push_result testrepo $the_commit heads/master &&
464 check_push_result testrepo $the_commit heads/local &&
Daniel Barkalow9f0ea7e2008-02-20 12:54:05 -0500465
466 # Without force rewinding should fail
467 git reset --hard HEAD^ &&
Stephan Beyerd492b312008-07-12 17:47:52 +0200468 test_must_fail git push testrepo HEAD &&
Jeff King2e433b72013-04-02 13:10:31 +0530469 check_push_result testrepo $the_commit heads/local &&
Daniel Barkalow9f0ea7e2008-02-20 12:54:05 -0500470
471 # With force rewinding should succeed
472 git push testrepo +HEAD &&
Jeff King2e433b72013-04-02 13:10:31 +0530473 check_push_result testrepo $the_first_commit heads/local
Daniel Barkalow9f0ea7e2008-02-20 12:54:05 -0500474
475'
476
Dmitry Ivankov7be8b3b2011-06-16 19:42:48 +0600477test_expect_success 'push HEAD with non-existent, incomplete dest' '
Jeff Kingf8aae122008-04-23 05:16:06 -0400478
Jeff King2e433b72013-04-02 13:10:31 +0530479 mk_test testrepo &&
Jeff Kingf8aae122008-04-23 05:16:06 -0400480 git checkout master &&
481 git push testrepo HEAD:branch &&
Jeff King2e433b72013-04-02 13:10:31 +0530482 check_push_result testrepo $the_commit heads/branch
Jeff Kingf8aae122008-04-23 05:16:06 -0400483
484'
485
Daniel Barkalow9f0ea7e2008-02-20 12:54:05 -0500486test_expect_success 'push with config remote.*.push = HEAD' '
487
Jeff King2e433b72013-04-02 13:10:31 +0530488 mk_test testrepo heads/local &&
Daniel Barkalow9f0ea7e2008-02-20 12:54:05 -0500489 git checkout master &&
490 git branch -f local $the_commit &&
491 (
492 cd testrepo &&
493 git checkout local &&
494 git reset --hard $the_first_commit
495 ) &&
Jonathan Nieder3c695522013-03-18 16:12:11 -0700496 test_config remote.there.url testrepo &&
497 test_config remote.there.push HEAD &&
498 test_config branch.master.remote there &&
Daniel Barkalow9f0ea7e2008-02-20 12:54:05 -0500499 git push &&
Jeff King2e433b72013-04-02 13:10:31 +0530500 check_push_result testrepo $the_commit heads/master &&
501 check_push_result testrepo $the_first_commit heads/local
Daniel Barkalow9f0ea7e2008-02-20 12:54:05 -0500502'
503
Ramkumar Ramachandra224c2172013-04-02 13:10:33 +0530504test_expect_success 'push with remote.pushdefault' '
505 mk_test up_repo heads/master &&
506 mk_test down_repo heads/master &&
507 test_config remote.up.url up_repo &&
508 test_config remote.down.url down_repo &&
509 test_config branch.master.remote up &&
510 test_config remote.pushdefault down &&
Junio C Hamano54a3c672013-04-18 11:47:59 -0700511 test_config push.default matching &&
Ramkumar Ramachandra224c2172013-04-02 13:10:33 +0530512 git push &&
513 check_push_result up_repo $the_first_commit heads/master &&
514 check_push_result down_repo $the_commit heads/master
Daniel Barkalow9f0ea7e2008-02-20 12:54:05 -0500515'
516
Michael J Grubere1ca4242009-06-09 18:01:35 +0200517test_expect_success 'push with config remote.*.pushurl' '
518
Jeff King2e433b72013-04-02 13:10:31 +0530519 mk_test testrepo heads/master &&
Michael J Grubere1ca4242009-06-09 18:01:35 +0200520 git checkout master &&
Jonathan Nieder3c695522013-03-18 16:12:11 -0700521 test_config remote.there.url test2repo &&
522 test_config remote.there.pushurl testrepo &&
Junio C Hamano0a42ac02013-01-04 16:08:26 -0800523 git push there : &&
Jeff King2e433b72013-04-02 13:10:31 +0530524 check_push_result testrepo $the_commit heads/master
Michael J Grubere1ca4242009-06-09 18:01:35 +0200525'
526
Ramkumar Ramachandra9f765ce2013-04-02 13:10:34 +0530527test_expect_success 'push with config branch.*.pushremote' '
528 mk_test up_repo heads/master &&
529 mk_test side_repo heads/master &&
530 mk_test down_repo heads/master &&
531 test_config remote.up.url up_repo &&
532 test_config remote.pushdefault side_repo &&
533 test_config remote.down.url down_repo &&
534 test_config branch.master.remote up &&
535 test_config branch.master.pushremote down &&
Junio C Hamano54a3c672013-04-18 11:47:59 -0700536 test_config push.default matching &&
Ramkumar Ramachandra9f765ce2013-04-02 13:10:34 +0530537 git push &&
538 check_push_result up_repo $the_first_commit heads/master &&
539 check_push_result side_repo $the_first_commit heads/master &&
540 check_push_result down_repo $the_commit heads/master
Michael J Grubere1ca4242009-06-09 18:01:35 +0200541'
542
Jeff King98b406f2014-02-24 03:59:03 -0500543test_expect_success 'branch.*.pushremote config order is irrelevant' '
544 mk_test one_repo heads/master &&
545 mk_test two_repo heads/master &&
546 test_config remote.one.url one_repo &&
547 test_config remote.two.url two_repo &&
548 test_config branch.master.pushremote two_repo &&
549 test_config remote.pushdefault one_repo &&
550 test_config push.default matching &&
551 git push &&
552 check_push_result one_repo $the_first_commit heads/master &&
553 check_push_result two_repo $the_commit heads/master
554'
555
Brian Ewins11f24412007-10-11 20:32:27 +0100556test_expect_success 'push with dry-run' '
557
Jeff King2e433b72013-04-02 13:10:31 +0530558 mk_test testrepo heads/master &&
SZEDER Gáborcfb482b2018-05-10 16:01:53 +0200559 old_commit=$(git -C testrepo show-ref -s --verify refs/heads/master) &&
Junio C Hamano0a42ac02013-01-04 16:08:26 -0800560 git push --dry-run testrepo : &&
Jeff King2e433b72013-04-02 13:10:31 +0530561 check_push_result testrepo $old_commit heads/master
Brian Ewins11f24412007-10-11 20:32:27 +0100562'
563
Johannes Schindelin28391a82007-11-29 01:02:53 +0000564test_expect_success 'push updates local refs' '
565
Jeff King2e433b72013-04-02 13:10:31 +0530566 mk_test testrepo heads/master &&
567 mk_child testrepo child &&
Jay Soffiand4785cd2010-04-19 18:08:31 -0400568 (
569 cd child &&
Jeff Kingb2dc9682008-11-07 17:20:33 -0500570 git pull .. master &&
Johannes Schindelin28391a82007-11-29 01:02:53 +0000571 git push &&
Jay Soffiand4785cd2010-04-19 18:08:31 -0400572 test $(git rev-parse master) = \
573 $(git rev-parse remotes/origin/master)
574 )
Johannes Schindelin28391a82007-11-29 01:02:53 +0000575
576'
577
Clemens Buchacher16ed2f42008-11-05 21:55:54 +0100578test_expect_success 'push updates up-to-date local refs' '
579
Jeff King2e433b72013-04-02 13:10:31 +0530580 mk_test testrepo heads/master &&
581 mk_child testrepo child1 &&
582 mk_child testrepo child2 &&
Jeff Kingb2dc9682008-11-07 17:20:33 -0500583 (cd child1 && git pull .. master && git push) &&
Jay Soffiand4785cd2010-04-19 18:08:31 -0400584 (
585 cd child2 &&
Clemens Buchacher16ed2f42008-11-05 21:55:54 +0100586 git pull ../child1 master &&
587 git push &&
Jay Soffiand4785cd2010-04-19 18:08:31 -0400588 test $(git rev-parse master) = \
589 $(git rev-parse remotes/origin/master)
590 )
Clemens Buchacher16ed2f42008-11-05 21:55:54 +0100591
592'
593
594test_expect_success 'push preserves up-to-date packed refs' '
595
Jeff King2e433b72013-04-02 13:10:31 +0530596 mk_test testrepo heads/master &&
597 mk_child testrepo child &&
Jay Soffiand4785cd2010-04-19 18:08:31 -0400598 (
599 cd child &&
Clemens Buchacher16ed2f42008-11-05 21:55:54 +0100600 git push &&
Jay Soffiand4785cd2010-04-19 18:08:31 -0400601 ! test -f .git/refs/remotes/origin/master
602 )
Clemens Buchacher16ed2f42008-11-05 21:55:54 +0100603
604'
605
Johannes Schindelin28391a82007-11-29 01:02:53 +0000606test_expect_success 'push does not update local refs on failure' '
607
Jeff King2e433b72013-04-02 13:10:31 +0530608 mk_test testrepo heads/master &&
609 mk_child testrepo child &&
Jeff Kingb2dc9682008-11-07 17:20:33 -0500610 mkdir testrepo/.git/hooks &&
bert Dvornikfc012c22010-05-20 20:57:52 +0200611 echo "#!/no/frobnication/today" >testrepo/.git/hooks/pre-receive &&
Jeff Kingb2dc9682008-11-07 17:20:33 -0500612 chmod +x testrepo/.git/hooks/pre-receive &&
Jay Soffiand4785cd2010-04-19 18:08:31 -0400613 (
614 cd child &&
SZEDER Gáborf6b82972018-05-10 16:01:54 +0200615 git pull .. master &&
Stephan Beyerd492b312008-07-12 17:47:52 +0200616 test_must_fail git push &&
Johannes Schindelin28391a82007-11-29 01:02:53 +0000617 test $(git rev-parse master) != \
Jay Soffiand4785cd2010-04-19 18:08:31 -0400618 $(git rev-parse remotes/origin/master)
619 )
Johannes Schindelin28391a82007-11-29 01:02:53 +0000620
621'
622
623test_expect_success 'allow deleting an invalid remote ref' '
624
Jeff King2e433b72013-04-02 13:10:31 +0530625 mk_test testrepo heads/master &&
Johannes Schindelin28391a82007-11-29 01:02:53 +0000626 rm -f testrepo/.git/objects/??/* &&
627 git push testrepo :refs/heads/master &&
Stephan Beyerd492b312008-07-12 17:47:52 +0200628 (cd testrepo && test_must_fail git rev-parse --verify refs/heads/master)
Johannes Schindelin28391a82007-11-29 01:02:53 +0000629
630'
631
Pang Yan Han160b81e2011-09-28 23:39:35 +0800632test_expect_success 'pushing valid refs triggers post-receive and post-update hooks' '
Jeff King2e433b72013-04-02 13:10:31 +0530633 mk_test_with_hooks testrepo heads/master heads/next &&
Pang Yan Han160b81e2011-09-28 23:39:35 +0800634 orgmaster=$(cd testrepo && git show-ref -s --verify refs/heads/master) &&
635 newmaster=$(git show-ref -s --verify refs/heads/master) &&
636 orgnext=$(cd testrepo && git show-ref -s --verify refs/heads/next) &&
brian m. carlson8125a582018-05-13 02:24:13 +0000637 newnext=$ZERO_OID &&
Pang Yan Han160b81e2011-09-28 23:39:35 +0800638 git push testrepo refs/heads/master:refs/heads/master :refs/heads/next &&
639 (
640 cd testrepo/.git &&
641 cat >pre-receive.expect <<-EOF &&
642 $orgmaster $newmaster refs/heads/master
643 $orgnext $newnext refs/heads/next
644 EOF
645
646 cat >update.expect <<-EOF &&
647 refs/heads/master $orgmaster $newmaster
648 refs/heads/next $orgnext $newnext
649 EOF
650
651 cat >post-receive.expect <<-EOF &&
652 $orgmaster $newmaster refs/heads/master
653 $orgnext $newnext refs/heads/next
654 EOF
655
656 cat >post-update.expect <<-EOF &&
657 refs/heads/master
658 refs/heads/next
659 EOF
660
661 test_cmp pre-receive.expect pre-receive.actual &&
662 test_cmp update.expect update.actual &&
663 test_cmp post-receive.expect post-receive.actual &&
664 test_cmp post-update.expect post-update.actual
665 )
666'
667
668test_expect_success 'deleting dangling ref triggers hooks with correct args' '
Jeff King2e433b72013-04-02 13:10:31 +0530669 mk_test_with_hooks testrepo heads/master &&
Pang Yan Han160b81e2011-09-28 23:39:35 +0800670 rm -f testrepo/.git/objects/??/* &&
671 git push testrepo :refs/heads/master &&
672 (
673 cd testrepo/.git &&
674 cat >pre-receive.expect <<-EOF &&
brian m. carlson8125a582018-05-13 02:24:13 +0000675 $ZERO_OID $ZERO_OID refs/heads/master
Pang Yan Han160b81e2011-09-28 23:39:35 +0800676 EOF
677
678 cat >update.expect <<-EOF &&
brian m. carlson8125a582018-05-13 02:24:13 +0000679 refs/heads/master $ZERO_OID $ZERO_OID
Pang Yan Han160b81e2011-09-28 23:39:35 +0800680 EOF
681
682 cat >post-receive.expect <<-EOF &&
brian m. carlson8125a582018-05-13 02:24:13 +0000683 $ZERO_OID $ZERO_OID refs/heads/master
Pang Yan Han160b81e2011-09-28 23:39:35 +0800684 EOF
685
686 cat >post-update.expect <<-EOF &&
687 refs/heads/master
688 EOF
689
690 test_cmp pre-receive.expect pre-receive.actual &&
691 test_cmp update.expect update.actual &&
692 test_cmp post-receive.expect post-receive.actual &&
693 test_cmp post-update.expect post-update.actual
694 )
695'
696
697test_expect_success 'deletion of a non-existent ref is not fed to post-receive and post-update hooks' '
Jeff King2e433b72013-04-02 13:10:31 +0530698 mk_test_with_hooks testrepo heads/master &&
Pang Yan Han160b81e2011-09-28 23:39:35 +0800699 orgmaster=$(cd testrepo && git show-ref -s --verify refs/heads/master) &&
700 newmaster=$(git show-ref -s --verify refs/heads/master) &&
701 git push testrepo master :refs/heads/nonexistent &&
702 (
703 cd testrepo/.git &&
704 cat >pre-receive.expect <<-EOF &&
705 $orgmaster $newmaster refs/heads/master
brian m. carlson8125a582018-05-13 02:24:13 +0000706 $ZERO_OID $ZERO_OID refs/heads/nonexistent
Pang Yan Han160b81e2011-09-28 23:39:35 +0800707 EOF
708
709 cat >update.expect <<-EOF &&
710 refs/heads/master $orgmaster $newmaster
brian m. carlson8125a582018-05-13 02:24:13 +0000711 refs/heads/nonexistent $ZERO_OID $ZERO_OID
Pang Yan Han160b81e2011-09-28 23:39:35 +0800712 EOF
713
714 cat >post-receive.expect <<-EOF &&
715 $orgmaster $newmaster refs/heads/master
716 EOF
717
718 cat >post-update.expect <<-EOF &&
719 refs/heads/master
720 EOF
721
722 test_cmp pre-receive.expect pre-receive.actual &&
723 test_cmp update.expect update.actual &&
724 test_cmp post-receive.expect post-receive.actual &&
725 test_cmp post-update.expect post-update.actual
726 )
727'
728
729test_expect_success 'deletion of a non-existent ref alone does trigger post-receive and post-update hooks' '
Jeff King2e433b72013-04-02 13:10:31 +0530730 mk_test_with_hooks testrepo heads/master &&
Pang Yan Han160b81e2011-09-28 23:39:35 +0800731 git push testrepo :refs/heads/nonexistent &&
732 (
733 cd testrepo/.git &&
734 cat >pre-receive.expect <<-EOF &&
brian m. carlson8125a582018-05-13 02:24:13 +0000735 $ZERO_OID $ZERO_OID refs/heads/nonexistent
Pang Yan Han160b81e2011-09-28 23:39:35 +0800736 EOF
737
738 cat >update.expect <<-EOF &&
brian m. carlson8125a582018-05-13 02:24:13 +0000739 refs/heads/nonexistent $ZERO_OID $ZERO_OID
Pang Yan Han160b81e2011-09-28 23:39:35 +0800740 EOF
741
742 test_cmp pre-receive.expect pre-receive.actual &&
743 test_cmp update.expect update.actual &&
744 test_path_is_missing post-receive.actual &&
745 test_path_is_missing post-update.actual
746 )
747'
748
749test_expect_success 'mixed ref updates, deletes, invalid deletes trigger hooks with correct input' '
Jeff King2e433b72013-04-02 13:10:31 +0530750 mk_test_with_hooks testrepo heads/master heads/next heads/pu &&
Pang Yan Han160b81e2011-09-28 23:39:35 +0800751 orgmaster=$(cd testrepo && git show-ref -s --verify refs/heads/master) &&
752 newmaster=$(git show-ref -s --verify refs/heads/master) &&
753 orgnext=$(cd testrepo && git show-ref -s --verify refs/heads/next) &&
brian m. carlson8125a582018-05-13 02:24:13 +0000754 newnext=$ZERO_OID &&
Pang Yan Han160b81e2011-09-28 23:39:35 +0800755 orgpu=$(cd testrepo && git show-ref -s --verify refs/heads/pu) &&
756 newpu=$(git show-ref -s --verify refs/heads/master) &&
757 git push testrepo refs/heads/master:refs/heads/master \
758 refs/heads/master:refs/heads/pu :refs/heads/next \
759 :refs/heads/nonexistent &&
760 (
761 cd testrepo/.git &&
762 cat >pre-receive.expect <<-EOF &&
763 $orgmaster $newmaster refs/heads/master
764 $orgnext $newnext refs/heads/next
765 $orgpu $newpu refs/heads/pu
brian m. carlson8125a582018-05-13 02:24:13 +0000766 $ZERO_OID $ZERO_OID refs/heads/nonexistent
Pang Yan Han160b81e2011-09-28 23:39:35 +0800767 EOF
768
769 cat >update.expect <<-EOF &&
770 refs/heads/master $orgmaster $newmaster
771 refs/heads/next $orgnext $newnext
772 refs/heads/pu $orgpu $newpu
brian m. carlson8125a582018-05-13 02:24:13 +0000773 refs/heads/nonexistent $ZERO_OID $ZERO_OID
Pang Yan Han160b81e2011-09-28 23:39:35 +0800774 EOF
775
776 cat >post-receive.expect <<-EOF &&
777 $orgmaster $newmaster refs/heads/master
778 $orgnext $newnext refs/heads/next
779 $orgpu $newpu refs/heads/pu
780 EOF
781
782 cat >post-update.expect <<-EOF &&
783 refs/heads/master
784 refs/heads/next
785 refs/heads/pu
786 EOF
787
788 test_cmp pre-receive.expect pre-receive.actual &&
789 test_cmp update.expect update.actual &&
790 test_cmp post-receive.expect post-receive.actual &&
791 test_cmp post-update.expect post-update.actual
792 )
793'
794
Jan Krügerf517f1f2009-12-30 20:57:42 +0100795test_expect_success 'allow deleting a ref using --delete' '
Jeff King2e433b72013-04-02 13:10:31 +0530796 mk_test testrepo heads/master &&
Jan Krügerf517f1f2009-12-30 20:57:42 +0100797 (cd testrepo && git config receive.denyDeleteCurrent warn) &&
798 git push testrepo --delete master &&
799 (cd testrepo && test_must_fail git rev-parse --verify refs/heads/master)
800'
801
802test_expect_success 'allow deleting a tag using --delete' '
Jeff King2e433b72013-04-02 13:10:31 +0530803 mk_test testrepo heads/master &&
Jan Krügerf517f1f2009-12-30 20:57:42 +0100804 git tag -a -m dummy_message deltag heads/master &&
805 git push testrepo --tags &&
806 (cd testrepo && git rev-parse --verify -q refs/tags/deltag) &&
807 git push testrepo --delete tag deltag &&
808 (cd testrepo && test_must_fail git rev-parse --verify refs/tags/deltag)
809'
810
811test_expect_success 'push --delete without args aborts' '
Jeff King2e433b72013-04-02 13:10:31 +0530812 mk_test testrepo heads/master &&
Jan Krügerf517f1f2009-12-30 20:57:42 +0100813 test_must_fail git push testrepo --delete
814'
815
816test_expect_success 'push --delete refuses src:dest refspecs' '
Jeff King2e433b72013-04-02 13:10:31 +0530817 mk_test testrepo heads/master &&
Jan Krügerf517f1f2009-12-30 20:57:42 +0100818 test_must_fail git push testrepo --delete master:foo
819'
820
Jeff King986e8232008-11-08 20:49:27 -0500821test_expect_success 'warn on push to HEAD of non-bare repository' '
Jeff King2e433b72013-04-02 13:10:31 +0530822 mk_test testrepo heads/master &&
Jay Soffiand4785cd2010-04-19 18:08:31 -0400823 (
824 cd testrepo &&
Jeff King986e8232008-11-08 20:49:27 -0500825 git checkout master &&
Jay Soffiand4785cd2010-04-19 18:08:31 -0400826 git config receive.denyCurrentBranch warn
827 ) &&
Jeff King986e8232008-11-08 20:49:27 -0500828 git push testrepo master 2>stderr &&
Junio C Hamano3d95d922009-01-31 17:34:05 -0800829 grep "warning: updating the current branch" stderr
Jeff King986e8232008-11-08 20:49:27 -0500830'
831
832test_expect_success 'deny push to HEAD of non-bare repository' '
Jeff King2e433b72013-04-02 13:10:31 +0530833 mk_test testrepo heads/master &&
Jay Soffiand4785cd2010-04-19 18:08:31 -0400834 (
835 cd testrepo &&
Jeff King986e8232008-11-08 20:49:27 -0500836 git checkout master &&
Jay Soffiand4785cd2010-04-19 18:08:31 -0400837 git config receive.denyCurrentBranch true
838 ) &&
Jeff King986e8232008-11-08 20:49:27 -0500839 test_must_fail git push testrepo master
840'
841
842test_expect_success 'allow push to HEAD of bare repository (bare)' '
Jeff King2e433b72013-04-02 13:10:31 +0530843 mk_test testrepo heads/master &&
Jay Soffiand4785cd2010-04-19 18:08:31 -0400844 (
845 cd testrepo &&
Jeff King986e8232008-11-08 20:49:27 -0500846 git checkout master &&
847 git config receive.denyCurrentBranch true &&
Jay Soffiand4785cd2010-04-19 18:08:31 -0400848 git config core.bare true
849 ) &&
Jeff King986e8232008-11-08 20:49:27 -0500850 git push testrepo master 2>stderr &&
Junio C Hamano3d95d922009-01-31 17:34:05 -0800851 ! grep "warning: updating the current branch" stderr
Jeff King986e8232008-11-08 20:49:27 -0500852'
853
854test_expect_success 'allow push to HEAD of non-bare repository (config)' '
Jeff King2e433b72013-04-02 13:10:31 +0530855 mk_test testrepo heads/master &&
Jay Soffiand4785cd2010-04-19 18:08:31 -0400856 (
857 cd testrepo &&
Jeff King986e8232008-11-08 20:49:27 -0500858 git checkout master &&
859 git config receive.denyCurrentBranch false
860 ) &&
861 git push testrepo master 2>stderr &&
Junio C Hamano3d95d922009-01-31 17:34:05 -0800862 ! grep "warning: updating the current branch" stderr
Jeff King986e8232008-11-08 20:49:27 -0500863'
864
Martin Koegler18afe102008-11-10 22:47:11 +0100865test_expect_success 'fetch with branches' '
Jeff King2e433b72013-04-02 13:10:31 +0530866 mk_empty testrepo &&
Martin Koegler18afe102008-11-10 22:47:11 +0100867 git branch second $the_first_commit &&
868 git checkout second &&
869 echo ".." > testrepo/.git/branches/branch1 &&
Jay Soffiand4785cd2010-04-19 18:08:31 -0400870 (
871 cd testrepo &&
Martin Koegler18afe102008-11-10 22:47:11 +0100872 git fetch branch1 &&
Jonathan Nieder848575d2013-03-18 16:13:41 -0700873 echo "$the_commit commit refs/heads/branch1" >expect &&
874 git for-each-ref refs/heads >actual &&
875 test_cmp expect actual
Martin Koegler18afe102008-11-10 22:47:11 +0100876 ) &&
877 git checkout master
878'
879
880test_expect_success 'fetch with branches containing #' '
Jeff King2e433b72013-04-02 13:10:31 +0530881 mk_empty testrepo &&
Martin Koegler18afe102008-11-10 22:47:11 +0100882 echo "..#second" > testrepo/.git/branches/branch2 &&
Jay Soffiand4785cd2010-04-19 18:08:31 -0400883 (
884 cd testrepo &&
Martin Koegler18afe102008-11-10 22:47:11 +0100885 git fetch branch2 &&
Jonathan Nieder848575d2013-03-18 16:13:41 -0700886 echo "$the_first_commit commit refs/heads/branch2" >expect &&
887 git for-each-ref refs/heads >actual &&
888 test_cmp expect actual
Martin Koegler18afe102008-11-10 22:47:11 +0100889 ) &&
890 git checkout master
891'
892
893test_expect_success 'push with branches' '
Jeff King2e433b72013-04-02 13:10:31 +0530894 mk_empty testrepo &&
Martin Koegler18afe102008-11-10 22:47:11 +0100895 git checkout second &&
896 echo "testrepo" > .git/branches/branch1 &&
897 git push branch1 &&
Jay Soffiand4785cd2010-04-19 18:08:31 -0400898 (
899 cd testrepo &&
Jonathan Nieder848575d2013-03-18 16:13:41 -0700900 echo "$the_first_commit commit refs/heads/master" >expect &&
901 git for-each-ref refs/heads >actual &&
902 test_cmp expect actual
Martin Koegler18afe102008-11-10 22:47:11 +0100903 )
904'
905
906test_expect_success 'push with branches containing #' '
Jeff King2e433b72013-04-02 13:10:31 +0530907 mk_empty testrepo &&
Martin Koegler18afe102008-11-10 22:47:11 +0100908 echo "testrepo#branch3" > .git/branches/branch2 &&
909 git push branch2 &&
Jay Soffiand4785cd2010-04-19 18:08:31 -0400910 (
911 cd testrepo &&
Jonathan Nieder848575d2013-03-18 16:13:41 -0700912 echo "$the_first_commit commit refs/heads/branch3" >expect &&
913 git for-each-ref refs/heads >actual &&
914 test_cmp expect actual
Martin Koegler18afe102008-11-10 22:47:11 +0100915 ) &&
916 git checkout master
917'
918
Jay Soffianda3efdb2010-04-19 18:19:18 -0400919test_expect_success 'push into aliased refs (consistent)' '
Jeff King2e433b72013-04-02 13:10:31 +0530920 mk_test testrepo heads/master &&
921 mk_child testrepo child1 &&
922 mk_child testrepo child2 &&
Jay Soffianda3efdb2010-04-19 18:19:18 -0400923 (
924 cd child1 &&
925 git branch foo &&
Eric Sunshine51b85472018-07-01 20:24:01 -0400926 git symbolic-ref refs/heads/bar refs/heads/foo &&
Jay Soffianda3efdb2010-04-19 18:19:18 -0400927 git config receive.denyCurrentBranch false
928 ) &&
929 (
930 cd child2 &&
931 >path2 &&
932 git add path2 &&
933 test_tick &&
934 git commit -a -m child2 &&
935 git branch foo &&
936 git branch bar &&
937 git push ../child1 foo bar
938 )
939'
940
941test_expect_success 'push into aliased refs (inconsistent)' '
Jeff King2e433b72013-04-02 13:10:31 +0530942 mk_test testrepo heads/master &&
943 mk_child testrepo child1 &&
944 mk_child testrepo child2 &&
Jay Soffianda3efdb2010-04-19 18:19:18 -0400945 (
946 cd child1 &&
947 git branch foo &&
Eric Sunshine51b85472018-07-01 20:24:01 -0400948 git symbolic-ref refs/heads/bar refs/heads/foo &&
Jay Soffianda3efdb2010-04-19 18:19:18 -0400949 git config receive.denyCurrentBranch false
950 ) &&
951 (
952 cd child2 &&
953 >path2 &&
954 git add path2 &&
955 test_tick &&
956 git commit -a -m child2 &&
957 git branch foo &&
958 >path3 &&
959 git add path3 &&
960 test_tick &&
961 git commit -a -m child2 &&
962 git branch bar &&
963 test_must_fail git push ../child1 foo bar 2>stderr &&
964 grep "refusing inconsistent update" stderr
965 )
966'
967
Ævar Arnfjörð Bjarmason380efb62018-07-31 13:07:13 +0000968test_force_push_tag () {
969 tag_type_description=$1
970 tag_args=$2
Ævar Arnfjörð Bjarmason941a7ba2018-07-31 13:07:12 +0000971
Ævar Arnfjörð Bjarmasonf08fb8d2018-08-31 20:09:57 +0000972 test_expect_success "force pushing required to update $tag_type_description" "
Ævar Arnfjörð Bjarmason380efb62018-07-31 13:07:13 +0000973 mk_test testrepo heads/master &&
974 mk_child testrepo child1 &&
975 mk_child testrepo child2 &&
976 (
977 cd child1 &&
978 git tag testTag &&
979 git push ../child2 testTag &&
980 >file1 &&
981 git add file1 &&
982 git commit -m 'file1' &&
983 git tag $tag_args testTag &&
984 test_must_fail git push ../child2 testTag &&
985 git push --force ../child2 testTag &&
986 git tag $tag_args testTag HEAD~ &&
987 test_must_fail git push ../child2 testTag &&
988 git push --force ../child2 testTag &&
Ævar Arnfjörð Bjarmason941a7ba2018-07-31 13:07:12 +0000989
Ævar Arnfjörð Bjarmason380efb62018-07-31 13:07:13 +0000990 # Clobbering without + in refspec needs --force
991 git tag -f testTag &&
992 test_must_fail git push ../child2 'refs/tags/*:refs/tags/*' &&
993 git push --force ../child2 'refs/tags/*:refs/tags/*' &&
Ævar Arnfjörð Bjarmason941a7ba2018-07-31 13:07:12 +0000994
Ævar Arnfjörð Bjarmason380efb62018-07-31 13:07:13 +0000995 # Clobbering with + in refspec does not need --force
996 git tag -f testTag HEAD~ &&
997 git push ../child2 '+refs/tags/*:refs/tags/*' &&
Ævar Arnfjörð Bjarmason941a7ba2018-07-31 13:07:12 +0000998
Ævar Arnfjörð Bjarmason380efb62018-07-31 13:07:13 +0000999 # Clobbering with --no-force still obeys + in refspec
1000 git tag -f testTag &&
1001 git push --no-force ../child2 '+refs/tags/*:refs/tags/*' &&
1002
1003 # Clobbering with/without --force and 'tag <name>' format
1004 git tag -f testTag HEAD~ &&
1005 test_must_fail git push ../child2 tag testTag &&
1006 git push --force ../child2 tag testTag
1007 )
1008 "
1009}
1010
1011test_force_push_tag "lightweight tag" "-f"
Ævar Arnfjörð Bjarmason253b3d42018-08-31 20:09:58 +00001012test_force_push_tag "annotated tag" "-f -a -m'tag message'"
Chris Rorvickdbfeddb2012-11-29 19:41:37 -06001013
Ævar Arnfjörð Bjarmason6b0b0672018-08-31 20:09:59 +00001014test_force_fetch_tag () {
1015 tag_type_description=$1
1016 tag_args=$2
1017
Ævar Arnfjörð Bjarmason0bc8d712018-08-31 20:10:04 +00001018 test_expect_success "fetch will not clobber an existing $tag_type_description without --force" "
Ævar Arnfjörð Bjarmason6b0b0672018-08-31 20:09:59 +00001019 mk_test testrepo heads/master &&
1020 mk_child testrepo child1 &&
1021 mk_child testrepo child2 &&
1022 (
1023 cd testrepo &&
1024 git tag testTag &&
1025 git -C ../child1 fetch origin tag testTag &&
1026 >file1 &&
1027 git add file1 &&
1028 git commit -m 'file1' &&
1029 git tag $tag_args testTag &&
Ævar Arnfjörð Bjarmason0bc8d712018-08-31 20:10:04 +00001030 test_must_fail git -C ../child1 fetch origin tag testTag &&
1031 git -C ../child1 fetch origin '+refs/tags/*:refs/tags/*'
Ævar Arnfjörð Bjarmason6b0b0672018-08-31 20:09:59 +00001032 )
1033 "
1034}
1035
1036test_force_fetch_tag "lightweight tag" "-f"
1037test_force_fetch_tag "annotated tag" "-f -a -m'tag message'"
Larry D'Annafbe4f442010-02-26 23:52:16 -05001038
Larry D'Annafbe4f442010-02-26 23:52:16 -05001039test_expect_success 'push --porcelain' '
Jeff King2e433b72013-04-02 13:10:31 +05301040 mk_empty testrepo &&
Larry D'Annafbe4f442010-02-26 23:52:16 -05001041 echo >.git/foo "To testrepo" &&
1042 echo >>.git/foo "* refs/heads/master:refs/remotes/origin/master [new branch]" &&
1043 echo >>.git/foo "Done" &&
1044 git push >.git/bar --porcelain testrepo refs/heads/master:refs/remotes/origin/master &&
1045 (
1046 cd testrepo &&
Jonathan Nieder848575d2013-03-18 16:13:41 -07001047 echo "$the_commit commit refs/remotes/origin/master" >expect &&
1048 git for-each-ref refs/remotes/origin >actual &&
1049 test_cmp expect actual
Larry D'Annafbe4f442010-02-26 23:52:16 -05001050 ) &&
1051 test_cmp .git/foo .git/bar
1052'
1053
1054test_expect_success 'push --porcelain bad url' '
Jeff King2e433b72013-04-02 13:10:31 +05301055 mk_empty testrepo &&
Larry D'Annafbe4f442010-02-26 23:52:16 -05001056 test_must_fail git push >.git/bar --porcelain asdfasdfasd refs/heads/master:refs/remotes/origin/master &&
Pranit Bauvac7cf9562017-01-04 01:27:07 +05301057 ! grep -q Done .git/bar
Larry D'Annafbe4f442010-02-26 23:52:16 -05001058'
1059
1060test_expect_success 'push --porcelain rejected' '
Jeff King2e433b72013-04-02 13:10:31 +05301061 mk_empty testrepo &&
Larry D'Annafbe4f442010-02-26 23:52:16 -05001062 git push testrepo refs/heads/master:refs/remotes/origin/master &&
1063 (cd testrepo &&
Eric Sunshine51b85472018-07-01 20:24:01 -04001064 git reset --hard origin/master^ &&
Larry D'Annafbe4f442010-02-26 23:52:16 -05001065 git config receive.denyCurrentBranch true) &&
1066
1067 echo >.git/foo "To testrepo" &&
1068 echo >>.git/foo "! refs/heads/master:refs/heads/master [remote rejected] (branch is currently checked out)" &&
1069
1070 test_must_fail git push >.git/bar --porcelain testrepo refs/heads/master:refs/heads/master &&
Junio C Hamanoc2961342010-03-11 21:51:57 -08001071 test_cmp .git/foo .git/bar
Larry D'Annafbe4f442010-02-26 23:52:16 -05001072'
1073
1074test_expect_success 'push --porcelain --dry-run rejected' '
Jeff King2e433b72013-04-02 13:10:31 +05301075 mk_empty testrepo &&
Larry D'Annafbe4f442010-02-26 23:52:16 -05001076 git push testrepo refs/heads/master:refs/remotes/origin/master &&
1077 (cd testrepo &&
Eric Sunshine51b85472018-07-01 20:24:01 -04001078 git reset --hard origin/master &&
Larry D'Annafbe4f442010-02-26 23:52:16 -05001079 git config receive.denyCurrentBranch true) &&
1080
1081 echo >.git/foo "To testrepo" &&
1082 echo >>.git/foo "! refs/heads/master^:refs/heads/master [rejected] (non-fast-forward)" &&
1083 echo >>.git/foo "Done" &&
1084
1085 test_must_fail git push >.git/bar --porcelain --dry-run testrepo refs/heads/master^:refs/heads/master &&
Junio C Hamanoc2961342010-03-11 21:51:57 -08001086 test_cmp .git/foo .git/bar
Larry D'Annafbe4f442010-02-26 23:52:16 -05001087'
1088
Felipe Contreras6ddba5e2012-02-23 00:43:41 +02001089test_expect_success 'push --prune' '
Jeff King2e433b72013-04-02 13:10:31 +05301090 mk_test testrepo heads/master heads/second heads/foo heads/bar &&
Junio C Hamano0a42ac02013-01-04 16:08:26 -08001091 git push --prune testrepo : &&
Jeff King2e433b72013-04-02 13:10:31 +05301092 check_push_result testrepo $the_commit heads/master &&
1093 check_push_result testrepo $the_first_commit heads/second &&
1094 ! check_push_result testrepo $the_first_commit heads/foo heads/bar
Felipe Contreras6ddba5e2012-02-23 00:43:41 +02001095'
1096
1097test_expect_success 'push --prune refspec' '
Jeff King2e433b72013-04-02 13:10:31 +05301098 mk_test testrepo tmp/master tmp/second tmp/foo tmp/bar &&
Felipe Contreras6ddba5e2012-02-23 00:43:41 +02001099 git push --prune testrepo "refs/heads/*:refs/tmp/*" &&
Jeff King2e433b72013-04-02 13:10:31 +05301100 check_push_result testrepo $the_commit tmp/master &&
1101 check_push_result testrepo $the_first_commit tmp/second &&
1102 ! check_push_result testrepo $the_first_commit tmp/foo tmp/bar
Felipe Contreras6ddba5e2012-02-23 00:43:41 +02001103'
1104
Junio C Hamanodaebaa72013-01-18 16:08:30 -08001105for configsection in transfer receive
1106do
1107 test_expect_success "push to update a ref hidden by $configsection.hiderefs" '
Jeff King2e433b72013-04-02 13:10:31 +05301108 mk_test testrepo heads/master hidden/one hidden/two hidden/three &&
Junio C Hamanodaebaa72013-01-18 16:08:30 -08001109 (
1110 cd testrepo &&
1111 git config $configsection.hiderefs refs/hidden
1112 ) &&
1113
1114 # push to unhidden ref succeeds normally
1115 git push testrepo master:refs/heads/master &&
Jeff King2e433b72013-04-02 13:10:31 +05301116 check_push_result testrepo $the_commit heads/master &&
Junio C Hamanodaebaa72013-01-18 16:08:30 -08001117
1118 # push to update a hidden ref should fail
1119 test_must_fail git push testrepo master:refs/hidden/one &&
Jeff King2e433b72013-04-02 13:10:31 +05301120 check_push_result testrepo $the_first_commit hidden/one &&
Junio C Hamanodaebaa72013-01-18 16:08:30 -08001121
1122 # push to delete a hidden ref should fail
1123 test_must_fail git push testrepo :refs/hidden/two &&
Jeff King2e433b72013-04-02 13:10:31 +05301124 check_push_result testrepo $the_first_commit hidden/two &&
Junio C Hamanodaebaa72013-01-18 16:08:30 -08001125
1126 # idempotent push to update a hidden ref should fail
1127 test_must_fail git push testrepo $the_first_commit:refs/hidden/three &&
Jeff King2e433b72013-04-02 13:10:31 +05301128 check_push_result testrepo $the_first_commit hidden/three
Junio C Hamanodaebaa72013-01-18 16:08:30 -08001129 '
1130done
1131
Junio C Hamano6e7b66e2013-01-29 14:02:15 -08001132test_expect_success 'fetch exact SHA1' '
Jeff King2e433b72013-04-02 13:10:31 +05301133 mk_test testrepo heads/master hidden/one &&
Junio C Hamano6e7b66e2013-01-29 14:02:15 -08001134 git push testrepo master:refs/hidden/one &&
1135 (
1136 cd testrepo &&
1137 git config transfer.hiderefs refs/hidden
1138 ) &&
Jeff King2e433b72013-04-02 13:10:31 +05301139 check_push_result testrepo $the_commit hidden/one &&
Junio C Hamano6e7b66e2013-01-29 14:02:15 -08001140
Jeff King2e433b72013-04-02 13:10:31 +05301141 mk_child testrepo child &&
Junio C Hamano6e7b66e2013-01-29 14:02:15 -08001142 (
1143 cd child &&
1144
1145 # make sure $the_commit does not exist here
1146 git repack -a -d &&
1147 git prune &&
1148 test_must_fail git cat-file -t $the_commit &&
1149
Jonathan Tanab0c5f52019-02-25 13:54:08 -08001150 # Some protocol versions (e.g. 2) support fetching
1151 # unadvertised objects, so restrict this test to v0.
1152
Junio C Hamano6e7b66e2013-01-29 14:02:15 -08001153 # fetching the hidden object should fail by default
Jonathan Tanab0c5f52019-02-25 13:54:08 -08001154 test_must_fail env GIT_TEST_PROTOCOL_VERSION= \
1155 git fetch -v ../testrepo $the_commit:refs/heads/copy 2>err &&
Matt McCutchend56583d2017-02-22 11:05:57 -05001156 test_i18ngrep "Server does not allow request for unadvertised object" err &&
Junio C Hamano6e7b66e2013-01-29 14:02:15 -08001157 test_must_fail git rev-parse --verify refs/heads/copy &&
1158
1159 # the server side can allow it to succeed
1160 (
1161 cd ../testrepo &&
1162 git config uploadpack.allowtipsha1inwant true
1163 ) &&
1164
Jeff Kingc3c17bf2015-03-19 16:37:09 -04001165 git fetch -v ../testrepo $the_commit:refs/heads/copy master:refs/heads/extra &&
1166 cat >expect <<-EOF &&
1167 $the_commit
1168 $the_first_commit
1169 EOF
1170 {
1171 git rev-parse --verify refs/heads/copy &&
1172 git rev-parse --verify refs/heads/extra
1173 } >actual &&
1174 test_cmp expect actual
Junio C Hamano6e7b66e2013-01-29 14:02:15 -08001175 )
1176'
1177
Jonathan Nieder6c301ad2018-05-31 00:23:39 -07001178test_expect_success 'fetch exact SHA1 in protocol v2' '
1179 mk_test testrepo heads/master hidden/one &&
1180 git push testrepo master:refs/hidden/one &&
1181 git -C testrepo config transfer.hiderefs refs/hidden &&
1182 check_push_result testrepo $the_commit hidden/one &&
1183
1184 mk_child testrepo child &&
1185 git -C child config protocol.version 2 &&
1186
1187 # make sure $the_commit does not exist here
1188 git -C child repack -a -d &&
1189 git -C child prune &&
1190 test_must_fail git -C child cat-file -t $the_commit &&
1191
1192 # fetching the hidden object succeeds by default
1193 # NEEDSWORK: should this match the v0 behavior instead?
1194 git -C child fetch -v ../testrepo $the_commit:refs/heads/copy
1195'
1196
Fredrik Medley68ee6282015-05-21 22:23:39 +02001197for configallowtipsha1inwant in true false
1198do
1199 test_expect_success "shallow fetch reachable SHA1 (but not a ref), allowtipsha1inwant=$configallowtipsha1inwant" '
1200 mk_empty testrepo &&
1201 (
1202 cd testrepo &&
1203 git config uploadpack.allowtipsha1inwant $configallowtipsha1inwant &&
1204 git commit --allow-empty -m foo &&
1205 git commit --allow-empty -m bar
1206 ) &&
1207 SHA1=$(git --git-dir=testrepo/.git rev-parse HEAD^) &&
1208 mk_empty shallow &&
1209 (
1210 cd shallow &&
Jonathan Tanab0c5f52019-02-25 13:54:08 -08001211 # Some protocol versions (e.g. 2) support fetching
1212 # unadvertised objects, so restrict this test to v0.
1213 test_must_fail env GIT_TEST_PROTOCOL_VERSION= \
1214 git fetch --depth=1 ../testrepo/.git $SHA1 &&
Fredrik Medley68ee6282015-05-21 22:23:39 +02001215 git --git-dir=../testrepo/.git config uploadpack.allowreachablesha1inwant true &&
1216 git fetch --depth=1 ../testrepo/.git $SHA1 &&
1217 git cat-file commit $SHA1
1218 )
1219 '
1220
1221 test_expect_success "deny fetch unreachable SHA1, allowtipsha1inwant=$configallowtipsha1inwant" '
1222 mk_empty testrepo &&
1223 (
1224 cd testrepo &&
1225 git config uploadpack.allowtipsha1inwant $configallowtipsha1inwant &&
1226 git commit --allow-empty -m foo &&
1227 git commit --allow-empty -m bar &&
1228 git commit --allow-empty -m xyz
1229 ) &&
1230 SHA1_1=$(git --git-dir=testrepo/.git rev-parse HEAD^^) &&
1231 SHA1_2=$(git --git-dir=testrepo/.git rev-parse HEAD^) &&
1232 SHA1_3=$(git --git-dir=testrepo/.git rev-parse HEAD) &&
1233 (
1234 cd testrepo &&
1235 git reset --hard $SHA1_2 &&
1236 git cat-file commit $SHA1_1 &&
1237 git cat-file commit $SHA1_3
1238 ) &&
1239 mk_empty shallow &&
1240 (
1241 cd shallow &&
Jonathan Tanab0c5f52019-02-25 13:54:08 -08001242 # Some protocol versions (e.g. 2) support fetching
1243 # unadvertised objects, so restrict this test to v0.
Junio C Hamano57a6b932019-04-25 16:41:23 +09001244 test_must_fail env GIT_TEST_PROTOCOL_VERSION= \
Jonathan Tanab0c5f52019-02-25 13:54:08 -08001245 git fetch ../testrepo/.git $SHA1_3 &&
Junio C Hamano57a6b932019-04-25 16:41:23 +09001246 test_must_fail env GIT_TEST_PROTOCOL_VERSION= \
Jonathan Tanab0c5f52019-02-25 13:54:08 -08001247 git fetch ../testrepo/.git $SHA1_1 &&
Fredrik Medley68ee6282015-05-21 22:23:39 +02001248 git --git-dir=../testrepo/.git config uploadpack.allowreachablesha1inwant true &&
1249 git fetch ../testrepo/.git $SHA1_1 &&
1250 git cat-file commit $SHA1_1 &&
1251 test_must_fail git cat-file commit $SHA1_2 &&
1252 git fetch ../testrepo/.git $SHA1_2 &&
1253 git cat-file commit $SHA1_2 &&
Junio C Hamano57a6b932019-04-25 16:41:23 +09001254 test_must_fail env GIT_TEST_PROTOCOL_VERSION= \
1255 git fetch ../testrepo/.git $SHA1_3 2>err &&
Jeff King533ddba2019-04-13 01:54:02 -04001256 test_i18ngrep "remote error:.*not our ref.*$SHA1_3\$" err
Fredrik Medley68ee6282015-05-21 22:23:39 +02001257 )
1258 '
1259done
1260
Junio C Hamanoc2aba152013-03-04 12:09:50 -08001261test_expect_success 'fetch follows tags by default' '
Jeff King2e433b72013-04-02 13:10:31 +05301262 mk_test testrepo heads/master &&
Junio C Hamanoc2aba152013-03-04 12:09:50 -08001263 rm -fr src dst &&
1264 git init src &&
1265 (
1266 cd src &&
1267 git pull ../testrepo master &&
1268 git tag -m "annotated" tag &&
1269 git for-each-ref >tmp1 &&
1270 (
1271 cat tmp1
1272 sed -n "s|refs/heads/master$|refs/remotes/origin/master|p" tmp1
1273 ) |
1274 sort -k 3 >../expect
1275 ) &&
1276 git init dst &&
1277 (
1278 cd dst &&
1279 git remote add origin ../src &&
1280 git config branch.master.remote origin &&
1281 git config branch.master.merge refs/heads/master &&
1282 git pull &&
1283 git for-each-ref >../actual
1284 ) &&
1285 test_cmp expect actual
1286'
1287
Jeff King34066f02019-04-13 01:57:37 -04001288test_expect_success 'peeled advertisements are not considered ref tips' '
1289 mk_empty testrepo &&
1290 git -C testrepo commit --allow-empty -m one &&
1291 git -C testrepo commit --allow-empty -m two &&
1292 git -C testrepo tag -m foo mytag HEAD^ &&
1293 oid=$(git -C testrepo rev-parse mytag^{commit}) &&
1294 test_must_fail env GIT_TEST_PROTOCOL_VERSION= \
1295 git fetch testrepo $oid 2>err &&
1296 test_i18ngrep "Server does not allow request for unadvertised object" err
1297'
1298
Junio C Hamanoca024652013-12-03 15:41:15 -08001299test_expect_success 'pushing a specific ref applies remote.$name.push as refmap' '
1300 mk_test testrepo heads/master &&
1301 rm -fr src dst &&
1302 git init src &&
1303 git init --bare dst &&
1304 (
1305 cd src &&
1306 git pull ../testrepo master &&
1307 git branch next &&
1308 git config remote.dst.url ../dst &&
1309 git config remote.dst.push "+refs/heads/*:refs/remotes/src/*" &&
1310 git push dst master &&
1311 git show-ref refs/heads/master |
1312 sed -e "s|refs/heads/|refs/remotes/src/|" >../dst/expect
1313 ) &&
1314 (
1315 cd dst &&
1316 test_must_fail git show-ref refs/heads/next &&
1317 test_must_fail git show-ref refs/heads/master &&
1318 git show-ref refs/remotes/src/master >actual
1319 ) &&
1320 test_cmp dst/expect dst/actual
1321'
1322
1323test_expect_success 'with no remote.$name.push, it is not used as refmap' '
1324 mk_test testrepo heads/master &&
1325 rm -fr src dst &&
1326 git init src &&
1327 git init --bare dst &&
1328 (
1329 cd src &&
1330 git pull ../testrepo master &&
1331 git branch next &&
1332 git config remote.dst.url ../dst &&
Junio C Hamanofc9261c2013-12-03 16:23:35 -08001333 git config push.default matching &&
Junio C Hamanoca024652013-12-03 15:41:15 -08001334 git push dst master &&
1335 git show-ref refs/heads/master >../dst/expect
1336 ) &&
1337 (
1338 cd dst &&
1339 test_must_fail git show-ref refs/heads/next &&
1340 git show-ref refs/heads/master >actual
1341 ) &&
1342 test_cmp dst/expect dst/actual
1343'
1344
Junio C Hamanofc9261c2013-12-03 16:23:35 -08001345test_expect_success 'with no remote.$name.push, upstream mapping is used' '
1346 mk_test testrepo heads/master &&
1347 rm -fr src dst &&
1348 git init src &&
1349 git init --bare dst &&
1350 (
1351 cd src &&
1352 git pull ../testrepo master &&
1353 git branch next &&
1354 git config remote.dst.url ../dst &&
1355 git config remote.dst.fetch "+refs/heads/*:refs/remotes/dst/*" &&
1356 git config push.default upstream &&
1357
1358 git config branch.master.merge refs/heads/trunk &&
1359 git config branch.master.remote dst &&
1360
1361 git push dst master &&
1362 git show-ref refs/heads/master |
1363 sed -e "s|refs/heads/master|refs/heads/trunk|" >../dst/expect
1364 ) &&
1365 (
1366 cd dst &&
1367 test_must_fail git show-ref refs/heads/master &&
1368 test_must_fail git show-ref refs/heads/next &&
1369 git show-ref refs/heads/trunk >actual
1370 ) &&
1371 test_cmp dst/expect dst/actual
1372'
1373
Junio C Hamanoc2aba152013-03-04 12:09:50 -08001374test_expect_success 'push does not follow tags by default' '
Jeff King2e433b72013-04-02 13:10:31 +05301375 mk_test testrepo heads/master &&
Junio C Hamanoc2aba152013-03-04 12:09:50 -08001376 rm -fr src dst &&
1377 git init src &&
1378 git init --bare dst &&
1379 (
1380 cd src &&
1381 git pull ../testrepo master &&
1382 git tag -m "annotated" tag &&
1383 git checkout -b another &&
1384 git commit --allow-empty -m "future commit" &&
1385 git tag -m "future" future &&
1386 git checkout master &&
1387 git for-each-ref refs/heads/master >../expect &&
1388 git push ../dst master
1389 ) &&
1390 (
1391 cd dst &&
1392 git for-each-ref >../actual
1393 ) &&
1394 test_cmp expect actual
1395'
1396
Johannes Schindelinf6188dc2019-03-25 11:14:21 -07001397test_expect_success 'push --follow-tags only pushes relevant tags' '
Jeff King2e433b72013-04-02 13:10:31 +05301398 mk_test testrepo heads/master &&
Junio C Hamanoc2aba152013-03-04 12:09:50 -08001399 rm -fr src dst &&
1400 git init src &&
1401 git init --bare dst &&
1402 (
1403 cd src &&
1404 git pull ../testrepo master &&
1405 git tag -m "annotated" tag &&
1406 git checkout -b another &&
1407 git commit --allow-empty -m "future commit" &&
1408 git tag -m "future" future &&
1409 git checkout master &&
Eric Sunshine51b85472018-07-01 20:24:01 -04001410 git for-each-ref refs/heads/master refs/tags/tag >../expect &&
Johannes Schindelinf6188dc2019-03-25 11:14:21 -07001411 git push --follow-tags ../dst master
Junio C Hamanoc2aba152013-03-04 12:09:50 -08001412 ) &&
1413 (
1414 cd dst &&
1415 git for-each-ref >../actual
1416 ) &&
1417 test_cmp expect actual
1418'
1419
Nguyễn Thái Ngọc Duyf7c815c2013-08-12 20:55:55 +07001420test_expect_success 'push --no-thin must produce non-thin pack' '
1421 cat >>path1 <<\EOF &&
1422keep base version of path1 big enough, compared to the new changes
1423later, in order to pass size heuristics in
1424builtin/pack-objects.c:try_delta()
1425EOF
1426 git commit -am initial &&
1427 git init no-thin &&
1428 git --git-dir=no-thin/.git config receive.unpacklimit 0 &&
1429 git push no-thin/.git refs/heads/master:refs/heads/foo &&
1430 echo modified >> path1 &&
1431 git commit -am modified &&
1432 git repack -adf &&
1433 rcvpck="git receive-pack --reject-thin-pack-for-testing" &&
1434 git push --no-thin --receive-pack="$rcvpck" no-thin/.git refs/heads/master:refs/heads/foo
1435'
1436
Jeff King458a7e52014-10-16 20:03:41 -04001437test_expect_success 'pushing a tag pushes the tagged object' '
1438 rm -rf dst.git &&
1439 blob=$(echo unreferenced | git hash-object -w --stdin) &&
1440 git tag -m foo tag-of-blob $blob &&
1441 git init --bare dst.git &&
1442 git push dst.git tag-of-blob &&
1443 # the receiving index-pack should have noticed
1444 # any problems, but we double check
1445 echo unreferenced >expect &&
1446 git --git-dir=dst.git cat-file blob tag-of-blob >actual &&
1447 test_cmp expect actual
1448'
1449
Jeff King72549df2014-11-04 08:11:19 -05001450test_expect_success 'push into bare respects core.logallrefupdates' '
1451 rm -rf dst.git &&
1452 git init --bare dst.git &&
1453 git -C dst.git config core.logallrefupdates true &&
1454
1455 # double push to test both with and without
1456 # the actual pack transfer
1457 git push dst.git master:one &&
1458 echo "one@{0} push" >expect &&
1459 git -C dst.git log -g --format="%gd %gs" one >actual &&
1460 test_cmp expect actual &&
1461
1462 git push dst.git master:two &&
1463 echo "two@{0} push" >expect &&
1464 git -C dst.git log -g --format="%gd %gs" two >actual &&
1465 test_cmp expect actual
1466'
1467
1468test_expect_success 'fetch into bare respects core.logallrefupdates' '
1469 rm -rf dst.git &&
1470 git init --bare dst.git &&
1471 (
1472 cd dst.git &&
1473 git config core.logallrefupdates true &&
1474
1475 # as above, we double-fetch to test both
1476 # with and without pack transfer
1477 git fetch .. master:one &&
1478 echo "one@{0} fetch .. master:one: storing head" >expect &&
1479 git log -g --format="%gd %gs" one >actual &&
1480 test_cmp expect actual &&
1481
1482 git fetch .. master:two &&
1483 echo "two@{0} fetch .. master:two: storing head" >expect &&
1484 git log -g --format="%gd %gs" two >actual &&
1485 test_cmp expect actual
1486 )
1487'
1488
Johannes Schindelin1404bcb2014-11-26 23:44:16 +01001489test_expect_success 'receive.denyCurrentBranch = updateInstead' '
1490 git push testrepo master &&
Junio C Hamano4d7a5ce2014-11-30 17:54:30 -08001491 (
1492 cd testrepo &&
Johannes Schindelin1404bcb2014-11-26 23:44:16 +01001493 git reset --hard &&
1494 git config receive.denyCurrentBranch updateInstead
1495 ) &&
1496 test_commit third path2 &&
Junio C Hamano4d7a5ce2014-11-30 17:54:30 -08001497
1498 # Try pushing into a repository with pristine working tree
Johannes Schindelin1404bcb2014-11-26 23:44:16 +01001499 git push testrepo master &&
Junio C Hamano4d7a5ce2014-11-30 17:54:30 -08001500 (
1501 cd testrepo &&
Johannes Schindelin1404bcb2014-11-26 23:44:16 +01001502 git update-index -q --refresh &&
1503 git diff-files --quiet -- &&
1504 git diff-index --quiet --cached HEAD -- &&
Junio C Hamano4d7a5ce2014-11-30 17:54:30 -08001505 test third = "$(cat path2)" &&
1506 test $(git -C .. rev-parse HEAD) = $(git rev-parse HEAD)
Johannes Schindelin1404bcb2014-11-26 23:44:16 +01001507 ) &&
Junio C Hamano4d7a5ce2014-11-30 17:54:30 -08001508
1509 # Try pushing into a repository with working tree needing a refresh
1510 (
1511 cd testrepo &&
1512 git reset --hard HEAD^ &&
1513 test $(git -C .. rev-parse HEAD^) = $(git rev-parse HEAD) &&
Nguyễn Thái Ngọc Duy0e496492018-03-24 08:44:31 +01001514 test-tool chmtime +100 path1
Junio C Hamano4d7a5ce2014-11-30 17:54:30 -08001515 ) &&
1516 git push testrepo master &&
1517 (
1518 cd testrepo &&
1519 git update-index -q --refresh &&
1520 git diff-files --quiet -- &&
1521 git diff-index --quiet --cached HEAD -- &&
1522 test_cmp ../path1 path1 &&
1523 test third = "$(cat path2)" &&
1524 test $(git -C .. rev-parse HEAD) = $(git rev-parse HEAD)
1525 ) &&
1526
1527 # Update what is to be pushed
Johannes Schindelin1404bcb2014-11-26 23:44:16 +01001528 test_commit fourth path2 &&
Junio C Hamano4d7a5ce2014-11-30 17:54:30 -08001529
1530 # Try pushing into a repository with a dirty working tree
1531 # (1) the working tree updated
1532 (
1533 cd testrepo &&
1534 echo changed >path1
1535 ) &&
Johannes Schindelin1404bcb2014-11-26 23:44:16 +01001536 test_must_fail git push testrepo master &&
Junio C Hamano4d7a5ce2014-11-30 17:54:30 -08001537 (
1538 cd testrepo &&
1539 test $(git -C .. rev-parse HEAD^) = $(git rev-parse HEAD) &&
1540 git diff --quiet --cached &&
1541 test changed = "$(cat path1)"
1542 ) &&
1543
1544 # (2) the index updated
1545 (
1546 cd testrepo &&
1547 echo changed >path1 &&
1548 git add path1
1549 ) &&
1550 test_must_fail git push testrepo master &&
1551 (
1552 cd testrepo &&
1553 test $(git -C .. rev-parse HEAD^) = $(git rev-parse HEAD) &&
Johannes Schindelin1404bcb2014-11-26 23:44:16 +01001554 git diff --quiet &&
Junio C Hamano4d7a5ce2014-11-30 17:54:30 -08001555 test changed = "$(cat path1)"
1556 ) &&
1557
1558 # Introduce a new file in the update
1559 test_commit fifth path3 &&
1560
1561 # (3) the working tree has an untracked file that would interfere
1562 (
1563 cd testrepo &&
1564 git reset --hard &&
1565 echo changed >path3
1566 ) &&
1567 test_must_fail git push testrepo master &&
1568 (
1569 cd testrepo &&
1570 test $(git -C .. rev-parse HEAD^^) = $(git rev-parse HEAD) &&
1571 git diff --quiet &&
1572 git diff --quiet --cached &&
1573 test changed = "$(cat path3)"
1574 ) &&
1575
1576 # (4) the target changes to what gets pushed but it still is a change
1577 (
1578 cd testrepo &&
1579 git reset --hard &&
1580 echo fifth >path3 &&
1581 git add path3
1582 ) &&
1583 test_must_fail git push testrepo master &&
1584 (
1585 cd testrepo &&
1586 test $(git -C .. rev-parse HEAD^^) = $(git rev-parse HEAD) &&
1587 git diff --quiet &&
1588 test fifth = "$(cat path3)"
Junio C Hamano1a51b522015-03-31 23:15:45 -07001589 ) &&
Junio C Hamano4d7a5ce2014-11-30 17:54:30 -08001590
Junio C Hamano1a51b522015-03-31 23:15:45 -07001591 # (5) push into void
1592 rm -fr void &&
1593 git init void &&
1594 (
1595 cd void &&
1596 git config receive.denyCurrentBranch updateInstead
1597 ) &&
1598 git push void master &&
1599 (
1600 cd void &&
1601 test $(git -C .. rev-parse master) = $(git rev-parse HEAD) &&
1602 git diff --quiet &&
1603 git diff --cached --quiet
Junio C Hamanob072a252018-10-19 13:57:35 +09001604 ) &&
1605
1606 # (6) updateInstead intervened by fast-forward check
1607 test_must_fail git push void master^:master &&
1608 test $(git -C void rev-parse HEAD) = $(git rev-parse master) &&
1609 git -C void diff --quiet &&
1610 git -C void diff --cached --quiet
Johannes Schindelin1404bcb2014-11-26 23:44:16 +01001611'
1612
Junio C Hamano08553312014-12-01 15:29:54 -08001613test_expect_success 'updateInstead with push-to-checkout hook' '
1614 rm -fr testrepo &&
1615 git init testrepo &&
1616 (
1617 cd testrepo &&
1618 git pull .. master &&
1619 git reset --hard HEAD^^ &&
1620 git tag initial &&
1621 git config receive.denyCurrentBranch updateInstead &&
1622 write_script .git/hooks/push-to-checkout <<-\EOF
1623 echo >&2 updating from $(git rev-parse HEAD)
1624 echo >&2 updating to "$1"
1625
1626 git update-index -q --refresh &&
1627 git read-tree -u -m HEAD "$1" || {
1628 status=$?
1629 echo >&2 read-tree failed
1630 exit $status
1631 }
1632 EOF
1633 ) &&
1634
1635 # Try pushing into a pristine
1636 git push testrepo master &&
1637 (
1638 cd testrepo &&
1639 git diff --quiet &&
1640 git diff HEAD --quiet &&
1641 test $(git -C .. rev-parse HEAD) = $(git rev-parse HEAD)
1642 ) &&
1643
1644 # Try pushing into a repository with conflicting change
1645 (
1646 cd testrepo &&
1647 git reset --hard initial &&
1648 echo conflicting >path2
1649 ) &&
1650 test_must_fail git push testrepo master &&
1651 (
1652 cd testrepo &&
1653 test $(git rev-parse initial) = $(git rev-parse HEAD) &&
1654 test conflicting = "$(cat path2)" &&
1655 git diff-index --quiet --cached HEAD
1656 ) &&
1657
1658 # Try pushing into a repository with unrelated change
1659 (
1660 cd testrepo &&
1661 git reset --hard initial &&
1662 echo unrelated >path1 &&
1663 echo irrelevant >path5 &&
1664 git add path5
1665 ) &&
1666 git push testrepo master &&
1667 (
1668 cd testrepo &&
1669 test "$(cat path1)" = unrelated &&
1670 test "$(cat path5)" = irrelevant &&
1671 test "$(git diff --name-only --cached HEAD)" = path5 &&
1672 test $(git -C .. rev-parse HEAD) = $(git rev-parse HEAD)
Junio C Hamano1a51b522015-03-31 23:15:45 -07001673 ) &&
1674
1675 # push into void
1676 rm -fr void &&
1677 git init void &&
1678 (
1679 cd void &&
1680 git config receive.denyCurrentBranch updateInstead &&
1681 write_script .git/hooks/push-to-checkout <<-\EOF
1682 if git rev-parse --quiet --verify HEAD
1683 then
1684 has_head=yes
1685 echo >&2 updating from $(git rev-parse HEAD)
1686 else
1687 has_head=no
1688 echo >&2 pushing into void
1689 fi
1690 echo >&2 updating to "$1"
1691
1692 git update-index -q --refresh &&
1693 case "$has_head" in
1694 yes)
1695 git read-tree -u -m HEAD "$1" ;;
1696 no)
1697 git read-tree -u -m "$1" ;;
1698 esac || {
1699 status=$?
1700 echo >&2 read-tree failed
1701 exit $status
1702 }
1703 EOF
1704 ) &&
1705
1706 git push void master &&
1707 (
1708 cd void &&
1709 git diff --quiet &&
1710 git diff --cached --quiet &&
1711 test $(git -C .. rev-parse HEAD) = $(git rev-parse HEAD)
Junio C Hamano08553312014-12-01 15:29:54 -08001712 )
1713'
1714
Junio C Hamanobcdb34f2007-06-08 00:43:22 -07001715test_done