Merge branch 'js/t7064-master-to-initial'
[git] / t / t1416-ref-transaction-hooks.sh
1 #!/bin/sh
2
3 test_description='reference transaction hooks'
4
5 . ./test-lib.sh
6
7 test_expect_success setup '
8         mkdir -p .git/hooks &&
9         test_commit PRE &&
10         PRE_OID=$(git rev-parse PRE) &&
11         test_commit POST &&
12         POST_OID=$(git rev-parse POST)
13 '
14
15 test_expect_success 'hook allows updating ref if successful' '
16         test_when_finished "rm .git/hooks/reference-transaction" &&
17         git reset --hard PRE &&
18         write_script .git/hooks/reference-transaction <<-\EOF &&
19                 echo "$*" >>actual
20         EOF
21         cat >expect <<-EOF &&
22                 prepared
23                 committed
24         EOF
25         git update-ref HEAD POST &&
26         test_cmp expect actual
27 '
28
29 test_expect_success 'hook aborts updating ref in prepared state' '
30         test_when_finished "rm .git/hooks/reference-transaction" &&
31         git reset --hard PRE &&
32         write_script .git/hooks/reference-transaction <<-\EOF &&
33                 if test "$1" = prepared
34                 then
35                         exit 1
36                 fi
37         EOF
38         test_must_fail git update-ref HEAD POST 2>err &&
39         test_i18ngrep "ref updates aborted by hook" err
40 '
41
42 test_expect_success 'hook gets all queued updates in prepared state' '
43         test_when_finished "rm .git/hooks/reference-transaction actual" &&
44         git reset --hard PRE &&
45         write_script .git/hooks/reference-transaction <<-\EOF &&
46                 if test "$1" = prepared
47                 then
48                         while read -r line
49                         do
50                                 printf "%s\n" "$line"
51                         done >actual
52                 fi
53         EOF
54         cat >expect <<-EOF &&
55                 $ZERO_OID $POST_OID HEAD
56                 $ZERO_OID $POST_OID refs/heads/master
57         EOF
58         git update-ref HEAD POST <<-EOF &&
59                 update HEAD $ZERO_OID $POST_OID
60                 update refs/heads/master $ZERO_OID $POST_OID
61         EOF
62         test_cmp expect actual
63 '
64
65 test_expect_success 'hook gets all queued updates in committed state' '
66         test_when_finished "rm .git/hooks/reference-transaction actual" &&
67         git reset --hard PRE &&
68         write_script .git/hooks/reference-transaction <<-\EOF &&
69                 if test "$1" = committed
70                 then
71                         while read -r line
72                         do
73                                 printf "%s\n" "$line"
74                         done >actual
75                 fi
76         EOF
77         cat >expect <<-EOF &&
78                 $ZERO_OID $POST_OID HEAD
79                 $ZERO_OID $POST_OID refs/heads/master
80         EOF
81         git update-ref HEAD POST &&
82         test_cmp expect actual
83 '
84
85 test_expect_success 'hook gets all queued updates in aborted state' '
86         test_when_finished "rm .git/hooks/reference-transaction actual" &&
87         git reset --hard PRE &&
88         write_script .git/hooks/reference-transaction <<-\EOF &&
89                 if test "$1" = aborted
90                 then
91                         while read -r line
92                         do
93                                 printf "%s\n" "$line"
94                         done >actual
95                 fi
96         EOF
97         cat >expect <<-EOF &&
98                 $ZERO_OID $POST_OID HEAD
99                 $ZERO_OID $POST_OID refs/heads/master
100         EOF
101         git update-ref --stdin <<-EOF &&
102                 start
103                 update HEAD POST $ZERO_OID
104                 update refs/heads/master POST $ZERO_OID
105                 abort
106         EOF
107         test_cmp expect actual
108 '
109
110 test_expect_success 'interleaving hook calls succeed' '
111         test_when_finished "rm -r target-repo.git" &&
112
113         git init --bare target-repo.git &&
114
115         write_script target-repo.git/hooks/reference-transaction <<-\EOF &&
116                 echo $0 "$@" >>actual
117         EOF
118
119         write_script target-repo.git/hooks/update <<-\EOF &&
120                 echo $0 "$@" >>actual
121         EOF
122
123         cat >expect <<-EOF &&
124                 hooks/update refs/tags/PRE $ZERO_OID $PRE_OID
125                 hooks/reference-transaction prepared
126                 hooks/reference-transaction committed
127                 hooks/update refs/tags/POST $ZERO_OID $POST_OID
128                 hooks/reference-transaction prepared
129                 hooks/reference-transaction committed
130         EOF
131
132         git push ./target-repo.git PRE POST &&
133         test_cmp expect target-repo.git/actual
134 '
135
136 test_done