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