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