]> git.ipfire.org Git - thirdparty/git.git/blame - t/t5516-fetch-push.sh
fetch+push tests: use "test_hook" and "test_when_finished" pattern
[thirdparty/git.git] / t / t5516-fetch-push.sh
CommitLineData
bcdb34f7
JH
1#!/bin/sh
2
2ead7a67
RR
3test_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
72549dfd 14* reflogs
2ead7a67 15'
bcdb34f7 16
bc925ce3 17GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
334afbc7
JS
18export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
19
bcdb34f7
JH
20. ./test-lib.sh
21
bf45242b 22D=$(pwd)
bcdb34f7
JH
23
24mk_empty () {
2e433b78 25 repo_name="$1"
f818f7f7
ÆAB
26 test_when_finished "rm -rf \"$repo_name\"" &&
27 test_path_is_missing "$repo_name" &&
28 git init "$repo_name" &&
29 git -C "$repo_name" config receive.denyCurrentBranch warn
bcdb34f7
JH
30}
31
6125796f 32mk_test () {
2e433b78
JK
33 repo_name="$1"
34 shift
35
36 mk_empty "$repo_name" &&
6125796f
JH
37 (
38 for ref in "$@"
39 do
2e433b78 40 git push "$repo_name" $the_first_commit:refs/$ref ||
5bd81c73 41 exit
6125796f 42 done &&
2e433b78 43 cd "$repo_name" &&
6125796f
JH
44 for ref in "$@"
45 do
848575d8
JN
46 echo "$the_first_commit" >expect &&
47 git show-ref -s --verify refs/$ref >actual &&
48 test_cmp expect actual ||
49 exit
6125796f
JH
50 done &&
51 git fsck --full
52 )
53}
54
160b81ed 55mk_test_with_hooks() {
2e433b78 56 repo_name=$1
160b81ed 57 mk_test "$@" &&
f818f7f7
ÆAB
58 test_hook -C "$repo_name" pre-receive <<-'EOF' &&
59 cat - >>pre-receive.actual
60 EOF
61
62 test_hook -C "$repo_name" update <<-'EOF' &&
63 printf "%s %s %s\n" "$@" >>update.actual
64 EOF
65
66 test_hook -C "$repo_name" post-receive <<-'EOF' &&
67 cat - >>post-receive.actual
68 EOF
69
70 test_hook -C "$repo_name" post-update <<-'EOF'
71 for ref in "$@"
72 do
73 printf "%s\n" "$ref" >>post-update.actual
74 done
75 EOF
160b81ed
PYH
76}
77
b2dc968e 78mk_child() {
f818f7f7 79 test_when_finished "rm -rf \"$2\"" &&
2e433b78 80 git clone "$1" "$2"
b2dc968e
JK
81}
82
6125796f 83check_push_result () {
cfb482b6 84 test $# -ge 3 ||
165293af 85 BUG "check_push_result requires at least 3 parameters"
cfb482b6 86
2e433b78
JK
87 repo_name="$1"
88 shift
89
6125796f 90 (
2e433b78 91 cd "$repo_name" &&
848575d8
JN
92 echo "$1" >expect &&
93 shift &&
6125796f
JH
94 for ref in "$@"
95 do
848575d8
JN
96 git show-ref -s --verify refs/$ref >actual &&
97 test_cmp expect actual ||
98 exit
6125796f
JH
99 done &&
100 git fsck --full
101 )
102}
103
bcdb34f7
JH
104test_expect_success setup '
105
d4785cd1 106 >path1 &&
bcdb34f7
JH
107 git add path1 &&
108 test_tick &&
109 git commit -a -m repo &&
bc925ce3 110 the_first_commit=$(git show-ref -s --verify refs/heads/main) &&
6125796f 111
d4785cd1 112 >path2 &&
6125796f
JH
113 git add path2 &&
114 test_tick &&
115 git commit -a -m second &&
bc925ce3 116 the_commit=$(git show-ref -s --verify refs/heads/main)
bcdb34f7
JH
117
118'
119
120test_expect_success 'fetch without wildcard' '
2e433b78 121 mk_empty testrepo &&
bcdb34f7
JH
122 (
123 cd testrepo &&
bc925ce3 124 git fetch .. refs/heads/main:refs/remotes/origin/main &&
bcdb34f7 125
bc925ce3 126 echo "$the_commit commit refs/remotes/origin/main" >expect &&
848575d8
JN
127 git for-each-ref refs/remotes/origin >actual &&
128 test_cmp expect actual
bcdb34f7
JH
129 )
130'
131
132test_expect_success 'fetch with wildcard' '
2e433b78 133 mk_empty testrepo &&
bcdb34f7
JH
134 (
135 cd testrepo &&
136 git config remote.up.url .. &&
137 git config remote.up.fetch "refs/heads/*:refs/remotes/origin/*" &&
138 git fetch up &&
139
bc925ce3 140 echo "$the_commit commit refs/remotes/origin/main" >expect &&
848575d8
JN
141 git for-each-ref refs/remotes/origin >actual &&
142 test_cmp expect actual
bcdb34f7
JH
143 )
144'
145
55029ae4 146test_expect_success 'fetch with insteadOf' '
2e433b78 147 mk_empty testrepo &&
55029ae4 148 (
60e3aba9 149 TRASH=$(pwd)/ &&
55029ae4 150 cd testrepo &&
f69e836f 151 git config "url.$TRASH.insteadOf" trash/ &&
55029ae4
DB
152 git config remote.up.url trash/. &&
153 git config remote.up.fetch "refs/heads/*:refs/remotes/origin/*" &&
154 git fetch up &&
155
bc925ce3 156 echo "$the_commit commit refs/remotes/origin/main" >expect &&
848575d8
JN
157 git for-each-ref refs/remotes/origin >actual &&
158 test_cmp expect actual
55029ae4
DB
159 )
160'
161
1c2eafb8 162test_expect_success 'fetch with pushInsteadOf (should not rewrite)' '
2e433b78 163 mk_empty testrepo &&
1c2eafb8
JT
164 (
165 TRASH=$(pwd)/ &&
166 cd testrepo &&
167 git config "url.trash/.pushInsteadOf" "$TRASH" &&
168 git config remote.up.url "$TRASH." &&
169 git config remote.up.fetch "refs/heads/*:refs/remotes/origin/*" &&
170 git fetch up &&
171
bc925ce3 172 echo "$the_commit commit refs/remotes/origin/main" >expect &&
848575d8
JN
173 git for-each-ref refs/remotes/origin >actual &&
174 test_cmp expect actual
1c2eafb8
JT
175 )
176'
177
477673d6
JT
178grep_wrote () {
179 object_count=$1
180 file_name=$2
181 grep 'write_pack_file/wrote.*"value":"'$1'"' $2
182}
183
f818f7f7 184test_expect_success 'push without negotiation' '
477673d6
JT
185 mk_empty testrepo &&
186 git push testrepo $the_first_commit:refs/remotes/origin/first_commit &&
54a03bc7 187 test_commit -C testrepo unrelated_commit &&
477673d6 188 git -C testrepo config receive.hideRefs refs/remotes/origin/first_commit &&
f818f7f7 189 test_when_finished "rm event" &&
477673d6 190 GIT_TRACE2_EVENT="$(pwd)/event" git -c protocol.version=2 push testrepo refs/heads/main:refs/remotes/origin/main &&
f818f7f7
ÆAB
191 grep_wrote 5 event # 2 commits, 2 trees, 1 blob
192'
477673d6 193
f818f7f7 194test_expect_success 'push with negotiation' '
477673d6
JT
195 mk_empty testrepo &&
196 git push testrepo $the_first_commit:refs/remotes/origin/first_commit &&
54a03bc7 197 test_commit -C testrepo unrelated_commit &&
477673d6 198 git -C testrepo config receive.hideRefs refs/remotes/origin/first_commit &&
f818f7f7 199 test_when_finished "rm event" &&
477673d6
JT
200 GIT_TRACE2_EVENT="$(pwd)/event" git -c protocol.version=2 -c push.negotiate=1 push testrepo refs/heads/main:refs/remotes/origin/main &&
201 grep_wrote 2 event # 1 commit, 1 tree
202'
203
204test_expect_success 'push with negotiation proceeds anyway even if negotiation fails' '
477673d6
JT
205 mk_empty testrepo &&
206 git push testrepo $the_first_commit:refs/remotes/origin/first_commit &&
54a03bc7 207 test_commit -C testrepo unrelated_commit &&
477673d6 208 git -C testrepo config receive.hideRefs refs/remotes/origin/first_commit &&
f818f7f7 209 test_when_finished "rm event" &&
477673d6
JT
210 GIT_TEST_PROTOCOL_VERSION=0 GIT_TRACE2_EVENT="$(pwd)/event" \
211 git -c push.negotiate=1 push testrepo refs/heads/main:refs/remotes/origin/main 2>err &&
212 grep_wrote 5 event && # 2 commits, 2 trees, 1 blob
213 test_i18ngrep "push negotiation failed" err
214'
215
386c076a
GC
216test_expect_success 'push with negotiation does not attempt to fetch submodules' '
217 mk_empty submodule_upstream &&
218 test_commit -C submodule_upstream submodule_commit &&
219 git submodule add ./submodule_upstream submodule &&
220 mk_empty testrepo &&
221 git push testrepo $the_first_commit:refs/remotes/origin/first_commit &&
222 test_commit -C testrepo unrelated_commit &&
223 git -C testrepo config receive.hideRefs refs/remotes/origin/first_commit &&
224 git -c submodule.recurse=true -c protocol.version=2 -c push.negotiate=1 push testrepo refs/heads/main:refs/remotes/origin/main 2>err &&
225 ! grep "Fetching submodule" err
226'
227
bcdb34f7 228test_expect_success 'push without wildcard' '
2e433b78 229 mk_empty testrepo &&
bcdb34f7 230
bc925ce3 231 git push testrepo refs/heads/main:refs/remotes/origin/main &&
bcdb34f7
JH
232 (
233 cd testrepo &&
bc925ce3 234 echo "$the_commit commit refs/remotes/origin/main" >expect &&
848575d8
JN
235 git for-each-ref refs/remotes/origin >actual &&
236 test_cmp expect actual
bcdb34f7
JH
237 )
238'
239
240test_expect_success 'push with wildcard' '
2e433b78 241 mk_empty testrepo &&
bcdb34f7
JH
242
243 git push testrepo "refs/heads/*:refs/remotes/origin/*" &&
244 (
245 cd testrepo &&
bc925ce3 246 echo "$the_commit commit refs/remotes/origin/main" >expect &&
848575d8
JN
247 git for-each-ref refs/remotes/origin >actual &&
248 test_cmp expect actual
bcdb34f7
JH
249 )
250'
251
55029ae4 252test_expect_success 'push with insteadOf' '
2e433b78 253 mk_empty testrepo &&
f69e836f 254 TRASH="$(pwd)/" &&
3c695523 255 test_config "url.$TRASH.insteadOf" trash/ &&
bc925ce3 256 git push trash/testrepo refs/heads/main:refs/remotes/origin/main &&
55029ae4
DB
257 (
258 cd testrepo &&
bc925ce3 259 echo "$the_commit commit refs/remotes/origin/main" >expect &&
848575d8
JN
260 git for-each-ref refs/remotes/origin >actual &&
261 test_cmp expect actual
55029ae4
DB
262 )
263'
264
1c2eafb8 265test_expect_success 'push with pushInsteadOf' '
2e433b78 266 mk_empty testrepo &&
1c2eafb8 267 TRASH="$(pwd)/" &&
3c695523 268 test_config "url.$TRASH.pushInsteadOf" trash/ &&
bc925ce3 269 git push trash/testrepo refs/heads/main:refs/remotes/origin/main &&
1c2eafb8
JT
270 (
271 cd testrepo &&
bc925ce3 272 echo "$the_commit commit refs/remotes/origin/main" >expect &&
848575d8
JN
273 git for-each-ref refs/remotes/origin >actual &&
274 test_cmp expect actual
1c2eafb8
JT
275 )
276'
277
278test_expect_success 'push with pushInsteadOf and explicit pushurl (pushInsteadOf should not rewrite)' '
2e433b78 279 mk_empty testrepo &&
41ae34d1 280 test_config "url.trash2/.pushInsteadOf" testrepo/ &&
eb32c66e 281 test_config "url.trash3/.pushInsteadOf" trash/wrong &&
3c695523 282 test_config remote.r.url trash/wrong &&
41ae34d1 283 test_config remote.r.pushurl "testrepo/" &&
bc925ce3 284 git push r refs/heads/main:refs/remotes/origin/main &&
1c2eafb8
JT
285 (
286 cd testrepo &&
bc925ce3 287 echo "$the_commit commit refs/remotes/origin/main" >expect &&
848575d8
JN
288 git for-each-ref refs/remotes/origin >actual &&
289 test_cmp expect actual
1c2eafb8
JT
290 )
291'
292
6125796f
JH
293test_expect_success 'push with matching heads' '
294
bc925ce3 295 mk_test testrepo heads/main &&
0a42ac03 296 git push testrepo : &&
bc925ce3 297 check_push_result testrepo $the_commit heads/main
6125796f
JH
298
299'
300
a83619d6
PB
301test_expect_success 'push with matching heads on the command line' '
302
bc925ce3 303 mk_test testrepo heads/main &&
a83619d6 304 git push testrepo : &&
bc925ce3 305 check_push_result testrepo $the_commit heads/main
a83619d6
PB
306
307'
308
309test_expect_success 'failed (non-fast-forward) push with matching heads' '
310
bc925ce3 311 mk_test testrepo heads/main &&
a83619d6
PB
312 git push testrepo : &&
313 git commit --amend -massaged &&
d492b31c 314 test_must_fail git push testrepo &&
bc925ce3 315 check_push_result testrepo $the_commit heads/main &&
a83619d6
PB
316 git reset --hard $the_commit
317
318'
319
320test_expect_success 'push --force with matching heads' '
321
bc925ce3 322 mk_test testrepo heads/main &&
a83619d6
PB
323 git push testrepo : &&
324 git commit --amend -massaged &&
0a42ac03 325 git push --force testrepo : &&
bc925ce3 326 ! check_push_result testrepo $the_commit heads/main &&
a83619d6
PB
327 git reset --hard $the_commit
328
329'
330
331test_expect_success 'push with matching heads and forced update' '
332
bc925ce3 333 mk_test testrepo heads/main &&
a83619d6
PB
334 git push testrepo : &&
335 git commit --amend -massaged &&
336 git push testrepo +: &&
bc925ce3 337 ! check_push_result testrepo $the_commit heads/main &&
a83619d6
PB
338 git reset --hard $the_commit
339
340'
341
6125796f
JH
342test_expect_success 'push with no ambiguity (1)' '
343
bc925ce3
JS
344 mk_test testrepo heads/main &&
345 git push testrepo main:main &&
346 check_push_result testrepo $the_commit heads/main
6125796f
JH
347
348'
349
350test_expect_success 'push with no ambiguity (2)' '
351
bc925ce3
JS
352 mk_test testrepo remotes/origin/main &&
353 git push testrepo main:origin/main &&
354 check_push_result testrepo $the_commit remotes/origin/main
6125796f
JH
355
356'
357
ae36bdcf
SP
358test_expect_success 'push with colon-less refspec, no ambiguity' '
359
bc925ce3
JS
360 mk_test testrepo heads/main heads/t/main &&
361 git branch -f t/main main &&
362 git push testrepo main &&
363 check_push_result testrepo $the_commit heads/main &&
364 check_push_result testrepo $the_first_commit heads/t/main
ae36bdcf
SP
365
366'
367
6125796f
JH
368test_expect_success 'push with weak ambiguity (1)' '
369
bc925ce3
JS
370 mk_test testrepo heads/main remotes/origin/main &&
371 git push testrepo main:main &&
372 check_push_result testrepo $the_commit heads/main &&
373 check_push_result testrepo $the_first_commit remotes/origin/main
6125796f
JH
374
375'
376
377test_expect_success 'push with weak ambiguity (2)' '
378
bc925ce3
JS
379 mk_test testrepo heads/main remotes/origin/main remotes/another/main &&
380 git push testrepo main:main &&
381 check_push_result testrepo $the_commit heads/main &&
382 check_push_result testrepo $the_first_commit remotes/origin/main remotes/another/main
6125796f
JH
383
384'
385
3ef6a1fe 386test_expect_success 'push with ambiguity' '
6125796f 387
2e433b78 388 mk_test testrepo heads/frotz tags/frotz &&
bc925ce3 389 test_must_fail git push testrepo main:frotz &&
2e433b78 390 check_push_result testrepo $the_first_commit heads/frotz tags/frotz
1ed10b88
JH
391
392'
393
394test_expect_success 'push with colon-less refspec (1)' '
395
2e433b78 396 mk_test testrepo heads/frotz tags/frotz &&
bc925ce3 397 git branch -f frotz main &&
1ed10b88 398 git push testrepo frotz &&
2e433b78
JK
399 check_push_result testrepo $the_commit heads/frotz &&
400 check_push_result testrepo $the_first_commit tags/frotz
1ed10b88
JH
401
402'
403
404test_expect_success 'push with colon-less refspec (2)' '
405
2e433b78 406 mk_test testrepo heads/frotz tags/frotz &&
1ed10b88
JH
407 if git show-ref --verify -q refs/heads/frotz
408 then
409 git branch -D frotz
410 fi &&
411 git tag -f frotz &&
dbfeddb1 412 git push -f testrepo frotz &&
2e433b78
JK
413 check_push_result testrepo $the_commit tags/frotz &&
414 check_push_result testrepo $the_first_commit heads/frotz
1ed10b88
JH
415
416'
417
418test_expect_success 'push with colon-less refspec (3)' '
419
2e433b78 420 mk_test testrepo &&
1ed10b88
JH
421 if git show-ref --verify -q refs/tags/frotz
422 then
423 git tag -d frotz
424 fi &&
bc925ce3 425 git branch -f frotz main &&
1ed10b88 426 git push testrepo frotz &&
2e433b78 427 check_push_result testrepo $the_commit heads/frotz &&
9a3c6f7b 428 test 1 = $( cd testrepo && git show-ref | wc -l )
1ed10b88
JH
429'
430
431test_expect_success 'push with colon-less refspec (4)' '
432
2e433b78 433 mk_test testrepo &&
1ed10b88
JH
434 if git show-ref --verify -q refs/heads/frotz
435 then
436 git branch -D frotz
437 fi &&
438 git tag -f frotz &&
439 git push testrepo frotz &&
2e433b78 440 check_push_result testrepo $the_commit tags/frotz &&
9a3c6f7b 441 test 1 = $( cd testrepo && git show-ref | wc -l )
1ed10b88 442
6125796f
JH
443'
444
7be8b3ba 445test_expect_success 'push head with non-existent, incomplete dest' '
f8aae120 446
2e433b78 447 mk_test testrepo &&
bc925ce3 448 git push testrepo main:branch &&
2e433b78 449 check_push_result testrepo $the_commit heads/branch
f8aae120
JK
450
451'
452
7be8b3ba 453test_expect_success 'push tag with non-existent, incomplete dest' '
f8aae120 454
2e433b78 455 mk_test testrepo &&
f8aae120
JK
456 git tag -f v1.0 &&
457 git push testrepo v1.0:tag &&
2e433b78 458 check_push_result testrepo $the_commit tags/tag
f8aae120
JK
459
460'
461
7be8b3ba 462test_expect_success 'push sha1 with non-existent, incomplete dest' '
f8aae120 463
2e433b78 464 mk_test testrepo &&
bc925ce3 465 test_must_fail git push testrepo $(git rev-parse main):foo
f8aae120
JK
466
467'
468
7be8b3ba 469test_expect_success 'push ref expression with non-existent, incomplete dest' '
f8aae120 470
2e433b78 471 mk_test testrepo &&
bc925ce3 472 test_must_fail git push testrepo main^:branch
f8aae120
JK
473
474'
475
374fbaef
FC
476for head in HEAD @
477do
478
479 test_expect_success "push with $head" '
27d7c859
JH
480 mk_test testrepo heads/main &&
481 git checkout main &&
374fbaef 482 git push testrepo $head &&
27d7c859 483 check_push_result testrepo $the_commit heads/main
374fbaef
FC
484 '
485
486 test_expect_success "push with $head nonexisting at remote" '
27d7c859
JH
487 mk_test testrepo heads/main &&
488 git checkout -b local main &&
489 test_when_finished "git checkout main; git branch -D local" &&
374fbaef
FC
490 git push testrepo $head &&
491 check_push_result testrepo $the_commit heads/local
492 '
493
494 test_expect_success "push with +$head" '
27d7c859
JH
495 mk_test testrepo heads/main &&
496 git checkout -b local main &&
497 test_when_finished "git checkout main; git branch -D local" &&
498 git push testrepo main local &&
499 check_push_result testrepo $the_commit heads/main &&
374fbaef
FC
500 check_push_result testrepo $the_commit heads/local &&
501
502 # Without force rewinding should fail
503 git reset --hard $head^ &&
504 test_must_fail git push testrepo $head &&
505 check_push_result testrepo $the_commit heads/local &&
506
507 # With force rewinding should succeed
508 git push testrepo +$head &&
509 check_push_result testrepo $the_first_commit heads/local
374fbaef
FC
510 '
511
512 test_expect_success "push $head with non-existent, incomplete dest" '
374fbaef 513 mk_test testrepo &&
27d7c859 514 git checkout main &&
374fbaef
FC
515 git push testrepo $head:branch &&
516 check_push_result testrepo $the_commit heads/branch
517
518 '
519
520 test_expect_success "push with config remote.*.push = $head" '
374fbaef 521 mk_test testrepo heads/local &&
27d7c859 522 git checkout main &&
374fbaef
FC
523 git branch -f local $the_commit &&
524 test_when_finished "git branch -D local" &&
525 (
526 cd testrepo &&
527 git checkout local &&
528 git reset --hard $the_first_commit
529 ) &&
530 test_config remote.there.url testrepo &&
531 test_config remote.there.push $head &&
27d7c859 532 test_config branch.main.remote there &&
374fbaef 533 git push &&
27d7c859 534 check_push_result testrepo $the_commit heads/main &&
374fbaef
FC
535 check_push_result testrepo $the_first_commit heads/local
536 '
537
538done
9f0ea7e8 539
e083ef5d
GC
540test_expect_success "push to remote with no explicit refspec and config remote.*.push = src:dest" '
541 mk_test testrepo heads/main &&
542 git checkout $the_first_commit &&
543 test_config remote.there.url testrepo &&
544 test_config remote.there.push refs/heads/main:refs/heads/main &&
545 git push there &&
546 check_push_result testrepo $the_commit heads/main
547'
548
224c2171 549test_expect_success 'push with remote.pushdefault' '
bc925ce3
JS
550 mk_test up_repo heads/main &&
551 mk_test down_repo heads/main &&
224c2171
RR
552 test_config remote.up.url up_repo &&
553 test_config remote.down.url down_repo &&
bc925ce3 554 test_config branch.main.remote up &&
224c2171 555 test_config remote.pushdefault down &&
54a3c673 556 test_config push.default matching &&
224c2171 557 git push &&
bc925ce3
JS
558 check_push_result up_repo $the_first_commit heads/main &&
559 check_push_result down_repo $the_commit heads/main
9f0ea7e8
DB
560'
561
e1ca4241
MG
562test_expect_success 'push with config remote.*.pushurl' '
563
bc925ce3
JS
564 mk_test testrepo heads/main &&
565 git checkout main &&
3c695523
JN
566 test_config remote.there.url test2repo &&
567 test_config remote.there.pushurl testrepo &&
0a42ac03 568 git push there : &&
bc925ce3 569 check_push_result testrepo $the_commit heads/main
e1ca4241
MG
570'
571
9f765ce6 572test_expect_success 'push with config branch.*.pushremote' '
bc925ce3
JS
573 mk_test up_repo heads/main &&
574 mk_test side_repo heads/main &&
575 mk_test down_repo heads/main &&
9f765ce6
RR
576 test_config remote.up.url up_repo &&
577 test_config remote.pushdefault side_repo &&
578 test_config remote.down.url down_repo &&
bc925ce3
JS
579 test_config branch.main.remote up &&
580 test_config branch.main.pushremote down &&
54a3c673 581 test_config push.default matching &&
9f765ce6 582 git push &&
bc925ce3
JS
583 check_push_result up_repo $the_first_commit heads/main &&
584 check_push_result side_repo $the_first_commit heads/main &&
585 check_push_result down_repo $the_commit heads/main
e1ca4241
MG
586'
587
98b406f3 588test_expect_success 'branch.*.pushremote config order is irrelevant' '
bc925ce3
JS
589 mk_test one_repo heads/main &&
590 mk_test two_repo heads/main &&
98b406f3
JK
591 test_config remote.one.url one_repo &&
592 test_config remote.two.url two_repo &&
bc925ce3 593 test_config branch.main.pushremote two_repo &&
98b406f3
JK
594 test_config remote.pushdefault one_repo &&
595 test_config push.default matching &&
596 git push &&
bc925ce3
JS
597 check_push_result one_repo $the_first_commit heads/main &&
598 check_push_result two_repo $the_commit heads/main
98b406f3
JK
599'
600
11f2441f
BE
601test_expect_success 'push with dry-run' '
602
bc925ce3
JS
603 mk_test testrepo heads/main &&
604 old_commit=$(git -C testrepo show-ref -s --verify refs/heads/main) &&
0a42ac03 605 git push --dry-run testrepo : &&
bc925ce3 606 check_push_result testrepo $old_commit heads/main
11f2441f
BE
607'
608
28391a80
JS
609test_expect_success 'push updates local refs' '
610
bc925ce3 611 mk_test testrepo heads/main &&
2e433b78 612 mk_child testrepo child &&
d4785cd1
JS
613 (
614 cd child &&
bc925ce3 615 git pull .. main &&
28391a80 616 git push &&
bc925ce3
JS
617 test $(git rev-parse main) = \
618 $(git rev-parse remotes/origin/main)
d4785cd1 619 )
28391a80
JS
620
621'
622
16ed2f48
CB
623test_expect_success 'push updates up-to-date local refs' '
624
bc925ce3 625 mk_test testrepo heads/main &&
2e433b78
JK
626 mk_child testrepo child1 &&
627 mk_child testrepo child2 &&
bc925ce3 628 (cd child1 && git pull .. main && git push) &&
d4785cd1
JS
629 (
630 cd child2 &&
bc925ce3 631 git pull ../child1 main &&
16ed2f48 632 git push &&
bc925ce3
JS
633 test $(git rev-parse main) = \
634 $(git rev-parse remotes/origin/main)
d4785cd1 635 )
16ed2f48
CB
636
637'
638
639test_expect_success 'push preserves up-to-date packed refs' '
640
bc925ce3 641 mk_test testrepo heads/main &&
2e433b78 642 mk_child testrepo child &&
d4785cd1
JS
643 (
644 cd child &&
16ed2f48 645 git push &&
bc925ce3 646 ! test -f .git/refs/remotes/origin/main
d4785cd1 647 )
16ed2f48
CB
648
649'
650
28391a80
JS
651test_expect_success 'push does not update local refs on failure' '
652
bc925ce3 653 mk_test testrepo heads/main &&
2e433b78 654 mk_child testrepo child &&
fc012c28 655 echo "#!/no/frobnication/today" >testrepo/.git/hooks/pre-receive &&
b2dc968e 656 chmod +x testrepo/.git/hooks/pre-receive &&
d4785cd1
JS
657 (
658 cd child &&
bc925ce3 659 git pull .. main &&
d492b31c 660 test_must_fail git push &&
bc925ce3
JS
661 test $(git rev-parse main) != \
662 $(git rev-parse remotes/origin/main)
d4785cd1 663 )
28391a80
JS
664
665'
666
667test_expect_success 'allow deleting an invalid remote ref' '
668
e9de7a52 669 mk_test testrepo heads/branch &&
28391a80 670 rm -f testrepo/.git/objects/??/* &&
e9de7a52
JK
671 git push testrepo :refs/heads/branch &&
672 (cd testrepo && test_must_fail git rev-parse --verify refs/heads/branch)
28391a80
JS
673
674'
675
160b81ed 676test_expect_success 'pushing valid refs triggers post-receive and post-update hooks' '
bc925ce3
JS
677 mk_test_with_hooks testrepo heads/main heads/next &&
678 orgmain=$(cd testrepo && git show-ref -s --verify refs/heads/main) &&
679 newmain=$(git show-ref -s --verify refs/heads/main) &&
160b81ed 680 orgnext=$(cd testrepo && git show-ref -s --verify refs/heads/next) &&
8125a58b 681 newnext=$ZERO_OID &&
bc925ce3 682 git push testrepo refs/heads/main:refs/heads/main :refs/heads/next &&
160b81ed
PYH
683 (
684 cd testrepo/.git &&
685 cat >pre-receive.expect <<-EOF &&
bc925ce3 686 $orgmain $newmain refs/heads/main
160b81ed
PYH
687 $orgnext $newnext refs/heads/next
688 EOF
689
690 cat >update.expect <<-EOF &&
bc925ce3 691 refs/heads/main $orgmain $newmain
160b81ed
PYH
692 refs/heads/next $orgnext $newnext
693 EOF
694
695 cat >post-receive.expect <<-EOF &&
bc925ce3 696 $orgmain $newmain refs/heads/main
160b81ed
PYH
697 $orgnext $newnext refs/heads/next
698 EOF
699
700 cat >post-update.expect <<-EOF &&
bc925ce3 701 refs/heads/main
160b81ed
PYH
702 refs/heads/next
703 EOF
704
705 test_cmp pre-receive.expect pre-receive.actual &&
706 test_cmp update.expect update.actual &&
707 test_cmp post-receive.expect post-receive.actual &&
708 test_cmp post-update.expect post-update.actual
709 )
710'
711
712test_expect_success 'deleting dangling ref triggers hooks with correct args' '
e9de7a52 713 mk_test_with_hooks testrepo heads/branch &&
968f12fd 714 orig=$(git -C testrepo rev-parse refs/heads/branch) &&
160b81ed 715 rm -f testrepo/.git/objects/??/* &&
e9de7a52 716 git push testrepo :refs/heads/branch &&
160b81ed
PYH
717 (
718 cd testrepo/.git &&
719 cat >pre-receive.expect <<-EOF &&
968f12fd 720 $orig $ZERO_OID refs/heads/branch
160b81ed
PYH
721 EOF
722
723 cat >update.expect <<-EOF &&
968f12fd 724 refs/heads/branch $orig $ZERO_OID
160b81ed
PYH
725 EOF
726
727 cat >post-receive.expect <<-EOF &&
968f12fd 728 $orig $ZERO_OID refs/heads/branch
160b81ed
PYH
729 EOF
730
731 cat >post-update.expect <<-EOF &&
e9de7a52 732 refs/heads/branch
160b81ed
PYH
733 EOF
734
735 test_cmp pre-receive.expect pre-receive.actual &&
736 test_cmp update.expect update.actual &&
737 test_cmp post-receive.expect post-receive.actual &&
738 test_cmp post-update.expect post-update.actual
739 )
740'
741
742test_expect_success 'deletion of a non-existent ref is not fed to post-receive and post-update hooks' '
bc925ce3
JS
743 mk_test_with_hooks testrepo heads/main &&
744 orgmain=$(cd testrepo && git show-ref -s --verify refs/heads/main) &&
745 newmain=$(git show-ref -s --verify refs/heads/main) &&
746 git push testrepo main :refs/heads/nonexistent &&
160b81ed
PYH
747 (
748 cd testrepo/.git &&
749 cat >pre-receive.expect <<-EOF &&
bc925ce3 750 $orgmain $newmain refs/heads/main
8125a58b 751 $ZERO_OID $ZERO_OID refs/heads/nonexistent
160b81ed
PYH
752 EOF
753
754 cat >update.expect <<-EOF &&
bc925ce3 755 refs/heads/main $orgmain $newmain
8125a58b 756 refs/heads/nonexistent $ZERO_OID $ZERO_OID
160b81ed
PYH
757 EOF
758
759 cat >post-receive.expect <<-EOF &&
bc925ce3 760 $orgmain $newmain refs/heads/main
160b81ed
PYH
761 EOF
762
763 cat >post-update.expect <<-EOF &&
bc925ce3 764 refs/heads/main
160b81ed
PYH
765 EOF
766
767 test_cmp pre-receive.expect pre-receive.actual &&
768 test_cmp update.expect update.actual &&
769 test_cmp post-receive.expect post-receive.actual &&
770 test_cmp post-update.expect post-update.actual
771 )
772'
773
774test_expect_success 'deletion of a non-existent ref alone does trigger post-receive and post-update hooks' '
bc925ce3 775 mk_test_with_hooks testrepo heads/main &&
160b81ed
PYH
776 git push testrepo :refs/heads/nonexistent &&
777 (
778 cd testrepo/.git &&
779 cat >pre-receive.expect <<-EOF &&
8125a58b 780 $ZERO_OID $ZERO_OID refs/heads/nonexistent
160b81ed
PYH
781 EOF
782
783 cat >update.expect <<-EOF &&
8125a58b 784 refs/heads/nonexistent $ZERO_OID $ZERO_OID
160b81ed
PYH
785 EOF
786
787 test_cmp pre-receive.expect pre-receive.actual &&
788 test_cmp update.expect update.actual &&
789 test_path_is_missing post-receive.actual &&
790 test_path_is_missing post-update.actual
791 )
792'
793
794test_expect_success 'mixed ref updates, deletes, invalid deletes trigger hooks with correct input' '
bc925ce3
JS
795 mk_test_with_hooks testrepo heads/main heads/next heads/seen &&
796 orgmain=$(cd testrepo && git show-ref -s --verify refs/heads/main) &&
797 newmain=$(git show-ref -s --verify refs/heads/main) &&
160b81ed 798 orgnext=$(cd testrepo && git show-ref -s --verify refs/heads/next) &&
8125a58b 799 newnext=$ZERO_OID &&
6dca5dbf 800 orgseen=$(cd testrepo && git show-ref -s --verify refs/heads/seen) &&
bc925ce3
JS
801 newseen=$(git show-ref -s --verify refs/heads/main) &&
802 git push testrepo refs/heads/main:refs/heads/main \
803 refs/heads/main:refs/heads/seen :refs/heads/next \
160b81ed
PYH
804 :refs/heads/nonexistent &&
805 (
806 cd testrepo/.git &&
807 cat >pre-receive.expect <<-EOF &&
bc925ce3 808 $orgmain $newmain refs/heads/main
160b81ed 809 $orgnext $newnext refs/heads/next
6dca5dbf 810 $orgseen $newseen refs/heads/seen
8125a58b 811 $ZERO_OID $ZERO_OID refs/heads/nonexistent
160b81ed
PYH
812 EOF
813
814 cat >update.expect <<-EOF &&
bc925ce3 815 refs/heads/main $orgmain $newmain
160b81ed 816 refs/heads/next $orgnext $newnext
6dca5dbf 817 refs/heads/seen $orgseen $newseen
8125a58b 818 refs/heads/nonexistent $ZERO_OID $ZERO_OID
160b81ed
PYH
819 EOF
820
821 cat >post-receive.expect <<-EOF &&
bc925ce3 822 $orgmain $newmain refs/heads/main
160b81ed 823 $orgnext $newnext refs/heads/next
6dca5dbf 824 $orgseen $newseen refs/heads/seen
160b81ed
PYH
825 EOF
826
827 cat >post-update.expect <<-EOF &&
bc925ce3 828 refs/heads/main
160b81ed 829 refs/heads/next
6dca5dbf 830 refs/heads/seen
160b81ed
PYH
831 EOF
832
833 test_cmp pre-receive.expect pre-receive.actual &&
834 test_cmp update.expect update.actual &&
835 test_cmp post-receive.expect post-receive.actual &&
836 test_cmp post-update.expect post-update.actual
837 )
838'
839
f517f1f2 840test_expect_success 'allow deleting a ref using --delete' '
bc925ce3 841 mk_test testrepo heads/main &&
f517f1f2 842 (cd testrepo && git config receive.denyDeleteCurrent warn) &&
bc925ce3
JS
843 git push testrepo --delete main &&
844 (cd testrepo && test_must_fail git rev-parse --verify refs/heads/main)
f517f1f2
JK
845'
846
847test_expect_success 'allow deleting a tag using --delete' '
bc925ce3
JS
848 mk_test testrepo heads/main &&
849 git tag -a -m dummy_message deltag heads/main &&
f517f1f2
JK
850 git push testrepo --tags &&
851 (cd testrepo && git rev-parse --verify -q refs/tags/deltag) &&
852 git push testrepo --delete tag deltag &&
853 (cd testrepo && test_must_fail git rev-parse --verify refs/tags/deltag)
854'
855
856test_expect_success 'push --delete without args aborts' '
bc925ce3 857 mk_test testrepo heads/main &&
f517f1f2
JK
858 test_must_fail git push testrepo --delete
859'
860
861test_expect_success 'push --delete refuses src:dest refspecs' '
bc925ce3
JS
862 mk_test testrepo heads/main &&
863 test_must_fail git push testrepo --delete main:foo
f517f1f2
JK
864'
865
20e41640
JH
866test_expect_success 'push --delete refuses empty string' '
867 mk_test testrepo heads/master &&
868 test_must_fail git push testrepo --delete ""
869'
870
986e8239 871test_expect_success 'warn on push to HEAD of non-bare repository' '
bc925ce3 872 mk_test testrepo heads/main &&
d4785cd1
JS
873 (
874 cd testrepo &&
bc925ce3 875 git checkout main &&
d4785cd1
JS
876 git config receive.denyCurrentBranch warn
877 ) &&
bc925ce3 878 git push testrepo main 2>stderr &&
3d95d92b 879 grep "warning: updating the current branch" stderr
986e8239
JK
880'
881
882test_expect_success 'deny push to HEAD of non-bare repository' '
bc925ce3 883 mk_test testrepo heads/main &&
d4785cd1
JS
884 (
885 cd testrepo &&
bc925ce3 886 git checkout main &&
d4785cd1
JS
887 git config receive.denyCurrentBranch true
888 ) &&
bc925ce3 889 test_must_fail git push testrepo main
986e8239
JK
890'
891
892test_expect_success 'allow push to HEAD of bare repository (bare)' '
bc925ce3 893 mk_test testrepo heads/main &&
d4785cd1
JS
894 (
895 cd testrepo &&
bc925ce3 896 git checkout main &&
986e8239 897 git config receive.denyCurrentBranch true &&
d4785cd1
JS
898 git config core.bare true
899 ) &&
bc925ce3 900 git push testrepo main 2>stderr &&
3d95d92b 901 ! grep "warning: updating the current branch" stderr
986e8239
JK
902'
903
904test_expect_success 'allow push to HEAD of non-bare repository (config)' '
bc925ce3 905 mk_test testrepo heads/main &&
d4785cd1
JS
906 (
907 cd testrepo &&
bc925ce3 908 git checkout main &&
986e8239
JK
909 git config receive.denyCurrentBranch false
910 ) &&
bc925ce3 911 git push testrepo main 2>stderr &&
3d95d92b 912 ! grep "warning: updating the current branch" stderr
986e8239
JK
913'
914
18afe101 915test_expect_success 'fetch with branches' '
2e433b78 916 mk_empty testrepo &&
18afe101
MK
917 git branch second $the_first_commit &&
918 git checkout second &&
919 echo ".." > testrepo/.git/branches/branch1 &&
d4785cd1
JS
920 (
921 cd testrepo &&
18afe101 922 git fetch branch1 &&
848575d8
JN
923 echo "$the_commit commit refs/heads/branch1" >expect &&
924 git for-each-ref refs/heads >actual &&
925 test_cmp expect actual
18afe101 926 ) &&
bc925ce3 927 git checkout main
18afe101
MK
928'
929
930test_expect_success 'fetch with branches containing #' '
2e433b78 931 mk_empty testrepo &&
18afe101 932 echo "..#second" > testrepo/.git/branches/branch2 &&
d4785cd1
JS
933 (
934 cd testrepo &&
18afe101 935 git fetch branch2 &&
848575d8
JN
936 echo "$the_first_commit commit refs/heads/branch2" >expect &&
937 git for-each-ref refs/heads >actual &&
938 test_cmp expect actual
18afe101 939 ) &&
bc925ce3 940 git checkout main
18afe101
MK
941'
942
943test_expect_success 'push with branches' '
2e433b78 944 mk_empty testrepo &&
18afe101
MK
945 git checkout second &&
946 echo "testrepo" > .git/branches/branch1 &&
947 git push branch1 &&
d4785cd1
JS
948 (
949 cd testrepo &&
bc925ce3 950 echo "$the_first_commit commit refs/heads/main" >expect &&
848575d8
JN
951 git for-each-ref refs/heads >actual &&
952 test_cmp expect actual
18afe101
MK
953 )
954'
955
956test_expect_success 'push with branches containing #' '
2e433b78 957 mk_empty testrepo &&
18afe101
MK
958 echo "testrepo#branch3" > .git/branches/branch2 &&
959 git push branch2 &&
d4785cd1
JS
960 (
961 cd testrepo &&
848575d8
JN
962 echo "$the_first_commit commit refs/heads/branch3" >expect &&
963 git for-each-ref refs/heads >actual &&
964 test_cmp expect actual
18afe101 965 ) &&
bc925ce3 966 git checkout main
18afe101
MK
967'
968
da3efdb1 969test_expect_success 'push into aliased refs (consistent)' '
bc925ce3 970 mk_test testrepo heads/main &&
2e433b78
JK
971 mk_child testrepo child1 &&
972 mk_child testrepo child2 &&
da3efdb1
JS
973 (
974 cd child1 &&
975 git branch foo &&
51b85471 976 git symbolic-ref refs/heads/bar refs/heads/foo &&
da3efdb1
JS
977 git config receive.denyCurrentBranch false
978 ) &&
979 (
980 cd child2 &&
981 >path2 &&
982 git add path2 &&
983 test_tick &&
984 git commit -a -m child2 &&
985 git branch foo &&
986 git branch bar &&
987 git push ../child1 foo bar
988 )
989'
990
991test_expect_success 'push into aliased refs (inconsistent)' '
bc925ce3 992 mk_test testrepo heads/main &&
2e433b78
JK
993 mk_child testrepo child1 &&
994 mk_child testrepo child2 &&
da3efdb1
JS
995 (
996 cd child1 &&
997 git branch foo &&
51b85471 998 git symbolic-ref refs/heads/bar refs/heads/foo &&
da3efdb1
JS
999 git config receive.denyCurrentBranch false
1000 ) &&
1001 (
1002 cd child2 &&
1003 >path2 &&
1004 git add path2 &&
1005 test_tick &&
1006 git commit -a -m child2 &&
1007 git branch foo &&
1008 >path3 &&
1009 git add path3 &&
1010 test_tick &&
1011 git commit -a -m child2 &&
1012 git branch bar &&
1013 test_must_fail git push ../child1 foo bar 2>stderr &&
1014 grep "refusing inconsistent update" stderr
1015 )
1016'
1017
380efb65
ÆAB
1018test_force_push_tag () {
1019 tag_type_description=$1
1020 tag_args=$2
1021
f08fb8df 1022 test_expect_success "force pushing required to update $tag_type_description" "
bc925ce3 1023 mk_test testrepo heads/main &&
380efb65
ÆAB
1024 mk_child testrepo child1 &&
1025 mk_child testrepo child2 &&
1026 (
1027 cd child1 &&
1028 git tag testTag &&
1029 git push ../child2 testTag &&
1030 >file1 &&
1031 git add file1 &&
1032 git commit -m 'file1' &&
1033 git tag $tag_args testTag &&
1034 test_must_fail git push ../child2 testTag &&
1035 git push --force ../child2 testTag &&
1036 git tag $tag_args testTag HEAD~ &&
1037 test_must_fail git push ../child2 testTag &&
1038 git push --force ../child2 testTag &&
1039
1040 # Clobbering without + in refspec needs --force
1041 git tag -f testTag &&
1042 test_must_fail git push ../child2 'refs/tags/*:refs/tags/*' &&
1043 git push --force ../child2 'refs/tags/*:refs/tags/*' &&
1044
1045 # Clobbering with + in refspec does not need --force
1046 git tag -f testTag HEAD~ &&
1047 git push ../child2 '+refs/tags/*:refs/tags/*' &&
1048
1049 # Clobbering with --no-force still obeys + in refspec
1050 git tag -f testTag &&
1051 git push --no-force ../child2 '+refs/tags/*:refs/tags/*' &&
1052
1053 # Clobbering with/without --force and 'tag <name>' format
1054 git tag -f testTag HEAD~ &&
1055 test_must_fail git push ../child2 tag testTag &&
1056 git push --force ../child2 tag testTag
1057 )
1058 "
1059}
1060
1061test_force_push_tag "lightweight tag" "-f"
253b3d4f 1062test_force_push_tag "annotated tag" "-f -a -m'tag message'"
dbfeddb1 1063
6b0b0677
ÆAB
1064test_force_fetch_tag () {
1065 tag_type_description=$1
1066 tag_args=$2
1067
0bc8d71b 1068 test_expect_success "fetch will not clobber an existing $tag_type_description without --force" "
bc925ce3 1069 mk_test testrepo heads/main &&
6b0b0677
ÆAB
1070 mk_child testrepo child1 &&
1071 mk_child testrepo child2 &&
1072 (
1073 cd testrepo &&
1074 git tag testTag &&
1075 git -C ../child1 fetch origin tag testTag &&
1076 >file1 &&
1077 git add file1 &&
1078 git commit -m 'file1' &&
1079 git tag $tag_args testTag &&
0bc8d71b
ÆAB
1080 test_must_fail git -C ../child1 fetch origin tag testTag &&
1081 git -C ../child1 fetch origin '+refs/tags/*:refs/tags/*'
6b0b0677
ÆAB
1082 )
1083 "
1084}
1085
1086test_force_fetch_tag "lightweight tag" "-f"
1087test_force_fetch_tag "annotated tag" "-f -a -m'tag message'"
dbfeddb1 1088
fbe4f447 1089test_expect_success 'push --porcelain' '
2e433b78 1090 mk_empty testrepo &&
fbe4f447 1091 echo >.git/foo "To testrepo" &&
bc925ce3 1092 echo >>.git/foo "* refs/heads/main:refs/remotes/origin/main [new reference]" &&
fbe4f447 1093 echo >>.git/foo "Done" &&
bc925ce3 1094 git push >.git/bar --porcelain testrepo refs/heads/main:refs/remotes/origin/main &&
fbe4f447
LA
1095 (
1096 cd testrepo &&
bc925ce3 1097 echo "$the_commit commit refs/remotes/origin/main" >expect &&
848575d8
JN
1098 git for-each-ref refs/remotes/origin >actual &&
1099 test_cmp expect actual
fbe4f447 1100 ) &&
c296134d 1101 test_cmp .git/foo .git/bar
fbe4f447
LA
1102'
1103
1104test_expect_success 'push --porcelain bad url' '
2e433b78 1105 mk_empty testrepo &&
bc925ce3 1106 test_must_fail git push >.git/bar --porcelain asdfasdfasd refs/heads/main:refs/remotes/origin/main &&
c7cf9566 1107 ! grep -q Done .git/bar
fbe4f447
LA
1108'
1109
1110test_expect_success 'push --porcelain rejected' '
2e433b78 1111 mk_empty testrepo &&
bc925ce3 1112 git push testrepo refs/heads/main:refs/remotes/origin/main &&
fbe4f447 1113 (cd testrepo &&
bc925ce3 1114 git reset --hard origin/main^ &&
fbe4f447
LA
1115 git config receive.denyCurrentBranch true) &&
1116
1117 echo >.git/foo "To testrepo" &&
bc925ce3 1118 echo >>.git/foo "! refs/heads/main:refs/heads/main [remote rejected] (branch is currently checked out)" &&
7dcbeaa0 1119 echo >>.git/foo "Done" &&
fbe4f447 1120
bc925ce3 1121 test_must_fail git push >.git/bar --porcelain testrepo refs/heads/main:refs/heads/main &&
c296134d 1122 test_cmp .git/foo .git/bar
fbe4f447
LA
1123'
1124
1125test_expect_success 'push --porcelain --dry-run rejected' '
2e433b78 1126 mk_empty testrepo &&
bc925ce3 1127 git push testrepo refs/heads/main:refs/remotes/origin/main &&
fbe4f447 1128 (cd testrepo &&
bc925ce3 1129 git reset --hard origin/main &&
fbe4f447
LA
1130 git config receive.denyCurrentBranch true) &&
1131
1132 echo >.git/foo "To testrepo" &&
bc925ce3 1133 echo >>.git/foo "! refs/heads/main^:refs/heads/main [rejected] (non-fast-forward)" &&
fbe4f447
LA
1134 echo >>.git/foo "Done" &&
1135
bc925ce3 1136 test_must_fail git push >.git/bar --porcelain --dry-run testrepo refs/heads/main^:refs/heads/main &&
c296134d 1137 test_cmp .git/foo .git/bar
fbe4f447
LA
1138'
1139
6ddba5e2 1140test_expect_success 'push --prune' '
bc925ce3 1141 mk_test testrepo heads/main heads/second heads/foo heads/bar &&
0a42ac03 1142 git push --prune testrepo : &&
bc925ce3 1143 check_push_result testrepo $the_commit heads/main &&
2e433b78
JK
1144 check_push_result testrepo $the_first_commit heads/second &&
1145 ! check_push_result testrepo $the_first_commit heads/foo heads/bar
6ddba5e2
FC
1146'
1147
1148test_expect_success 'push --prune refspec' '
bc925ce3 1149 mk_test testrepo tmp/main tmp/second tmp/foo tmp/bar &&
6ddba5e2 1150 git push --prune testrepo "refs/heads/*:refs/tmp/*" &&
bc925ce3 1151 check_push_result testrepo $the_commit tmp/main &&
2e433b78
JK
1152 check_push_result testrepo $the_first_commit tmp/second &&
1153 ! check_push_result testrepo $the_first_commit tmp/foo tmp/bar
6ddba5e2
FC
1154'
1155
daebaa78
JH
1156for configsection in transfer receive
1157do
1158 test_expect_success "push to update a ref hidden by $configsection.hiderefs" '
bc925ce3 1159 mk_test testrepo heads/main hidden/one hidden/two hidden/three &&
daebaa78
JH
1160 (
1161 cd testrepo &&
1162 git config $configsection.hiderefs refs/hidden
1163 ) &&
1164
1165 # push to unhidden ref succeeds normally
bc925ce3
JS
1166 git push testrepo main:refs/heads/main &&
1167 check_push_result testrepo $the_commit heads/main &&
daebaa78
JH
1168
1169 # push to update a hidden ref should fail
bc925ce3 1170 test_must_fail git push testrepo main:refs/hidden/one &&
2e433b78 1171 check_push_result testrepo $the_first_commit hidden/one &&
daebaa78
JH
1172
1173 # push to delete a hidden ref should fail
1174 test_must_fail git push testrepo :refs/hidden/two &&
2e433b78 1175 check_push_result testrepo $the_first_commit hidden/two &&
daebaa78
JH
1176
1177 # idempotent push to update a hidden ref should fail
1178 test_must_fail git push testrepo $the_first_commit:refs/hidden/three &&
2e433b78 1179 check_push_result testrepo $the_first_commit hidden/three
daebaa78
JH
1180 '
1181done
1182
6e7b66ee 1183test_expect_success 'fetch exact SHA1' '
bc925ce3
JS
1184 mk_test testrepo heads/main hidden/one &&
1185 git push testrepo main:refs/hidden/one &&
6e7b66ee
JH
1186 (
1187 cd testrepo &&
1188 git config transfer.hiderefs refs/hidden
1189 ) &&
2e433b78 1190 check_push_result testrepo $the_commit hidden/one &&
6e7b66ee 1191
2e433b78 1192 mk_child testrepo child &&
6e7b66ee
JH
1193 (
1194 cd child &&
1195
1196 # make sure $the_commit does not exist here
1197 git repack -a -d &&
1198 git prune &&
1199 test_must_fail git cat-file -t $the_commit &&
1200
ab0c5f50
JT
1201 # Some protocol versions (e.g. 2) support fetching
1202 # unadvertised objects, so restrict this test to v0.
1203
6e7b66ee 1204 # fetching the hidden object should fail by default
8a1b0978 1205 test_must_fail env GIT_TEST_PROTOCOL_VERSION=0 \
ab0c5f50 1206 git fetch -v ../testrepo $the_commit:refs/heads/copy 2>err &&
d56583de 1207 test_i18ngrep "Server does not allow request for unadvertised object" err &&
6e7b66ee
JH
1208 test_must_fail git rev-parse --verify refs/heads/copy &&
1209
1210 # the server side can allow it to succeed
1211 (
1212 cd ../testrepo &&
1213 git config uploadpack.allowtipsha1inwant true
1214 ) &&
1215
bc925ce3 1216 git fetch -v ../testrepo $the_commit:refs/heads/copy main:refs/heads/extra &&
c3c17bf1
JK
1217 cat >expect <<-EOF &&
1218 $the_commit
1219 $the_first_commit
1220 EOF
1221 {
1222 git rev-parse --verify refs/heads/copy &&
1223 git rev-parse --verify refs/heads/extra
1224 } >actual &&
1225 test_cmp expect actual
6e7b66ee
JH
1226 )
1227'
1228
6c301adb 1229test_expect_success 'fetch exact SHA1 in protocol v2' '
bc925ce3
JS
1230 mk_test testrepo heads/main hidden/one &&
1231 git push testrepo main:refs/hidden/one &&
6c301adb
JN
1232 git -C testrepo config transfer.hiderefs refs/hidden &&
1233 check_push_result testrepo $the_commit hidden/one &&
1234
1235 mk_child testrepo child &&
1236 git -C child config protocol.version 2 &&
1237
1238 # make sure $the_commit does not exist here
1239 git -C child repack -a -d &&
1240 git -C child prune &&
1241 test_must_fail git -C child cat-file -t $the_commit &&
1242
1243 # fetching the hidden object succeeds by default
1244 # NEEDSWORK: should this match the v0 behavior instead?
1245 git -C child fetch -v ../testrepo $the_commit:refs/heads/copy
1246'
1247
68ee6289
FM
1248for configallowtipsha1inwant in true false
1249do
1250 test_expect_success "shallow fetch reachable SHA1 (but not a ref), allowtipsha1inwant=$configallowtipsha1inwant" '
1251 mk_empty testrepo &&
1252 (
1253 cd testrepo &&
1254 git config uploadpack.allowtipsha1inwant $configallowtipsha1inwant &&
1255 git commit --allow-empty -m foo &&
1256 git commit --allow-empty -m bar
1257 ) &&
1258 SHA1=$(git --git-dir=testrepo/.git rev-parse HEAD^) &&
1259 mk_empty shallow &&
1260 (
1261 cd shallow &&
ab0c5f50
JT
1262 # Some protocol versions (e.g. 2) support fetching
1263 # unadvertised objects, so restrict this test to v0.
8a1b0978 1264 test_must_fail env GIT_TEST_PROTOCOL_VERSION=0 \
ab0c5f50 1265 git fetch --depth=1 ../testrepo/.git $SHA1 &&
68ee6289
FM
1266 git --git-dir=../testrepo/.git config uploadpack.allowreachablesha1inwant true &&
1267 git fetch --depth=1 ../testrepo/.git $SHA1 &&
1268 git cat-file commit $SHA1
1269 )
1270 '
1271
1272 test_expect_success "deny fetch unreachable SHA1, allowtipsha1inwant=$configallowtipsha1inwant" '
1273 mk_empty testrepo &&
1274 (
1275 cd testrepo &&
1276 git config uploadpack.allowtipsha1inwant $configallowtipsha1inwant &&
1277 git commit --allow-empty -m foo &&
1278 git commit --allow-empty -m bar &&
1279 git commit --allow-empty -m xyz
1280 ) &&
1281 SHA1_1=$(git --git-dir=testrepo/.git rev-parse HEAD^^) &&
1282 SHA1_2=$(git --git-dir=testrepo/.git rev-parse HEAD^) &&
1283 SHA1_3=$(git --git-dir=testrepo/.git rev-parse HEAD) &&
1284 (
1285 cd testrepo &&
1286 git reset --hard $SHA1_2 &&
1287 git cat-file commit $SHA1_1 &&
1288 git cat-file commit $SHA1_3
1289 ) &&
1290 mk_empty shallow &&
1291 (
1292 cd shallow &&
ab0c5f50
JT
1293 # Some protocol versions (e.g. 2) support fetching
1294 # unadvertised objects, so restrict this test to v0.
8a1b0978 1295 test_must_fail env GIT_TEST_PROTOCOL_VERSION=0 \
ab0c5f50 1296 git fetch ../testrepo/.git $SHA1_3 &&
8a1b0978 1297 test_must_fail env GIT_TEST_PROTOCOL_VERSION=0 \
ab0c5f50 1298 git fetch ../testrepo/.git $SHA1_1 &&
68ee6289
FM
1299 git --git-dir=../testrepo/.git config uploadpack.allowreachablesha1inwant true &&
1300 git fetch ../testrepo/.git $SHA1_1 &&
1301 git cat-file commit $SHA1_1 &&
1302 test_must_fail git cat-file commit $SHA1_2 &&
1303 git fetch ../testrepo/.git $SHA1_2 &&
1304 git cat-file commit $SHA1_2 &&
8a1b0978 1305 test_must_fail env GIT_TEST_PROTOCOL_VERSION=0 \
57a6b932 1306 git fetch ../testrepo/.git $SHA1_3 2>err &&
acaabcf3
JK
1307 # ideally we would insist this be on a "remote error:"
1308 # line, but it is racy; see the commit message
1309 test_i18ngrep "not our ref.*$SHA1_3\$" err
68ee6289
FM
1310 )
1311 '
1312done
1313
c2aba155 1314test_expect_success 'fetch follows tags by default' '
bc925ce3 1315 mk_test testrepo heads/main &&
f818f7f7 1316 test_when_finished "rm -rf src" &&
c2aba155
JH
1317 git init src &&
1318 (
1319 cd src &&
bc925ce3 1320 git pull ../testrepo main &&
c2aba155
JH
1321 git tag -m "annotated" tag &&
1322 git for-each-ref >tmp1 &&
e57ea501 1323 sed -n "p; s|refs/heads/main$|refs/remotes/origin/main|p" tmp1 |
c2aba155
JH
1324 sort -k 3 >../expect
1325 ) &&
f818f7f7 1326 test_when_finished "rm -rf dst" &&
c2aba155
JH
1327 git init dst &&
1328 (
1329 cd dst &&
1330 git remote add origin ../src &&
bc925ce3
JS
1331 git config branch.main.remote origin &&
1332 git config branch.main.merge refs/heads/main &&
c2aba155
JH
1333 git pull &&
1334 git for-each-ref >../actual
1335 ) &&
1336 test_cmp expect actual
1337'
1338
34066f06
JK
1339test_expect_success 'peeled advertisements are not considered ref tips' '
1340 mk_empty testrepo &&
1341 git -C testrepo commit --allow-empty -m one &&
1342 git -C testrepo commit --allow-empty -m two &&
1343 git -C testrepo tag -m foo mytag HEAD^ &&
1344 oid=$(git -C testrepo rev-parse mytag^{commit}) &&
8a1b0978 1345 test_must_fail env GIT_TEST_PROTOCOL_VERSION=0 \
34066f06
JK
1346 git fetch testrepo $oid 2>err &&
1347 test_i18ngrep "Server does not allow request for unadvertised object" err
1348'
1349
ca02465b 1350test_expect_success 'pushing a specific ref applies remote.$name.push as refmap' '
bc925ce3 1351 mk_test testrepo heads/main &&
f818f7f7 1352 test_when_finished "rm -rf src" &&
ca02465b 1353 git init src &&
f818f7f7 1354 test_when_finished "rm -rf dst" &&
ca02465b
JH
1355 git init --bare dst &&
1356 (
1357 cd src &&
bc925ce3 1358 git pull ../testrepo main &&
ca02465b
JH
1359 git branch next &&
1360 git config remote.dst.url ../dst &&
1361 git config remote.dst.push "+refs/heads/*:refs/remotes/src/*" &&
bc925ce3
JS
1362 git push dst main &&
1363 git show-ref refs/heads/main |
ca02465b
JH
1364 sed -e "s|refs/heads/|refs/remotes/src/|" >../dst/expect
1365 ) &&
1366 (
1367 cd dst &&
1368 test_must_fail git show-ref refs/heads/next &&
bc925ce3
JS
1369 test_must_fail git show-ref refs/heads/main &&
1370 git show-ref refs/remotes/src/main >actual
ca02465b
JH
1371 ) &&
1372 test_cmp dst/expect dst/actual
1373'
1374
1375test_expect_success 'with no remote.$name.push, it is not used as refmap' '
bc925ce3 1376 mk_test testrepo heads/main &&
f818f7f7 1377 test_when_finished "rm -rf src" &&
ca02465b 1378 git init src &&
f818f7f7 1379 test_when_finished "rm -rf dst" &&
ca02465b
JH
1380 git init --bare dst &&
1381 (
1382 cd src &&
bc925ce3 1383 git pull ../testrepo main &&
ca02465b
JH
1384 git branch next &&
1385 git config remote.dst.url ../dst &&
fc9261ca 1386 git config push.default matching &&
bc925ce3
JS
1387 git push dst main &&
1388 git show-ref refs/heads/main >../dst/expect
ca02465b
JH
1389 ) &&
1390 (
1391 cd dst &&
1392 test_must_fail git show-ref refs/heads/next &&
bc925ce3 1393 git show-ref refs/heads/main >actual
ca02465b
JH
1394 ) &&
1395 test_cmp dst/expect dst/actual
1396'
1397
fc9261ca 1398test_expect_success 'with no remote.$name.push, upstream mapping is used' '
bc925ce3 1399 mk_test testrepo heads/main &&
f818f7f7 1400 test_when_finished "rm -rf src" &&
fc9261ca 1401 git init src &&
f818f7f7 1402 test_when_finished "rm -rf dst" &&
fc9261ca
JH
1403 git init --bare dst &&
1404 (
1405 cd src &&
bc925ce3 1406 git pull ../testrepo main &&
fc9261ca
JH
1407 git branch next &&
1408 git config remote.dst.url ../dst &&
1409 git config remote.dst.fetch "+refs/heads/*:refs/remotes/dst/*" &&
1410 git config push.default upstream &&
1411
bc925ce3
JS
1412 git config branch.main.merge refs/heads/trunk &&
1413 git config branch.main.remote dst &&
fc9261ca 1414
bc925ce3
JS
1415 git push dst main &&
1416 git show-ref refs/heads/main |
1417 sed -e "s|refs/heads/main|refs/heads/trunk|" >../dst/expect
fc9261ca
JH
1418 ) &&
1419 (
1420 cd dst &&
bc925ce3 1421 test_must_fail git show-ref refs/heads/main &&
fc9261ca
JH
1422 test_must_fail git show-ref refs/heads/next &&
1423 git show-ref refs/heads/trunk >actual
1424 ) &&
1425 test_cmp dst/expect dst/actual
1426'
1427
c2aba155 1428test_expect_success 'push does not follow tags by default' '
bc925ce3 1429 mk_test testrepo heads/main &&
f818f7f7 1430 test_when_finished "rm -rf src" &&
c2aba155 1431 git init src &&
f818f7f7 1432 test_when_finished "rm -rf dst" &&
c2aba155
JH
1433 git init --bare dst &&
1434 (
1435 cd src &&
bc925ce3 1436 git pull ../testrepo main &&
c2aba155
JH
1437 git tag -m "annotated" tag &&
1438 git checkout -b another &&
1439 git commit --allow-empty -m "future commit" &&
1440 git tag -m "future" future &&
bc925ce3
JS
1441 git checkout main &&
1442 git for-each-ref refs/heads/main >../expect &&
1443 git push ../dst main
c2aba155
JH
1444 ) &&
1445 (
1446 cd dst &&
1447 git for-each-ref >../actual
1448 ) &&
1449 test_cmp expect actual
1450'
1451
f6188dcc 1452test_expect_success 'push --follow-tags only pushes relevant tags' '
bc925ce3 1453 mk_test testrepo heads/main &&
f818f7f7 1454 test_when_finished "rm -rf src" &&
c2aba155 1455 git init src &&
f818f7f7 1456 test_when_finished "rm -rf dst" &&
c2aba155
JH
1457 git init --bare dst &&
1458 (
1459 cd src &&
bc925ce3 1460 git pull ../testrepo main &&
c2aba155
JH
1461 git tag -m "annotated" tag &&
1462 git checkout -b another &&
1463 git commit --allow-empty -m "future commit" &&
1464 git tag -m "future" future &&
bc925ce3
JS
1465 git checkout main &&
1466 git for-each-ref refs/heads/main refs/tags/tag >../expect &&
1467 git push --follow-tags ../dst main
c2aba155
JH
1468 ) &&
1469 (
1470 cd dst &&
1471 git for-each-ref >../actual
1472 ) &&
1473 test_cmp expect actual
1474'
1475
f7c815c3
NTND
1476test_expect_success 'push --no-thin must produce non-thin pack' '
1477 cat >>path1 <<\EOF &&
1478keep base version of path1 big enough, compared to the new changes
1479later, in order to pass size heuristics in
1480builtin/pack-objects.c:try_delta()
1481EOF
1482 git commit -am initial &&
1483 git init no-thin &&
1484 git --git-dir=no-thin/.git config receive.unpacklimit 0 &&
bc925ce3 1485 git push no-thin/.git refs/heads/main:refs/heads/foo &&
f7c815c3
NTND
1486 echo modified >> path1 &&
1487 git commit -am modified &&
1488 git repack -adf &&
1489 rcvpck="git receive-pack --reject-thin-pack-for-testing" &&
bc925ce3 1490 git push --no-thin --receive-pack="$rcvpck" no-thin/.git refs/heads/main:refs/heads/foo
f7c815c3
NTND
1491'
1492
458a7e50 1493test_expect_success 'pushing a tag pushes the tagged object' '
458a7e50
JK
1494 blob=$(echo unreferenced | git hash-object -w --stdin) &&
1495 git tag -m foo tag-of-blob $blob &&
f818f7f7 1496 test_when_finished "rm -rf dst.git" &&
458a7e50
JK
1497 git init --bare dst.git &&
1498 git push dst.git tag-of-blob &&
1499 # the receiving index-pack should have noticed
1500 # any problems, but we double check
1501 echo unreferenced >expect &&
1502 git --git-dir=dst.git cat-file blob tag-of-blob >actual &&
1503 test_cmp expect actual
1504'
1505
72549dfd 1506test_expect_success 'push into bare respects core.logallrefupdates' '
f818f7f7 1507 test_when_finished "rm -rf dst.git" &&
72549dfd
JK
1508 git init --bare dst.git &&
1509 git -C dst.git config core.logallrefupdates true &&
1510
1511 # double push to test both with and without
1512 # the actual pack transfer
bc925ce3 1513 git push dst.git main:one &&
72549dfd
JK
1514 echo "one@{0} push" >expect &&
1515 git -C dst.git log -g --format="%gd %gs" one >actual &&
1516 test_cmp expect actual &&
1517
bc925ce3 1518 git push dst.git main:two &&
72549dfd
JK
1519 echo "two@{0} push" >expect &&
1520 git -C dst.git log -g --format="%gd %gs" two >actual &&
1521 test_cmp expect actual
1522'
1523
1524test_expect_success 'fetch into bare respects core.logallrefupdates' '
f818f7f7 1525 test_when_finished "rm -rf dst.git" &&
72549dfd
JK
1526 git init --bare dst.git &&
1527 (
1528 cd dst.git &&
1529 git config core.logallrefupdates true &&
1530
1531 # as above, we double-fetch to test both
1532 # with and without pack transfer
bc925ce3
JS
1533 git fetch .. main:one &&
1534 echo "one@{0} fetch .. main:one: storing head" >expect &&
72549dfd
JK
1535 git log -g --format="%gd %gs" one >actual &&
1536 test_cmp expect actual &&
1537
bc925ce3
JS
1538 git fetch .. main:two &&
1539 echo "two@{0} fetch .. main:two: storing head" >expect &&
72549dfd
JK
1540 git log -g --format="%gd %gs" two >actual &&
1541 test_cmp expect actual
1542 )
1543'
1544
1404bcbb 1545test_expect_success 'receive.denyCurrentBranch = updateInstead' '
f818f7f7 1546 mk_empty testrepo &&
bc925ce3 1547 git push testrepo main &&
4d7a5cea
JH
1548 (
1549 cd testrepo &&
1404bcbb
JS
1550 git reset --hard &&
1551 git config receive.denyCurrentBranch updateInstead
1552 ) &&
1553 test_commit third path2 &&
4d7a5cea
JH
1554
1555 # Try pushing into a repository with pristine working tree
bc925ce3 1556 git push testrepo main &&
4d7a5cea
JH
1557 (
1558 cd testrepo &&
1559 git update-index -q --refresh &&
1560 git diff-files --quiet -- &&
1561 git diff-index --quiet --cached HEAD -- &&
1562 test third = "$(cat path2)" &&
1563 test $(git -C .. rev-parse HEAD) = $(git rev-parse HEAD)
1564 ) &&
1565
1566 # Try pushing into a repository with working tree needing a refresh
1567 (
1568 cd testrepo &&
1569 git reset --hard HEAD^ &&
1570 test $(git -C .. rev-parse HEAD^) = $(git rev-parse HEAD) &&
0e496492 1571 test-tool chmtime +100 path1
4d7a5cea 1572 ) &&
bc925ce3 1573 git push testrepo main &&
4d7a5cea
JH
1574 (
1575 cd testrepo &&
1404bcbb
JS
1576 git update-index -q --refresh &&
1577 git diff-files --quiet -- &&
1578 git diff-index --quiet --cached HEAD -- &&
4d7a5cea
JH
1579 test_cmp ../path1 path1 &&
1580 test third = "$(cat path2)" &&
1581 test $(git -C .. rev-parse HEAD) = $(git rev-parse HEAD)
1404bcbb 1582 ) &&
4d7a5cea
JH
1583
1584 # Update what is to be pushed
1404bcbb 1585 test_commit fourth path2 &&
4d7a5cea
JH
1586
1587 # Try pushing into a repository with a dirty working tree
1588 # (1) the working tree updated
1589 (
1590 cd testrepo &&
1591 echo changed >path1
1592 ) &&
bc925ce3 1593 test_must_fail git push testrepo main &&
4d7a5cea
JH
1594 (
1595 cd testrepo &&
1596 test $(git -C .. rev-parse HEAD^) = $(git rev-parse HEAD) &&
1597 git diff --quiet --cached &&
1598 test changed = "$(cat path1)"
1599 ) &&
1600
1601 # (2) the index updated
1602 (
1603 cd testrepo &&
1604 echo changed >path1 &&
1605 git add path1
1606 ) &&
bc925ce3 1607 test_must_fail git push testrepo main &&
4d7a5cea
JH
1608 (
1609 cd testrepo &&
1610 test $(git -C .. rev-parse HEAD^) = $(git rev-parse HEAD) &&
1611 git diff --quiet &&
1612 test changed = "$(cat path1)"
1613 ) &&
1614
1615 # Introduce a new file in the update
1616 test_commit fifth path3 &&
1617
1618 # (3) the working tree has an untracked file that would interfere
1619 (
1620 cd testrepo &&
1621 git reset --hard &&
1622 echo changed >path3
1623 ) &&
bc925ce3 1624 test_must_fail git push testrepo main &&
4d7a5cea
JH
1625 (
1626 cd testrepo &&
1627 test $(git -C .. rev-parse HEAD^^) = $(git rev-parse HEAD) &&
1628 git diff --quiet &&
1629 git diff --quiet --cached &&
1630 test changed = "$(cat path3)"
1631 ) &&
1632
1633 # (4) the target changes to what gets pushed but it still is a change
1634 (
1635 cd testrepo &&
1636 git reset --hard &&
1637 echo fifth >path3 &&
1638 git add path3
1639 ) &&
bc925ce3 1640 test_must_fail git push testrepo main &&
4d7a5cea
JH
1641 (
1642 cd testrepo &&
1643 test $(git -C .. rev-parse HEAD^^) = $(git rev-parse HEAD) &&
1404bcbb 1644 git diff --quiet &&
4d7a5cea 1645 test fifth = "$(cat path3)"
1a51b524 1646 ) &&
4d7a5cea 1647
1a51b524 1648 # (5) push into void
f818f7f7 1649 test_when_finished "rm -rf void" &&
1a51b524
JH
1650 git init void &&
1651 (
1652 cd void &&
1653 git config receive.denyCurrentBranch updateInstead
1654 ) &&
bc925ce3 1655 git push void main &&
1a51b524
JH
1656 (
1657 cd void &&
bc925ce3 1658 test $(git -C .. rev-parse main) = $(git rev-parse HEAD) &&
1a51b524
JH
1659 git diff --quiet &&
1660 git diff --cached --quiet
b072a25f
JH
1661 ) &&
1662
1663 # (6) updateInstead intervened by fast-forward check
bc925ce3
JS
1664 test_must_fail git push void main^:main &&
1665 test $(git -C void rev-parse HEAD) = $(git rev-parse main) &&
b072a25f
JH
1666 git -C void diff --quiet &&
1667 git -C void diff --cached --quiet
1404bcbb
JS
1668'
1669
08553319 1670test_expect_success 'updateInstead with push-to-checkout hook' '
f818f7f7 1671 test_when_finished "rm -rf testrepo" &&
08553319 1672 git init testrepo &&
f818f7f7
ÆAB
1673 git -C testrepo pull .. main &&
1674 git -C testrepo reset --hard HEAD^^ &&
1675 git -C testrepo tag initial &&
1676 git -C testrepo config receive.denyCurrentBranch updateInstead &&
1677 test_hook -C testrepo push-to-checkout <<-\EOF &&
1678 echo >&2 updating from $(git rev-parse HEAD)
1679 echo >&2 updating to "$1"
1680
1681 git update-index -q --refresh &&
1682 git read-tree -u -m HEAD "$1" || {
1683 status=$?
1684 echo >&2 read-tree failed
1685 exit $status
1686 }
1687 EOF
08553319
JH
1688
1689 # Try pushing into a pristine
bc925ce3 1690 git push testrepo main &&
08553319
JH
1691 (
1692 cd testrepo &&
1693 git diff --quiet &&
1694 git diff HEAD --quiet &&
1695 test $(git -C .. rev-parse HEAD) = $(git rev-parse HEAD)
1696 ) &&
1697
1698 # Try pushing into a repository with conflicting change
1699 (
1700 cd testrepo &&
1701 git reset --hard initial &&
1702 echo conflicting >path2
1703 ) &&
bc925ce3 1704 test_must_fail git push testrepo main &&
08553319
JH
1705 (
1706 cd testrepo &&
1707 test $(git rev-parse initial) = $(git rev-parse HEAD) &&
1708 test conflicting = "$(cat path2)" &&
1709 git diff-index --quiet --cached HEAD
1710 ) &&
1711
1712 # Try pushing into a repository with unrelated change
1713 (
1714 cd testrepo &&
1715 git reset --hard initial &&
1716 echo unrelated >path1 &&
1717 echo irrelevant >path5 &&
1718 git add path5
1719 ) &&
bc925ce3 1720 git push testrepo main &&
08553319
JH
1721 (
1722 cd testrepo &&
1723 test "$(cat path1)" = unrelated &&
1724 test "$(cat path5)" = irrelevant &&
1725 test "$(git diff --name-only --cached HEAD)" = path5 &&
1726 test $(git -C .. rev-parse HEAD) = $(git rev-parse HEAD)
1a51b524
JH
1727 ) &&
1728
1729 # push into void
f818f7f7 1730 test_when_finished "rm -rf void" &&
1a51b524 1731 git init void &&
f818f7f7
ÆAB
1732 git -C void config receive.denyCurrentBranch updateInstead &&
1733 test_hook -C void push-to-checkout <<-\EOF &&
1734 if git rev-parse --quiet --verify HEAD
1735 then
1736 has_head=yes
1737 echo >&2 updating from $(git rev-parse HEAD)
1738 else
1739 has_head=no
1740 echo >&2 pushing into void
1741 fi
1742 echo >&2 updating to "$1"
1743
1744 git update-index -q --refresh &&
1745 case "$has_head" in
1746 yes)
1747 git read-tree -u -m HEAD "$1" ;;
1748 no)
1749 git read-tree -u -m "$1" ;;
1750 esac || {
1751 status=$?
1752 echo >&2 read-tree failed
1753 exit $status
1754 }
1755 EOF
1a51b524 1756
bc925ce3 1757 git push void main &&
1a51b524
JH
1758 (
1759 cd void &&
1760 git diff --quiet &&
1761 git diff --cached --quiet &&
1762 test $(git -C .. rev-parse HEAD) = $(git rev-parse HEAD)
08553319
JH
1763 )
1764'
1765
4ef34648
HV
1766test_expect_success 'denyCurrentBranch and worktrees' '
1767 git worktree add new-wt &&
1768 git clone . cloned &&
1769 test_commit -C cloned first &&
1770 test_config receive.denyCurrentBranch refuse &&
1771 test_must_fail git -C cloned push origin HEAD:new-wt &&
1772 test_config receive.denyCurrentBranch updateInstead &&
1773 git -C cloned push origin HEAD:new-wt &&
9fdf4f1d 1774 test_path_exists new-wt/first.t &&
4ef34648
HV
1775 test_must_fail git -C cloned push --delete origin new-wt
1776'
8bc1f39f 1777
9fdf4f1d
AK
1778test_expect_success 'denyCurrentBranch and bare repository worktrees' '
1779 test_when_finished "rm -fr bare.git" &&
1780 git clone --bare . bare.git &&
1781 git -C bare.git worktree add wt &&
1782 test_commit grape &&
1783 git -C bare.git config receive.denyCurrentBranch refuse &&
1784 test_must_fail git push bare.git HEAD:wt &&
1785 git -C bare.git config receive.denyCurrentBranch updateInstead &&
1786 git push bare.git HEAD:wt &&
1787 test_path_exists bare.git/wt/grape.t &&
1788 test_must_fail git push --delete bare.git wt
1789'
1790
8bc1f39f
AK
1791test_expect_success 'refuse fetch to current branch of worktree' '
1792 test_when_finished "git worktree remove --force wt && git branch -D wt" &&
1793 git worktree add wt &&
1794 test_commit apple &&
1795 test_must_fail git fetch . HEAD:wt &&
1796 git fetch -u . HEAD:wt
1797'
1798
1799test_expect_success 'refuse fetch to current branch of bare repository worktree' '
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 banana &&
1804 test_must_fail git -C bare.git fetch .. HEAD:wt &&
1805 git -C bare.git fetch -u .. HEAD:wt
1806'
1807
54077640
CB
1808test_expect_success 'refuse to push a hidden ref, and make sure do not pollute the repository' '
1809 mk_empty testrepo &&
1810 git -C testrepo config receive.hiderefs refs/hidden &&
1811 git -C testrepo config receive.unpackLimit 1 &&
1812 test_must_fail git push testrepo HEAD:refs/hidden/foo &&
1813 test_dir_is_empty testrepo/.git/objects/pack
1814'
1815
bcdb34f7 1816test_done