blob: de0bab398e762245fd3799ca69e283e4928bfcaf [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
Johannes Schindelinbc925ce2020-11-18 23:44:32 +000017GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
Johannes Schindelin334afbc2020-11-18 23:44:19 +000018export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
19
Junio C Hamanobcdb34f2007-06-08 00:43:22 -070020. ./test-lib.sh
21
Elia Pintobf452422015-12-23 14:45:57 +010022D=$(pwd)
Junio C Hamanobcdb34f2007-06-08 00:43:22 -070023
24mk_empty () {
Jeff King2e433b72013-04-02 13:10:31 +053025 repo_name="$1"
26 rm -fr "$repo_name" &&
27 mkdir "$repo_name" &&
Junio C Hamanobcdb34f2007-06-08 00:43:22 -070028 (
Jeff King2e433b72013-04-02 13:10:31 +053029 cd "$repo_name" &&
Alex Riesen586e4ce2007-07-04 14:06:04 +020030 git init &&
Junio C Hamanoacd2a452009-02-11 02:28:03 -080031 git config receive.denyCurrentBranch warn &&
Alex Riesen586e4ce2007-07-04 14:06:04 +020032 mv .git/hooks .git/hooks-disabled
Junio C Hamanobcdb34f2007-06-08 00:43:22 -070033 )
34}
35
Junio C Hamano61257962007-06-09 01:23:53 -070036mk_test () {
Jeff King2e433b72013-04-02 13:10:31 +053037 repo_name="$1"
38 shift
39
40 mk_empty "$repo_name" &&
Junio C Hamano61257962007-06-09 01:23:53 -070041 (
42 for ref in "$@"
43 do
Jeff King2e433b72013-04-02 13:10:31 +053044 git push "$repo_name" $the_first_commit:refs/$ref ||
Jonathan Nieder5bd81c72013-03-18 16:14:26 -070045 exit
Junio C Hamano61257962007-06-09 01:23:53 -070046 done &&
Jeff King2e433b72013-04-02 13:10:31 +053047 cd "$repo_name" &&
Junio C Hamano61257962007-06-09 01:23:53 -070048 for ref in "$@"
49 do
Jonathan Nieder848575d2013-03-18 16:13:41 -070050 echo "$the_first_commit" >expect &&
51 git show-ref -s --verify refs/$ref >actual &&
52 test_cmp expect actual ||
53 exit
Junio C Hamano61257962007-06-09 01:23:53 -070054 done &&
55 git fsck --full
56 )
57}
58
Pang Yan Han160b81e2011-09-28 23:39:35 +080059mk_test_with_hooks() {
Jeff King2e433b72013-04-02 13:10:31 +053060 repo_name=$1
Pang Yan Han160b81e2011-09-28 23:39:35 +080061 mk_test "$@" &&
62 (
Jeff King2e433b72013-04-02 13:10:31 +053063 cd "$repo_name" &&
Pang Yan Han160b81e2011-09-28 23:39:35 +080064 mkdir .git/hooks &&
65 cd .git/hooks &&
66
67 cat >pre-receive <<-'EOF' &&
68 #!/bin/sh
69 cat - >>pre-receive.actual
70 EOF
71
72 cat >update <<-'EOF' &&
73 #!/bin/sh
74 printf "%s %s %s\n" "$@" >>update.actual
75 EOF
76
77 cat >post-receive <<-'EOF' &&
78 #!/bin/sh
79 cat - >>post-receive.actual
80 EOF
81
82 cat >post-update <<-'EOF' &&
83 #!/bin/sh
84 for ref in "$@"
85 do
86 printf "%s\n" "$ref" >>post-update.actual
87 done
88 EOF
89
90 chmod +x pre-receive update post-receive post-update
91 )
92}
93
Jeff Kingb2dc9682008-11-07 17:20:33 -050094mk_child() {
Jeff King2e433b72013-04-02 13:10:31 +053095 rm -rf "$2" &&
96 git clone "$1" "$2"
Jeff Kingb2dc9682008-11-07 17:20:33 -050097}
98
Junio C Hamano61257962007-06-09 01:23:53 -070099check_push_result () {
SZEDER Gáborcfb482b2018-05-10 16:01:53 +0200100 test $# -ge 3 ||
SZEDER Gábor165293a2018-11-19 14:13:26 +0100101 BUG "check_push_result requires at least 3 parameters"
SZEDER Gáborcfb482b2018-05-10 16:01:53 +0200102
Jeff King2e433b72013-04-02 13:10:31 +0530103 repo_name="$1"
104 shift
105
Junio C Hamano61257962007-06-09 01:23:53 -0700106 (
Jeff King2e433b72013-04-02 13:10:31 +0530107 cd "$repo_name" &&
Jonathan Nieder848575d2013-03-18 16:13:41 -0700108 echo "$1" >expect &&
109 shift &&
Junio C Hamano61257962007-06-09 01:23:53 -0700110 for ref in "$@"
111 do
Jonathan Nieder848575d2013-03-18 16:13:41 -0700112 git show-ref -s --verify refs/$ref >actual &&
113 test_cmp expect actual ||
114 exit
Junio C Hamano61257962007-06-09 01:23:53 -0700115 done &&
116 git fsck --full
117 )
118}
119
Junio C Hamanobcdb34f2007-06-08 00:43:22 -0700120test_expect_success setup '
121
Jay Soffiand4785cd2010-04-19 18:08:31 -0400122 >path1 &&
Junio C Hamanobcdb34f2007-06-08 00:43:22 -0700123 git add path1 &&
124 test_tick &&
125 git commit -a -m repo &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000126 the_first_commit=$(git show-ref -s --verify refs/heads/main) &&
Junio C Hamano61257962007-06-09 01:23:53 -0700127
Jay Soffiand4785cd2010-04-19 18:08:31 -0400128 >path2 &&
Junio C Hamano61257962007-06-09 01:23:53 -0700129 git add path2 &&
130 test_tick &&
131 git commit -a -m second &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000132 the_commit=$(git show-ref -s --verify refs/heads/main)
Junio C Hamanobcdb34f2007-06-08 00:43:22 -0700133
134'
135
136test_expect_success 'fetch without wildcard' '
Jeff King2e433b72013-04-02 13:10:31 +0530137 mk_empty testrepo &&
Junio C Hamanobcdb34f2007-06-08 00:43:22 -0700138 (
139 cd testrepo &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000140 git fetch .. refs/heads/main:refs/remotes/origin/main &&
Junio C Hamanobcdb34f2007-06-08 00:43:22 -0700141
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000142 echo "$the_commit commit refs/remotes/origin/main" >expect &&
Jonathan Nieder848575d2013-03-18 16:13:41 -0700143 git for-each-ref refs/remotes/origin >actual &&
144 test_cmp expect actual
Junio C Hamanobcdb34f2007-06-08 00:43:22 -0700145 )
146'
147
148test_expect_success 'fetch with wildcard' '
Jeff King2e433b72013-04-02 13:10:31 +0530149 mk_empty testrepo &&
Junio C Hamanobcdb34f2007-06-08 00:43:22 -0700150 (
151 cd testrepo &&
152 git config remote.up.url .. &&
153 git config remote.up.fetch "refs/heads/*:refs/remotes/origin/*" &&
154 git fetch up &&
155
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000156 echo "$the_commit commit refs/remotes/origin/main" >expect &&
Jonathan Nieder848575d2013-03-18 16:13:41 -0700157 git for-each-ref refs/remotes/origin >actual &&
158 test_cmp expect actual
Junio C Hamanobcdb34f2007-06-08 00:43:22 -0700159 )
160'
161
Daniel Barkalow55029ae2008-02-20 13:43:53 -0500162test_expect_success 'fetch with insteadOf' '
Jeff King2e433b72013-04-02 13:10:31 +0530163 mk_empty testrepo &&
Daniel Barkalow55029ae2008-02-20 13:43:53 -0500164 (
Daniel Barkalow60e3aba2008-04-12 15:32:00 -0400165 TRASH=$(pwd)/ &&
Daniel Barkalow55029ae2008-02-20 13:43:53 -0500166 cd testrepo &&
Bryan Donlanf69e8362008-05-04 01:37:59 -0400167 git config "url.$TRASH.insteadOf" trash/ &&
Daniel Barkalow55029ae2008-02-20 13:43:53 -0500168 git config remote.up.url trash/. &&
169 git config remote.up.fetch "refs/heads/*:refs/remotes/origin/*" &&
170 git fetch up &&
171
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000172 echo "$the_commit commit refs/remotes/origin/main" >expect &&
Jonathan Nieder848575d2013-03-18 16:13:41 -0700173 git for-each-ref refs/remotes/origin >actual &&
174 test_cmp expect actual
Daniel Barkalow55029ae2008-02-20 13:43:53 -0500175 )
176'
177
Josh Triplett1c2eafb2009-09-07 01:56:33 -0700178test_expect_success 'fetch with pushInsteadOf (should not rewrite)' '
Jeff King2e433b72013-04-02 13:10:31 +0530179 mk_empty testrepo &&
Josh Triplett1c2eafb2009-09-07 01:56:33 -0700180 (
181 TRASH=$(pwd)/ &&
182 cd testrepo &&
183 git config "url.trash/.pushInsteadOf" "$TRASH" &&
184 git config remote.up.url "$TRASH." &&
185 git config remote.up.fetch "refs/heads/*:refs/remotes/origin/*" &&
186 git fetch up &&
187
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000188 echo "$the_commit commit refs/remotes/origin/main" >expect &&
Jonathan Nieder848575d2013-03-18 16:13:41 -0700189 git for-each-ref refs/remotes/origin >actual &&
190 test_cmp expect actual
Josh Triplett1c2eafb2009-09-07 01:56:33 -0700191 )
192'
193
Jonathan Tan477673d2021-05-04 14:16:02 -0700194grep_wrote () {
195 object_count=$1
196 file_name=$2
197 grep 'write_pack_file/wrote.*"value":"'$1'"' $2
198}
199
200test_expect_success 'push with negotiation' '
201 # Without negotiation
202 mk_empty testrepo &&
203 git push testrepo $the_first_commit:refs/remotes/origin/first_commit &&
Jonathan Tan54a03bc2021-07-15 10:44:31 -0700204 test_commit -C testrepo unrelated_commit &&
Jonathan Tan477673d2021-05-04 14:16:02 -0700205 git -C testrepo config receive.hideRefs refs/remotes/origin/first_commit &&
206 echo now pushing without negotiation &&
207 GIT_TRACE2_EVENT="$(pwd)/event" git -c protocol.version=2 push testrepo refs/heads/main:refs/remotes/origin/main &&
208 grep_wrote 5 event && # 2 commits, 2 trees, 1 blob
209
210 # Same commands, but with negotiation
211 rm event &&
212 mk_empty testrepo &&
213 git push testrepo $the_first_commit:refs/remotes/origin/first_commit &&
Jonathan Tan54a03bc2021-07-15 10:44:31 -0700214 test_commit -C testrepo unrelated_commit &&
Jonathan Tan477673d2021-05-04 14:16:02 -0700215 git -C testrepo config receive.hideRefs refs/remotes/origin/first_commit &&
216 GIT_TRACE2_EVENT="$(pwd)/event" git -c protocol.version=2 -c push.negotiate=1 push testrepo refs/heads/main:refs/remotes/origin/main &&
217 grep_wrote 2 event # 1 commit, 1 tree
218'
219
220test_expect_success 'push with negotiation proceeds anyway even if negotiation fails' '
221 rm event &&
222 mk_empty testrepo &&
223 git push testrepo $the_first_commit:refs/remotes/origin/first_commit &&
Jonathan Tan54a03bc2021-07-15 10:44:31 -0700224 test_commit -C testrepo unrelated_commit &&
Jonathan Tan477673d2021-05-04 14:16:02 -0700225 git -C testrepo config receive.hideRefs refs/remotes/origin/first_commit &&
226 GIT_TEST_PROTOCOL_VERSION=0 GIT_TRACE2_EVENT="$(pwd)/event" \
227 git -c push.negotiate=1 push testrepo refs/heads/main:refs/remotes/origin/main 2>err &&
228 grep_wrote 5 event && # 2 commits, 2 trees, 1 blob
229 test_i18ngrep "push negotiation failed" err
230'
231
Junio C Hamanobcdb34f2007-06-08 00:43:22 -0700232test_expect_success 'push without wildcard' '
Jeff King2e433b72013-04-02 13:10:31 +0530233 mk_empty testrepo &&
Junio C Hamanobcdb34f2007-06-08 00:43:22 -0700234
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000235 git push testrepo refs/heads/main:refs/remotes/origin/main &&
Junio C Hamanobcdb34f2007-06-08 00:43:22 -0700236 (
237 cd testrepo &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000238 echo "$the_commit commit refs/remotes/origin/main" >expect &&
Jonathan Nieder848575d2013-03-18 16:13:41 -0700239 git for-each-ref refs/remotes/origin >actual &&
240 test_cmp expect actual
Junio C Hamanobcdb34f2007-06-08 00:43:22 -0700241 )
242'
243
244test_expect_success 'push with wildcard' '
Jeff King2e433b72013-04-02 13:10:31 +0530245 mk_empty testrepo &&
Junio C Hamanobcdb34f2007-06-08 00:43:22 -0700246
247 git push testrepo "refs/heads/*:refs/remotes/origin/*" &&
248 (
249 cd testrepo &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000250 echo "$the_commit commit refs/remotes/origin/main" >expect &&
Jonathan Nieder848575d2013-03-18 16:13:41 -0700251 git for-each-ref refs/remotes/origin >actual &&
252 test_cmp expect actual
Junio C Hamanobcdb34f2007-06-08 00:43:22 -0700253 )
254'
255
Daniel Barkalow55029ae2008-02-20 13:43:53 -0500256test_expect_success 'push with insteadOf' '
Jeff King2e433b72013-04-02 13:10:31 +0530257 mk_empty testrepo &&
Bryan Donlanf69e8362008-05-04 01:37:59 -0400258 TRASH="$(pwd)/" &&
Jonathan Nieder3c695522013-03-18 16:12:11 -0700259 test_config "url.$TRASH.insteadOf" trash/ &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000260 git push trash/testrepo refs/heads/main:refs/remotes/origin/main &&
Daniel Barkalow55029ae2008-02-20 13:43:53 -0500261 (
262 cd testrepo &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000263 echo "$the_commit commit refs/remotes/origin/main" >expect &&
Jonathan Nieder848575d2013-03-18 16:13:41 -0700264 git for-each-ref refs/remotes/origin >actual &&
265 test_cmp expect actual
Daniel Barkalow55029ae2008-02-20 13:43:53 -0500266 )
267'
268
Josh Triplett1c2eafb2009-09-07 01:56:33 -0700269test_expect_success 'push with pushInsteadOf' '
Jeff King2e433b72013-04-02 13:10:31 +0530270 mk_empty testrepo &&
Josh Triplett1c2eafb2009-09-07 01:56:33 -0700271 TRASH="$(pwd)/" &&
Jonathan Nieder3c695522013-03-18 16:12:11 -0700272 test_config "url.$TRASH.pushInsteadOf" trash/ &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000273 git push trash/testrepo refs/heads/main:refs/remotes/origin/main &&
Josh Triplett1c2eafb2009-09-07 01:56:33 -0700274 (
275 cd testrepo &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000276 echo "$the_commit commit refs/remotes/origin/main" >expect &&
Jonathan Nieder848575d2013-03-18 16:13:41 -0700277 git for-each-ref refs/remotes/origin >actual &&
278 test_cmp expect actual
Josh Triplett1c2eafb2009-09-07 01:56:33 -0700279 )
280'
281
282test_expect_success 'push with pushInsteadOf and explicit pushurl (pushInsteadOf should not rewrite)' '
Jeff King2e433b72013-04-02 13:10:31 +0530283 mk_empty testrepo &&
Junio C Hamano41ae34d2013-04-03 09:34:48 -0700284 test_config "url.trash2/.pushInsteadOf" testrepo/ &&
Anders Kaseorgeb32c662015-02-28 23:18:14 -0500285 test_config "url.trash3/.pushInsteadOf" trash/wrong &&
Jonathan Nieder3c695522013-03-18 16:12:11 -0700286 test_config remote.r.url trash/wrong &&
Junio C Hamano41ae34d2013-04-03 09:34:48 -0700287 test_config remote.r.pushurl "testrepo/" &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000288 git push r refs/heads/main:refs/remotes/origin/main &&
Josh Triplett1c2eafb2009-09-07 01:56:33 -0700289 (
290 cd testrepo &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000291 echo "$the_commit commit refs/remotes/origin/main" >expect &&
Jonathan Nieder848575d2013-03-18 16:13:41 -0700292 git for-each-ref refs/remotes/origin >actual &&
293 test_cmp expect actual
Josh Triplett1c2eafb2009-09-07 01:56:33 -0700294 )
295'
296
Junio C Hamano61257962007-06-09 01:23:53 -0700297test_expect_success 'push with matching heads' '
298
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000299 mk_test testrepo heads/main &&
Junio C Hamano0a42ac02013-01-04 16:08:26 -0800300 git push testrepo : &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000301 check_push_result testrepo $the_commit heads/main
Junio C Hamano61257962007-06-09 01:23:53 -0700302
303'
304
Paolo Bonzinia83619d2008-04-28 11:32:12 -0400305test_expect_success 'push with matching heads on the command line' '
306
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000307 mk_test testrepo heads/main &&
Paolo Bonzinia83619d2008-04-28 11:32:12 -0400308 git push testrepo : &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000309 check_push_result testrepo $the_commit heads/main
Paolo Bonzinia83619d2008-04-28 11:32:12 -0400310
311'
312
313test_expect_success 'failed (non-fast-forward) push with matching heads' '
314
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000315 mk_test testrepo heads/main &&
Paolo Bonzinia83619d2008-04-28 11:32:12 -0400316 git push testrepo : &&
317 git commit --amend -massaged &&
Stephan Beyerd492b312008-07-12 17:47:52 +0200318 test_must_fail git push testrepo &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000319 check_push_result testrepo $the_commit heads/main &&
Paolo Bonzinia83619d2008-04-28 11:32:12 -0400320 git reset --hard $the_commit
321
322'
323
324test_expect_success 'push --force with matching heads' '
325
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000326 mk_test testrepo heads/main &&
Paolo Bonzinia83619d2008-04-28 11:32:12 -0400327 git push testrepo : &&
328 git commit --amend -massaged &&
Junio C Hamano0a42ac02013-01-04 16:08:26 -0800329 git push --force testrepo : &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000330 ! check_push_result testrepo $the_commit heads/main &&
Paolo Bonzinia83619d2008-04-28 11:32:12 -0400331 git reset --hard $the_commit
332
333'
334
335test_expect_success 'push with matching heads and forced update' '
336
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000337 mk_test testrepo heads/main &&
Paolo Bonzinia83619d2008-04-28 11:32:12 -0400338 git push testrepo : &&
339 git commit --amend -massaged &&
340 git push testrepo +: &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000341 ! check_push_result testrepo $the_commit heads/main &&
Paolo Bonzinia83619d2008-04-28 11:32:12 -0400342 git reset --hard $the_commit
343
344'
345
Junio C Hamano61257962007-06-09 01:23:53 -0700346test_expect_success 'push with no ambiguity (1)' '
347
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000348 mk_test testrepo heads/main &&
349 git push testrepo main:main &&
350 check_push_result testrepo $the_commit heads/main
Junio C Hamano61257962007-06-09 01:23:53 -0700351
352'
353
354test_expect_success 'push with no ambiguity (2)' '
355
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000356 mk_test testrepo remotes/origin/main &&
357 git push testrepo main:origin/main &&
358 check_push_result testrepo $the_commit remotes/origin/main
Junio C Hamano61257962007-06-09 01:23:53 -0700359
360'
361
Steffen Prohaskaae36bdc2007-11-11 15:01:47 +0100362test_expect_success 'push with colon-less refspec, no ambiguity' '
363
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000364 mk_test testrepo heads/main heads/t/main &&
365 git branch -f t/main main &&
366 git push testrepo main &&
367 check_push_result testrepo $the_commit heads/main &&
368 check_push_result testrepo $the_first_commit heads/t/main
Steffen Prohaskaae36bdc2007-11-11 15:01:47 +0100369
370'
371
Junio C Hamano61257962007-06-09 01:23:53 -0700372test_expect_success 'push with weak ambiguity (1)' '
373
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000374 mk_test testrepo heads/main remotes/origin/main &&
375 git push testrepo main:main &&
376 check_push_result testrepo $the_commit heads/main &&
377 check_push_result testrepo $the_first_commit remotes/origin/main
Junio C Hamano61257962007-06-09 01:23:53 -0700378
379'
380
381test_expect_success 'push with weak ambiguity (2)' '
382
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000383 mk_test testrepo heads/main remotes/origin/main remotes/another/main &&
384 git push testrepo main:main &&
385 check_push_result testrepo $the_commit heads/main &&
386 check_push_result testrepo $the_first_commit remotes/origin/main remotes/another/main
Junio C Hamano61257962007-06-09 01:23:53 -0700387
388'
389
Jeff King3ef6a1f2008-04-23 05:21:45 -0400390test_expect_success 'push with ambiguity' '
Junio C Hamano61257962007-06-09 01:23:53 -0700391
Jeff King2e433b72013-04-02 13:10:31 +0530392 mk_test testrepo heads/frotz tags/frotz &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000393 test_must_fail git push testrepo main:frotz &&
Jeff King2e433b72013-04-02 13:10:31 +0530394 check_push_result testrepo $the_first_commit heads/frotz tags/frotz
Junio C Hamano1ed10b82007-06-09 01:37:14 -0700395
396'
397
398test_expect_success 'push with colon-less refspec (1)' '
399
Jeff King2e433b72013-04-02 13:10:31 +0530400 mk_test testrepo heads/frotz tags/frotz &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000401 git branch -f frotz main &&
Junio C Hamano1ed10b82007-06-09 01:37:14 -0700402 git push testrepo frotz &&
Jeff King2e433b72013-04-02 13:10:31 +0530403 check_push_result testrepo $the_commit heads/frotz &&
404 check_push_result testrepo $the_first_commit tags/frotz
Junio C Hamano1ed10b82007-06-09 01:37:14 -0700405
406'
407
408test_expect_success 'push with colon-less refspec (2)' '
409
Jeff King2e433b72013-04-02 13:10:31 +0530410 mk_test testrepo heads/frotz tags/frotz &&
Junio C Hamano1ed10b82007-06-09 01:37:14 -0700411 if git show-ref --verify -q refs/heads/frotz
412 then
413 git branch -D frotz
414 fi &&
415 git tag -f frotz &&
Chris Rorvickdbfeddb2012-11-29 19:41:37 -0600416 git push -f testrepo frotz &&
Jeff King2e433b72013-04-02 13:10:31 +0530417 check_push_result testrepo $the_commit tags/frotz &&
418 check_push_result testrepo $the_first_commit heads/frotz
Junio C Hamano1ed10b82007-06-09 01:37:14 -0700419
420'
421
422test_expect_success 'push with colon-less refspec (3)' '
423
Jeff King2e433b72013-04-02 13:10:31 +0530424 mk_test testrepo &&
Junio C Hamano1ed10b82007-06-09 01:37:14 -0700425 if git show-ref --verify -q refs/tags/frotz
426 then
427 git tag -d frotz
428 fi &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000429 git branch -f frotz main &&
Junio C Hamano1ed10b82007-06-09 01:37:14 -0700430 git push testrepo frotz &&
Jeff King2e433b72013-04-02 13:10:31 +0530431 check_push_result testrepo $the_commit heads/frotz &&
Brian Gernhardt9a3c6f72007-07-01 11:48:54 -0400432 test 1 = $( cd testrepo && git show-ref | wc -l )
Junio C Hamano1ed10b82007-06-09 01:37:14 -0700433'
434
435test_expect_success 'push with colon-less refspec (4)' '
436
Jeff King2e433b72013-04-02 13:10:31 +0530437 mk_test testrepo &&
Junio C Hamano1ed10b82007-06-09 01:37:14 -0700438 if git show-ref --verify -q refs/heads/frotz
439 then
440 git branch -D frotz
441 fi &&
442 git tag -f frotz &&
443 git push testrepo frotz &&
Jeff King2e433b72013-04-02 13:10:31 +0530444 check_push_result testrepo $the_commit tags/frotz &&
Brian Gernhardt9a3c6f72007-07-01 11:48:54 -0400445 test 1 = $( cd testrepo && git show-ref | wc -l )
Junio C Hamano1ed10b82007-06-09 01:37:14 -0700446
Junio C Hamano61257962007-06-09 01:23:53 -0700447'
448
Dmitry Ivankov7be8b3b2011-06-16 19:42:48 +0600449test_expect_success 'push head with non-existent, incomplete dest' '
Jeff Kingf8aae122008-04-23 05:16:06 -0400450
Jeff King2e433b72013-04-02 13:10:31 +0530451 mk_test testrepo &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000452 git push testrepo main:branch &&
Jeff King2e433b72013-04-02 13:10:31 +0530453 check_push_result testrepo $the_commit heads/branch
Jeff Kingf8aae122008-04-23 05:16:06 -0400454
455'
456
Dmitry Ivankov7be8b3b2011-06-16 19:42:48 +0600457test_expect_success 'push tag with non-existent, incomplete dest' '
Jeff Kingf8aae122008-04-23 05:16:06 -0400458
Jeff King2e433b72013-04-02 13:10:31 +0530459 mk_test testrepo &&
Jeff Kingf8aae122008-04-23 05:16:06 -0400460 git tag -f v1.0 &&
461 git push testrepo v1.0:tag &&
Jeff King2e433b72013-04-02 13:10:31 +0530462 check_push_result testrepo $the_commit tags/tag
Jeff Kingf8aae122008-04-23 05:16:06 -0400463
464'
465
Dmitry Ivankov7be8b3b2011-06-16 19:42:48 +0600466test_expect_success 'push sha1 with non-existent, incomplete dest' '
Jeff Kingf8aae122008-04-23 05:16:06 -0400467
Jeff King2e433b72013-04-02 13:10:31 +0530468 mk_test testrepo &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000469 test_must_fail git push testrepo $(git rev-parse main):foo
Jeff Kingf8aae122008-04-23 05:16:06 -0400470
471'
472
Dmitry Ivankov7be8b3b2011-06-16 19:42:48 +0600473test_expect_success 'push ref expression with non-existent, incomplete dest' '
Jeff Kingf8aae122008-04-23 05:16:06 -0400474
Jeff King2e433b72013-04-02 13:10:31 +0530475 mk_test testrepo &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000476 test_must_fail git push testrepo main^:branch
Jeff Kingf8aae122008-04-23 05:16:06 -0400477
478'
479
Felipe Contreras374fbae2020-11-25 18:16:16 -0600480for head in HEAD @
481do
Steffen Prohaska47d996a2007-11-11 15:35:07 +0100482
Felipe Contreras374fbae2020-11-25 18:16:16 -0600483 test_expect_success "push with $head" '
Junio C Hamano27d7c852021-01-25 14:19:18 -0800484 mk_test testrepo heads/main &&
485 git checkout main &&
Felipe Contreras374fbae2020-11-25 18:16:16 -0600486 git push testrepo $head &&
Junio C Hamano27d7c852021-01-25 14:19:18 -0800487 check_push_result testrepo $the_commit heads/main
Felipe Contreras374fbae2020-11-25 18:16:16 -0600488 '
Steffen Prohaska47d996a2007-11-11 15:35:07 +0100489
Felipe Contreras374fbae2020-11-25 18:16:16 -0600490 test_expect_success "push with $head nonexisting at remote" '
Junio C Hamano27d7c852021-01-25 14:19:18 -0800491 mk_test testrepo heads/main &&
492 git checkout -b local main &&
493 test_when_finished "git checkout main; git branch -D local" &&
Felipe Contreras374fbae2020-11-25 18:16:16 -0600494 git push testrepo $head &&
495 check_push_result testrepo $the_commit heads/local
496 '
Daniel Barkalow9f0ea7e2008-02-20 12:54:05 -0500497
Felipe Contreras374fbae2020-11-25 18:16:16 -0600498 test_expect_success "push with +$head" '
Junio C Hamano27d7c852021-01-25 14:19:18 -0800499 mk_test testrepo heads/main &&
500 git checkout -b local main &&
501 test_when_finished "git checkout main; git branch -D local" &&
502 git push testrepo main local &&
503 check_push_result testrepo $the_commit heads/main &&
Felipe Contreras374fbae2020-11-25 18:16:16 -0600504 check_push_result testrepo $the_commit heads/local &&
Daniel Barkalow9f0ea7e2008-02-20 12:54:05 -0500505
Felipe Contreras374fbae2020-11-25 18:16:16 -0600506 # Without force rewinding should fail
507 git reset --hard $head^ &&
508 test_must_fail git push testrepo $head &&
509 check_push_result testrepo $the_commit heads/local &&
Daniel Barkalow9f0ea7e2008-02-20 12:54:05 -0500510
Felipe Contreras374fbae2020-11-25 18:16:16 -0600511 # With force rewinding should succeed
512 git push testrepo +$head &&
513 check_push_result testrepo $the_first_commit heads/local
Felipe Contreras374fbae2020-11-25 18:16:16 -0600514 '
Jeff Kingf8aae122008-04-23 05:16:06 -0400515
Felipe Contreras374fbae2020-11-25 18:16:16 -0600516 test_expect_success "push $head with non-existent, incomplete dest" '
Felipe Contreras374fbae2020-11-25 18:16:16 -0600517 mk_test testrepo &&
Junio C Hamano27d7c852021-01-25 14:19:18 -0800518 git checkout main &&
Felipe Contreras374fbae2020-11-25 18:16:16 -0600519 git push testrepo $head:branch &&
520 check_push_result testrepo $the_commit heads/branch
Jeff Kingf8aae122008-04-23 05:16:06 -0400521
Felipe Contreras374fbae2020-11-25 18:16:16 -0600522 '
Daniel Barkalow9f0ea7e2008-02-20 12:54:05 -0500523
Felipe Contreras374fbae2020-11-25 18:16:16 -0600524 test_expect_success "push with config remote.*.push = $head" '
Felipe Contreras374fbae2020-11-25 18:16:16 -0600525 mk_test testrepo heads/local &&
Junio C Hamano27d7c852021-01-25 14:19:18 -0800526 git checkout main &&
Felipe Contreras374fbae2020-11-25 18:16:16 -0600527 git branch -f local $the_commit &&
528 test_when_finished "git branch -D local" &&
529 (
530 cd testrepo &&
531 git checkout local &&
532 git reset --hard $the_first_commit
533 ) &&
534 test_config remote.there.url testrepo &&
535 test_config remote.there.push $head &&
Junio C Hamano27d7c852021-01-25 14:19:18 -0800536 test_config branch.main.remote there &&
Felipe Contreras374fbae2020-11-25 18:16:16 -0600537 git push &&
Junio C Hamano27d7c852021-01-25 14:19:18 -0800538 check_push_result testrepo $the_commit heads/main &&
Felipe Contreras374fbae2020-11-25 18:16:16 -0600539 check_push_result testrepo $the_first_commit heads/local
540 '
541
542done
Daniel Barkalow9f0ea7e2008-02-20 12:54:05 -0500543
Glen Chooe083ef52021-11-17 16:53:21 -0800544test_expect_success "push to remote with no explicit refspec and config remote.*.push = src:dest" '
545 mk_test testrepo heads/main &&
546 git checkout $the_first_commit &&
547 test_config remote.there.url testrepo &&
548 test_config remote.there.push refs/heads/main:refs/heads/main &&
549 git push there &&
550 check_push_result testrepo $the_commit heads/main
551'
552
Ramkumar Ramachandra224c2172013-04-02 13:10:33 +0530553test_expect_success 'push with remote.pushdefault' '
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000554 mk_test up_repo heads/main &&
555 mk_test down_repo heads/main &&
Ramkumar Ramachandra224c2172013-04-02 13:10:33 +0530556 test_config remote.up.url up_repo &&
557 test_config remote.down.url down_repo &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000558 test_config branch.main.remote up &&
Ramkumar Ramachandra224c2172013-04-02 13:10:33 +0530559 test_config remote.pushdefault down &&
Junio C Hamano54a3c672013-04-18 11:47:59 -0700560 test_config push.default matching &&
Ramkumar Ramachandra224c2172013-04-02 13:10:33 +0530561 git push &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000562 check_push_result up_repo $the_first_commit heads/main &&
563 check_push_result down_repo $the_commit heads/main
Daniel Barkalow9f0ea7e2008-02-20 12:54:05 -0500564'
565
Michael J Grubere1ca4242009-06-09 18:01:35 +0200566test_expect_success 'push with config remote.*.pushurl' '
567
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000568 mk_test testrepo heads/main &&
569 git checkout main &&
Jonathan Nieder3c695522013-03-18 16:12:11 -0700570 test_config remote.there.url test2repo &&
571 test_config remote.there.pushurl testrepo &&
Junio C Hamano0a42ac02013-01-04 16:08:26 -0800572 git push there : &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000573 check_push_result testrepo $the_commit heads/main
Michael J Grubere1ca4242009-06-09 18:01:35 +0200574'
575
Ramkumar Ramachandra9f765ce2013-04-02 13:10:34 +0530576test_expect_success 'push with config branch.*.pushremote' '
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000577 mk_test up_repo heads/main &&
578 mk_test side_repo heads/main &&
579 mk_test down_repo heads/main &&
Ramkumar Ramachandra9f765ce2013-04-02 13:10:34 +0530580 test_config remote.up.url up_repo &&
581 test_config remote.pushdefault side_repo &&
582 test_config remote.down.url down_repo &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000583 test_config branch.main.remote up &&
584 test_config branch.main.pushremote down &&
Junio C Hamano54a3c672013-04-18 11:47:59 -0700585 test_config push.default matching &&
Ramkumar Ramachandra9f765ce2013-04-02 13:10:34 +0530586 git push &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000587 check_push_result up_repo $the_first_commit heads/main &&
588 check_push_result side_repo $the_first_commit heads/main &&
589 check_push_result down_repo $the_commit heads/main
Michael J Grubere1ca4242009-06-09 18:01:35 +0200590'
591
Jeff King98b406f2014-02-24 03:59:03 -0500592test_expect_success 'branch.*.pushremote config order is irrelevant' '
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000593 mk_test one_repo heads/main &&
594 mk_test two_repo heads/main &&
Jeff King98b406f2014-02-24 03:59:03 -0500595 test_config remote.one.url one_repo &&
596 test_config remote.two.url two_repo &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000597 test_config branch.main.pushremote two_repo &&
Jeff King98b406f2014-02-24 03:59:03 -0500598 test_config remote.pushdefault one_repo &&
599 test_config push.default matching &&
600 git push &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000601 check_push_result one_repo $the_first_commit heads/main &&
602 check_push_result two_repo $the_commit heads/main
Jeff King98b406f2014-02-24 03:59:03 -0500603'
604
Glen Choo91e2e8f2022-05-31 23:12:33 +0000605test_expect_success 'push ignores empty branch name entries' '
606 mk_test one_repo heads/main &&
607 test_config remote.one.url one_repo &&
608 test_config branch..remote one &&
609 test_config branch..merge refs/heads/ &&
610 test_config branch.main.remote one &&
611 test_config branch.main.merge refs/heads/main &&
612 git push
613'
614
Brian Ewins11f24412007-10-11 20:32:27 +0100615test_expect_success 'push with dry-run' '
616
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000617 mk_test testrepo heads/main &&
618 old_commit=$(git -C testrepo show-ref -s --verify refs/heads/main) &&
Junio C Hamano0a42ac02013-01-04 16:08:26 -0800619 git push --dry-run testrepo : &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000620 check_push_result testrepo $old_commit heads/main
Brian Ewins11f24412007-10-11 20:32:27 +0100621'
622
Johannes Schindelin28391a82007-11-29 01:02:53 +0000623test_expect_success 'push updates local refs' '
624
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000625 mk_test testrepo heads/main &&
Jeff King2e433b72013-04-02 13:10:31 +0530626 mk_child testrepo child &&
Jay Soffiand4785cd2010-04-19 18:08:31 -0400627 (
628 cd child &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000629 git pull .. main &&
Johannes Schindelin28391a82007-11-29 01:02:53 +0000630 git push &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000631 test $(git rev-parse main) = \
632 $(git rev-parse remotes/origin/main)
Jay Soffiand4785cd2010-04-19 18:08:31 -0400633 )
Johannes Schindelin28391a82007-11-29 01:02:53 +0000634
635'
636
Clemens Buchacher16ed2f42008-11-05 21:55:54 +0100637test_expect_success 'push updates up-to-date local refs' '
638
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000639 mk_test testrepo heads/main &&
Jeff King2e433b72013-04-02 13:10:31 +0530640 mk_child testrepo child1 &&
641 mk_child testrepo child2 &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000642 (cd child1 && git pull .. main && git push) &&
Jay Soffiand4785cd2010-04-19 18:08:31 -0400643 (
644 cd child2 &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000645 git pull ../child1 main &&
Clemens Buchacher16ed2f42008-11-05 21:55:54 +0100646 git push &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000647 test $(git rev-parse main) = \
648 $(git rev-parse remotes/origin/main)
Jay Soffiand4785cd2010-04-19 18:08:31 -0400649 )
Clemens Buchacher16ed2f42008-11-05 21:55:54 +0100650
651'
652
653test_expect_success 'push preserves up-to-date packed refs' '
654
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000655 mk_test testrepo heads/main &&
Jeff King2e433b72013-04-02 13:10:31 +0530656 mk_child testrepo child &&
Jay Soffiand4785cd2010-04-19 18:08:31 -0400657 (
658 cd child &&
Clemens Buchacher16ed2f42008-11-05 21:55:54 +0100659 git push &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000660 ! test -f .git/refs/remotes/origin/main
Jay Soffiand4785cd2010-04-19 18:08:31 -0400661 )
Clemens Buchacher16ed2f42008-11-05 21:55:54 +0100662
663'
664
Johannes Schindelin28391a82007-11-29 01:02:53 +0000665test_expect_success 'push does not update local refs on failure' '
666
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000667 mk_test testrepo heads/main &&
Jeff King2e433b72013-04-02 13:10:31 +0530668 mk_child testrepo child &&
Jeff Kingb2dc9682008-11-07 17:20:33 -0500669 mkdir testrepo/.git/hooks &&
bert Dvornikfc012c22010-05-20 20:57:52 +0200670 echo "#!/no/frobnication/today" >testrepo/.git/hooks/pre-receive &&
Jeff Kingb2dc9682008-11-07 17:20:33 -0500671 chmod +x testrepo/.git/hooks/pre-receive &&
Jay Soffiand4785cd2010-04-19 18:08:31 -0400672 (
673 cd child &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000674 git pull .. main &&
Stephan Beyerd492b312008-07-12 17:47:52 +0200675 test_must_fail git push &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000676 test $(git rev-parse main) != \
677 $(git rev-parse remotes/origin/main)
Jay Soffiand4785cd2010-04-19 18:08:31 -0400678 )
Johannes Schindelin28391a82007-11-29 01:02:53 +0000679
680'
681
682test_expect_success 'allow deleting an invalid remote ref' '
683
Jeff Kinge9de7a52021-09-24 14:33:00 -0400684 mk_test testrepo heads/branch &&
Johannes Schindelin28391a82007-11-29 01:02:53 +0000685 rm -f testrepo/.git/objects/??/* &&
Jeff Kinge9de7a52021-09-24 14:33:00 -0400686 git push testrepo :refs/heads/branch &&
687 (cd testrepo && test_must_fail git rev-parse --verify refs/heads/branch)
Johannes Schindelin28391a82007-11-29 01:02:53 +0000688
689'
690
Pang Yan Han160b81e2011-09-28 23:39:35 +0800691test_expect_success 'pushing valid refs triggers post-receive and post-update hooks' '
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000692 mk_test_with_hooks testrepo heads/main heads/next &&
693 orgmain=$(cd testrepo && git show-ref -s --verify refs/heads/main) &&
694 newmain=$(git show-ref -s --verify refs/heads/main) &&
Pang Yan Han160b81e2011-09-28 23:39:35 +0800695 orgnext=$(cd testrepo && git show-ref -s --verify refs/heads/next) &&
brian m. carlson8125a582018-05-13 02:24:13 +0000696 newnext=$ZERO_OID &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000697 git push testrepo refs/heads/main:refs/heads/main :refs/heads/next &&
Pang Yan Han160b81e2011-09-28 23:39:35 +0800698 (
699 cd testrepo/.git &&
700 cat >pre-receive.expect <<-EOF &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000701 $orgmain $newmain refs/heads/main
Pang Yan Han160b81e2011-09-28 23:39:35 +0800702 $orgnext $newnext refs/heads/next
703 EOF
704
705 cat >update.expect <<-EOF &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000706 refs/heads/main $orgmain $newmain
Pang Yan Han160b81e2011-09-28 23:39:35 +0800707 refs/heads/next $orgnext $newnext
708 EOF
709
710 cat >post-receive.expect <<-EOF &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000711 $orgmain $newmain refs/heads/main
Pang Yan Han160b81e2011-09-28 23:39:35 +0800712 $orgnext $newnext refs/heads/next
713 EOF
714
715 cat >post-update.expect <<-EOF &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000716 refs/heads/main
Pang Yan Han160b81e2011-09-28 23:39:35 +0800717 refs/heads/next
718 EOF
719
720 test_cmp pre-receive.expect pre-receive.actual &&
721 test_cmp update.expect update.actual &&
722 test_cmp post-receive.expect post-receive.actual &&
723 test_cmp post-update.expect post-update.actual
724 )
725'
726
727test_expect_success 'deleting dangling ref triggers hooks with correct args' '
Jeff Kinge9de7a52021-09-24 14:33:00 -0400728 mk_test_with_hooks testrepo heads/branch &&
Jeff King968f12f2021-09-24 14:46:13 -0400729 orig=$(git -C testrepo rev-parse refs/heads/branch) &&
Pang Yan Han160b81e2011-09-28 23:39:35 +0800730 rm -f testrepo/.git/objects/??/* &&
Jeff Kinge9de7a52021-09-24 14:33:00 -0400731 git push testrepo :refs/heads/branch &&
Pang Yan Han160b81e2011-09-28 23:39:35 +0800732 (
733 cd testrepo/.git &&
734 cat >pre-receive.expect <<-EOF &&
Jeff King968f12f2021-09-24 14:46:13 -0400735 $orig $ZERO_OID refs/heads/branch
Pang Yan Han160b81e2011-09-28 23:39:35 +0800736 EOF
737
738 cat >update.expect <<-EOF &&
Jeff King968f12f2021-09-24 14:46:13 -0400739 refs/heads/branch $orig $ZERO_OID
Pang Yan Han160b81e2011-09-28 23:39:35 +0800740 EOF
741
742 cat >post-receive.expect <<-EOF &&
Jeff King968f12f2021-09-24 14:46:13 -0400743 $orig $ZERO_OID refs/heads/branch
Pang Yan Han160b81e2011-09-28 23:39:35 +0800744 EOF
745
746 cat >post-update.expect <<-EOF &&
Jeff Kinge9de7a52021-09-24 14:33:00 -0400747 refs/heads/branch
Pang Yan Han160b81e2011-09-28 23:39:35 +0800748 EOF
749
750 test_cmp pre-receive.expect pre-receive.actual &&
751 test_cmp update.expect update.actual &&
752 test_cmp post-receive.expect post-receive.actual &&
753 test_cmp post-update.expect post-update.actual
754 )
755'
756
757test_expect_success 'deletion of a non-existent ref is not fed to post-receive and post-update hooks' '
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000758 mk_test_with_hooks testrepo heads/main &&
759 orgmain=$(cd testrepo && git show-ref -s --verify refs/heads/main) &&
760 newmain=$(git show-ref -s --verify refs/heads/main) &&
761 git push testrepo main :refs/heads/nonexistent &&
Pang Yan Han160b81e2011-09-28 23:39:35 +0800762 (
763 cd testrepo/.git &&
764 cat >pre-receive.expect <<-EOF &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000765 $orgmain $newmain refs/heads/main
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 &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000770 refs/heads/main $orgmain $newmain
brian m. carlson8125a582018-05-13 02:24:13 +0000771 refs/heads/nonexistent $ZERO_OID $ZERO_OID
Pang Yan Han160b81e2011-09-28 23:39:35 +0800772 EOF
773
774 cat >post-receive.expect <<-EOF &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000775 $orgmain $newmain refs/heads/main
Pang Yan Han160b81e2011-09-28 23:39:35 +0800776 EOF
777
778 cat >post-update.expect <<-EOF &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000779 refs/heads/main
Pang Yan Han160b81e2011-09-28 23:39:35 +0800780 EOF
781
782 test_cmp pre-receive.expect pre-receive.actual &&
783 test_cmp update.expect update.actual &&
784 test_cmp post-receive.expect post-receive.actual &&
785 test_cmp post-update.expect post-update.actual
786 )
787'
788
789test_expect_success 'deletion of a non-existent ref alone does trigger post-receive and post-update hooks' '
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000790 mk_test_with_hooks testrepo heads/main &&
Pang Yan Han160b81e2011-09-28 23:39:35 +0800791 git push testrepo :refs/heads/nonexistent &&
792 (
793 cd testrepo/.git &&
794 cat >pre-receive.expect <<-EOF &&
brian m. carlson8125a582018-05-13 02:24:13 +0000795 $ZERO_OID $ZERO_OID refs/heads/nonexistent
Pang Yan Han160b81e2011-09-28 23:39:35 +0800796 EOF
797
798 cat >update.expect <<-EOF &&
brian m. carlson8125a582018-05-13 02:24:13 +0000799 refs/heads/nonexistent $ZERO_OID $ZERO_OID
Pang Yan Han160b81e2011-09-28 23:39:35 +0800800 EOF
801
802 test_cmp pre-receive.expect pre-receive.actual &&
803 test_cmp update.expect update.actual &&
804 test_path_is_missing post-receive.actual &&
805 test_path_is_missing post-update.actual
806 )
807'
808
809test_expect_success 'mixed ref updates, deletes, invalid deletes trigger hooks with correct input' '
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000810 mk_test_with_hooks testrepo heads/main heads/next heads/seen &&
811 orgmain=$(cd testrepo && git show-ref -s --verify refs/heads/main) &&
812 newmain=$(git show-ref -s --verify refs/heads/main) &&
Pang Yan Han160b81e2011-09-28 23:39:35 +0800813 orgnext=$(cd testrepo && git show-ref -s --verify refs/heads/next) &&
brian m. carlson8125a582018-05-13 02:24:13 +0000814 newnext=$ZERO_OID &&
Johannes Schindelin6dca5db2020-06-25 12:18:59 +0000815 orgseen=$(cd testrepo && git show-ref -s --verify refs/heads/seen) &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000816 newseen=$(git show-ref -s --verify refs/heads/main) &&
817 git push testrepo refs/heads/main:refs/heads/main \
818 refs/heads/main:refs/heads/seen :refs/heads/next \
Pang Yan Han160b81e2011-09-28 23:39:35 +0800819 :refs/heads/nonexistent &&
820 (
821 cd testrepo/.git &&
822 cat >pre-receive.expect <<-EOF &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000823 $orgmain $newmain refs/heads/main
Pang Yan Han160b81e2011-09-28 23:39:35 +0800824 $orgnext $newnext refs/heads/next
Johannes Schindelin6dca5db2020-06-25 12:18:59 +0000825 $orgseen $newseen refs/heads/seen
brian m. carlson8125a582018-05-13 02:24:13 +0000826 $ZERO_OID $ZERO_OID refs/heads/nonexistent
Pang Yan Han160b81e2011-09-28 23:39:35 +0800827 EOF
828
829 cat >update.expect <<-EOF &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000830 refs/heads/main $orgmain $newmain
Pang Yan Han160b81e2011-09-28 23:39:35 +0800831 refs/heads/next $orgnext $newnext
Johannes Schindelin6dca5db2020-06-25 12:18:59 +0000832 refs/heads/seen $orgseen $newseen
brian m. carlson8125a582018-05-13 02:24:13 +0000833 refs/heads/nonexistent $ZERO_OID $ZERO_OID
Pang Yan Han160b81e2011-09-28 23:39:35 +0800834 EOF
835
836 cat >post-receive.expect <<-EOF &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000837 $orgmain $newmain refs/heads/main
Pang Yan Han160b81e2011-09-28 23:39:35 +0800838 $orgnext $newnext refs/heads/next
Johannes Schindelin6dca5db2020-06-25 12:18:59 +0000839 $orgseen $newseen refs/heads/seen
Pang Yan Han160b81e2011-09-28 23:39:35 +0800840 EOF
841
842 cat >post-update.expect <<-EOF &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000843 refs/heads/main
Pang Yan Han160b81e2011-09-28 23:39:35 +0800844 refs/heads/next
Johannes Schindelin6dca5db2020-06-25 12:18:59 +0000845 refs/heads/seen
Pang Yan Han160b81e2011-09-28 23:39:35 +0800846 EOF
847
848 test_cmp pre-receive.expect pre-receive.actual &&
849 test_cmp update.expect update.actual &&
850 test_cmp post-receive.expect post-receive.actual &&
851 test_cmp post-update.expect post-update.actual
852 )
853'
854
Jan Krügerf517f1f2009-12-30 20:57:42 +0100855test_expect_success 'allow deleting a ref using --delete' '
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000856 mk_test testrepo heads/main &&
Jan Krügerf517f1f2009-12-30 20:57:42 +0100857 (cd testrepo && git config receive.denyDeleteCurrent warn) &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000858 git push testrepo --delete main &&
859 (cd testrepo && test_must_fail git rev-parse --verify refs/heads/main)
Jan Krügerf517f1f2009-12-30 20:57:42 +0100860'
861
862test_expect_success 'allow deleting a tag using --delete' '
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000863 mk_test testrepo heads/main &&
864 git tag -a -m dummy_message deltag heads/main &&
Jan Krügerf517f1f2009-12-30 20:57:42 +0100865 git push testrepo --tags &&
866 (cd testrepo && git rev-parse --verify -q refs/tags/deltag) &&
867 git push testrepo --delete tag deltag &&
868 (cd testrepo && test_must_fail git rev-parse --verify refs/tags/deltag)
869'
870
871test_expect_success 'push --delete without args aborts' '
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000872 mk_test testrepo heads/main &&
Jan Krügerf517f1f2009-12-30 20:57:42 +0100873 test_must_fail git push testrepo --delete
874'
875
876test_expect_success 'push --delete refuses src:dest refspecs' '
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000877 mk_test testrepo heads/main &&
878 test_must_fail git push testrepo --delete main:foo
Jan Krügerf517f1f2009-12-30 20:57:42 +0100879'
880
Junio C Hamano20e41642021-02-23 15:13:32 -0800881test_expect_success 'push --delete refuses empty string' '
882 mk_test testrepo heads/master &&
883 test_must_fail git push testrepo --delete ""
884'
885
Jeff King986e8232008-11-08 20:49:27 -0500886test_expect_success 'warn on push to HEAD of non-bare repository' '
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000887 mk_test testrepo heads/main &&
Jay Soffiand4785cd2010-04-19 18:08:31 -0400888 (
889 cd testrepo &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000890 git checkout main &&
Jay Soffiand4785cd2010-04-19 18:08:31 -0400891 git config receive.denyCurrentBranch warn
892 ) &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000893 git push testrepo main 2>stderr &&
Junio C Hamano3d95d922009-01-31 17:34:05 -0800894 grep "warning: updating the current branch" stderr
Jeff King986e8232008-11-08 20:49:27 -0500895'
896
897test_expect_success 'deny push to HEAD of non-bare repository' '
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000898 mk_test testrepo heads/main &&
Jay Soffiand4785cd2010-04-19 18:08:31 -0400899 (
900 cd testrepo &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000901 git checkout main &&
Jay Soffiand4785cd2010-04-19 18:08:31 -0400902 git config receive.denyCurrentBranch true
903 ) &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000904 test_must_fail git push testrepo main
Jeff King986e8232008-11-08 20:49:27 -0500905'
906
907test_expect_success 'allow push to HEAD of bare repository (bare)' '
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000908 mk_test testrepo heads/main &&
Jay Soffiand4785cd2010-04-19 18:08:31 -0400909 (
910 cd testrepo &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000911 git checkout main &&
Jeff King986e8232008-11-08 20:49:27 -0500912 git config receive.denyCurrentBranch true &&
Jay Soffiand4785cd2010-04-19 18:08:31 -0400913 git config core.bare true
914 ) &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000915 git push testrepo main 2>stderr &&
Junio C Hamano3d95d922009-01-31 17:34:05 -0800916 ! grep "warning: updating the current branch" stderr
Jeff King986e8232008-11-08 20:49:27 -0500917'
918
919test_expect_success 'allow push to HEAD of non-bare repository (config)' '
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000920 mk_test testrepo heads/main &&
Jay Soffiand4785cd2010-04-19 18:08:31 -0400921 (
922 cd testrepo &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000923 git checkout main &&
Jeff King986e8232008-11-08 20:49:27 -0500924 git config receive.denyCurrentBranch false
925 ) &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000926 git push testrepo main 2>stderr &&
Junio C Hamano3d95d922009-01-31 17:34:05 -0800927 ! grep "warning: updating the current branch" stderr
Jeff King986e8232008-11-08 20:49:27 -0500928'
929
Martin Koegler18afe102008-11-10 22:47:11 +0100930test_expect_success 'fetch with branches' '
Jeff King2e433b72013-04-02 13:10:31 +0530931 mk_empty testrepo &&
Martin Koegler18afe102008-11-10 22:47:11 +0100932 git branch second $the_first_commit &&
933 git checkout second &&
934 echo ".." > testrepo/.git/branches/branch1 &&
Jay Soffiand4785cd2010-04-19 18:08:31 -0400935 (
936 cd testrepo &&
Martin Koegler18afe102008-11-10 22:47:11 +0100937 git fetch branch1 &&
Jonathan Nieder848575d2013-03-18 16:13:41 -0700938 echo "$the_commit commit refs/heads/branch1" >expect &&
939 git for-each-ref refs/heads >actual &&
940 test_cmp expect actual
Martin Koegler18afe102008-11-10 22:47:11 +0100941 ) &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000942 git checkout main
Martin Koegler18afe102008-11-10 22:47:11 +0100943'
944
945test_expect_success 'fetch with branches containing #' '
Jeff King2e433b72013-04-02 13:10:31 +0530946 mk_empty testrepo &&
Martin Koegler18afe102008-11-10 22:47:11 +0100947 echo "..#second" > testrepo/.git/branches/branch2 &&
Jay Soffiand4785cd2010-04-19 18:08:31 -0400948 (
949 cd testrepo &&
Martin Koegler18afe102008-11-10 22:47:11 +0100950 git fetch branch2 &&
Jonathan Nieder848575d2013-03-18 16:13:41 -0700951 echo "$the_first_commit commit refs/heads/branch2" >expect &&
952 git for-each-ref refs/heads >actual &&
953 test_cmp expect actual
Martin Koegler18afe102008-11-10 22:47:11 +0100954 ) &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000955 git checkout main
Martin Koegler18afe102008-11-10 22:47:11 +0100956'
957
958test_expect_success 'push with branches' '
Jeff King2e433b72013-04-02 13:10:31 +0530959 mk_empty testrepo &&
Martin Koegler18afe102008-11-10 22:47:11 +0100960 git checkout second &&
961 echo "testrepo" > .git/branches/branch1 &&
962 git push branch1 &&
Jay Soffiand4785cd2010-04-19 18:08:31 -0400963 (
964 cd testrepo &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000965 echo "$the_first_commit commit refs/heads/main" >expect &&
Jonathan Nieder848575d2013-03-18 16:13:41 -0700966 git for-each-ref refs/heads >actual &&
967 test_cmp expect actual
Martin Koegler18afe102008-11-10 22:47:11 +0100968 )
969'
970
971test_expect_success 'push with branches containing #' '
Jeff King2e433b72013-04-02 13:10:31 +0530972 mk_empty testrepo &&
Martin Koegler18afe102008-11-10 22:47:11 +0100973 echo "testrepo#branch3" > .git/branches/branch2 &&
974 git push branch2 &&
Jay Soffiand4785cd2010-04-19 18:08:31 -0400975 (
976 cd testrepo &&
Jonathan Nieder848575d2013-03-18 16:13:41 -0700977 echo "$the_first_commit commit refs/heads/branch3" >expect &&
978 git for-each-ref refs/heads >actual &&
979 test_cmp expect actual
Martin Koegler18afe102008-11-10 22:47:11 +0100980 ) &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000981 git checkout main
Martin Koegler18afe102008-11-10 22:47:11 +0100982'
983
Jay Soffianda3efdb2010-04-19 18:19:18 -0400984test_expect_success 'push into aliased refs (consistent)' '
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000985 mk_test testrepo heads/main &&
Jeff King2e433b72013-04-02 13:10:31 +0530986 mk_child testrepo child1 &&
987 mk_child testrepo child2 &&
Jay Soffianda3efdb2010-04-19 18:19:18 -0400988 (
989 cd child1 &&
990 git branch foo &&
Eric Sunshine51b85472018-07-01 20:24:01 -0400991 git symbolic-ref refs/heads/bar refs/heads/foo &&
Jay Soffianda3efdb2010-04-19 18:19:18 -0400992 git config receive.denyCurrentBranch false
993 ) &&
994 (
995 cd child2 &&
996 >path2 &&
997 git add path2 &&
998 test_tick &&
999 git commit -a -m child2 &&
1000 git branch foo &&
1001 git branch bar &&
1002 git push ../child1 foo bar
1003 )
1004'
1005
1006test_expect_success 'push into aliased refs (inconsistent)' '
Johannes Schindelinbc925ce2020-11-18 23:44:32 +00001007 mk_test testrepo heads/main &&
Jeff King2e433b72013-04-02 13:10:31 +05301008 mk_child testrepo child1 &&
1009 mk_child testrepo child2 &&
Jay Soffianda3efdb2010-04-19 18:19:18 -04001010 (
1011 cd child1 &&
1012 git branch foo &&
Eric Sunshine51b85472018-07-01 20:24:01 -04001013 git symbolic-ref refs/heads/bar refs/heads/foo &&
Jay Soffianda3efdb2010-04-19 18:19:18 -04001014 git config receive.denyCurrentBranch false
1015 ) &&
1016 (
1017 cd child2 &&
1018 >path2 &&
1019 git add path2 &&
1020 test_tick &&
1021 git commit -a -m child2 &&
1022 git branch foo &&
1023 >path3 &&
1024 git add path3 &&
1025 test_tick &&
1026 git commit -a -m child2 &&
1027 git branch bar &&
1028 test_must_fail git push ../child1 foo bar 2>stderr &&
1029 grep "refusing inconsistent update" stderr
1030 )
1031'
1032
Ævar Arnfjörð Bjarmason380efb62018-07-31 13:07:13 +00001033test_force_push_tag () {
1034 tag_type_description=$1
1035 tag_args=$2
Ævar Arnfjörð Bjarmason941a7ba2018-07-31 13:07:12 +00001036
Ævar Arnfjörð Bjarmasonf08fb8d2018-08-31 20:09:57 +00001037 test_expect_success "force pushing required to update $tag_type_description" "
Johannes Schindelinbc925ce2020-11-18 23:44:32 +00001038 mk_test testrepo heads/main &&
Ævar Arnfjörð Bjarmason380efb62018-07-31 13:07:13 +00001039 mk_child testrepo child1 &&
1040 mk_child testrepo child2 &&
1041 (
1042 cd child1 &&
1043 git tag testTag &&
1044 git push ../child2 testTag &&
1045 >file1 &&
1046 git add file1 &&
1047 git commit -m 'file1' &&
1048 git tag $tag_args testTag &&
1049 test_must_fail git push ../child2 testTag &&
1050 git push --force ../child2 testTag &&
1051 git tag $tag_args testTag HEAD~ &&
1052 test_must_fail git push ../child2 testTag &&
1053 git push --force ../child2 testTag &&
Ævar Arnfjörð Bjarmason941a7ba2018-07-31 13:07:12 +00001054
Ævar Arnfjörð Bjarmason380efb62018-07-31 13:07:13 +00001055 # Clobbering without + in refspec needs --force
1056 git tag -f testTag &&
1057 test_must_fail git push ../child2 'refs/tags/*:refs/tags/*' &&
1058 git push --force ../child2 'refs/tags/*:refs/tags/*' &&
Ævar Arnfjörð Bjarmason941a7ba2018-07-31 13:07:12 +00001059
Ævar Arnfjörð Bjarmason380efb62018-07-31 13:07:13 +00001060 # Clobbering with + in refspec does not need --force
1061 git tag -f testTag HEAD~ &&
1062 git push ../child2 '+refs/tags/*:refs/tags/*' &&
Ævar Arnfjörð Bjarmason941a7ba2018-07-31 13:07:12 +00001063
Ævar Arnfjörð Bjarmason380efb62018-07-31 13:07:13 +00001064 # Clobbering with --no-force still obeys + in refspec
1065 git tag -f testTag &&
1066 git push --no-force ../child2 '+refs/tags/*:refs/tags/*' &&
1067
1068 # Clobbering with/without --force and 'tag <name>' format
1069 git tag -f testTag HEAD~ &&
1070 test_must_fail git push ../child2 tag testTag &&
1071 git push --force ../child2 tag testTag
1072 )
1073 "
1074}
1075
1076test_force_push_tag "lightweight tag" "-f"
Ævar Arnfjörð Bjarmason253b3d42018-08-31 20:09:58 +00001077test_force_push_tag "annotated tag" "-f -a -m'tag message'"
Chris Rorvickdbfeddb2012-11-29 19:41:37 -06001078
Ævar Arnfjörð Bjarmason6b0b0672018-08-31 20:09:59 +00001079test_force_fetch_tag () {
1080 tag_type_description=$1
1081 tag_args=$2
1082
Ævar Arnfjörð Bjarmason0bc8d712018-08-31 20:10:04 +00001083 test_expect_success "fetch will not clobber an existing $tag_type_description without --force" "
Johannes Schindelinbc925ce2020-11-18 23:44:32 +00001084 mk_test testrepo heads/main &&
Ævar Arnfjörð Bjarmason6b0b0672018-08-31 20:09:59 +00001085 mk_child testrepo child1 &&
1086 mk_child testrepo child2 &&
1087 (
1088 cd testrepo &&
1089 git tag testTag &&
1090 git -C ../child1 fetch origin tag testTag &&
1091 >file1 &&
1092 git add file1 &&
1093 git commit -m 'file1' &&
1094 git tag $tag_args testTag &&
Ævar Arnfjörð Bjarmason0bc8d712018-08-31 20:10:04 +00001095 test_must_fail git -C ../child1 fetch origin tag testTag &&
1096 git -C ../child1 fetch origin '+refs/tags/*:refs/tags/*'
Ævar Arnfjörð Bjarmason6b0b0672018-08-31 20:09:59 +00001097 )
1098 "
1099}
1100
1101test_force_fetch_tag "lightweight tag" "-f"
1102test_force_fetch_tag "annotated tag" "-f -a -m'tag message'"
Larry D'Annafbe4f442010-02-26 23:52:16 -05001103
Larry D'Annafbe4f442010-02-26 23:52:16 -05001104test_expect_success 'push --porcelain' '
Jeff King2e433b72013-04-02 13:10:31 +05301105 mk_empty testrepo &&
Larry D'Annafbe4f442010-02-26 23:52:16 -05001106 echo >.git/foo "To testrepo" &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +00001107 echo >>.git/foo "* refs/heads/main:refs/remotes/origin/main [new reference]" &&
Larry D'Annafbe4f442010-02-26 23:52:16 -05001108 echo >>.git/foo "Done" &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +00001109 git push >.git/bar --porcelain testrepo refs/heads/main:refs/remotes/origin/main &&
Larry D'Annafbe4f442010-02-26 23:52:16 -05001110 (
1111 cd testrepo &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +00001112 echo "$the_commit commit refs/remotes/origin/main" >expect &&
Jonathan Nieder848575d2013-03-18 16:13:41 -07001113 git for-each-ref refs/remotes/origin >actual &&
1114 test_cmp expect actual
Larry D'Annafbe4f442010-02-26 23:52:16 -05001115 ) &&
1116 test_cmp .git/foo .git/bar
1117'
1118
1119test_expect_success 'push --porcelain bad url' '
Jeff King2e433b72013-04-02 13:10:31 +05301120 mk_empty testrepo &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +00001121 test_must_fail git push >.git/bar --porcelain asdfasdfasd refs/heads/main:refs/remotes/origin/main &&
Pranit Bauvac7cf9562017-01-04 01:27:07 +05301122 ! grep -q Done .git/bar
Larry D'Annafbe4f442010-02-26 23:52:16 -05001123'
1124
1125test_expect_success 'push --porcelain rejected' '
Jeff King2e433b72013-04-02 13:10:31 +05301126 mk_empty testrepo &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +00001127 git push testrepo refs/heads/main:refs/remotes/origin/main &&
Larry D'Annafbe4f442010-02-26 23:52:16 -05001128 (cd testrepo &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +00001129 git reset --hard origin/main^ &&
Larry D'Annafbe4f442010-02-26 23:52:16 -05001130 git config receive.denyCurrentBranch true) &&
1131
1132 echo >.git/foo "To testrepo" &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +00001133 echo >>.git/foo "! refs/heads/main:refs/heads/main [remote rejected] (branch is currently checked out)" &&
Jiang Xin7dcbeaa2020-04-17 05:45:32 -04001134 echo >>.git/foo "Done" &&
Larry D'Annafbe4f442010-02-26 23:52:16 -05001135
Johannes Schindelinbc925ce2020-11-18 23:44:32 +00001136 test_must_fail git push >.git/bar --porcelain testrepo refs/heads/main:refs/heads/main &&
Junio C Hamanoc2961342010-03-11 21:51:57 -08001137 test_cmp .git/foo .git/bar
Larry D'Annafbe4f442010-02-26 23:52:16 -05001138'
1139
1140test_expect_success 'push --porcelain --dry-run rejected' '
Jeff King2e433b72013-04-02 13:10:31 +05301141 mk_empty testrepo &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +00001142 git push testrepo refs/heads/main:refs/remotes/origin/main &&
Larry D'Annafbe4f442010-02-26 23:52:16 -05001143 (cd testrepo &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +00001144 git reset --hard origin/main &&
Larry D'Annafbe4f442010-02-26 23:52:16 -05001145 git config receive.denyCurrentBranch true) &&
1146
1147 echo >.git/foo "To testrepo" &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +00001148 echo >>.git/foo "! refs/heads/main^:refs/heads/main [rejected] (non-fast-forward)" &&
Larry D'Annafbe4f442010-02-26 23:52:16 -05001149 echo >>.git/foo "Done" &&
1150
Johannes Schindelinbc925ce2020-11-18 23:44:32 +00001151 test_must_fail git push >.git/bar --porcelain --dry-run testrepo refs/heads/main^:refs/heads/main &&
Junio C Hamanoc2961342010-03-11 21:51:57 -08001152 test_cmp .git/foo .git/bar
Larry D'Annafbe4f442010-02-26 23:52:16 -05001153'
1154
Felipe Contreras6ddba5e2012-02-23 00:43:41 +02001155test_expect_success 'push --prune' '
Johannes Schindelinbc925ce2020-11-18 23:44:32 +00001156 mk_test testrepo heads/main heads/second heads/foo heads/bar &&
Junio C Hamano0a42ac02013-01-04 16:08:26 -08001157 git push --prune testrepo : &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +00001158 check_push_result testrepo $the_commit heads/main &&
Jeff King2e433b72013-04-02 13:10:31 +05301159 check_push_result testrepo $the_first_commit heads/second &&
1160 ! check_push_result testrepo $the_first_commit heads/foo heads/bar
Felipe Contreras6ddba5e2012-02-23 00:43:41 +02001161'
1162
1163test_expect_success 'push --prune refspec' '
Johannes Schindelinbc925ce2020-11-18 23:44:32 +00001164 mk_test testrepo tmp/main tmp/second tmp/foo tmp/bar &&
Felipe Contreras6ddba5e2012-02-23 00:43:41 +02001165 git push --prune testrepo "refs/heads/*:refs/tmp/*" &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +00001166 check_push_result testrepo $the_commit tmp/main &&
Jeff King2e433b72013-04-02 13:10:31 +05301167 check_push_result testrepo $the_first_commit tmp/second &&
1168 ! check_push_result testrepo $the_first_commit tmp/foo tmp/bar
Felipe Contreras6ddba5e2012-02-23 00:43:41 +02001169'
1170
Junio C Hamanodaebaa72013-01-18 16:08:30 -08001171for configsection in transfer receive
1172do
1173 test_expect_success "push to update a ref hidden by $configsection.hiderefs" '
Johannes Schindelinbc925ce2020-11-18 23:44:32 +00001174 mk_test testrepo heads/main hidden/one hidden/two hidden/three &&
Junio C Hamanodaebaa72013-01-18 16:08:30 -08001175 (
1176 cd testrepo &&
1177 git config $configsection.hiderefs refs/hidden
1178 ) &&
1179
1180 # push to unhidden ref succeeds normally
Johannes Schindelinbc925ce2020-11-18 23:44:32 +00001181 git push testrepo main:refs/heads/main &&
1182 check_push_result testrepo $the_commit heads/main &&
Junio C Hamanodaebaa72013-01-18 16:08:30 -08001183
1184 # push to update a hidden ref should fail
Johannes Schindelinbc925ce2020-11-18 23:44:32 +00001185 test_must_fail git push testrepo main:refs/hidden/one &&
Jeff King2e433b72013-04-02 13:10:31 +05301186 check_push_result testrepo $the_first_commit hidden/one &&
Junio C Hamanodaebaa72013-01-18 16:08:30 -08001187
1188 # push to delete a hidden ref should fail
1189 test_must_fail git push testrepo :refs/hidden/two &&
Jeff King2e433b72013-04-02 13:10:31 +05301190 check_push_result testrepo $the_first_commit hidden/two &&
Junio C Hamanodaebaa72013-01-18 16:08:30 -08001191
1192 # idempotent push to update a hidden ref should fail
1193 test_must_fail git push testrepo $the_first_commit:refs/hidden/three &&
Jeff King2e433b72013-04-02 13:10:31 +05301194 check_push_result testrepo $the_first_commit hidden/three
Junio C Hamanodaebaa72013-01-18 16:08:30 -08001195 '
1196done
1197
Junio C Hamano6e7b66e2013-01-29 14:02:15 -08001198test_expect_success 'fetch exact SHA1' '
Johannes Schindelinbc925ce2020-11-18 23:44:32 +00001199 mk_test testrepo heads/main hidden/one &&
1200 git push testrepo main:refs/hidden/one &&
Junio C Hamano6e7b66e2013-01-29 14:02:15 -08001201 (
1202 cd testrepo &&
1203 git config transfer.hiderefs refs/hidden
1204 ) &&
Jeff King2e433b72013-04-02 13:10:31 +05301205 check_push_result testrepo $the_commit hidden/one &&
Junio C Hamano6e7b66e2013-01-29 14:02:15 -08001206
Jeff King2e433b72013-04-02 13:10:31 +05301207 mk_child testrepo child &&
Junio C Hamano6e7b66e2013-01-29 14:02:15 -08001208 (
1209 cd child &&
1210
1211 # make sure $the_commit does not exist here
1212 git repack -a -d &&
1213 git prune &&
1214 test_must_fail git cat-file -t $the_commit &&
1215
Jonathan Tanab0c5f52019-02-25 13:54:08 -08001216 # Some protocol versions (e.g. 2) support fetching
1217 # unadvertised objects, so restrict this test to v0.
1218
Junio C Hamano6e7b66e2013-01-29 14:02:15 -08001219 # fetching the hidden object should fail by default
Jonathan Nieder8a1b0972019-12-23 17:01:10 -08001220 test_must_fail env GIT_TEST_PROTOCOL_VERSION=0 \
Jonathan Tanab0c5f52019-02-25 13:54:08 -08001221 git fetch -v ../testrepo $the_commit:refs/heads/copy 2>err &&
Matt McCutchend56583d2017-02-22 11:05:57 -05001222 test_i18ngrep "Server does not allow request for unadvertised object" err &&
Junio C Hamano6e7b66e2013-01-29 14:02:15 -08001223 test_must_fail git rev-parse --verify refs/heads/copy &&
1224
1225 # the server side can allow it to succeed
1226 (
1227 cd ../testrepo &&
1228 git config uploadpack.allowtipsha1inwant true
1229 ) &&
1230
Johannes Schindelinbc925ce2020-11-18 23:44:32 +00001231 git fetch -v ../testrepo $the_commit:refs/heads/copy main:refs/heads/extra &&
Jeff Kingc3c17bf2015-03-19 16:37:09 -04001232 cat >expect <<-EOF &&
1233 $the_commit
1234 $the_first_commit
1235 EOF
1236 {
1237 git rev-parse --verify refs/heads/copy &&
1238 git rev-parse --verify refs/heads/extra
1239 } >actual &&
1240 test_cmp expect actual
Junio C Hamano6e7b66e2013-01-29 14:02:15 -08001241 )
1242'
1243
Jonathan Nieder6c301ad2018-05-31 00:23:39 -07001244test_expect_success 'fetch exact SHA1 in protocol v2' '
Johannes Schindelinbc925ce2020-11-18 23:44:32 +00001245 mk_test testrepo heads/main hidden/one &&
1246 git push testrepo main:refs/hidden/one &&
Jonathan Nieder6c301ad2018-05-31 00:23:39 -07001247 git -C testrepo config transfer.hiderefs refs/hidden &&
1248 check_push_result testrepo $the_commit hidden/one &&
1249
1250 mk_child testrepo child &&
1251 git -C child config protocol.version 2 &&
1252
1253 # make sure $the_commit does not exist here
1254 git -C child repack -a -d &&
1255 git -C child prune &&
1256 test_must_fail git -C child cat-file -t $the_commit &&
1257
1258 # fetching the hidden object succeeds by default
1259 # NEEDSWORK: should this match the v0 behavior instead?
1260 git -C child fetch -v ../testrepo $the_commit:refs/heads/copy
1261'
1262
Fredrik Medley68ee6282015-05-21 22:23:39 +02001263for configallowtipsha1inwant in true false
1264do
1265 test_expect_success "shallow fetch reachable SHA1 (but not a ref), allowtipsha1inwant=$configallowtipsha1inwant" '
1266 mk_empty testrepo &&
1267 (
1268 cd testrepo &&
1269 git config uploadpack.allowtipsha1inwant $configallowtipsha1inwant &&
1270 git commit --allow-empty -m foo &&
1271 git commit --allow-empty -m bar
1272 ) &&
1273 SHA1=$(git --git-dir=testrepo/.git rev-parse HEAD^) &&
1274 mk_empty shallow &&
1275 (
1276 cd shallow &&
Jonathan Tanab0c5f52019-02-25 13:54:08 -08001277 # Some protocol versions (e.g. 2) support fetching
1278 # unadvertised objects, so restrict this test to v0.
Jonathan Nieder8a1b0972019-12-23 17:01:10 -08001279 test_must_fail env GIT_TEST_PROTOCOL_VERSION=0 \
Jonathan Tanab0c5f52019-02-25 13:54:08 -08001280 git fetch --depth=1 ../testrepo/.git $SHA1 &&
Fredrik Medley68ee6282015-05-21 22:23:39 +02001281 git --git-dir=../testrepo/.git config uploadpack.allowreachablesha1inwant true &&
1282 git fetch --depth=1 ../testrepo/.git $SHA1 &&
1283 git cat-file commit $SHA1
1284 )
1285 '
1286
1287 test_expect_success "deny fetch unreachable SHA1, allowtipsha1inwant=$configallowtipsha1inwant" '
1288 mk_empty testrepo &&
1289 (
1290 cd testrepo &&
1291 git config uploadpack.allowtipsha1inwant $configallowtipsha1inwant &&
1292 git commit --allow-empty -m foo &&
1293 git commit --allow-empty -m bar &&
1294 git commit --allow-empty -m xyz
1295 ) &&
1296 SHA1_1=$(git --git-dir=testrepo/.git rev-parse HEAD^^) &&
1297 SHA1_2=$(git --git-dir=testrepo/.git rev-parse HEAD^) &&
1298 SHA1_3=$(git --git-dir=testrepo/.git rev-parse HEAD) &&
1299 (
1300 cd testrepo &&
1301 git reset --hard $SHA1_2 &&
1302 git cat-file commit $SHA1_1 &&
1303 git cat-file commit $SHA1_3
1304 ) &&
1305 mk_empty shallow &&
1306 (
1307 cd shallow &&
Jonathan Tanab0c5f52019-02-25 13:54:08 -08001308 # Some protocol versions (e.g. 2) support fetching
1309 # unadvertised objects, so restrict this test to v0.
Jonathan Nieder8a1b0972019-12-23 17:01:10 -08001310 test_must_fail env GIT_TEST_PROTOCOL_VERSION=0 \
Jonathan Tanab0c5f52019-02-25 13:54:08 -08001311 git fetch ../testrepo/.git $SHA1_3 &&
Jonathan Nieder8a1b0972019-12-23 17:01:10 -08001312 test_must_fail env GIT_TEST_PROTOCOL_VERSION=0 \
Jonathan Tanab0c5f52019-02-25 13:54:08 -08001313 git fetch ../testrepo/.git $SHA1_1 &&
Fredrik Medley68ee6282015-05-21 22:23:39 +02001314 git --git-dir=../testrepo/.git config uploadpack.allowreachablesha1inwant true &&
1315 git fetch ../testrepo/.git $SHA1_1 &&
1316 git cat-file commit $SHA1_1 &&
1317 test_must_fail git cat-file commit $SHA1_2 &&
1318 git fetch ../testrepo/.git $SHA1_2 &&
1319 git cat-file commit $SHA1_2 &&
Jonathan Nieder8a1b0972019-12-23 17:01:10 -08001320 test_must_fail env GIT_TEST_PROTOCOL_VERSION=0 \
Junio C Hamano57a6b932019-04-25 16:41:23 +09001321 git fetch ../testrepo/.git $SHA1_3 2>err &&
Jeff Kingacaabcf2021-01-09 22:23:09 -05001322 # ideally we would insist this be on a "remote error:"
1323 # line, but it is racy; see the commit message
1324 test_i18ngrep "not our ref.*$SHA1_3\$" err
Fredrik Medley68ee6282015-05-21 22:23:39 +02001325 )
1326 '
1327done
1328
Junio C Hamanoc2aba152013-03-04 12:09:50 -08001329test_expect_success 'fetch follows tags by default' '
Johannes Schindelinbc925ce2020-11-18 23:44:32 +00001330 mk_test testrepo heads/main &&
Junio C Hamanoc2aba152013-03-04 12:09:50 -08001331 rm -fr src dst &&
1332 git init src &&
1333 (
1334 cd src &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +00001335 git pull ../testrepo main &&
Junio C Hamanoc2aba152013-03-04 12:09:50 -08001336 git tag -m "annotated" tag &&
1337 git for-each-ref >tmp1 &&
1338 (
1339 cat tmp1
Johannes Schindelinbc925ce2020-11-18 23:44:32 +00001340 sed -n "s|refs/heads/main$|refs/remotes/origin/main|p" tmp1
Junio C Hamanoc2aba152013-03-04 12:09:50 -08001341 ) |
1342 sort -k 3 >../expect
1343 ) &&
1344 git init dst &&
1345 (
1346 cd dst &&
1347 git remote add origin ../src &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +00001348 git config branch.main.remote origin &&
1349 git config branch.main.merge refs/heads/main &&
Junio C Hamanoc2aba152013-03-04 12:09:50 -08001350 git pull &&
1351 git for-each-ref >../actual
1352 ) &&
1353 test_cmp expect actual
1354'
1355
Jeff King34066f02019-04-13 01:57:37 -04001356test_expect_success 'peeled advertisements are not considered ref tips' '
1357 mk_empty testrepo &&
1358 git -C testrepo commit --allow-empty -m one &&
1359 git -C testrepo commit --allow-empty -m two &&
1360 git -C testrepo tag -m foo mytag HEAD^ &&
1361 oid=$(git -C testrepo rev-parse mytag^{commit}) &&
Jonathan Nieder8a1b0972019-12-23 17:01:10 -08001362 test_must_fail env GIT_TEST_PROTOCOL_VERSION=0 \
Jeff King34066f02019-04-13 01:57:37 -04001363 git fetch testrepo $oid 2>err &&
1364 test_i18ngrep "Server does not allow request for unadvertised object" err
1365'
1366
Junio C Hamanoca024652013-12-03 15:41:15 -08001367test_expect_success 'pushing a specific ref applies remote.$name.push as refmap' '
Johannes Schindelinbc925ce2020-11-18 23:44:32 +00001368 mk_test testrepo heads/main &&
Junio C Hamanoca024652013-12-03 15:41:15 -08001369 rm -fr src dst &&
1370 git init src &&
1371 git init --bare dst &&
1372 (
1373 cd src &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +00001374 git pull ../testrepo main &&
Junio C Hamanoca024652013-12-03 15:41:15 -08001375 git branch next &&
1376 git config remote.dst.url ../dst &&
1377 git config remote.dst.push "+refs/heads/*:refs/remotes/src/*" &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +00001378 git push dst main &&
1379 git show-ref refs/heads/main |
Junio C Hamanoca024652013-12-03 15:41:15 -08001380 sed -e "s|refs/heads/|refs/remotes/src/|" >../dst/expect
1381 ) &&
1382 (
1383 cd dst &&
1384 test_must_fail git show-ref refs/heads/next &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +00001385 test_must_fail git show-ref refs/heads/main &&
1386 git show-ref refs/remotes/src/main >actual
Junio C Hamanoca024652013-12-03 15:41:15 -08001387 ) &&
1388 test_cmp dst/expect dst/actual
1389'
1390
1391test_expect_success 'with no remote.$name.push, it is not used as refmap' '
Johannes Schindelinbc925ce2020-11-18 23:44:32 +00001392 mk_test testrepo heads/main &&
Junio C Hamanoca024652013-12-03 15:41:15 -08001393 rm -fr src dst &&
1394 git init src &&
1395 git init --bare dst &&
1396 (
1397 cd src &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +00001398 git pull ../testrepo main &&
Junio C Hamanoca024652013-12-03 15:41:15 -08001399 git branch next &&
1400 git config remote.dst.url ../dst &&
Junio C Hamanofc9261c2013-12-03 16:23:35 -08001401 git config push.default matching &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +00001402 git push dst main &&
1403 git show-ref refs/heads/main >../dst/expect
Junio C Hamanoca024652013-12-03 15:41:15 -08001404 ) &&
1405 (
1406 cd dst &&
1407 test_must_fail git show-ref refs/heads/next &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +00001408 git show-ref refs/heads/main >actual
Junio C Hamanoca024652013-12-03 15:41:15 -08001409 ) &&
1410 test_cmp dst/expect dst/actual
1411'
1412
Junio C Hamanofc9261c2013-12-03 16:23:35 -08001413test_expect_success 'with no remote.$name.push, upstream mapping is used' '
Johannes Schindelinbc925ce2020-11-18 23:44:32 +00001414 mk_test testrepo heads/main &&
Junio C Hamanofc9261c2013-12-03 16:23:35 -08001415 rm -fr src dst &&
1416 git init src &&
1417 git init --bare dst &&
1418 (
1419 cd src &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +00001420 git pull ../testrepo main &&
Junio C Hamanofc9261c2013-12-03 16:23:35 -08001421 git branch next &&
1422 git config remote.dst.url ../dst &&
1423 git config remote.dst.fetch "+refs/heads/*:refs/remotes/dst/*" &&
1424 git config push.default upstream &&
1425
Johannes Schindelinbc925ce2020-11-18 23:44:32 +00001426 git config branch.main.merge refs/heads/trunk &&
1427 git config branch.main.remote dst &&
Junio C Hamanofc9261c2013-12-03 16:23:35 -08001428
Johannes Schindelinbc925ce2020-11-18 23:44:32 +00001429 git push dst main &&
1430 git show-ref refs/heads/main |
1431 sed -e "s|refs/heads/main|refs/heads/trunk|" >../dst/expect
Junio C Hamanofc9261c2013-12-03 16:23:35 -08001432 ) &&
1433 (
1434 cd dst &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +00001435 test_must_fail git show-ref refs/heads/main &&
Junio C Hamanofc9261c2013-12-03 16:23:35 -08001436 test_must_fail git show-ref refs/heads/next &&
1437 git show-ref refs/heads/trunk >actual
1438 ) &&
1439 test_cmp dst/expect dst/actual
1440'
1441
Junio C Hamanoc2aba152013-03-04 12:09:50 -08001442test_expect_success 'push does not follow tags by default' '
Johannes Schindelinbc925ce2020-11-18 23:44:32 +00001443 mk_test testrepo heads/main &&
Junio C Hamanoc2aba152013-03-04 12:09:50 -08001444 rm -fr src dst &&
1445 git init src &&
1446 git init --bare dst &&
1447 (
1448 cd src &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +00001449 git pull ../testrepo main &&
Junio C Hamanoc2aba152013-03-04 12:09:50 -08001450 git tag -m "annotated" tag &&
1451 git checkout -b another &&
1452 git commit --allow-empty -m "future commit" &&
1453 git tag -m "future" future &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +00001454 git checkout main &&
1455 git for-each-ref refs/heads/main >../expect &&
1456 git push ../dst main
Junio C Hamanoc2aba152013-03-04 12:09:50 -08001457 ) &&
1458 (
1459 cd dst &&
1460 git for-each-ref >../actual
1461 ) &&
1462 test_cmp expect actual
1463'
1464
Johannes Schindelinf6188dc2019-03-25 11:14:21 -07001465test_expect_success 'push --follow-tags only pushes relevant tags' '
Johannes Schindelinbc925ce2020-11-18 23:44:32 +00001466 mk_test testrepo heads/main &&
Junio C Hamanoc2aba152013-03-04 12:09:50 -08001467 rm -fr src dst &&
1468 git init src &&
1469 git init --bare dst &&
1470 (
1471 cd src &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +00001472 git pull ../testrepo main &&
Junio C Hamanoc2aba152013-03-04 12:09:50 -08001473 git tag -m "annotated" tag &&
1474 git checkout -b another &&
1475 git commit --allow-empty -m "future commit" &&
1476 git tag -m "future" future &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +00001477 git checkout main &&
1478 git for-each-ref refs/heads/main refs/tags/tag >../expect &&
1479 git push --follow-tags ../dst main
Junio C Hamanoc2aba152013-03-04 12:09:50 -08001480 ) &&
1481 (
1482 cd dst &&
1483 git for-each-ref >../actual
1484 ) &&
1485 test_cmp expect actual
1486'
1487
Nguyễn Thái Ngọc Duyf7c815c2013-08-12 20:55:55 +07001488test_expect_success 'push --no-thin must produce non-thin pack' '
1489 cat >>path1 <<\EOF &&
1490keep base version of path1 big enough, compared to the new changes
1491later, in order to pass size heuristics in
1492builtin/pack-objects.c:try_delta()
1493EOF
1494 git commit -am initial &&
1495 git init no-thin &&
1496 git --git-dir=no-thin/.git config receive.unpacklimit 0 &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +00001497 git push no-thin/.git refs/heads/main:refs/heads/foo &&
Nguyễn Thái Ngọc Duyf7c815c2013-08-12 20:55:55 +07001498 echo modified >> path1 &&
1499 git commit -am modified &&
1500 git repack -adf &&
1501 rcvpck="git receive-pack --reject-thin-pack-for-testing" &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +00001502 git push --no-thin --receive-pack="$rcvpck" no-thin/.git refs/heads/main:refs/heads/foo
Nguyễn Thái Ngọc Duyf7c815c2013-08-12 20:55:55 +07001503'
1504
Jeff King458a7e52014-10-16 20:03:41 -04001505test_expect_success 'pushing a tag pushes the tagged object' '
1506 rm -rf dst.git &&
1507 blob=$(echo unreferenced | git hash-object -w --stdin) &&
1508 git tag -m foo tag-of-blob $blob &&
1509 git init --bare dst.git &&
1510 git push dst.git tag-of-blob &&
1511 # the receiving index-pack should have noticed
1512 # any problems, but we double check
1513 echo unreferenced >expect &&
1514 git --git-dir=dst.git cat-file blob tag-of-blob >actual &&
1515 test_cmp expect actual
1516'
1517
Jeff King72549df2014-11-04 08:11:19 -05001518test_expect_success 'push into bare respects core.logallrefupdates' '
1519 rm -rf dst.git &&
1520 git init --bare dst.git &&
1521 git -C dst.git config core.logallrefupdates true &&
1522
1523 # double push to test both with and without
1524 # the actual pack transfer
Johannes Schindelinbc925ce2020-11-18 23:44:32 +00001525 git push dst.git main:one &&
Jeff King72549df2014-11-04 08:11:19 -05001526 echo "one@{0} push" >expect &&
1527 git -C dst.git log -g --format="%gd %gs" one >actual &&
1528 test_cmp expect actual &&
1529
Johannes Schindelinbc925ce2020-11-18 23:44:32 +00001530 git push dst.git main:two &&
Jeff King72549df2014-11-04 08:11:19 -05001531 echo "two@{0} push" >expect &&
1532 git -C dst.git log -g --format="%gd %gs" two >actual &&
1533 test_cmp expect actual
1534'
1535
1536test_expect_success 'fetch into bare respects core.logallrefupdates' '
1537 rm -rf dst.git &&
1538 git init --bare dst.git &&
1539 (
1540 cd dst.git &&
1541 git config core.logallrefupdates true &&
1542
1543 # as above, we double-fetch to test both
1544 # with and without pack transfer
Johannes Schindelinbc925ce2020-11-18 23:44:32 +00001545 git fetch .. main:one &&
1546 echo "one@{0} fetch .. main:one: storing head" >expect &&
Jeff King72549df2014-11-04 08:11:19 -05001547 git log -g --format="%gd %gs" one >actual &&
1548 test_cmp expect actual &&
1549
Johannes Schindelinbc925ce2020-11-18 23:44:32 +00001550 git fetch .. main:two &&
1551 echo "two@{0} fetch .. main:two: storing head" >expect &&
Jeff King72549df2014-11-04 08:11:19 -05001552 git log -g --format="%gd %gs" two >actual &&
1553 test_cmp expect actual
1554 )
1555'
1556
Johannes Schindelin1404bcb2014-11-26 23:44:16 +01001557test_expect_success 'receive.denyCurrentBranch = updateInstead' '
Johannes Schindelinbc925ce2020-11-18 23:44:32 +00001558 git push testrepo main &&
Junio C Hamano4d7a5ce2014-11-30 17:54:30 -08001559 (
1560 cd testrepo &&
Johannes Schindelin1404bcb2014-11-26 23:44:16 +01001561 git reset --hard &&
1562 git config receive.denyCurrentBranch updateInstead
1563 ) &&
1564 test_commit third path2 &&
Junio C Hamano4d7a5ce2014-11-30 17:54:30 -08001565
1566 # Try pushing into a repository with pristine working tree
Johannes Schindelinbc925ce2020-11-18 23:44:32 +00001567 git push testrepo main &&
Junio C Hamano4d7a5ce2014-11-30 17:54:30 -08001568 (
1569 cd testrepo &&
Johannes Schindelin1404bcb2014-11-26 23:44:16 +01001570 git update-index -q --refresh &&
1571 git diff-files --quiet -- &&
1572 git diff-index --quiet --cached HEAD -- &&
Junio C Hamano4d7a5ce2014-11-30 17:54:30 -08001573 test third = "$(cat path2)" &&
1574 test $(git -C .. rev-parse HEAD) = $(git rev-parse HEAD)
Johannes Schindelin1404bcb2014-11-26 23:44:16 +01001575 ) &&
Junio C Hamano4d7a5ce2014-11-30 17:54:30 -08001576
1577 # Try pushing into a repository with working tree needing a refresh
1578 (
1579 cd testrepo &&
1580 git reset --hard HEAD^ &&
1581 test $(git -C .. rev-parse HEAD^) = $(git rev-parse HEAD) &&
Nguyễn Thái Ngọc Duy0e496492018-03-24 08:44:31 +01001582 test-tool chmtime +100 path1
Junio C Hamano4d7a5ce2014-11-30 17:54:30 -08001583 ) &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +00001584 git push testrepo main &&
Junio C Hamano4d7a5ce2014-11-30 17:54:30 -08001585 (
1586 cd testrepo &&
1587 git update-index -q --refresh &&
1588 git diff-files --quiet -- &&
1589 git diff-index --quiet --cached HEAD -- &&
1590 test_cmp ../path1 path1 &&
1591 test third = "$(cat path2)" &&
1592 test $(git -C .. rev-parse HEAD) = $(git rev-parse HEAD)
1593 ) &&
1594
1595 # Update what is to be pushed
Johannes Schindelin1404bcb2014-11-26 23:44:16 +01001596 test_commit fourth path2 &&
Junio C Hamano4d7a5ce2014-11-30 17:54:30 -08001597
1598 # Try pushing into a repository with a dirty working tree
1599 # (1) the working tree updated
1600 (
1601 cd testrepo &&
1602 echo changed >path1
1603 ) &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +00001604 test_must_fail git push testrepo main &&
Junio C Hamano4d7a5ce2014-11-30 17:54:30 -08001605 (
1606 cd testrepo &&
1607 test $(git -C .. rev-parse HEAD^) = $(git rev-parse HEAD) &&
1608 git diff --quiet --cached &&
1609 test changed = "$(cat path1)"
1610 ) &&
1611
1612 # (2) the index updated
1613 (
1614 cd testrepo &&
1615 echo changed >path1 &&
1616 git add path1
1617 ) &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +00001618 test_must_fail git push testrepo main &&
Junio C Hamano4d7a5ce2014-11-30 17:54:30 -08001619 (
1620 cd testrepo &&
1621 test $(git -C .. rev-parse HEAD^) = $(git rev-parse HEAD) &&
Johannes Schindelin1404bcb2014-11-26 23:44:16 +01001622 git diff --quiet &&
Junio C Hamano4d7a5ce2014-11-30 17:54:30 -08001623 test changed = "$(cat path1)"
1624 ) &&
1625
1626 # Introduce a new file in the update
1627 test_commit fifth path3 &&
1628
1629 # (3) the working tree has an untracked file that would interfere
1630 (
1631 cd testrepo &&
1632 git reset --hard &&
1633 echo changed >path3
1634 ) &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +00001635 test_must_fail git push testrepo main &&
Junio C Hamano4d7a5ce2014-11-30 17:54:30 -08001636 (
1637 cd testrepo &&
1638 test $(git -C .. rev-parse HEAD^^) = $(git rev-parse HEAD) &&
1639 git diff --quiet &&
1640 git diff --quiet --cached &&
1641 test changed = "$(cat path3)"
1642 ) &&
1643
1644 # (4) the target changes to what gets pushed but it still is a change
1645 (
1646 cd testrepo &&
1647 git reset --hard &&
1648 echo fifth >path3 &&
1649 git add path3
1650 ) &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +00001651 test_must_fail git push testrepo main &&
Junio C Hamano4d7a5ce2014-11-30 17:54:30 -08001652 (
1653 cd testrepo &&
1654 test $(git -C .. rev-parse HEAD^^) = $(git rev-parse HEAD) &&
1655 git diff --quiet &&
1656 test fifth = "$(cat path3)"
Junio C Hamano1a51b522015-03-31 23:15:45 -07001657 ) &&
Junio C Hamano4d7a5ce2014-11-30 17:54:30 -08001658
Junio C Hamano1a51b522015-03-31 23:15:45 -07001659 # (5) push into void
1660 rm -fr void &&
1661 git init void &&
1662 (
1663 cd void &&
1664 git config receive.denyCurrentBranch updateInstead
1665 ) &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +00001666 git push void main &&
Junio C Hamano1a51b522015-03-31 23:15:45 -07001667 (
1668 cd void &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +00001669 test $(git -C .. rev-parse main) = $(git rev-parse HEAD) &&
Junio C Hamano1a51b522015-03-31 23:15:45 -07001670 git diff --quiet &&
1671 git diff --cached --quiet
Junio C Hamanob072a252018-10-19 13:57:35 +09001672 ) &&
1673
1674 # (6) updateInstead intervened by fast-forward check
Johannes Schindelinbc925ce2020-11-18 23:44:32 +00001675 test_must_fail git push void main^:main &&
1676 test $(git -C void rev-parse HEAD) = $(git rev-parse main) &&
Junio C Hamanob072a252018-10-19 13:57:35 +09001677 git -C void diff --quiet &&
1678 git -C void diff --cached --quiet
Johannes Schindelin1404bcb2014-11-26 23:44:16 +01001679'
1680
Junio C Hamano08553312014-12-01 15:29:54 -08001681test_expect_success 'updateInstead with push-to-checkout hook' '
1682 rm -fr testrepo &&
1683 git init testrepo &&
1684 (
1685 cd testrepo &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +00001686 git pull .. main &&
Junio C Hamano08553312014-12-01 15:29:54 -08001687 git reset --hard HEAD^^ &&
1688 git tag initial &&
1689 git config receive.denyCurrentBranch updateInstead &&
1690 write_script .git/hooks/push-to-checkout <<-\EOF
1691 echo >&2 updating from $(git rev-parse HEAD)
1692 echo >&2 updating to "$1"
1693
1694 git update-index -q --refresh &&
1695 git read-tree -u -m HEAD "$1" || {
1696 status=$?
1697 echo >&2 read-tree failed
1698 exit $status
1699 }
1700 EOF
1701 ) &&
1702
1703 # Try pushing into a pristine
Johannes Schindelinbc925ce2020-11-18 23:44:32 +00001704 git push testrepo main &&
Junio C Hamano08553312014-12-01 15:29:54 -08001705 (
1706 cd testrepo &&
1707 git diff --quiet &&
1708 git diff HEAD --quiet &&
1709 test $(git -C .. rev-parse HEAD) = $(git rev-parse HEAD)
1710 ) &&
1711
1712 # Try pushing into a repository with conflicting change
1713 (
1714 cd testrepo &&
1715 git reset --hard initial &&
1716 echo conflicting >path2
1717 ) &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +00001718 test_must_fail git push testrepo main &&
Junio C Hamano08553312014-12-01 15:29:54 -08001719 (
1720 cd testrepo &&
1721 test $(git rev-parse initial) = $(git rev-parse HEAD) &&
1722 test conflicting = "$(cat path2)" &&
1723 git diff-index --quiet --cached HEAD
1724 ) &&
1725
1726 # Try pushing into a repository with unrelated change
1727 (
1728 cd testrepo &&
1729 git reset --hard initial &&
1730 echo unrelated >path1 &&
1731 echo irrelevant >path5 &&
1732 git add path5
1733 ) &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +00001734 git push testrepo main &&
Junio C Hamano08553312014-12-01 15:29:54 -08001735 (
1736 cd testrepo &&
1737 test "$(cat path1)" = unrelated &&
1738 test "$(cat path5)" = irrelevant &&
1739 test "$(git diff --name-only --cached HEAD)" = path5 &&
1740 test $(git -C .. rev-parse HEAD) = $(git rev-parse HEAD)
Junio C Hamano1a51b522015-03-31 23:15:45 -07001741 ) &&
1742
1743 # push into void
1744 rm -fr void &&
1745 git init void &&
1746 (
1747 cd void &&
1748 git config receive.denyCurrentBranch updateInstead &&
1749 write_script .git/hooks/push-to-checkout <<-\EOF
1750 if git rev-parse --quiet --verify HEAD
1751 then
1752 has_head=yes
1753 echo >&2 updating from $(git rev-parse HEAD)
1754 else
1755 has_head=no
1756 echo >&2 pushing into void
1757 fi
1758 echo >&2 updating to "$1"
1759
1760 git update-index -q --refresh &&
1761 case "$has_head" in
1762 yes)
1763 git read-tree -u -m HEAD "$1" ;;
1764 no)
1765 git read-tree -u -m "$1" ;;
1766 esac || {
1767 status=$?
1768 echo >&2 read-tree failed
1769 exit $status
1770 }
1771 EOF
1772 ) &&
1773
Johannes Schindelinbc925ce2020-11-18 23:44:32 +00001774 git push void main &&
Junio C Hamano1a51b522015-03-31 23:15:45 -07001775 (
1776 cd void &&
1777 git diff --quiet &&
1778 git diff --cached --quiet &&
1779 test $(git -C .. rev-parse HEAD) = $(git rev-parse HEAD)
Junio C Hamano08553312014-12-01 15:29:54 -08001780 )
1781'
1782
Hariom Verma4ef34642020-02-23 18:57:10 +00001783test_expect_success 'denyCurrentBranch and worktrees' '
1784 git worktree add new-wt &&
1785 git clone . cloned &&
1786 test_commit -C cloned first &&
1787 test_config receive.denyCurrentBranch refuse &&
1788 test_must_fail git -C cloned push origin HEAD:new-wt &&
1789 test_config receive.denyCurrentBranch updateInstead &&
1790 git -C cloned push origin HEAD:new-wt &&
1791 test_must_fail git -C cloned push --delete origin new-wt
1792'
Junio C Hamanobcdb34f2007-06-08 00:43:22 -07001793test_done