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