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