blob: 541adbb31034f11820c77972719258ef49131353 [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
Derrick Stolee6dcbdc02022-06-06 14:36:16 +000015* URL validation
Ramkumar Ramachandra2ead7a62013-04-02 13:10:30 +053016'
Junio C Hamanobcdb34f2007-06-08 00:43:22 -070017
Johannes Schindelinbc925ce2020-11-18 23:44:32 +000018GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
Johannes Schindelin334afbc2020-11-18 23:44:19 +000019export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
20
Junio C Hamanobcdb34f2007-06-08 00:43:22 -070021. ./test-lib.sh
22
Elia Pintobf452422015-12-23 14:45:57 +010023D=$(pwd)
Junio C Hamanobcdb34f2007-06-08 00:43:22 -070024
25mk_empty () {
Jeff King2e433b72013-04-02 13:10:31 +053026 repo_name="$1"
Ævar Arnfjörð Bjarmasonf818f7f2022-03-17 11:13:11 +010027 test_when_finished "rm -rf \"$repo_name\"" &&
28 test_path_is_missing "$repo_name" &&
29 git init "$repo_name" &&
30 git -C "$repo_name" config receive.denyCurrentBranch warn
Junio C Hamanobcdb34f2007-06-08 00:43:22 -070031}
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 "$@" &&
Ævar Arnfjörð Bjarmasonf818f7f2022-03-17 11:13:11 +010059 test_hook -C "$repo_name" pre-receive <<-'EOF' &&
60 cat - >>pre-receive.actual
61 EOF
Pang Yan Han160b81e2011-09-28 23:39:35 +080062
Ævar Arnfjörð Bjarmasonf818f7f2022-03-17 11:13:11 +010063 test_hook -C "$repo_name" update <<-'EOF' &&
64 printf "%s %s %s\n" "$@" >>update.actual
65 EOF
Pang Yan Han160b81e2011-09-28 23:39:35 +080066
Ævar Arnfjörð Bjarmasonf818f7f2022-03-17 11:13:11 +010067 test_hook -C "$repo_name" post-receive <<-'EOF' &&
68 cat - >>post-receive.actual
69 EOF
Pang Yan Han160b81e2011-09-28 23:39:35 +080070
Ævar Arnfjörð Bjarmasonf818f7f2022-03-17 11:13:11 +010071 test_hook -C "$repo_name" post-update <<-'EOF'
72 for ref in "$@"
73 do
74 printf "%s\n" "$ref" >>post-update.actual
75 done
76 EOF
Pang Yan Han160b81e2011-09-28 23:39:35 +080077}
78
Jeff Kingb2dc9682008-11-07 17:20:33 -050079mk_child() {
Ævar Arnfjörð Bjarmasonf818f7f2022-03-17 11:13:11 +010080 test_when_finished "rm -rf \"$2\"" &&
Jeff King2e433b72013-04-02 13:10:31 +053081 git clone "$1" "$2"
Jeff Kingb2dc9682008-11-07 17:20:33 -050082}
83
Junio C Hamano61257962007-06-09 01:23:53 -070084check_push_result () {
SZEDER Gáborcfb482b2018-05-10 16:01:53 +020085 test $# -ge 3 ||
SZEDER Gábor165293a2018-11-19 14:13:26 +010086 BUG "check_push_result requires at least 3 parameters"
SZEDER Gáborcfb482b2018-05-10 16:01:53 +020087
Jeff King2e433b72013-04-02 13:10:31 +053088 repo_name="$1"
89 shift
90
Junio C Hamano61257962007-06-09 01:23:53 -070091 (
Jeff King2e433b72013-04-02 13:10:31 +053092 cd "$repo_name" &&
Jonathan Nieder848575d2013-03-18 16:13:41 -070093 echo "$1" >expect &&
94 shift &&
Junio C Hamano61257962007-06-09 01:23:53 -070095 for ref in "$@"
96 do
Jonathan Nieder848575d2013-03-18 16:13:41 -070097 git show-ref -s --verify refs/$ref >actual &&
98 test_cmp expect actual ||
99 exit
Junio C Hamano61257962007-06-09 01:23:53 -0700100 done &&
101 git fsck --full
102 )
103}
104
Junio C Hamanobcdb34f2007-06-08 00:43:22 -0700105test_expect_success setup '
106
Jay Soffiand4785cd2010-04-19 18:08:31 -0400107 >path1 &&
Junio C Hamanobcdb34f2007-06-08 00:43:22 -0700108 git add path1 &&
109 test_tick &&
110 git commit -a -m repo &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000111 the_first_commit=$(git show-ref -s --verify refs/heads/main) &&
Junio C Hamano61257962007-06-09 01:23:53 -0700112
Jay Soffiand4785cd2010-04-19 18:08:31 -0400113 >path2 &&
Junio C Hamano61257962007-06-09 01:23:53 -0700114 git add path2 &&
115 test_tick &&
116 git commit -a -m second &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000117 the_commit=$(git show-ref -s --verify refs/heads/main)
Junio C Hamanobcdb34f2007-06-08 00:43:22 -0700118
119'
120
121test_expect_success 'fetch without wildcard' '
Jeff King2e433b72013-04-02 13:10:31 +0530122 mk_empty testrepo &&
Junio C Hamanobcdb34f2007-06-08 00:43:22 -0700123 (
124 cd testrepo &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000125 git fetch .. refs/heads/main:refs/remotes/origin/main &&
Junio C Hamanobcdb34f2007-06-08 00:43:22 -0700126
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000127 echo "$the_commit commit refs/remotes/origin/main" >expect &&
Jonathan Nieder848575d2013-03-18 16:13:41 -0700128 git for-each-ref refs/remotes/origin >actual &&
129 test_cmp expect actual
Junio C Hamanobcdb34f2007-06-08 00:43:22 -0700130 )
131'
132
133test_expect_success 'fetch with 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 config remote.up.url .. &&
138 git config remote.up.fetch "refs/heads/*:refs/remotes/origin/*" &&
139 git fetch up &&
140
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000141 echo "$the_commit commit refs/remotes/origin/main" >expect &&
Jonathan Nieder848575d2013-03-18 16:13:41 -0700142 git for-each-ref refs/remotes/origin >actual &&
143 test_cmp expect actual
Junio C Hamanobcdb34f2007-06-08 00:43:22 -0700144 )
145'
146
Daniel Barkalow55029ae2008-02-20 13:43:53 -0500147test_expect_success 'fetch with insteadOf' '
Jeff King2e433b72013-04-02 13:10:31 +0530148 mk_empty testrepo &&
Daniel Barkalow55029ae2008-02-20 13:43:53 -0500149 (
Daniel Barkalow60e3aba2008-04-12 15:32:00 -0400150 TRASH=$(pwd)/ &&
Daniel Barkalow55029ae2008-02-20 13:43:53 -0500151 cd testrepo &&
Bryan Donlanf69e8362008-05-04 01:37:59 -0400152 git config "url.$TRASH.insteadOf" trash/ &&
Daniel Barkalow55029ae2008-02-20 13:43:53 -0500153 git config remote.up.url trash/. &&
154 git config remote.up.fetch "refs/heads/*:refs/remotes/origin/*" &&
155 git fetch up &&
156
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000157 echo "$the_commit commit refs/remotes/origin/main" >expect &&
Jonathan Nieder848575d2013-03-18 16:13:41 -0700158 git for-each-ref refs/remotes/origin >actual &&
159 test_cmp expect actual
Daniel Barkalow55029ae2008-02-20 13:43:53 -0500160 )
161'
162
Josh Triplett1c2eafb2009-09-07 01:56:33 -0700163test_expect_success 'fetch with pushInsteadOf (should not rewrite)' '
Jeff King2e433b72013-04-02 13:10:31 +0530164 mk_empty testrepo &&
Josh Triplett1c2eafb2009-09-07 01:56:33 -0700165 (
166 TRASH=$(pwd)/ &&
167 cd testrepo &&
168 git config "url.trash/.pushInsteadOf" "$TRASH" &&
169 git config remote.up.url "$TRASH." &&
170 git config remote.up.fetch "refs/heads/*:refs/remotes/origin/*" &&
171 git fetch up &&
172
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000173 echo "$the_commit commit refs/remotes/origin/main" >expect &&
Jonathan Nieder848575d2013-03-18 16:13:41 -0700174 git for-each-ref refs/remotes/origin >actual &&
175 test_cmp expect actual
Josh Triplett1c2eafb2009-09-07 01:56:33 -0700176 )
177'
178
Jonathan Tan477673d2021-05-04 14:16:02 -0700179grep_wrote () {
180 object_count=$1
181 file_name=$2
182 grep 'write_pack_file/wrote.*"value":"'$1'"' $2
183}
184
Ævar Arnfjörð Bjarmasonf818f7f2022-03-17 11:13:11 +0100185test_expect_success 'push without negotiation' '
Jonathan Tan477673d2021-05-04 14:16:02 -0700186 mk_empty testrepo &&
187 git push testrepo $the_first_commit:refs/remotes/origin/first_commit &&
Jonathan Tan54a03bc2021-07-15 10:44:31 -0700188 test_commit -C testrepo unrelated_commit &&
Jonathan Tan477673d2021-05-04 14:16:02 -0700189 git -C testrepo config receive.hideRefs refs/remotes/origin/first_commit &&
Ævar Arnfjörð Bjarmasonf818f7f2022-03-17 11:13:11 +0100190 test_when_finished "rm event" &&
Jonathan Tan477673d2021-05-04 14:16:02 -0700191 GIT_TRACE2_EVENT="$(pwd)/event" git -c protocol.version=2 push testrepo refs/heads/main:refs/remotes/origin/main &&
Ævar Arnfjörð Bjarmasonf818f7f2022-03-17 11:13:11 +0100192 grep_wrote 5 event # 2 commits, 2 trees, 1 blob
193'
Jonathan Tan477673d2021-05-04 14:16:02 -0700194
Ævar Arnfjörð Bjarmasonf818f7f2022-03-17 11:13:11 +0100195test_expect_success 'push with negotiation' '
Jonathan Tan477673d2021-05-04 14:16:02 -0700196 mk_empty testrepo &&
197 git push testrepo $the_first_commit:refs/remotes/origin/first_commit &&
Jonathan Tan54a03bc2021-07-15 10:44:31 -0700198 test_commit -C testrepo unrelated_commit &&
Jonathan Tan477673d2021-05-04 14:16:02 -0700199 git -C testrepo config receive.hideRefs refs/remotes/origin/first_commit &&
Ævar Arnfjörð Bjarmasonf818f7f2022-03-17 11:13:11 +0100200 test_when_finished "rm event" &&
Jonathan Tan477673d2021-05-04 14:16:02 -0700201 GIT_TRACE2_EVENT="$(pwd)/event" git -c protocol.version=2 -c push.negotiate=1 push testrepo refs/heads/main:refs/remotes/origin/main &&
202 grep_wrote 2 event # 1 commit, 1 tree
203'
204
205test_expect_success 'push with negotiation proceeds anyway even if negotiation fails' '
Jonathan Tan477673d2021-05-04 14:16:02 -0700206 mk_empty testrepo &&
207 git push testrepo $the_first_commit:refs/remotes/origin/first_commit &&
Jonathan Tan54a03bc2021-07-15 10:44:31 -0700208 test_commit -C testrepo unrelated_commit &&
Jonathan Tan477673d2021-05-04 14:16:02 -0700209 git -C testrepo config receive.hideRefs refs/remotes/origin/first_commit &&
Ævar Arnfjörð Bjarmasonf818f7f2022-03-17 11:13:11 +0100210 test_when_finished "rm event" &&
Jonathan Tan477673d2021-05-04 14:16:02 -0700211 GIT_TEST_PROTOCOL_VERSION=0 GIT_TRACE2_EVENT="$(pwd)/event" \
212 git -c push.negotiate=1 push testrepo refs/heads/main:refs/remotes/origin/main 2>err &&
213 grep_wrote 5 event && # 2 commits, 2 trees, 1 blob
214 test_i18ngrep "push negotiation failed" err
215'
216
Glen Choo386c0762022-01-18 16:00:56 -0800217test_expect_success 'push with negotiation does not attempt to fetch submodules' '
218 mk_empty submodule_upstream &&
219 test_commit -C submodule_upstream submodule_commit &&
220 git submodule add ./submodule_upstream submodule &&
221 mk_empty testrepo &&
222 git push testrepo $the_first_commit:refs/remotes/origin/first_commit &&
223 test_commit -C testrepo unrelated_commit &&
224 git -C testrepo config receive.hideRefs refs/remotes/origin/first_commit &&
225 git -c submodule.recurse=true -c protocol.version=2 -c push.negotiate=1 push testrepo refs/heads/main:refs/remotes/origin/main 2>err &&
226 ! grep "Fetching submodule" err
227'
228
Junio C Hamanobcdb34f2007-06-08 00:43:22 -0700229test_expect_success 'push without wildcard' '
Jeff King2e433b72013-04-02 13:10:31 +0530230 mk_empty testrepo &&
Junio C Hamanobcdb34f2007-06-08 00:43:22 -0700231
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000232 git push testrepo refs/heads/main:refs/remotes/origin/main &&
Junio C Hamanobcdb34f2007-06-08 00:43:22 -0700233 (
234 cd testrepo &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000235 echo "$the_commit commit refs/remotes/origin/main" >expect &&
Jonathan Nieder848575d2013-03-18 16:13:41 -0700236 git for-each-ref refs/remotes/origin >actual &&
237 test_cmp expect actual
Junio C Hamanobcdb34f2007-06-08 00:43:22 -0700238 )
239'
240
241test_expect_success 'push with wildcard' '
Jeff King2e433b72013-04-02 13:10:31 +0530242 mk_empty testrepo &&
Junio C Hamanobcdb34f2007-06-08 00:43:22 -0700243
244 git push testrepo "refs/heads/*:refs/remotes/origin/*" &&
245 (
246 cd testrepo &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000247 echo "$the_commit commit refs/remotes/origin/main" >expect &&
Jonathan Nieder848575d2013-03-18 16:13:41 -0700248 git for-each-ref refs/remotes/origin >actual &&
249 test_cmp expect actual
Junio C Hamanobcdb34f2007-06-08 00:43:22 -0700250 )
251'
252
Daniel Barkalow55029ae2008-02-20 13:43:53 -0500253test_expect_success 'push with insteadOf' '
Jeff King2e433b72013-04-02 13:10:31 +0530254 mk_empty testrepo &&
Bryan Donlanf69e8362008-05-04 01:37:59 -0400255 TRASH="$(pwd)/" &&
Jonathan Nieder3c695522013-03-18 16:12:11 -0700256 test_config "url.$TRASH.insteadOf" trash/ &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000257 git push trash/testrepo refs/heads/main:refs/remotes/origin/main &&
Daniel Barkalow55029ae2008-02-20 13:43:53 -0500258 (
259 cd testrepo &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000260 echo "$the_commit commit refs/remotes/origin/main" >expect &&
Jonathan Nieder848575d2013-03-18 16:13:41 -0700261 git for-each-ref refs/remotes/origin >actual &&
262 test_cmp expect actual
Daniel Barkalow55029ae2008-02-20 13:43:53 -0500263 )
264'
265
Josh Triplett1c2eafb2009-09-07 01:56:33 -0700266test_expect_success 'push with pushInsteadOf' '
Jeff King2e433b72013-04-02 13:10:31 +0530267 mk_empty testrepo &&
Josh Triplett1c2eafb2009-09-07 01:56:33 -0700268 TRASH="$(pwd)/" &&
Jonathan Nieder3c695522013-03-18 16:12:11 -0700269 test_config "url.$TRASH.pushInsteadOf" trash/ &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000270 git push trash/testrepo refs/heads/main:refs/remotes/origin/main &&
Josh Triplett1c2eafb2009-09-07 01:56:33 -0700271 (
272 cd testrepo &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000273 echo "$the_commit commit refs/remotes/origin/main" >expect &&
Jonathan Nieder848575d2013-03-18 16:13:41 -0700274 git for-each-ref refs/remotes/origin >actual &&
275 test_cmp expect actual
Josh Triplett1c2eafb2009-09-07 01:56:33 -0700276 )
277'
278
279test_expect_success 'push with pushInsteadOf and explicit pushurl (pushInsteadOf should not rewrite)' '
Jeff King2e433b72013-04-02 13:10:31 +0530280 mk_empty testrepo &&
Junio C Hamano41ae34d2013-04-03 09:34:48 -0700281 test_config "url.trash2/.pushInsteadOf" testrepo/ &&
Anders Kaseorgeb32c662015-02-28 23:18:14 -0500282 test_config "url.trash3/.pushInsteadOf" trash/wrong &&
Jonathan Nieder3c695522013-03-18 16:12:11 -0700283 test_config remote.r.url trash/wrong &&
Junio C Hamano41ae34d2013-04-03 09:34:48 -0700284 test_config remote.r.pushurl "testrepo/" &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000285 git push r refs/heads/main:refs/remotes/origin/main &&
Josh Triplett1c2eafb2009-09-07 01:56:33 -0700286 (
287 cd testrepo &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000288 echo "$the_commit commit refs/remotes/origin/main" >expect &&
Jonathan Nieder848575d2013-03-18 16:13:41 -0700289 git for-each-ref refs/remotes/origin >actual &&
290 test_cmp expect actual
Josh Triplett1c2eafb2009-09-07 01:56:33 -0700291 )
292'
293
Junio C Hamano61257962007-06-09 01:23:53 -0700294test_expect_success 'push with matching heads' '
295
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000296 mk_test testrepo heads/main &&
Junio C Hamano0a42ac02013-01-04 16:08:26 -0800297 git push testrepo : &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000298 check_push_result testrepo $the_commit heads/main
Junio C Hamano61257962007-06-09 01:23:53 -0700299
300'
301
Paolo Bonzinia83619d2008-04-28 11:32:12 -0400302test_expect_success 'push with matching heads on the command line' '
303
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000304 mk_test testrepo heads/main &&
Paolo Bonzinia83619d2008-04-28 11:32:12 -0400305 git push testrepo : &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000306 check_push_result testrepo $the_commit heads/main
Paolo Bonzinia83619d2008-04-28 11:32:12 -0400307
308'
309
310test_expect_success 'failed (non-fast-forward) push with matching heads' '
311
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000312 mk_test testrepo heads/main &&
Paolo Bonzinia83619d2008-04-28 11:32:12 -0400313 git push testrepo : &&
314 git commit --amend -massaged &&
Stephan Beyerd492b312008-07-12 17:47:52 +0200315 test_must_fail git push testrepo &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000316 check_push_result testrepo $the_commit heads/main &&
Paolo Bonzinia83619d2008-04-28 11:32:12 -0400317 git reset --hard $the_commit
318
319'
320
321test_expect_success 'push --force with matching heads' '
322
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000323 mk_test testrepo heads/main &&
Paolo Bonzinia83619d2008-04-28 11:32:12 -0400324 git push testrepo : &&
325 git commit --amend -massaged &&
Junio C Hamano0a42ac02013-01-04 16:08:26 -0800326 git push --force testrepo : &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000327 ! check_push_result testrepo $the_commit heads/main &&
Paolo Bonzinia83619d2008-04-28 11:32:12 -0400328 git reset --hard $the_commit
329
330'
331
332test_expect_success 'push with matching heads and forced update' '
333
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000334 mk_test testrepo heads/main &&
Paolo Bonzinia83619d2008-04-28 11:32:12 -0400335 git push testrepo : &&
336 git commit --amend -massaged &&
337 git push testrepo +: &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000338 ! check_push_result testrepo $the_commit heads/main &&
Paolo Bonzinia83619d2008-04-28 11:32:12 -0400339 git reset --hard $the_commit
340
341'
342
Junio C Hamano61257962007-06-09 01:23:53 -0700343test_expect_success 'push with no ambiguity (1)' '
344
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000345 mk_test testrepo heads/main &&
346 git push testrepo main:main &&
347 check_push_result testrepo $the_commit heads/main
Junio C Hamano61257962007-06-09 01:23:53 -0700348
349'
350
351test_expect_success 'push with no ambiguity (2)' '
352
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000353 mk_test testrepo remotes/origin/main &&
354 git push testrepo main:origin/main &&
355 check_push_result testrepo $the_commit remotes/origin/main
Junio C Hamano61257962007-06-09 01:23:53 -0700356
357'
358
Steffen Prohaskaae36bdc2007-11-11 15:01:47 +0100359test_expect_success 'push with colon-less refspec, no ambiguity' '
360
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000361 mk_test testrepo heads/main heads/t/main &&
362 git branch -f t/main main &&
363 git push testrepo main &&
364 check_push_result testrepo $the_commit heads/main &&
365 check_push_result testrepo $the_first_commit heads/t/main
Steffen Prohaskaae36bdc2007-11-11 15:01:47 +0100366
367'
368
Junio C Hamano61257962007-06-09 01:23:53 -0700369test_expect_success 'push with weak ambiguity (1)' '
370
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000371 mk_test testrepo heads/main remotes/origin/main &&
372 git push testrepo main:main &&
373 check_push_result testrepo $the_commit heads/main &&
374 check_push_result testrepo $the_first_commit remotes/origin/main
Junio C Hamano61257962007-06-09 01:23:53 -0700375
376'
377
378test_expect_success 'push with weak ambiguity (2)' '
379
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000380 mk_test testrepo heads/main remotes/origin/main remotes/another/main &&
381 git push testrepo main:main &&
382 check_push_result testrepo $the_commit heads/main &&
383 check_push_result testrepo $the_first_commit remotes/origin/main remotes/another/main
Junio C Hamano61257962007-06-09 01:23:53 -0700384
385'
386
Jeff King3ef6a1f2008-04-23 05:21:45 -0400387test_expect_success 'push with ambiguity' '
Junio C Hamano61257962007-06-09 01:23:53 -0700388
Jeff King2e433b72013-04-02 13:10:31 +0530389 mk_test testrepo heads/frotz tags/frotz &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000390 test_must_fail git push testrepo main:frotz &&
Jeff King2e433b72013-04-02 13:10:31 +0530391 check_push_result testrepo $the_first_commit heads/frotz tags/frotz
Junio C Hamano1ed10b82007-06-09 01:37:14 -0700392
393'
394
395test_expect_success 'push with colon-less refspec (1)' '
396
Jeff King2e433b72013-04-02 13:10:31 +0530397 mk_test testrepo heads/frotz tags/frotz &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000398 git branch -f frotz main &&
Junio C Hamano1ed10b82007-06-09 01:37:14 -0700399 git push testrepo frotz &&
Jeff King2e433b72013-04-02 13:10:31 +0530400 check_push_result testrepo $the_commit heads/frotz &&
401 check_push_result testrepo $the_first_commit tags/frotz
Junio C Hamano1ed10b82007-06-09 01:37:14 -0700402
403'
404
405test_expect_success 'push with colon-less refspec (2)' '
406
Jeff King2e433b72013-04-02 13:10:31 +0530407 mk_test testrepo heads/frotz tags/frotz &&
Junio C Hamano1ed10b82007-06-09 01:37:14 -0700408 if git show-ref --verify -q refs/heads/frotz
409 then
410 git branch -D frotz
411 fi &&
412 git tag -f frotz &&
Chris Rorvickdbfeddb2012-11-29 19:41:37 -0600413 git push -f testrepo frotz &&
Jeff King2e433b72013-04-02 13:10:31 +0530414 check_push_result testrepo $the_commit tags/frotz &&
415 check_push_result testrepo $the_first_commit heads/frotz
Junio C Hamano1ed10b82007-06-09 01:37:14 -0700416
417'
418
419test_expect_success 'push with colon-less refspec (3)' '
420
Jeff King2e433b72013-04-02 13:10:31 +0530421 mk_test testrepo &&
Junio C Hamano1ed10b82007-06-09 01:37:14 -0700422 if git show-ref --verify -q refs/tags/frotz
423 then
424 git tag -d frotz
425 fi &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000426 git branch -f frotz main &&
Junio C Hamano1ed10b82007-06-09 01:37:14 -0700427 git push testrepo frotz &&
Jeff King2e433b72013-04-02 13:10:31 +0530428 check_push_result testrepo $the_commit heads/frotz &&
Brian Gernhardt9a3c6f72007-07-01 11:48:54 -0400429 test 1 = $( cd testrepo && git show-ref | wc -l )
Junio C Hamano1ed10b82007-06-09 01:37:14 -0700430'
431
432test_expect_success 'push with colon-less refspec (4)' '
433
Jeff King2e433b72013-04-02 13:10:31 +0530434 mk_test testrepo &&
Junio C Hamano1ed10b82007-06-09 01:37:14 -0700435 if git show-ref --verify -q refs/heads/frotz
436 then
437 git branch -D frotz
438 fi &&
439 git tag -f frotz &&
440 git push testrepo frotz &&
Jeff King2e433b72013-04-02 13:10:31 +0530441 check_push_result testrepo $the_commit tags/frotz &&
Brian Gernhardt9a3c6f72007-07-01 11:48:54 -0400442 test 1 = $( cd testrepo && git show-ref | wc -l )
Junio C Hamano1ed10b82007-06-09 01:37:14 -0700443
Junio C Hamano61257962007-06-09 01:23:53 -0700444'
445
Dmitry Ivankov7be8b3b2011-06-16 19:42:48 +0600446test_expect_success 'push head with non-existent, incomplete dest' '
Jeff Kingf8aae122008-04-23 05:16:06 -0400447
Jeff King2e433b72013-04-02 13:10:31 +0530448 mk_test testrepo &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000449 git push testrepo main:branch &&
Jeff King2e433b72013-04-02 13:10:31 +0530450 check_push_result testrepo $the_commit heads/branch
Jeff Kingf8aae122008-04-23 05:16:06 -0400451
452'
453
Dmitry Ivankov7be8b3b2011-06-16 19:42:48 +0600454test_expect_success 'push tag with non-existent, incomplete dest' '
Jeff Kingf8aae122008-04-23 05:16:06 -0400455
Jeff King2e433b72013-04-02 13:10:31 +0530456 mk_test testrepo &&
Jeff Kingf8aae122008-04-23 05:16:06 -0400457 git tag -f v1.0 &&
458 git push testrepo v1.0:tag &&
Jeff King2e433b72013-04-02 13:10:31 +0530459 check_push_result testrepo $the_commit tags/tag
Jeff Kingf8aae122008-04-23 05:16:06 -0400460
461'
462
Dmitry Ivankov7be8b3b2011-06-16 19:42:48 +0600463test_expect_success 'push sha1 with non-existent, incomplete dest' '
Jeff Kingf8aae122008-04-23 05:16:06 -0400464
Jeff King2e433b72013-04-02 13:10:31 +0530465 mk_test testrepo &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000466 test_must_fail git push testrepo $(git rev-parse main):foo
Jeff Kingf8aae122008-04-23 05:16:06 -0400467
468'
469
Dmitry Ivankov7be8b3b2011-06-16 19:42:48 +0600470test_expect_success 'push ref expression with non-existent, incomplete dest' '
Jeff Kingf8aae122008-04-23 05:16:06 -0400471
Jeff King2e433b72013-04-02 13:10:31 +0530472 mk_test testrepo &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000473 test_must_fail git push testrepo main^:branch
Jeff Kingf8aae122008-04-23 05:16:06 -0400474
475'
476
Felipe Contreras374fbae2020-11-25 18:16:16 -0600477for head in HEAD @
478do
Steffen Prohaska47d996a2007-11-11 15:35:07 +0100479
Felipe Contreras374fbae2020-11-25 18:16:16 -0600480 test_expect_success "push with $head" '
Junio C Hamano27d7c852021-01-25 14:19:18 -0800481 mk_test testrepo heads/main &&
482 git checkout main &&
Felipe Contreras374fbae2020-11-25 18:16:16 -0600483 git push testrepo $head &&
Junio C Hamano27d7c852021-01-25 14:19:18 -0800484 check_push_result testrepo $the_commit heads/main
Felipe Contreras374fbae2020-11-25 18:16:16 -0600485 '
Steffen Prohaska47d996a2007-11-11 15:35:07 +0100486
Felipe Contreras374fbae2020-11-25 18:16:16 -0600487 test_expect_success "push with $head nonexisting at remote" '
Junio C Hamano27d7c852021-01-25 14:19:18 -0800488 mk_test testrepo heads/main &&
489 git checkout -b local main &&
490 test_when_finished "git checkout main; git branch -D local" &&
Felipe Contreras374fbae2020-11-25 18:16:16 -0600491 git push testrepo $head &&
492 check_push_result testrepo $the_commit heads/local
493 '
Daniel Barkalow9f0ea7e2008-02-20 12:54:05 -0500494
Felipe Contreras374fbae2020-11-25 18:16:16 -0600495 test_expect_success "push with +$head" '
Junio C Hamano27d7c852021-01-25 14:19:18 -0800496 mk_test testrepo heads/main &&
497 git checkout -b local main &&
498 test_when_finished "git checkout main; git branch -D local" &&
499 git push testrepo main local &&
500 check_push_result testrepo $the_commit heads/main &&
Felipe Contreras374fbae2020-11-25 18:16:16 -0600501 check_push_result testrepo $the_commit heads/local &&
Daniel Barkalow9f0ea7e2008-02-20 12:54:05 -0500502
Felipe Contreras374fbae2020-11-25 18:16:16 -0600503 # Without force rewinding should fail
504 git reset --hard $head^ &&
505 test_must_fail git push testrepo $head &&
506 check_push_result testrepo $the_commit heads/local &&
Daniel Barkalow9f0ea7e2008-02-20 12:54:05 -0500507
Felipe Contreras374fbae2020-11-25 18:16:16 -0600508 # With force rewinding should succeed
509 git push testrepo +$head &&
510 check_push_result testrepo $the_first_commit heads/local
Felipe Contreras374fbae2020-11-25 18:16:16 -0600511 '
Jeff Kingf8aae122008-04-23 05:16:06 -0400512
Felipe Contreras374fbae2020-11-25 18:16:16 -0600513 test_expect_success "push $head with non-existent, incomplete dest" '
Felipe Contreras374fbae2020-11-25 18:16:16 -0600514 mk_test testrepo &&
Junio C Hamano27d7c852021-01-25 14:19:18 -0800515 git checkout main &&
Felipe Contreras374fbae2020-11-25 18:16:16 -0600516 git push testrepo $head:branch &&
517 check_push_result testrepo $the_commit heads/branch
Jeff Kingf8aae122008-04-23 05:16:06 -0400518
Felipe Contreras374fbae2020-11-25 18:16:16 -0600519 '
Daniel Barkalow9f0ea7e2008-02-20 12:54:05 -0500520
Felipe Contreras374fbae2020-11-25 18:16:16 -0600521 test_expect_success "push with config remote.*.push = $head" '
Felipe Contreras374fbae2020-11-25 18:16:16 -0600522 mk_test testrepo heads/local &&
Junio C Hamano27d7c852021-01-25 14:19:18 -0800523 git checkout main &&
Felipe Contreras374fbae2020-11-25 18:16:16 -0600524 git branch -f local $the_commit &&
525 test_when_finished "git branch -D local" &&
526 (
527 cd testrepo &&
528 git checkout local &&
529 git reset --hard $the_first_commit
530 ) &&
531 test_config remote.there.url testrepo &&
532 test_config remote.there.push $head &&
Junio C Hamano27d7c852021-01-25 14:19:18 -0800533 test_config branch.main.remote there &&
Felipe Contreras374fbae2020-11-25 18:16:16 -0600534 git push &&
Junio C Hamano27d7c852021-01-25 14:19:18 -0800535 check_push_result testrepo $the_commit heads/main &&
Felipe Contreras374fbae2020-11-25 18:16:16 -0600536 check_push_result testrepo $the_first_commit heads/local
537 '
538
539done
Daniel Barkalow9f0ea7e2008-02-20 12:54:05 -0500540
Glen Chooe083ef52021-11-17 16:53:21 -0800541test_expect_success "push to remote with no explicit refspec and config remote.*.push = src:dest" '
542 mk_test testrepo heads/main &&
543 git checkout $the_first_commit &&
544 test_config remote.there.url testrepo &&
545 test_config remote.there.push refs/heads/main:refs/heads/main &&
546 git push there &&
547 check_push_result testrepo $the_commit heads/main
548'
549
Ramkumar Ramachandra224c2172013-04-02 13:10:33 +0530550test_expect_success 'push with remote.pushdefault' '
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000551 mk_test up_repo heads/main &&
552 mk_test down_repo heads/main &&
Ramkumar Ramachandra224c2172013-04-02 13:10:33 +0530553 test_config remote.up.url up_repo &&
554 test_config remote.down.url down_repo &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000555 test_config branch.main.remote up &&
Ramkumar Ramachandra224c2172013-04-02 13:10:33 +0530556 test_config remote.pushdefault down &&
Junio C Hamano54a3c672013-04-18 11:47:59 -0700557 test_config push.default matching &&
Ramkumar Ramachandra224c2172013-04-02 13:10:33 +0530558 git push &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000559 check_push_result up_repo $the_first_commit heads/main &&
560 check_push_result down_repo $the_commit heads/main
Daniel Barkalow9f0ea7e2008-02-20 12:54:05 -0500561'
562
Michael J Grubere1ca4242009-06-09 18:01:35 +0200563test_expect_success 'push with config remote.*.pushurl' '
564
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000565 mk_test testrepo heads/main &&
566 git checkout main &&
Jonathan Nieder3c695522013-03-18 16:12:11 -0700567 test_config remote.there.url test2repo &&
568 test_config remote.there.pushurl testrepo &&
Junio C Hamano0a42ac02013-01-04 16:08:26 -0800569 git push there : &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000570 check_push_result testrepo $the_commit heads/main
Michael J Grubere1ca4242009-06-09 18:01:35 +0200571'
572
Ramkumar Ramachandra9f765ce2013-04-02 13:10:34 +0530573test_expect_success 'push with config branch.*.pushremote' '
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000574 mk_test up_repo heads/main &&
575 mk_test side_repo heads/main &&
576 mk_test down_repo heads/main &&
Ramkumar Ramachandra9f765ce2013-04-02 13:10:34 +0530577 test_config remote.up.url up_repo &&
578 test_config remote.pushdefault side_repo &&
579 test_config remote.down.url down_repo &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000580 test_config branch.main.remote up &&
581 test_config branch.main.pushremote down &&
Junio C Hamano54a3c672013-04-18 11:47:59 -0700582 test_config push.default matching &&
Ramkumar Ramachandra9f765ce2013-04-02 13:10:34 +0530583 git push &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000584 check_push_result up_repo $the_first_commit heads/main &&
585 check_push_result side_repo $the_first_commit heads/main &&
586 check_push_result down_repo $the_commit heads/main
Michael J Grubere1ca4242009-06-09 18:01:35 +0200587'
588
Jeff King98b406f2014-02-24 03:59:03 -0500589test_expect_success 'branch.*.pushremote config order is irrelevant' '
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000590 mk_test one_repo heads/main &&
591 mk_test two_repo heads/main &&
Jeff King98b406f2014-02-24 03:59:03 -0500592 test_config remote.one.url one_repo &&
593 test_config remote.two.url two_repo &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000594 test_config branch.main.pushremote two_repo &&
Jeff King98b406f2014-02-24 03:59:03 -0500595 test_config remote.pushdefault one_repo &&
596 test_config push.default matching &&
597 git push &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000598 check_push_result one_repo $the_first_commit heads/main &&
599 check_push_result two_repo $the_commit heads/main
Jeff King98b406f2014-02-24 03:59:03 -0500600'
601
Glen Choof1dfbd92022-05-31 23:12:34 +0000602test_expect_success 'push rejects empty branch name entries' '
Glen Choo91e2e8f2022-05-31 23:12:33 +0000603 mk_test one_repo heads/main &&
604 test_config remote.one.url one_repo &&
605 test_config branch..remote one &&
606 test_config branch..merge refs/heads/ &&
607 test_config branch.main.remote one &&
608 test_config branch.main.merge refs/heads/main &&
Glen Choof1dfbd92022-05-31 23:12:34 +0000609 test_must_fail git push 2>err &&
610 grep "bad config variable .branch\.\." err
611'
612
613test_expect_success 'push ignores "branch." config without subsection' '
614 mk_test one_repo heads/main &&
615 test_config remote.one.url one_repo &&
616 test_config branch.autoSetupMerge true &&
617 test_config branch.main.remote one &&
618 test_config branch.main.merge refs/heads/main &&
Glen Choo91e2e8f2022-05-31 23:12:33 +0000619 git push
620'
621
Brian Ewins11f24412007-10-11 20:32:27 +0100622test_expect_success 'push with dry-run' '
623
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000624 mk_test testrepo heads/main &&
625 old_commit=$(git -C testrepo show-ref -s --verify refs/heads/main) &&
Junio C Hamano0a42ac02013-01-04 16:08:26 -0800626 git push --dry-run testrepo : &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000627 check_push_result testrepo $old_commit heads/main
Brian Ewins11f24412007-10-11 20:32:27 +0100628'
629
Johannes Schindelin28391a82007-11-29 01:02:53 +0000630test_expect_success 'push updates local refs' '
631
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000632 mk_test testrepo heads/main &&
Jeff King2e433b72013-04-02 13:10:31 +0530633 mk_child testrepo child &&
Jay Soffiand4785cd2010-04-19 18:08:31 -0400634 (
635 cd child &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000636 git pull .. main &&
Johannes Schindelin28391a82007-11-29 01:02:53 +0000637 git push &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000638 test $(git rev-parse main) = \
639 $(git rev-parse remotes/origin/main)
Jay Soffiand4785cd2010-04-19 18:08:31 -0400640 )
Johannes Schindelin28391a82007-11-29 01:02:53 +0000641
642'
643
Clemens Buchacher16ed2f42008-11-05 21:55:54 +0100644test_expect_success 'push updates up-to-date local refs' '
645
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000646 mk_test testrepo heads/main &&
Jeff King2e433b72013-04-02 13:10:31 +0530647 mk_child testrepo child1 &&
648 mk_child testrepo child2 &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000649 (cd child1 && git pull .. main && git push) &&
Jay Soffiand4785cd2010-04-19 18:08:31 -0400650 (
651 cd child2 &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000652 git pull ../child1 main &&
Clemens Buchacher16ed2f42008-11-05 21:55:54 +0100653 git push &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000654 test $(git rev-parse main) = \
655 $(git rev-parse remotes/origin/main)
Jay Soffiand4785cd2010-04-19 18:08:31 -0400656 )
Clemens Buchacher16ed2f42008-11-05 21:55:54 +0100657
658'
659
660test_expect_success 'push preserves up-to-date packed refs' '
661
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000662 mk_test testrepo heads/main &&
Jeff King2e433b72013-04-02 13:10:31 +0530663 mk_child testrepo child &&
Jay Soffiand4785cd2010-04-19 18:08:31 -0400664 (
665 cd child &&
Clemens Buchacher16ed2f42008-11-05 21:55:54 +0100666 git push &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000667 ! test -f .git/refs/remotes/origin/main
Jay Soffiand4785cd2010-04-19 18:08:31 -0400668 )
Clemens Buchacher16ed2f42008-11-05 21:55:54 +0100669
670'
671
Johannes Schindelin28391a82007-11-29 01:02:53 +0000672test_expect_success 'push does not update local refs on failure' '
673
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000674 mk_test testrepo heads/main &&
Jeff King2e433b72013-04-02 13:10:31 +0530675 mk_child testrepo child &&
bert Dvornikfc012c22010-05-20 20:57:52 +0200676 echo "#!/no/frobnication/today" >testrepo/.git/hooks/pre-receive &&
Jeff Kingb2dc9682008-11-07 17:20:33 -0500677 chmod +x testrepo/.git/hooks/pre-receive &&
Jay Soffiand4785cd2010-04-19 18:08:31 -0400678 (
679 cd child &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000680 git pull .. main &&
Stephan Beyerd492b312008-07-12 17:47:52 +0200681 test_must_fail git push &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000682 test $(git rev-parse main) != \
683 $(git rev-parse remotes/origin/main)
Jay Soffiand4785cd2010-04-19 18:08:31 -0400684 )
Johannes Schindelin28391a82007-11-29 01:02:53 +0000685
686'
687
688test_expect_success 'allow deleting an invalid remote ref' '
689
Jeff Kinge9de7a52021-09-24 14:33:00 -0400690 mk_test testrepo heads/branch &&
Johannes Schindelin28391a82007-11-29 01:02:53 +0000691 rm -f testrepo/.git/objects/??/* &&
Jeff Kinge9de7a52021-09-24 14:33:00 -0400692 git push testrepo :refs/heads/branch &&
693 (cd testrepo && test_must_fail git rev-parse --verify refs/heads/branch)
Johannes Schindelin28391a82007-11-29 01:02:53 +0000694
695'
696
Pang Yan Han160b81e2011-09-28 23:39:35 +0800697test_expect_success 'pushing valid refs triggers post-receive and post-update hooks' '
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000698 mk_test_with_hooks testrepo heads/main heads/next &&
699 orgmain=$(cd testrepo && git show-ref -s --verify refs/heads/main) &&
700 newmain=$(git show-ref -s --verify refs/heads/main) &&
Pang Yan Han160b81e2011-09-28 23:39:35 +0800701 orgnext=$(cd testrepo && git show-ref -s --verify refs/heads/next) &&
brian m. carlson8125a582018-05-13 02:24:13 +0000702 newnext=$ZERO_OID &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000703 git push testrepo refs/heads/main:refs/heads/main :refs/heads/next &&
Pang Yan Han160b81e2011-09-28 23:39:35 +0800704 (
705 cd testrepo/.git &&
706 cat >pre-receive.expect <<-EOF &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000707 $orgmain $newmain refs/heads/main
Pang Yan Han160b81e2011-09-28 23:39:35 +0800708 $orgnext $newnext refs/heads/next
709 EOF
710
711 cat >update.expect <<-EOF &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000712 refs/heads/main $orgmain $newmain
Pang Yan Han160b81e2011-09-28 23:39:35 +0800713 refs/heads/next $orgnext $newnext
714 EOF
715
716 cat >post-receive.expect <<-EOF &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000717 $orgmain $newmain refs/heads/main
Pang Yan Han160b81e2011-09-28 23:39:35 +0800718 $orgnext $newnext refs/heads/next
719 EOF
720
721 cat >post-update.expect <<-EOF &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000722 refs/heads/main
Pang Yan Han160b81e2011-09-28 23:39:35 +0800723 refs/heads/next
724 EOF
725
726 test_cmp pre-receive.expect pre-receive.actual &&
727 test_cmp update.expect update.actual &&
728 test_cmp post-receive.expect post-receive.actual &&
729 test_cmp post-update.expect post-update.actual
730 )
731'
732
733test_expect_success 'deleting dangling ref triggers hooks with correct args' '
Jeff Kinge9de7a52021-09-24 14:33:00 -0400734 mk_test_with_hooks testrepo heads/branch &&
Jeff King968f12f2021-09-24 14:46:13 -0400735 orig=$(git -C testrepo rev-parse refs/heads/branch) &&
Pang Yan Han160b81e2011-09-28 23:39:35 +0800736 rm -f testrepo/.git/objects/??/* &&
Jeff Kinge9de7a52021-09-24 14:33:00 -0400737 git push testrepo :refs/heads/branch &&
Pang Yan Han160b81e2011-09-28 23:39:35 +0800738 (
739 cd testrepo/.git &&
740 cat >pre-receive.expect <<-EOF &&
Jeff King968f12f2021-09-24 14:46:13 -0400741 $orig $ZERO_OID refs/heads/branch
Pang Yan Han160b81e2011-09-28 23:39:35 +0800742 EOF
743
744 cat >update.expect <<-EOF &&
Jeff King968f12f2021-09-24 14:46:13 -0400745 refs/heads/branch $orig $ZERO_OID
Pang Yan Han160b81e2011-09-28 23:39:35 +0800746 EOF
747
748 cat >post-receive.expect <<-EOF &&
Jeff King968f12f2021-09-24 14:46:13 -0400749 $orig $ZERO_OID refs/heads/branch
Pang Yan Han160b81e2011-09-28 23:39:35 +0800750 EOF
751
752 cat >post-update.expect <<-EOF &&
Jeff Kinge9de7a52021-09-24 14:33:00 -0400753 refs/heads/branch
Pang Yan Han160b81e2011-09-28 23:39:35 +0800754 EOF
755
756 test_cmp pre-receive.expect pre-receive.actual &&
757 test_cmp update.expect update.actual &&
758 test_cmp post-receive.expect post-receive.actual &&
759 test_cmp post-update.expect post-update.actual
760 )
761'
762
763test_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 +0000764 mk_test_with_hooks testrepo heads/main &&
765 orgmain=$(cd testrepo && git show-ref -s --verify refs/heads/main) &&
766 newmain=$(git show-ref -s --verify refs/heads/main) &&
767 git push testrepo main :refs/heads/nonexistent &&
Pang Yan Han160b81e2011-09-28 23:39:35 +0800768 (
769 cd testrepo/.git &&
770 cat >pre-receive.expect <<-EOF &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000771 $orgmain $newmain refs/heads/main
brian m. carlson8125a582018-05-13 02:24:13 +0000772 $ZERO_OID $ZERO_OID refs/heads/nonexistent
Pang Yan Han160b81e2011-09-28 23:39:35 +0800773 EOF
774
775 cat >update.expect <<-EOF &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000776 refs/heads/main $orgmain $newmain
brian m. carlson8125a582018-05-13 02:24:13 +0000777 refs/heads/nonexistent $ZERO_OID $ZERO_OID
Pang Yan Han160b81e2011-09-28 23:39:35 +0800778 EOF
779
780 cat >post-receive.expect <<-EOF &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000781 $orgmain $newmain refs/heads/main
Pang Yan Han160b81e2011-09-28 23:39:35 +0800782 EOF
783
784 cat >post-update.expect <<-EOF &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000785 refs/heads/main
Pang Yan Han160b81e2011-09-28 23:39:35 +0800786 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
795test_expect_success 'deletion of a non-existent ref alone does trigger post-receive and post-update hooks' '
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000796 mk_test_with_hooks testrepo heads/main &&
Pang Yan Han160b81e2011-09-28 23:39:35 +0800797 git push testrepo :refs/heads/nonexistent &&
798 (
799 cd testrepo/.git &&
800 cat >pre-receive.expect <<-EOF &&
brian m. carlson8125a582018-05-13 02:24:13 +0000801 $ZERO_OID $ZERO_OID refs/heads/nonexistent
Pang Yan Han160b81e2011-09-28 23:39:35 +0800802 EOF
803
804 cat >update.expect <<-EOF &&
brian m. carlson8125a582018-05-13 02:24:13 +0000805 refs/heads/nonexistent $ZERO_OID $ZERO_OID
Pang Yan Han160b81e2011-09-28 23:39:35 +0800806 EOF
807
808 test_cmp pre-receive.expect pre-receive.actual &&
809 test_cmp update.expect update.actual &&
810 test_path_is_missing post-receive.actual &&
811 test_path_is_missing post-update.actual
812 )
813'
814
815test_expect_success 'mixed ref updates, deletes, invalid deletes trigger hooks with correct input' '
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000816 mk_test_with_hooks testrepo heads/main heads/next heads/seen &&
817 orgmain=$(cd testrepo && git show-ref -s --verify refs/heads/main) &&
818 newmain=$(git show-ref -s --verify refs/heads/main) &&
Pang Yan Han160b81e2011-09-28 23:39:35 +0800819 orgnext=$(cd testrepo && git show-ref -s --verify refs/heads/next) &&
brian m. carlson8125a582018-05-13 02:24:13 +0000820 newnext=$ZERO_OID &&
Johannes Schindelin6dca5db2020-06-25 12:18:59 +0000821 orgseen=$(cd testrepo && git show-ref -s --verify refs/heads/seen) &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000822 newseen=$(git show-ref -s --verify refs/heads/main) &&
823 git push testrepo refs/heads/main:refs/heads/main \
824 refs/heads/main:refs/heads/seen :refs/heads/next \
Pang Yan Han160b81e2011-09-28 23:39:35 +0800825 :refs/heads/nonexistent &&
826 (
827 cd testrepo/.git &&
828 cat >pre-receive.expect <<-EOF &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000829 $orgmain $newmain refs/heads/main
Pang Yan Han160b81e2011-09-28 23:39:35 +0800830 $orgnext $newnext refs/heads/next
Johannes Schindelin6dca5db2020-06-25 12:18:59 +0000831 $orgseen $newseen refs/heads/seen
brian m. carlson8125a582018-05-13 02:24:13 +0000832 $ZERO_OID $ZERO_OID refs/heads/nonexistent
Pang Yan Han160b81e2011-09-28 23:39:35 +0800833 EOF
834
835 cat >update.expect <<-EOF &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000836 refs/heads/main $orgmain $newmain
Pang Yan Han160b81e2011-09-28 23:39:35 +0800837 refs/heads/next $orgnext $newnext
Johannes Schindelin6dca5db2020-06-25 12:18:59 +0000838 refs/heads/seen $orgseen $newseen
brian m. carlson8125a582018-05-13 02:24:13 +0000839 refs/heads/nonexistent $ZERO_OID $ZERO_OID
Pang Yan Han160b81e2011-09-28 23:39:35 +0800840 EOF
841
842 cat >post-receive.expect <<-EOF &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000843 $orgmain $newmain refs/heads/main
Pang Yan Han160b81e2011-09-28 23:39:35 +0800844 $orgnext $newnext refs/heads/next
Johannes Schindelin6dca5db2020-06-25 12:18:59 +0000845 $orgseen $newseen refs/heads/seen
Pang Yan Han160b81e2011-09-28 23:39:35 +0800846 EOF
847
848 cat >post-update.expect <<-EOF &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000849 refs/heads/main
Pang Yan Han160b81e2011-09-28 23:39:35 +0800850 refs/heads/next
Johannes Schindelin6dca5db2020-06-25 12:18:59 +0000851 refs/heads/seen
Pang Yan Han160b81e2011-09-28 23:39:35 +0800852 EOF
853
854 test_cmp pre-receive.expect pre-receive.actual &&
855 test_cmp update.expect update.actual &&
856 test_cmp post-receive.expect post-receive.actual &&
857 test_cmp post-update.expect post-update.actual
858 )
859'
860
Jan Krügerf517f1f2009-12-30 20:57:42 +0100861test_expect_success 'allow deleting a ref using --delete' '
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000862 mk_test testrepo heads/main &&
Jan Krügerf517f1f2009-12-30 20:57:42 +0100863 (cd testrepo && git config receive.denyDeleteCurrent warn) &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000864 git push testrepo --delete main &&
865 (cd testrepo && test_must_fail git rev-parse --verify refs/heads/main)
Jan Krügerf517f1f2009-12-30 20:57:42 +0100866'
867
868test_expect_success 'allow deleting a tag using --delete' '
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000869 mk_test testrepo heads/main &&
870 git tag -a -m dummy_message deltag heads/main &&
Jan Krügerf517f1f2009-12-30 20:57:42 +0100871 git push testrepo --tags &&
872 (cd testrepo && git rev-parse --verify -q refs/tags/deltag) &&
873 git push testrepo --delete tag deltag &&
874 (cd testrepo && test_must_fail git rev-parse --verify refs/tags/deltag)
875'
876
877test_expect_success 'push --delete without args aborts' '
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000878 mk_test testrepo heads/main &&
Jan Krügerf517f1f2009-12-30 20:57:42 +0100879 test_must_fail git push testrepo --delete
880'
881
882test_expect_success 'push --delete refuses src:dest refspecs' '
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000883 mk_test testrepo heads/main &&
884 test_must_fail git push testrepo --delete main:foo
Jan Krügerf517f1f2009-12-30 20:57:42 +0100885'
886
Junio C Hamano20e41642021-02-23 15:13:32 -0800887test_expect_success 'push --delete refuses empty string' '
888 mk_test testrepo heads/master &&
889 test_must_fail git push testrepo --delete ""
890'
891
Jeff King986e8232008-11-08 20:49:27 -0500892test_expect_success 'warn on push to HEAD of non-bare repository' '
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000893 mk_test testrepo heads/main &&
Jay Soffiand4785cd2010-04-19 18:08:31 -0400894 (
895 cd testrepo &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000896 git checkout main &&
Jay Soffiand4785cd2010-04-19 18:08:31 -0400897 git config receive.denyCurrentBranch warn
898 ) &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000899 git push testrepo main 2>stderr &&
Junio C Hamano3d95d922009-01-31 17:34:05 -0800900 grep "warning: updating the current branch" stderr
Jeff King986e8232008-11-08 20:49:27 -0500901'
902
903test_expect_success 'deny push to HEAD of non-bare repository' '
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000904 mk_test testrepo heads/main &&
Jay Soffiand4785cd2010-04-19 18:08:31 -0400905 (
906 cd testrepo &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000907 git checkout main &&
Jay Soffiand4785cd2010-04-19 18:08:31 -0400908 git config receive.denyCurrentBranch true
909 ) &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000910 test_must_fail git push testrepo main
Jeff King986e8232008-11-08 20:49:27 -0500911'
912
913test_expect_success 'allow push to HEAD of bare repository (bare)' '
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000914 mk_test testrepo heads/main &&
Jay Soffiand4785cd2010-04-19 18:08:31 -0400915 (
916 cd testrepo &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000917 git checkout main &&
Jeff King986e8232008-11-08 20:49:27 -0500918 git config receive.denyCurrentBranch true &&
Jay Soffiand4785cd2010-04-19 18:08:31 -0400919 git config core.bare true
920 ) &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000921 git push testrepo main 2>stderr &&
Junio C Hamano3d95d922009-01-31 17:34:05 -0800922 ! grep "warning: updating the current branch" stderr
Jeff King986e8232008-11-08 20:49:27 -0500923'
924
925test_expect_success 'allow push to HEAD of non-bare repository (config)' '
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000926 mk_test testrepo heads/main &&
Jay Soffiand4785cd2010-04-19 18:08:31 -0400927 (
928 cd testrepo &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000929 git checkout main &&
Jeff King986e8232008-11-08 20:49:27 -0500930 git config receive.denyCurrentBranch false
931 ) &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000932 git push testrepo main 2>stderr &&
Junio C Hamano3d95d922009-01-31 17:34:05 -0800933 ! grep "warning: updating the current branch" stderr
Jeff King986e8232008-11-08 20:49:27 -0500934'
935
Martin Koegler18afe102008-11-10 22:47:11 +0100936test_expect_success 'fetch with branches' '
Jeff King2e433b72013-04-02 13:10:31 +0530937 mk_empty testrepo &&
Martin Koegler18afe102008-11-10 22:47:11 +0100938 git branch second $the_first_commit &&
939 git checkout second &&
940 echo ".." > testrepo/.git/branches/branch1 &&
Jay Soffiand4785cd2010-04-19 18:08:31 -0400941 (
942 cd testrepo &&
Martin Koegler18afe102008-11-10 22:47:11 +0100943 git fetch branch1 &&
Jonathan Nieder848575d2013-03-18 16:13:41 -0700944 echo "$the_commit commit refs/heads/branch1" >expect &&
945 git for-each-ref refs/heads >actual &&
946 test_cmp expect actual
Martin Koegler18afe102008-11-10 22:47:11 +0100947 ) &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000948 git checkout main
Martin Koegler18afe102008-11-10 22:47:11 +0100949'
950
951test_expect_success 'fetch with branches containing #' '
Jeff King2e433b72013-04-02 13:10:31 +0530952 mk_empty testrepo &&
Martin Koegler18afe102008-11-10 22:47:11 +0100953 echo "..#second" > testrepo/.git/branches/branch2 &&
Jay Soffiand4785cd2010-04-19 18:08:31 -0400954 (
955 cd testrepo &&
Martin Koegler18afe102008-11-10 22:47:11 +0100956 git fetch branch2 &&
Jonathan Nieder848575d2013-03-18 16:13:41 -0700957 echo "$the_first_commit commit refs/heads/branch2" >expect &&
958 git for-each-ref refs/heads >actual &&
959 test_cmp expect actual
Martin Koegler18afe102008-11-10 22:47:11 +0100960 ) &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000961 git checkout main
Martin Koegler18afe102008-11-10 22:47:11 +0100962'
963
964test_expect_success 'push with branches' '
Jeff King2e433b72013-04-02 13:10:31 +0530965 mk_empty testrepo &&
Martin Koegler18afe102008-11-10 22:47:11 +0100966 git checkout second &&
967 echo "testrepo" > .git/branches/branch1 &&
968 git push branch1 &&
Jay Soffiand4785cd2010-04-19 18:08:31 -0400969 (
970 cd testrepo &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000971 echo "$the_first_commit commit refs/heads/main" >expect &&
Jonathan Nieder848575d2013-03-18 16:13:41 -0700972 git for-each-ref refs/heads >actual &&
973 test_cmp expect actual
Martin Koegler18afe102008-11-10 22:47:11 +0100974 )
975'
976
977test_expect_success 'push with branches containing #' '
Jeff King2e433b72013-04-02 13:10:31 +0530978 mk_empty testrepo &&
Martin Koegler18afe102008-11-10 22:47:11 +0100979 echo "testrepo#branch3" > .git/branches/branch2 &&
980 git push branch2 &&
Jay Soffiand4785cd2010-04-19 18:08:31 -0400981 (
982 cd testrepo &&
Jonathan Nieder848575d2013-03-18 16:13:41 -0700983 echo "$the_first_commit commit refs/heads/branch3" >expect &&
984 git for-each-ref refs/heads >actual &&
985 test_cmp expect actual
Martin Koegler18afe102008-11-10 22:47:11 +0100986 ) &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000987 git checkout main
Martin Koegler18afe102008-11-10 22:47:11 +0100988'
989
Jay Soffianda3efdb2010-04-19 18:19:18 -0400990test_expect_success 'push into aliased refs (consistent)' '
Johannes Schindelinbc925ce2020-11-18 23:44:32 +0000991 mk_test testrepo heads/main &&
Jeff King2e433b72013-04-02 13:10:31 +0530992 mk_child testrepo child1 &&
993 mk_child testrepo child2 &&
Jay Soffianda3efdb2010-04-19 18:19:18 -0400994 (
995 cd child1 &&
996 git branch foo &&
Eric Sunshine51b85472018-07-01 20:24:01 -0400997 git symbolic-ref refs/heads/bar refs/heads/foo &&
Jay Soffianda3efdb2010-04-19 18:19:18 -0400998 git config receive.denyCurrentBranch false
999 ) &&
1000 (
1001 cd child2 &&
1002 >path2 &&
1003 git add path2 &&
1004 test_tick &&
1005 git commit -a -m child2 &&
1006 git branch foo &&
1007 git branch bar &&
1008 git push ../child1 foo bar
1009 )
1010'
1011
1012test_expect_success 'push into aliased refs (inconsistent)' '
Johannes Schindelinbc925ce2020-11-18 23:44:32 +00001013 mk_test testrepo heads/main &&
Jeff King2e433b72013-04-02 13:10:31 +05301014 mk_child testrepo child1 &&
1015 mk_child testrepo child2 &&
Jay Soffianda3efdb2010-04-19 18:19:18 -04001016 (
1017 cd child1 &&
1018 git branch foo &&
Eric Sunshine51b85472018-07-01 20:24:01 -04001019 git symbolic-ref refs/heads/bar refs/heads/foo &&
Jay Soffianda3efdb2010-04-19 18:19:18 -04001020 git config receive.denyCurrentBranch false
1021 ) &&
1022 (
1023 cd child2 &&
1024 >path2 &&
1025 git add path2 &&
1026 test_tick &&
1027 git commit -a -m child2 &&
1028 git branch foo &&
1029 >path3 &&
1030 git add path3 &&
1031 test_tick &&
1032 git commit -a -m child2 &&
1033 git branch bar &&
1034 test_must_fail git push ../child1 foo bar 2>stderr &&
1035 grep "refusing inconsistent update" stderr
1036 )
1037'
1038
Ævar Arnfjörð Bjarmason380efb62018-07-31 13:07:13 +00001039test_force_push_tag () {
1040 tag_type_description=$1
1041 tag_args=$2
Ævar Arnfjörð Bjarmason941a7ba2018-07-31 13:07:12 +00001042
Ævar Arnfjörð Bjarmasonf08fb8d2018-08-31 20:09:57 +00001043 test_expect_success "force pushing required to update $tag_type_description" "
Johannes Schindelinbc925ce2020-11-18 23:44:32 +00001044 mk_test testrepo heads/main &&
Ævar Arnfjörð Bjarmason380efb62018-07-31 13:07:13 +00001045 mk_child testrepo child1 &&
1046 mk_child testrepo child2 &&
1047 (
1048 cd child1 &&
1049 git tag testTag &&
1050 git push ../child2 testTag &&
1051 >file1 &&
1052 git add file1 &&
1053 git commit -m 'file1' &&
1054 git tag $tag_args testTag &&
1055 test_must_fail git push ../child2 testTag &&
1056 git push --force ../child2 testTag &&
1057 git tag $tag_args testTag HEAD~ &&
1058 test_must_fail git push ../child2 testTag &&
1059 git push --force ../child2 testTag &&
Ævar Arnfjörð Bjarmason941a7ba2018-07-31 13:07:12 +00001060
Ævar Arnfjörð Bjarmason380efb62018-07-31 13:07:13 +00001061 # Clobbering without + in refspec needs --force
1062 git tag -f testTag &&
1063 test_must_fail git push ../child2 'refs/tags/*:refs/tags/*' &&
1064 git push --force ../child2 'refs/tags/*:refs/tags/*' &&
Ævar Arnfjörð Bjarmason941a7ba2018-07-31 13:07:12 +00001065
Ævar Arnfjörð Bjarmason380efb62018-07-31 13:07:13 +00001066 # Clobbering with + in refspec does not need --force
1067 git tag -f testTag HEAD~ &&
1068 git push ../child2 '+refs/tags/*:refs/tags/*' &&
Ævar Arnfjörð Bjarmason941a7ba2018-07-31 13:07:12 +00001069
Ævar Arnfjörð Bjarmason380efb62018-07-31 13:07:13 +00001070 # Clobbering with --no-force still obeys + in refspec
1071 git tag -f testTag &&
1072 git push --no-force ../child2 '+refs/tags/*:refs/tags/*' &&
1073
1074 # Clobbering with/without --force and 'tag <name>' format
1075 git tag -f testTag HEAD~ &&
1076 test_must_fail git push ../child2 tag testTag &&
1077 git push --force ../child2 tag testTag
1078 )
1079 "
1080}
1081
1082test_force_push_tag "lightweight tag" "-f"
Ævar Arnfjörð Bjarmason253b3d42018-08-31 20:09:58 +00001083test_force_push_tag "annotated tag" "-f -a -m'tag message'"
Chris Rorvickdbfeddb2012-11-29 19:41:37 -06001084
Ævar Arnfjörð Bjarmason6b0b0672018-08-31 20:09:59 +00001085test_force_fetch_tag () {
1086 tag_type_description=$1
1087 tag_args=$2
1088
Ævar Arnfjörð Bjarmason0bc8d712018-08-31 20:10:04 +00001089 test_expect_success "fetch will not clobber an existing $tag_type_description without --force" "
Johannes Schindelinbc925ce2020-11-18 23:44:32 +00001090 mk_test testrepo heads/main &&
Ævar Arnfjörð Bjarmason6b0b0672018-08-31 20:09:59 +00001091 mk_child testrepo child1 &&
1092 mk_child testrepo child2 &&
1093 (
1094 cd testrepo &&
1095 git tag testTag &&
1096 git -C ../child1 fetch origin tag testTag &&
1097 >file1 &&
1098 git add file1 &&
1099 git commit -m 'file1' &&
1100 git tag $tag_args testTag &&
Ævar Arnfjörð Bjarmason0bc8d712018-08-31 20:10:04 +00001101 test_must_fail git -C ../child1 fetch origin tag testTag &&
1102 git -C ../child1 fetch origin '+refs/tags/*:refs/tags/*'
Ævar Arnfjörð Bjarmason6b0b0672018-08-31 20:09:59 +00001103 )
1104 "
1105}
1106
1107test_force_fetch_tag "lightweight tag" "-f"
1108test_force_fetch_tag "annotated tag" "-f -a -m'tag message'"
Larry D'Annafbe4f442010-02-26 23:52:16 -05001109
Larry D'Annafbe4f442010-02-26 23:52:16 -05001110test_expect_success 'push --porcelain' '
Jeff King2e433b72013-04-02 13:10:31 +05301111 mk_empty testrepo &&
Larry D'Annafbe4f442010-02-26 23:52:16 -05001112 echo >.git/foo "To testrepo" &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +00001113 echo >>.git/foo "* refs/heads/main:refs/remotes/origin/main [new reference]" &&
Larry D'Annafbe4f442010-02-26 23:52:16 -05001114 echo >>.git/foo "Done" &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +00001115 git push >.git/bar --porcelain testrepo refs/heads/main:refs/remotes/origin/main &&
Larry D'Annafbe4f442010-02-26 23:52:16 -05001116 (
1117 cd testrepo &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +00001118 echo "$the_commit commit refs/remotes/origin/main" >expect &&
Jonathan Nieder848575d2013-03-18 16:13:41 -07001119 git for-each-ref refs/remotes/origin >actual &&
1120 test_cmp expect actual
Larry D'Annafbe4f442010-02-26 23:52:16 -05001121 ) &&
1122 test_cmp .git/foo .git/bar
1123'
1124
1125test_expect_success 'push --porcelain bad url' '
Jeff King2e433b72013-04-02 13:10:31 +05301126 mk_empty testrepo &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +00001127 test_must_fail git push >.git/bar --porcelain asdfasdfasd refs/heads/main:refs/remotes/origin/main &&
Pranit Bauvac7cf9562017-01-04 01:27:07 +05301128 ! grep -q Done .git/bar
Larry D'Annafbe4f442010-02-26 23:52:16 -05001129'
1130
1131test_expect_success 'push --porcelain rejected' '
Jeff King2e433b72013-04-02 13:10:31 +05301132 mk_empty testrepo &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +00001133 git push testrepo refs/heads/main:refs/remotes/origin/main &&
Larry D'Annafbe4f442010-02-26 23:52:16 -05001134 (cd testrepo &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +00001135 git reset --hard origin/main^ &&
Larry D'Annafbe4f442010-02-26 23:52:16 -05001136 git config receive.denyCurrentBranch true) &&
1137
1138 echo >.git/foo "To testrepo" &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +00001139 echo >>.git/foo "! refs/heads/main:refs/heads/main [remote rejected] (branch is currently checked out)" &&
Jiang Xin7dcbeaa2020-04-17 05:45:32 -04001140 echo >>.git/foo "Done" &&
Larry D'Annafbe4f442010-02-26 23:52:16 -05001141
Johannes Schindelinbc925ce2020-11-18 23:44:32 +00001142 test_must_fail git push >.git/bar --porcelain testrepo refs/heads/main:refs/heads/main &&
Junio C Hamanoc2961342010-03-11 21:51:57 -08001143 test_cmp .git/foo .git/bar
Larry D'Annafbe4f442010-02-26 23:52:16 -05001144'
1145
1146test_expect_success 'push --porcelain --dry-run rejected' '
Jeff King2e433b72013-04-02 13:10:31 +05301147 mk_empty testrepo &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +00001148 git push testrepo refs/heads/main:refs/remotes/origin/main &&
Larry D'Annafbe4f442010-02-26 23:52:16 -05001149 (cd testrepo &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +00001150 git reset --hard origin/main &&
Larry D'Annafbe4f442010-02-26 23:52:16 -05001151 git config receive.denyCurrentBranch true) &&
1152
1153 echo >.git/foo "To testrepo" &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +00001154 echo >>.git/foo "! refs/heads/main^:refs/heads/main [rejected] (non-fast-forward)" &&
Larry D'Annafbe4f442010-02-26 23:52:16 -05001155 echo >>.git/foo "Done" &&
1156
Johannes Schindelinbc925ce2020-11-18 23:44:32 +00001157 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 -08001158 test_cmp .git/foo .git/bar
Larry D'Annafbe4f442010-02-26 23:52:16 -05001159'
1160
Felipe Contreras6ddba5e2012-02-23 00:43:41 +02001161test_expect_success 'push --prune' '
Johannes Schindelinbc925ce2020-11-18 23:44:32 +00001162 mk_test testrepo heads/main heads/second heads/foo heads/bar &&
Junio C Hamano0a42ac02013-01-04 16:08:26 -08001163 git push --prune testrepo : &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +00001164 check_push_result testrepo $the_commit heads/main &&
Jeff King2e433b72013-04-02 13:10:31 +05301165 check_push_result testrepo $the_first_commit heads/second &&
1166 ! check_push_result testrepo $the_first_commit heads/foo heads/bar
Felipe Contreras6ddba5e2012-02-23 00:43:41 +02001167'
1168
1169test_expect_success 'push --prune refspec' '
Johannes Schindelinbc925ce2020-11-18 23:44:32 +00001170 mk_test testrepo tmp/main tmp/second tmp/foo tmp/bar &&
Felipe Contreras6ddba5e2012-02-23 00:43:41 +02001171 git push --prune testrepo "refs/heads/*:refs/tmp/*" &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +00001172 check_push_result testrepo $the_commit tmp/main &&
Jeff King2e433b72013-04-02 13:10:31 +05301173 check_push_result testrepo $the_first_commit tmp/second &&
1174 ! check_push_result testrepo $the_first_commit tmp/foo tmp/bar
Felipe Contreras6ddba5e2012-02-23 00:43:41 +02001175'
1176
Junio C Hamanodaebaa72013-01-18 16:08:30 -08001177for configsection in transfer receive
1178do
1179 test_expect_success "push to update a ref hidden by $configsection.hiderefs" '
Johannes Schindelinbc925ce2020-11-18 23:44:32 +00001180 mk_test testrepo heads/main hidden/one hidden/two hidden/three &&
Junio C Hamanodaebaa72013-01-18 16:08:30 -08001181 (
1182 cd testrepo &&
1183 git config $configsection.hiderefs refs/hidden
1184 ) &&
1185
1186 # push to unhidden ref succeeds normally
Johannes Schindelinbc925ce2020-11-18 23:44:32 +00001187 git push testrepo main:refs/heads/main &&
1188 check_push_result testrepo $the_commit heads/main &&
Junio C Hamanodaebaa72013-01-18 16:08:30 -08001189
1190 # push to update a hidden ref should fail
Johannes Schindelinbc925ce2020-11-18 23:44:32 +00001191 test_must_fail git push testrepo main:refs/hidden/one &&
Jeff King2e433b72013-04-02 13:10:31 +05301192 check_push_result testrepo $the_first_commit hidden/one &&
Junio C Hamanodaebaa72013-01-18 16:08:30 -08001193
1194 # push to delete a hidden ref should fail
1195 test_must_fail git push testrepo :refs/hidden/two &&
Jeff King2e433b72013-04-02 13:10:31 +05301196 check_push_result testrepo $the_first_commit hidden/two &&
Junio C Hamanodaebaa72013-01-18 16:08:30 -08001197
1198 # idempotent push to update a hidden ref should fail
1199 test_must_fail git push testrepo $the_first_commit:refs/hidden/three &&
Jeff King2e433b72013-04-02 13:10:31 +05301200 check_push_result testrepo $the_first_commit hidden/three
Junio C Hamanodaebaa72013-01-18 16:08:30 -08001201 '
1202done
1203
Junio C Hamano6e7b66e2013-01-29 14:02:15 -08001204test_expect_success 'fetch exact SHA1' '
Johannes Schindelinbc925ce2020-11-18 23:44:32 +00001205 mk_test testrepo heads/main hidden/one &&
1206 git push testrepo main:refs/hidden/one &&
Junio C Hamano6e7b66e2013-01-29 14:02:15 -08001207 (
1208 cd testrepo &&
1209 git config transfer.hiderefs refs/hidden
1210 ) &&
Jeff King2e433b72013-04-02 13:10:31 +05301211 check_push_result testrepo $the_commit hidden/one &&
Junio C Hamano6e7b66e2013-01-29 14:02:15 -08001212
Jeff King2e433b72013-04-02 13:10:31 +05301213 mk_child testrepo child &&
Junio C Hamano6e7b66e2013-01-29 14:02:15 -08001214 (
1215 cd child &&
1216
1217 # make sure $the_commit does not exist here
1218 git repack -a -d &&
1219 git prune &&
1220 test_must_fail git cat-file -t $the_commit &&
1221
Jonathan Tanab0c5f52019-02-25 13:54:08 -08001222 # Some protocol versions (e.g. 2) support fetching
1223 # unadvertised objects, so restrict this test to v0.
1224
Junio C Hamano6e7b66e2013-01-29 14:02:15 -08001225 # fetching the hidden object should fail by default
Jonathan Nieder8a1b0972019-12-23 17:01:10 -08001226 test_must_fail env GIT_TEST_PROTOCOL_VERSION=0 \
Jonathan Tanab0c5f52019-02-25 13:54:08 -08001227 git fetch -v ../testrepo $the_commit:refs/heads/copy 2>err &&
Matt McCutchend56583d2017-02-22 11:05:57 -05001228 test_i18ngrep "Server does not allow request for unadvertised object" err &&
Junio C Hamano6e7b66e2013-01-29 14:02:15 -08001229 test_must_fail git rev-parse --verify refs/heads/copy &&
1230
1231 # the server side can allow it to succeed
1232 (
1233 cd ../testrepo &&
1234 git config uploadpack.allowtipsha1inwant true
1235 ) &&
1236
Johannes Schindelinbc925ce2020-11-18 23:44:32 +00001237 git fetch -v ../testrepo $the_commit:refs/heads/copy main:refs/heads/extra &&
Jeff Kingc3c17bf2015-03-19 16:37:09 -04001238 cat >expect <<-EOF &&
1239 $the_commit
1240 $the_first_commit
1241 EOF
1242 {
1243 git rev-parse --verify refs/heads/copy &&
1244 git rev-parse --verify refs/heads/extra
1245 } >actual &&
1246 test_cmp expect actual
Junio C Hamano6e7b66e2013-01-29 14:02:15 -08001247 )
1248'
1249
Jonathan Nieder6c301ad2018-05-31 00:23:39 -07001250test_expect_success 'fetch exact SHA1 in protocol v2' '
Johannes Schindelinbc925ce2020-11-18 23:44:32 +00001251 mk_test testrepo heads/main hidden/one &&
1252 git push testrepo main:refs/hidden/one &&
Jonathan Nieder6c301ad2018-05-31 00:23:39 -07001253 git -C testrepo config transfer.hiderefs refs/hidden &&
1254 check_push_result testrepo $the_commit hidden/one &&
1255
1256 mk_child testrepo child &&
1257 git -C child config protocol.version 2 &&
1258
1259 # make sure $the_commit does not exist here
1260 git -C child repack -a -d &&
1261 git -C child prune &&
1262 test_must_fail git -C child cat-file -t $the_commit &&
1263
1264 # fetching the hidden object succeeds by default
1265 # NEEDSWORK: should this match the v0 behavior instead?
1266 git -C child fetch -v ../testrepo $the_commit:refs/heads/copy
1267'
1268
Fredrik Medley68ee6282015-05-21 22:23:39 +02001269for configallowtipsha1inwant in true false
1270do
1271 test_expect_success "shallow fetch reachable SHA1 (but not a ref), allowtipsha1inwant=$configallowtipsha1inwant" '
1272 mk_empty testrepo &&
1273 (
1274 cd testrepo &&
1275 git config uploadpack.allowtipsha1inwant $configallowtipsha1inwant &&
1276 git commit --allow-empty -m foo &&
1277 git commit --allow-empty -m bar
1278 ) &&
1279 SHA1=$(git --git-dir=testrepo/.git rev-parse HEAD^) &&
1280 mk_empty shallow &&
1281 (
1282 cd shallow &&
Jonathan Tanab0c5f52019-02-25 13:54:08 -08001283 # Some protocol versions (e.g. 2) support fetching
1284 # unadvertised objects, so restrict this test to v0.
Jonathan Nieder8a1b0972019-12-23 17:01:10 -08001285 test_must_fail env GIT_TEST_PROTOCOL_VERSION=0 \
Jonathan Tanab0c5f52019-02-25 13:54:08 -08001286 git fetch --depth=1 ../testrepo/.git $SHA1 &&
Fredrik Medley68ee6282015-05-21 22:23:39 +02001287 git --git-dir=../testrepo/.git config uploadpack.allowreachablesha1inwant true &&
1288 git fetch --depth=1 ../testrepo/.git $SHA1 &&
1289 git cat-file commit $SHA1
1290 )
1291 '
1292
1293 test_expect_success "deny fetch unreachable SHA1, allowtipsha1inwant=$configallowtipsha1inwant" '
1294 mk_empty testrepo &&
1295 (
1296 cd testrepo &&
1297 git config uploadpack.allowtipsha1inwant $configallowtipsha1inwant &&
1298 git commit --allow-empty -m foo &&
1299 git commit --allow-empty -m bar &&
1300 git commit --allow-empty -m xyz
1301 ) &&
1302 SHA1_1=$(git --git-dir=testrepo/.git rev-parse HEAD^^) &&
1303 SHA1_2=$(git --git-dir=testrepo/.git rev-parse HEAD^) &&
1304 SHA1_3=$(git --git-dir=testrepo/.git rev-parse HEAD) &&
1305 (
1306 cd testrepo &&
1307 git reset --hard $SHA1_2 &&
1308 git cat-file commit $SHA1_1 &&
1309 git cat-file commit $SHA1_3
1310 ) &&
1311 mk_empty shallow &&
1312 (
1313 cd shallow &&
Jonathan Tanab0c5f52019-02-25 13:54:08 -08001314 # Some protocol versions (e.g. 2) support fetching
1315 # unadvertised objects, so restrict this test to v0.
Jonathan Nieder8a1b0972019-12-23 17:01:10 -08001316 test_must_fail env GIT_TEST_PROTOCOL_VERSION=0 \
Jonathan Tanab0c5f52019-02-25 13:54:08 -08001317 git fetch ../testrepo/.git $SHA1_3 &&
Jonathan Nieder8a1b0972019-12-23 17:01:10 -08001318 test_must_fail env GIT_TEST_PROTOCOL_VERSION=0 \
Jonathan Tanab0c5f52019-02-25 13:54:08 -08001319 git fetch ../testrepo/.git $SHA1_1 &&
Fredrik Medley68ee6282015-05-21 22:23:39 +02001320 git --git-dir=../testrepo/.git config uploadpack.allowreachablesha1inwant true &&
1321 git fetch ../testrepo/.git $SHA1_1 &&
1322 git cat-file commit $SHA1_1 &&
1323 test_must_fail git cat-file commit $SHA1_2 &&
1324 git fetch ../testrepo/.git $SHA1_2 &&
1325 git cat-file commit $SHA1_2 &&
Jonathan Nieder8a1b0972019-12-23 17:01:10 -08001326 test_must_fail env GIT_TEST_PROTOCOL_VERSION=0 \
Junio C Hamano57a6b932019-04-25 16:41:23 +09001327 git fetch ../testrepo/.git $SHA1_3 2>err &&
Jeff Kingacaabcf2021-01-09 22:23:09 -05001328 # ideally we would insist this be on a "remote error:"
1329 # line, but it is racy; see the commit message
1330 test_i18ngrep "not our ref.*$SHA1_3\$" err
Fredrik Medley68ee6282015-05-21 22:23:39 +02001331 )
1332 '
1333done
1334
Junio C Hamanoc2aba152013-03-04 12:09:50 -08001335test_expect_success 'fetch follows tags by default' '
Johannes Schindelinbc925ce2020-11-18 23:44:32 +00001336 mk_test testrepo heads/main &&
Ævar Arnfjörð Bjarmasonf818f7f2022-03-17 11:13:11 +01001337 test_when_finished "rm -rf src" &&
Junio C Hamanoc2aba152013-03-04 12:09:50 -08001338 git init src &&
1339 (
1340 cd src &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +00001341 git pull ../testrepo main &&
Junio C Hamanoc2aba152013-03-04 12:09:50 -08001342 git tag -m "annotated" tag &&
1343 git for-each-ref >tmp1 &&
Eric Sunshinee57ea502021-12-09 00:11:01 -05001344 sed -n "p; s|refs/heads/main$|refs/remotes/origin/main|p" tmp1 |
Junio C Hamanoc2aba152013-03-04 12:09:50 -08001345 sort -k 3 >../expect
1346 ) &&
Ævar Arnfjörð Bjarmasonf818f7f2022-03-17 11:13:11 +01001347 test_when_finished "rm -rf dst" &&
Junio C Hamanoc2aba152013-03-04 12:09:50 -08001348 git init dst &&
1349 (
1350 cd dst &&
1351 git remote add origin ../src &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +00001352 git config branch.main.remote origin &&
1353 git config branch.main.merge refs/heads/main &&
Junio C Hamanoc2aba152013-03-04 12:09:50 -08001354 git pull &&
1355 git for-each-ref >../actual
1356 ) &&
1357 test_cmp expect actual
1358'
1359
Jeff King34066f02019-04-13 01:57:37 -04001360test_expect_success 'peeled advertisements are not considered ref tips' '
1361 mk_empty testrepo &&
1362 git -C testrepo commit --allow-empty -m one &&
1363 git -C testrepo commit --allow-empty -m two &&
1364 git -C testrepo tag -m foo mytag HEAD^ &&
1365 oid=$(git -C testrepo rev-parse mytag^{commit}) &&
Jonathan Nieder8a1b0972019-12-23 17:01:10 -08001366 test_must_fail env GIT_TEST_PROTOCOL_VERSION=0 \
Jeff King34066f02019-04-13 01:57:37 -04001367 git fetch testrepo $oid 2>err &&
1368 test_i18ngrep "Server does not allow request for unadvertised object" err
1369'
1370
Junio C Hamanoca024652013-12-03 15:41:15 -08001371test_expect_success 'pushing a specific ref applies remote.$name.push as refmap' '
Johannes Schindelinbc925ce2020-11-18 23:44:32 +00001372 mk_test testrepo heads/main &&
Ævar Arnfjörð Bjarmasonf818f7f2022-03-17 11:13:11 +01001373 test_when_finished "rm -rf src" &&
Junio C Hamanoca024652013-12-03 15:41:15 -08001374 git init src &&
Ævar Arnfjörð Bjarmasonf818f7f2022-03-17 11:13:11 +01001375 test_when_finished "rm -rf dst" &&
Junio C Hamanoca024652013-12-03 15:41:15 -08001376 git init --bare dst &&
1377 (
1378 cd src &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +00001379 git pull ../testrepo main &&
Junio C Hamanoca024652013-12-03 15:41:15 -08001380 git branch next &&
1381 git config remote.dst.url ../dst &&
1382 git config remote.dst.push "+refs/heads/*:refs/remotes/src/*" &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +00001383 git push dst main &&
1384 git show-ref refs/heads/main |
Junio C Hamanoca024652013-12-03 15:41:15 -08001385 sed -e "s|refs/heads/|refs/remotes/src/|" >../dst/expect
1386 ) &&
1387 (
1388 cd dst &&
1389 test_must_fail git show-ref refs/heads/next &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +00001390 test_must_fail git show-ref refs/heads/main &&
1391 git show-ref refs/remotes/src/main >actual
Junio C Hamanoca024652013-12-03 15:41:15 -08001392 ) &&
1393 test_cmp dst/expect dst/actual
1394'
1395
1396test_expect_success 'with no remote.$name.push, it is not used as refmap' '
Johannes Schindelinbc925ce2020-11-18 23:44:32 +00001397 mk_test testrepo heads/main &&
Ævar Arnfjörð Bjarmasonf818f7f2022-03-17 11:13:11 +01001398 test_when_finished "rm -rf src" &&
Junio C Hamanoca024652013-12-03 15:41:15 -08001399 git init src &&
Ævar Arnfjörð Bjarmasonf818f7f2022-03-17 11:13:11 +01001400 test_when_finished "rm -rf dst" &&
Junio C Hamanoca024652013-12-03 15:41:15 -08001401 git init --bare dst &&
1402 (
1403 cd src &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +00001404 git pull ../testrepo main &&
Junio C Hamanoca024652013-12-03 15:41:15 -08001405 git branch next &&
1406 git config remote.dst.url ../dst &&
Junio C Hamanofc9261c2013-12-03 16:23:35 -08001407 git config push.default matching &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +00001408 git push dst main &&
1409 git show-ref refs/heads/main >../dst/expect
Junio C Hamanoca024652013-12-03 15:41:15 -08001410 ) &&
1411 (
1412 cd dst &&
1413 test_must_fail git show-ref refs/heads/next &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +00001414 git show-ref refs/heads/main >actual
Junio C Hamanoca024652013-12-03 15:41:15 -08001415 ) &&
1416 test_cmp dst/expect dst/actual
1417'
1418
Junio C Hamanofc9261c2013-12-03 16:23:35 -08001419test_expect_success 'with no remote.$name.push, upstream mapping is used' '
Johannes Schindelinbc925ce2020-11-18 23:44:32 +00001420 mk_test testrepo heads/main &&
Ævar Arnfjörð Bjarmasonf818f7f2022-03-17 11:13:11 +01001421 test_when_finished "rm -rf src" &&
Junio C Hamanofc9261c2013-12-03 16:23:35 -08001422 git init src &&
Ævar Arnfjörð Bjarmasonf818f7f2022-03-17 11:13:11 +01001423 test_when_finished "rm -rf dst" &&
Junio C Hamanofc9261c2013-12-03 16:23:35 -08001424 git init --bare dst &&
1425 (
1426 cd src &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +00001427 git pull ../testrepo main &&
Junio C Hamanofc9261c2013-12-03 16:23:35 -08001428 git branch next &&
1429 git config remote.dst.url ../dst &&
1430 git config remote.dst.fetch "+refs/heads/*:refs/remotes/dst/*" &&
1431 git config push.default upstream &&
1432
Johannes Schindelinbc925ce2020-11-18 23:44:32 +00001433 git config branch.main.merge refs/heads/trunk &&
1434 git config branch.main.remote dst &&
Junio C Hamanofc9261c2013-12-03 16:23:35 -08001435
Johannes Schindelinbc925ce2020-11-18 23:44:32 +00001436 git push dst main &&
1437 git show-ref refs/heads/main |
1438 sed -e "s|refs/heads/main|refs/heads/trunk|" >../dst/expect
Junio C Hamanofc9261c2013-12-03 16:23:35 -08001439 ) &&
1440 (
1441 cd dst &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +00001442 test_must_fail git show-ref refs/heads/main &&
Junio C Hamanofc9261c2013-12-03 16:23:35 -08001443 test_must_fail git show-ref refs/heads/next &&
1444 git show-ref refs/heads/trunk >actual
1445 ) &&
1446 test_cmp dst/expect dst/actual
1447'
1448
Junio C Hamanoc2aba152013-03-04 12:09:50 -08001449test_expect_success 'push does not follow tags by default' '
Johannes Schindelinbc925ce2020-11-18 23:44:32 +00001450 mk_test testrepo heads/main &&
Ævar Arnfjörð Bjarmasonf818f7f2022-03-17 11:13:11 +01001451 test_when_finished "rm -rf src" &&
Junio C Hamanoc2aba152013-03-04 12:09:50 -08001452 git init src &&
Ævar Arnfjörð Bjarmasonf818f7f2022-03-17 11:13:11 +01001453 test_when_finished "rm -rf dst" &&
Junio C Hamanoc2aba152013-03-04 12:09:50 -08001454 git init --bare dst &&
1455 (
1456 cd src &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +00001457 git pull ../testrepo main &&
Junio C Hamanoc2aba152013-03-04 12:09:50 -08001458 git tag -m "annotated" tag &&
1459 git checkout -b another &&
1460 git commit --allow-empty -m "future commit" &&
1461 git tag -m "future" future &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +00001462 git checkout main &&
1463 git for-each-ref refs/heads/main >../expect &&
1464 git push ../dst main
Junio C Hamanoc2aba152013-03-04 12:09:50 -08001465 ) &&
1466 (
1467 cd dst &&
1468 git for-each-ref >../actual
1469 ) &&
1470 test_cmp expect actual
1471'
1472
Johannes Schindelinf6188dc2019-03-25 11:14:21 -07001473test_expect_success 'push --follow-tags only pushes relevant tags' '
Johannes Schindelinbc925ce2020-11-18 23:44:32 +00001474 mk_test testrepo heads/main &&
Ævar Arnfjörð Bjarmasonf818f7f2022-03-17 11:13:11 +01001475 test_when_finished "rm -rf src" &&
Junio C Hamanoc2aba152013-03-04 12:09:50 -08001476 git init src &&
Ævar Arnfjörð Bjarmasonf818f7f2022-03-17 11:13:11 +01001477 test_when_finished "rm -rf dst" &&
Junio C Hamanoc2aba152013-03-04 12:09:50 -08001478 git init --bare dst &&
1479 (
1480 cd src &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +00001481 git pull ../testrepo main &&
Junio C Hamanoc2aba152013-03-04 12:09:50 -08001482 git tag -m "annotated" tag &&
1483 git checkout -b another &&
1484 git commit --allow-empty -m "future commit" &&
1485 git tag -m "future" future &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +00001486 git checkout main &&
1487 git for-each-ref refs/heads/main refs/tags/tag >../expect &&
1488 git push --follow-tags ../dst main
Junio C Hamanoc2aba152013-03-04 12:09:50 -08001489 ) &&
1490 (
1491 cd dst &&
1492 git for-each-ref >../actual
1493 ) &&
1494 test_cmp expect actual
1495'
1496
Nguyễn Thái Ngọc Duyf7c815c2013-08-12 20:55:55 +07001497test_expect_success 'push --no-thin must produce non-thin pack' '
1498 cat >>path1 <<\EOF &&
1499keep base version of path1 big enough, compared to the new changes
1500later, in order to pass size heuristics in
1501builtin/pack-objects.c:try_delta()
1502EOF
1503 git commit -am initial &&
1504 git init no-thin &&
1505 git --git-dir=no-thin/.git config receive.unpacklimit 0 &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +00001506 git push no-thin/.git refs/heads/main:refs/heads/foo &&
Nguyễn Thái Ngọc Duyf7c815c2013-08-12 20:55:55 +07001507 echo modified >> path1 &&
1508 git commit -am modified &&
1509 git repack -adf &&
1510 rcvpck="git receive-pack --reject-thin-pack-for-testing" &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +00001511 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 +07001512'
1513
Jeff King458a7e52014-10-16 20:03:41 -04001514test_expect_success 'pushing a tag pushes the tagged object' '
Jeff King458a7e52014-10-16 20:03:41 -04001515 blob=$(echo unreferenced | git hash-object -w --stdin) &&
1516 git tag -m foo tag-of-blob $blob &&
Ævar Arnfjörð Bjarmasonf818f7f2022-03-17 11:13:11 +01001517 test_when_finished "rm -rf dst.git" &&
Jeff King458a7e52014-10-16 20:03:41 -04001518 git init --bare dst.git &&
1519 git push dst.git tag-of-blob &&
1520 # the receiving index-pack should have noticed
1521 # any problems, but we double check
1522 echo unreferenced >expect &&
1523 git --git-dir=dst.git cat-file blob tag-of-blob >actual &&
1524 test_cmp expect actual
1525'
1526
Jeff King72549df2014-11-04 08:11:19 -05001527test_expect_success 'push into bare respects core.logallrefupdates' '
Ævar Arnfjörð Bjarmasonf818f7f2022-03-17 11:13:11 +01001528 test_when_finished "rm -rf dst.git" &&
Jeff King72549df2014-11-04 08:11:19 -05001529 git init --bare dst.git &&
1530 git -C dst.git config core.logallrefupdates true &&
1531
1532 # double push to test both with and without
1533 # the actual pack transfer
Johannes Schindelinbc925ce2020-11-18 23:44:32 +00001534 git push dst.git main:one &&
Jeff King72549df2014-11-04 08:11:19 -05001535 echo "one@{0} push" >expect &&
1536 git -C dst.git log -g --format="%gd %gs" one >actual &&
1537 test_cmp expect actual &&
1538
Johannes Schindelinbc925ce2020-11-18 23:44:32 +00001539 git push dst.git main:two &&
Jeff King72549df2014-11-04 08:11:19 -05001540 echo "two@{0} push" >expect &&
1541 git -C dst.git log -g --format="%gd %gs" two >actual &&
1542 test_cmp expect actual
1543'
1544
1545test_expect_success 'fetch into bare respects core.logallrefupdates' '
Ævar Arnfjörð Bjarmasonf818f7f2022-03-17 11:13:11 +01001546 test_when_finished "rm -rf dst.git" &&
Jeff King72549df2014-11-04 08:11:19 -05001547 git init --bare dst.git &&
1548 (
1549 cd dst.git &&
1550 git config core.logallrefupdates true &&
1551
1552 # as above, we double-fetch to test both
1553 # with and without pack transfer
Johannes Schindelinbc925ce2020-11-18 23:44:32 +00001554 git fetch .. main:one &&
1555 echo "one@{0} fetch .. main:one: storing head" >expect &&
Jeff King72549df2014-11-04 08:11:19 -05001556 git log -g --format="%gd %gs" one >actual &&
1557 test_cmp expect actual &&
1558
Johannes Schindelinbc925ce2020-11-18 23:44:32 +00001559 git fetch .. main:two &&
1560 echo "two@{0} fetch .. main:two: storing head" >expect &&
Jeff King72549df2014-11-04 08:11:19 -05001561 git log -g --format="%gd %gs" two >actual &&
1562 test_cmp expect actual
1563 )
1564'
1565
Johannes Schindelin1404bcb2014-11-26 23:44:16 +01001566test_expect_success 'receive.denyCurrentBranch = updateInstead' '
Ævar Arnfjörð Bjarmasonf818f7f2022-03-17 11:13:11 +01001567 mk_empty testrepo &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +00001568 git push testrepo main &&
Junio C Hamano4d7a5ce2014-11-30 17:54:30 -08001569 (
1570 cd testrepo &&
Johannes Schindelin1404bcb2014-11-26 23:44:16 +01001571 git reset --hard &&
1572 git config receive.denyCurrentBranch updateInstead
1573 ) &&
1574 test_commit third path2 &&
Junio C Hamano4d7a5ce2014-11-30 17:54:30 -08001575
1576 # Try pushing into a repository with pristine working tree
Johannes Schindelinbc925ce2020-11-18 23:44:32 +00001577 git push testrepo main &&
Junio C Hamano4d7a5ce2014-11-30 17:54:30 -08001578 (
1579 cd testrepo &&
Johannes Schindelin1404bcb2014-11-26 23:44:16 +01001580 git update-index -q --refresh &&
1581 git diff-files --quiet -- &&
1582 git diff-index --quiet --cached HEAD -- &&
Junio C Hamano4d7a5ce2014-11-30 17:54:30 -08001583 test third = "$(cat path2)" &&
1584 test $(git -C .. rev-parse HEAD) = $(git rev-parse HEAD)
Johannes Schindelin1404bcb2014-11-26 23:44:16 +01001585 ) &&
Junio C Hamano4d7a5ce2014-11-30 17:54:30 -08001586
1587 # Try pushing into a repository with working tree needing a refresh
1588 (
1589 cd testrepo &&
1590 git reset --hard HEAD^ &&
1591 test $(git -C .. rev-parse HEAD^) = $(git rev-parse HEAD) &&
Nguyễn Thái Ngọc Duy0e496492018-03-24 08:44:31 +01001592 test-tool chmtime +100 path1
Junio C Hamano4d7a5ce2014-11-30 17:54:30 -08001593 ) &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +00001594 git push testrepo main &&
Junio C Hamano4d7a5ce2014-11-30 17:54:30 -08001595 (
1596 cd testrepo &&
1597 git update-index -q --refresh &&
1598 git diff-files --quiet -- &&
1599 git diff-index --quiet --cached HEAD -- &&
1600 test_cmp ../path1 path1 &&
1601 test third = "$(cat path2)" &&
1602 test $(git -C .. rev-parse HEAD) = $(git rev-parse HEAD)
1603 ) &&
1604
1605 # Update what is to be pushed
Johannes Schindelin1404bcb2014-11-26 23:44:16 +01001606 test_commit fourth path2 &&
Junio C Hamano4d7a5ce2014-11-30 17:54:30 -08001607
1608 # Try pushing into a repository with a dirty working tree
1609 # (1) the working tree updated
1610 (
1611 cd testrepo &&
1612 echo changed >path1
1613 ) &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +00001614 test_must_fail git push testrepo main &&
Junio C Hamano4d7a5ce2014-11-30 17:54:30 -08001615 (
1616 cd testrepo &&
1617 test $(git -C .. rev-parse HEAD^) = $(git rev-parse HEAD) &&
1618 git diff --quiet --cached &&
1619 test changed = "$(cat path1)"
1620 ) &&
1621
1622 # (2) the index updated
1623 (
1624 cd testrepo &&
1625 echo changed >path1 &&
1626 git add path1
1627 ) &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +00001628 test_must_fail git push testrepo main &&
Junio C Hamano4d7a5ce2014-11-30 17:54:30 -08001629 (
1630 cd testrepo &&
1631 test $(git -C .. rev-parse HEAD^) = $(git rev-parse HEAD) &&
Johannes Schindelin1404bcb2014-11-26 23:44:16 +01001632 git diff --quiet &&
Junio C Hamano4d7a5ce2014-11-30 17:54:30 -08001633 test changed = "$(cat path1)"
1634 ) &&
1635
1636 # Introduce a new file in the update
1637 test_commit fifth path3 &&
1638
1639 # (3) the working tree has an untracked file that would interfere
1640 (
1641 cd testrepo &&
1642 git reset --hard &&
1643 echo changed >path3
1644 ) &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +00001645 test_must_fail git push testrepo main &&
Junio C Hamano4d7a5ce2014-11-30 17:54:30 -08001646 (
1647 cd testrepo &&
1648 test $(git -C .. rev-parse HEAD^^) = $(git rev-parse HEAD) &&
1649 git diff --quiet &&
1650 git diff --quiet --cached &&
1651 test changed = "$(cat path3)"
1652 ) &&
1653
1654 # (4) the target changes to what gets pushed but it still is a change
1655 (
1656 cd testrepo &&
1657 git reset --hard &&
1658 echo fifth >path3 &&
1659 git add path3
1660 ) &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +00001661 test_must_fail git push testrepo main &&
Junio C Hamano4d7a5ce2014-11-30 17:54:30 -08001662 (
1663 cd testrepo &&
1664 test $(git -C .. rev-parse HEAD^^) = $(git rev-parse HEAD) &&
1665 git diff --quiet &&
1666 test fifth = "$(cat path3)"
Junio C Hamano1a51b522015-03-31 23:15:45 -07001667 ) &&
Junio C Hamano4d7a5ce2014-11-30 17:54:30 -08001668
Junio C Hamano1a51b522015-03-31 23:15:45 -07001669 # (5) push into void
Ævar Arnfjörð Bjarmasonf818f7f2022-03-17 11:13:11 +01001670 test_when_finished "rm -rf void" &&
Junio C Hamano1a51b522015-03-31 23:15:45 -07001671 git init void &&
1672 (
1673 cd void &&
1674 git config receive.denyCurrentBranch updateInstead
1675 ) &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +00001676 git push void main &&
Junio C Hamano1a51b522015-03-31 23:15:45 -07001677 (
1678 cd void &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +00001679 test $(git -C .. rev-parse main) = $(git rev-parse HEAD) &&
Junio C Hamano1a51b522015-03-31 23:15:45 -07001680 git diff --quiet &&
1681 git diff --cached --quiet
Junio C Hamanob072a252018-10-19 13:57:35 +09001682 ) &&
1683
1684 # (6) updateInstead intervened by fast-forward check
Johannes Schindelinbc925ce2020-11-18 23:44:32 +00001685 test_must_fail git push void main^:main &&
1686 test $(git -C void rev-parse HEAD) = $(git rev-parse main) &&
Junio C Hamanob072a252018-10-19 13:57:35 +09001687 git -C void diff --quiet &&
1688 git -C void diff --cached --quiet
Johannes Schindelin1404bcb2014-11-26 23:44:16 +01001689'
1690
Junio C Hamano08553312014-12-01 15:29:54 -08001691test_expect_success 'updateInstead with push-to-checkout hook' '
Ævar Arnfjörð Bjarmasonf818f7f2022-03-17 11:13:11 +01001692 test_when_finished "rm -rf testrepo" &&
Junio C Hamano08553312014-12-01 15:29:54 -08001693 git init testrepo &&
Ævar Arnfjörð Bjarmasonf818f7f2022-03-17 11:13:11 +01001694 git -C testrepo pull .. main &&
1695 git -C testrepo reset --hard HEAD^^ &&
1696 git -C testrepo tag initial &&
1697 git -C testrepo config receive.denyCurrentBranch updateInstead &&
1698 test_hook -C testrepo push-to-checkout <<-\EOF &&
1699 echo >&2 updating from $(git rev-parse HEAD)
1700 echo >&2 updating to "$1"
Junio C Hamano08553312014-12-01 15:29:54 -08001701
Ævar Arnfjörð Bjarmasonf818f7f2022-03-17 11:13:11 +01001702 git update-index -q --refresh &&
1703 git read-tree -u -m HEAD "$1" || {
1704 status=$?
1705 echo >&2 read-tree failed
1706 exit $status
1707 }
1708 EOF
Junio C Hamano08553312014-12-01 15:29:54 -08001709
1710 # Try pushing into a pristine
Johannes Schindelinbc925ce2020-11-18 23:44:32 +00001711 git push testrepo main &&
Junio C Hamano08553312014-12-01 15:29:54 -08001712 (
1713 cd testrepo &&
1714 git diff --quiet &&
1715 git diff HEAD --quiet &&
1716 test $(git -C .. rev-parse HEAD) = $(git rev-parse HEAD)
1717 ) &&
1718
1719 # Try pushing into a repository with conflicting change
1720 (
1721 cd testrepo &&
1722 git reset --hard initial &&
1723 echo conflicting >path2
1724 ) &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +00001725 test_must_fail git push testrepo main &&
Junio C Hamano08553312014-12-01 15:29:54 -08001726 (
1727 cd testrepo &&
1728 test $(git rev-parse initial) = $(git rev-parse HEAD) &&
1729 test conflicting = "$(cat path2)" &&
1730 git diff-index --quiet --cached HEAD
1731 ) &&
1732
1733 # Try pushing into a repository with unrelated change
1734 (
1735 cd testrepo &&
1736 git reset --hard initial &&
1737 echo unrelated >path1 &&
1738 echo irrelevant >path5 &&
1739 git add path5
1740 ) &&
Johannes Schindelinbc925ce2020-11-18 23:44:32 +00001741 git push testrepo main &&
Junio C Hamano08553312014-12-01 15:29:54 -08001742 (
1743 cd testrepo &&
1744 test "$(cat path1)" = unrelated &&
1745 test "$(cat path5)" = irrelevant &&
1746 test "$(git diff --name-only --cached HEAD)" = path5 &&
1747 test $(git -C .. rev-parse HEAD) = $(git rev-parse HEAD)
Junio C Hamano1a51b522015-03-31 23:15:45 -07001748 ) &&
1749
1750 # push into void
Ævar Arnfjörð Bjarmasonf818f7f2022-03-17 11:13:11 +01001751 test_when_finished "rm -rf void" &&
Junio C Hamano1a51b522015-03-31 23:15:45 -07001752 git init void &&
Ævar Arnfjörð Bjarmasonf818f7f2022-03-17 11:13:11 +01001753 git -C void config receive.denyCurrentBranch updateInstead &&
1754 test_hook -C void push-to-checkout <<-\EOF &&
1755 if git rev-parse --quiet --verify HEAD
1756 then
1757 has_head=yes
1758 echo >&2 updating from $(git rev-parse HEAD)
1759 else
1760 has_head=no
1761 echo >&2 pushing into void
1762 fi
1763 echo >&2 updating to "$1"
Junio C Hamano1a51b522015-03-31 23:15:45 -07001764
Ævar Arnfjörð Bjarmasonf818f7f2022-03-17 11:13:11 +01001765 git update-index -q --refresh &&
1766 case "$has_head" in
1767 yes)
1768 git read-tree -u -m HEAD "$1" ;;
1769 no)
1770 git read-tree -u -m "$1" ;;
1771 esac || {
1772 status=$?
1773 echo >&2 read-tree failed
1774 exit $status
1775 }
1776 EOF
Junio C Hamano1a51b522015-03-31 23:15:45 -07001777
Johannes Schindelinbc925ce2020-11-18 23:44:32 +00001778 git push void main &&
Junio C Hamano1a51b522015-03-31 23:15:45 -07001779 (
1780 cd void &&
1781 git diff --quiet &&
1782 git diff --cached --quiet &&
1783 test $(git -C .. rev-parse HEAD) = $(git rev-parse HEAD)
Junio C Hamano08553312014-12-01 15:29:54 -08001784 )
1785'
1786
Hariom Verma4ef34642020-02-23 18:57:10 +00001787test_expect_success 'denyCurrentBranch and worktrees' '
1788 git worktree add new-wt &&
1789 git clone . cloned &&
1790 test_commit -C cloned first &&
1791 test_config receive.denyCurrentBranch refuse &&
1792 test_must_fail git -C cloned push origin HEAD:new-wt &&
1793 test_config receive.denyCurrentBranch updateInstead &&
1794 git -C cloned push origin HEAD:new-wt &&
Anders Kaseorg9fdf4f12021-12-01 14:15:46 -08001795 test_path_exists new-wt/first.t &&
Hariom Verma4ef34642020-02-23 18:57:10 +00001796 test_must_fail git -C cloned push --delete origin new-wt
1797'
Anders Kaseorg8bc1f392021-12-01 14:15:44 -08001798
Anders Kaseorg9fdf4f12021-12-01 14:15:46 -08001799test_expect_success 'denyCurrentBranch and bare repository worktrees' '
1800 test_when_finished "rm -fr bare.git" &&
1801 git clone --bare . bare.git &&
1802 git -C bare.git worktree add wt &&
1803 test_commit grape &&
1804 git -C bare.git config receive.denyCurrentBranch refuse &&
1805 test_must_fail git push bare.git HEAD:wt &&
1806 git -C bare.git config receive.denyCurrentBranch updateInstead &&
1807 git push bare.git HEAD:wt &&
1808 test_path_exists bare.git/wt/grape.t &&
1809 test_must_fail git push --delete bare.git wt
1810'
1811
Anders Kaseorg8bc1f392021-12-01 14:15:44 -08001812test_expect_success 'refuse fetch to current branch of worktree' '
1813 test_when_finished "git worktree remove --force wt && git branch -D wt" &&
1814 git worktree add wt &&
1815 test_commit apple &&
1816 test_must_fail git fetch . HEAD:wt &&
1817 git fetch -u . HEAD:wt
1818'
1819
1820test_expect_success 'refuse fetch to current branch of bare repository worktree' '
1821 test_when_finished "rm -fr bare.git" &&
1822 git clone --bare . bare.git &&
1823 git -C bare.git worktree add wt &&
1824 test_commit banana &&
1825 test_must_fail git -C bare.git fetch .. HEAD:wt &&
1826 git -C bare.git fetch -u .. HEAD:wt
1827'
1828
Chen Bojun54077642022-01-29 14:35:38 +08001829test_expect_success 'refuse to push a hidden ref, and make sure do not pollute the repository' '
1830 mk_empty testrepo &&
1831 git -C testrepo config receive.hiderefs refs/hidden &&
1832 git -C testrepo config receive.unpackLimit 1 &&
1833 test_must_fail git push testrepo HEAD:refs/hidden/foo &&
1834 test_dir_is_empty testrepo/.git/objects/pack
1835'
1836
Ævar Arnfjörð Bjarmason7596fe92022-06-15 12:36:32 +02001837test_expect_success LIBCURL 'fetch warns or fails when using username:password' '
Derrick Stolee6dcbdc02022-06-06 14:36:16 +00001838 message="URL '\''https://username:<redacted>@localhost/'\'' uses plaintext credentials" &&
Ævar Arnfjörð Bjarmason7281c192022-06-15 12:44:12 +02001839 test_must_fail git -c transfer.credentialsInUrl=allow fetch https://username:password@localhost 2>err &&
Derrick Stolee6dcbdc02022-06-06 14:36:16 +00001840 ! grep "$message" err &&
1841
Ævar Arnfjörð Bjarmason7281c192022-06-15 12:44:12 +02001842 test_must_fail git -c transfer.credentialsInUrl=warn fetch https://username:password@localhost 2>err &&
Derrick Stolee6dcbdc02022-06-06 14:36:16 +00001843 grep "warning: $message" err >warnings &&
1844 test_line_count = 3 warnings &&
1845
Ævar Arnfjörð Bjarmason7281c192022-06-15 12:44:12 +02001846 test_must_fail git -c transfer.credentialsInUrl=die fetch https://username:password@localhost 2>err &&
Derrick Stolee6dcbdc02022-06-06 14:36:16 +00001847 grep "fatal: $message" err >warnings &&
1848 test_line_count = 1 warnings &&
1849
Ævar Arnfjörð Bjarmason7281c192022-06-15 12:44:12 +02001850 test_must_fail git -c transfer.credentialsInUrl=die fetch https://username:@localhost 2>err &&
Derrick Stolee6dcbdc02022-06-06 14:36:16 +00001851 grep "fatal: $message" err >warnings &&
1852 test_line_count = 1 warnings
1853'
1854
1855
Ævar Arnfjörð Bjarmason7596fe92022-06-15 12:36:32 +02001856test_expect_success LIBCURL 'push warns or fails when using username:password' '
Derrick Stolee6dcbdc02022-06-06 14:36:16 +00001857 message="URL '\''https://username:<redacted>@localhost/'\'' uses plaintext credentials" &&
Ævar Arnfjörð Bjarmason7281c192022-06-15 12:44:12 +02001858 test_must_fail git -c transfer.credentialsInUrl=allow push https://username:password@localhost 2>err &&
Derrick Stolee6dcbdc02022-06-06 14:36:16 +00001859 ! grep "$message" err &&
1860
Ævar Arnfjörð Bjarmason7281c192022-06-15 12:44:12 +02001861 test_must_fail git -c transfer.credentialsInUrl=warn push https://username:password@localhost 2>err &&
Derrick Stolee6dcbdc02022-06-06 14:36:16 +00001862 grep "warning: $message" err >warnings &&
Ævar Arnfjörð Bjarmason7281c192022-06-15 12:44:12 +02001863 test_must_fail git -c transfer.credentialsInUrl=die push https://username:password@localhost 2>err &&
Derrick Stolee6dcbdc02022-06-06 14:36:16 +00001864 grep "fatal: $message" err >warnings &&
1865 test_line_count = 1 warnings
1866'
1867
Junio C Hamanobcdb34f2007-06-08 00:43:22 -07001868test_done