@@ -40,6 +40,40 @@ mk_test () {
40
40
)
41
41
}
42
42
43
+ mk_test_with_hooks () {
44
+ mk_test " $@ " &&
45
+ (
46
+ cd testrepo &&
47
+ mkdir .git/hooks &&
48
+ cd .git/hooks &&
49
+
50
+ cat > pre-receive << -'EOF ' &&
51
+ #!/bin/sh
52
+ cat - >>pre-receive.actual
53
+ EOF
54
+
55
+ cat > update << -'EOF ' &&
56
+ #!/bin/sh
57
+ printf "%s %s %s\n" "$@" >>update.actual
58
+ EOF
59
+
60
+ cat > post-receive << -'EOF ' &&
61
+ #!/bin/sh
62
+ cat - >>post-receive.actual
63
+ EOF
64
+
65
+ cat > post-update << -'EOF ' &&
66
+ #!/bin/sh
67
+ for ref in "$@"
68
+ do
69
+ printf "%s\n" "$ref" >>post-update.actual
70
+ done
71
+ EOF
72
+
73
+ chmod +x pre-receive update post-receive post-update
74
+ )
75
+ }
76
+
43
77
mk_child () {
44
78
rm -rf " $1 " &&
45
79
git clone testrepo " $1 "
@@ -559,6 +593,169 @@ test_expect_success 'allow deleting an invalid remote ref' '
559
593
560
594
'
561
595
596
+ test_expect_success ' pushing valid refs triggers post-receive and post-update hooks' '
597
+ mk_test_with_hooks heads/master heads/next &&
598
+ orgmaster=$(cd testrepo && git show-ref -s --verify refs/heads/master) &&
599
+ newmaster=$(git show-ref -s --verify refs/heads/master) &&
600
+ orgnext=$(cd testrepo && git show-ref -s --verify refs/heads/next) &&
601
+ newnext=$_z40 &&
602
+ git push testrepo refs/heads/master:refs/heads/master :refs/heads/next &&
603
+ (
604
+ cd testrepo/.git &&
605
+ cat >pre-receive.expect <<-EOF &&
606
+ $orgmaster $newmaster refs/heads/master
607
+ $orgnext $newnext refs/heads/next
608
+ EOF
609
+
610
+ cat >update.expect <<-EOF &&
611
+ refs/heads/master $orgmaster $newmaster
612
+ refs/heads/next $orgnext $newnext
613
+ EOF
614
+
615
+ cat >post-receive.expect <<-EOF &&
616
+ $orgmaster $newmaster refs/heads/master
617
+ $orgnext $newnext refs/heads/next
618
+ EOF
619
+
620
+ cat >post-update.expect <<-EOF &&
621
+ refs/heads/master
622
+ refs/heads/next
623
+ EOF
624
+
625
+ test_cmp pre-receive.expect pre-receive.actual &&
626
+ test_cmp update.expect update.actual &&
627
+ test_cmp post-receive.expect post-receive.actual &&
628
+ test_cmp post-update.expect post-update.actual
629
+ )
630
+ '
631
+
632
+ test_expect_success ' deleting dangling ref triggers hooks with correct args' '
633
+ mk_test_with_hooks heads/master &&
634
+ rm -f testrepo/.git/objects/??/* &&
635
+ git push testrepo :refs/heads/master &&
636
+ (
637
+ cd testrepo/.git &&
638
+ cat >pre-receive.expect <<-EOF &&
639
+ $_z40 $_z40 refs/heads/master
640
+ EOF
641
+
642
+ cat >update.expect <<-EOF &&
643
+ refs/heads/master $_z40 $_z40
644
+ EOF
645
+
646
+ cat >post-receive.expect <<-EOF &&
647
+ $_z40 $_z40 refs/heads/master
648
+ EOF
649
+
650
+ cat >post-update.expect <<-EOF &&
651
+ refs/heads/master
652
+ EOF
653
+
654
+ test_cmp pre-receive.expect pre-receive.actual &&
655
+ test_cmp update.expect update.actual &&
656
+ test_cmp post-receive.expect post-receive.actual &&
657
+ test_cmp post-update.expect post-update.actual
658
+ )
659
+ '
660
+
661
+ test_expect_success ' deletion of a non-existent ref is not fed to post-receive and post-update hooks' '
662
+ mk_test_with_hooks heads/master &&
663
+ orgmaster=$(cd testrepo && git show-ref -s --verify refs/heads/master) &&
664
+ newmaster=$(git show-ref -s --verify refs/heads/master) &&
665
+ git push testrepo master :refs/heads/nonexistent &&
666
+ (
667
+ cd testrepo/.git &&
668
+ cat >pre-receive.expect <<-EOF &&
669
+ $orgmaster $newmaster refs/heads/master
670
+ $_z40 $_z40 refs/heads/nonexistent
671
+ EOF
672
+
673
+ cat >update.expect <<-EOF &&
674
+ refs/heads/master $orgmaster $newmaster
675
+ refs/heads/nonexistent $_z40 $_z40
676
+ EOF
677
+
678
+ cat >post-receive.expect <<-EOF &&
679
+ $orgmaster $newmaster refs/heads/master
680
+ EOF
681
+
682
+ cat >post-update.expect <<-EOF &&
683
+ refs/heads/master
684
+ EOF
685
+
686
+ test_cmp pre-receive.expect pre-receive.actual &&
687
+ test_cmp update.expect update.actual &&
688
+ test_cmp post-receive.expect post-receive.actual &&
689
+ test_cmp post-update.expect post-update.actual
690
+ )
691
+ '
692
+
693
+ test_expect_success ' deletion of a non-existent ref alone does trigger post-receive and post-update hooks' '
694
+ mk_test_with_hooks heads/master &&
695
+ git push testrepo :refs/heads/nonexistent &&
696
+ (
697
+ cd testrepo/.git &&
698
+ cat >pre-receive.expect <<-EOF &&
699
+ $_z40 $_z40 refs/heads/nonexistent
700
+ EOF
701
+
702
+ cat >update.expect <<-EOF &&
703
+ refs/heads/nonexistent $_z40 $_z40
704
+ EOF
705
+
706
+ test_cmp pre-receive.expect pre-receive.actual &&
707
+ test_cmp update.expect update.actual &&
708
+ test_path_is_missing post-receive.actual &&
709
+ test_path_is_missing post-update.actual
710
+ )
711
+ '
712
+
713
+ test_expect_success ' mixed ref updates, deletes, invalid deletes trigger hooks with correct input' '
714
+ mk_test_with_hooks heads/master heads/next heads/pu &&
715
+ orgmaster=$(cd testrepo && git show-ref -s --verify refs/heads/master) &&
716
+ newmaster=$(git show-ref -s --verify refs/heads/master) &&
717
+ orgnext=$(cd testrepo && git show-ref -s --verify refs/heads/next) &&
718
+ newnext=$_z40 &&
719
+ orgpu=$(cd testrepo && git show-ref -s --verify refs/heads/pu) &&
720
+ newpu=$(git show-ref -s --verify refs/heads/master) &&
721
+ git push testrepo refs/heads/master:refs/heads/master \
722
+ refs/heads/master:refs/heads/pu :refs/heads/next \
723
+ :refs/heads/nonexistent &&
724
+ (
725
+ cd testrepo/.git &&
726
+ cat >pre-receive.expect <<-EOF &&
727
+ $orgmaster $newmaster refs/heads/master
728
+ $orgnext $newnext refs/heads/next
729
+ $orgpu $newpu refs/heads/pu
730
+ $_z40 $_z40 refs/heads/nonexistent
731
+ EOF
732
+
733
+ cat >update.expect <<-EOF &&
734
+ refs/heads/master $orgmaster $newmaster
735
+ refs/heads/next $orgnext $newnext
736
+ refs/heads/pu $orgpu $newpu
737
+ refs/heads/nonexistent $_z40 $_z40
738
+ EOF
739
+
740
+ cat >post-receive.expect <<-EOF &&
741
+ $orgmaster $newmaster refs/heads/master
742
+ $orgnext $newnext refs/heads/next
743
+ $orgpu $newpu refs/heads/pu
744
+ EOF
745
+
746
+ cat >post-update.expect <<-EOF &&
747
+ refs/heads/master
748
+ refs/heads/next
749
+ refs/heads/pu
750
+ EOF
751
+
752
+ test_cmp pre-receive.expect pre-receive.actual &&
753
+ test_cmp update.expect update.actual &&
754
+ test_cmp post-receive.expect post-receive.actual &&
755
+ test_cmp post-update.expect post-update.actual
756
+ )
757
+ '
758
+
562
759
test_expect_success ' allow deleting a ref using --delete' '
563
760
mk_test heads/master &&
564
761
(cd testrepo && git config receive.denyDeleteCurrent warn) &&
0 commit comments