Raw
1 #!/bin/sh
2
3 test_description='reference transaction hooks'
4
5 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
6 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
7
8 . ./test-lib.sh
9
10 test_expect_success setup '
11 test_commit PRE &&
12 PRE_OID=$(git rev-parse PRE) &&
13 test_commit POST &&
14 POST_OID=$(git rev-parse POST)
15 '
16
17 test_expect_success 'hook allows updating ref if successful' '
18 git reset --hard PRE &&
19 test_hook reference-transaction <<-\EOF &&
20 echo "$*" >>actual
21 EOF
22 cat >expect <<-EOF &&
23 preparing
24 prepared
25 committed
26 EOF
27 git update-ref HEAD POST &&
28 test_cmp expect actual
29 '
30
31 test_expect_success 'hook aborts updating ref in preparing state' '
32 git reset --hard PRE &&
33 test_hook reference-transaction <<-\EOF &&
34 if test "$1" = preparing
35 then
36 exit 1
37 fi
38 EOF
39 test_must_fail git update-ref HEAD POST 2>err &&
40 test_grep "in '\''preparing'\'' phase, update aborted by the reference-transaction hook" err
41 '
42
43 test_expect_success 'hook aborts updating ref in prepared state' '
44 git reset --hard PRE &&
45 test_hook reference-transaction <<-\EOF &&
46 if test "$1" = prepared
47 then
48 exit 1
49 fi
50 EOF
51 test_must_fail git update-ref HEAD POST 2>err &&
52 test_grep "in '\''prepared'\'' phase, update aborted by the reference-transaction hook" err
53 '
54
55 test_expect_success 'hook gets all queued updates in prepared state' '
56 test_when_finished "rm actual" &&
57 git reset --hard PRE &&
58 test_hook reference-transaction <<-\EOF &&
59 if test "$1" = prepared
60 then
61 while read -r line
62 do
63 printf "%s\n" "$line"
64 done >actual
65 fi
66 EOF
67 cat >expect <<-EOF &&
68 $ZERO_OID $POST_OID refs/heads/main
69 EOF
70 git update-ref HEAD POST <<-EOF &&
71 update HEAD $ZERO_OID $POST_OID
72 update refs/heads/main $ZERO_OID $POST_OID
73 EOF
74 test_cmp expect actual
75 '
76
77 test_expect_success 'hook gets all queued updates in committed state' '
78 test_when_finished "rm actual" &&
79 git reset --hard PRE &&
80 test_hook reference-transaction <<-\EOF &&
81 if test "$1" = committed
82 then
83 while read -r line
84 do
85 printf "%s\n" "$line"
86 done >actual
87 fi
88 EOF
89 cat >expect <<-EOF &&
90 $ZERO_OID $POST_OID refs/heads/main
91 EOF
92 git update-ref HEAD POST &&
93 test_cmp expect actual
94 '
95
96 test_expect_success 'hook gets all queued updates in aborted state' '
97 test_when_finished "rm actual" &&
98 git reset --hard PRE &&
99 test_hook reference-transaction <<-\EOF &&
100 if test "$1" = aborted
101 then
102 while read -r line
103 do
104 printf "%s\n" "$line"
105 done >actual
106 fi
107 EOF
108 cat >expect <<-EOF &&
109 $ZERO_OID $POST_OID HEAD
110 $ZERO_OID $POST_OID refs/heads/main
111 EOF
112 git update-ref --stdin <<-EOF &&
113 start
114 update HEAD POST $ZERO_OID
115 update refs/heads/main POST $ZERO_OID
116 abort
117 EOF
118 test_cmp expect actual
119 '
120
121 test_expect_success 'interleaving hook calls succeed' '
122 test_when_finished "rm -r target-repo.git" &&
123
124 git init --bare target-repo.git &&
125
126 test_hook -C target-repo.git reference-transaction <<-\EOF &&
127 echo $0 "$@" >>actual
128 EOF
129
130 test_hook -C target-repo.git update <<-\EOF &&
131 echo $0 "$@" >>actual
132 EOF
133
134 cat >expect <<-EOF &&
135 hooks/update refs/tags/PRE $ZERO_OID $PRE_OID
136 hooks/update refs/tags/POST $ZERO_OID $POST_OID
137 hooks/reference-transaction preparing
138 hooks/reference-transaction prepared
139 hooks/reference-transaction committed
140 EOF
141
142 git push ./target-repo.git PRE POST &&
143 test_cmp expect target-repo.git/actual
144 '
145
146 test_expect_success 'hook captures git-symbolic-ref updates' '
147 test_when_finished "rm actual" &&
148
149 test_hook reference-transaction <<-\EOF &&
150 echo "$*" >>actual
151 while read -r line
152 do
153 printf "%s\n" "$line"
154 done >>actual
155 EOF
156
157 git symbolic-ref refs/heads/symref refs/heads/main &&
158
159 cat >expect <<-EOF &&
160 preparing
161 $ZERO_OID ref:refs/heads/main refs/heads/symref
162 prepared
163 $ZERO_OID ref:refs/heads/main refs/heads/symref
164 committed
165 $ZERO_OID ref:refs/heads/main refs/heads/symref
166 EOF
167
168 test_cmp expect actual
169 '
170
171 test_expect_success 'hook gets all queued symref updates' '
172 test_when_finished "rm actual" &&
173
174 git update-ref refs/heads/branch $POST_OID &&
175 git symbolic-ref refs/heads/symref refs/heads/main &&
176 git symbolic-ref refs/heads/symrefd refs/heads/main &&
177 git symbolic-ref refs/heads/symrefu refs/heads/main &&
178
179 test_hook reference-transaction <<-\EOF &&
180 echo "$*" >>actual
181 while read -r line
182 do
183 printf "%s\n" "$line"
184 done >>actual
185 EOF
186
187 # In the files backend, "delete" also triggers an additional transaction
188 # update on the packed-refs backend, which constitutes additional reflog
189 # entries.
190 cat >expect <<-EOF &&
191 preparing
192 ref:refs/heads/main $ZERO_OID refs/heads/symref
193 ref:refs/heads/main $ZERO_OID refs/heads/symrefd
194 $ZERO_OID ref:refs/heads/main refs/heads/symrefc
195 ref:refs/heads/main ref:refs/heads/branch refs/heads/symrefu
196 EOF
197
198 if test_have_prereq REFFILES
199 then
200 cat >>expect <<-EOF
201 aborted
202 $ZERO_OID $ZERO_OID refs/heads/symrefd
203 EOF
204 fi &&
205
206 cat >>expect <<-EOF &&
207 prepared
208 ref:refs/heads/main $ZERO_OID refs/heads/symref
209 ref:refs/heads/main $ZERO_OID refs/heads/symrefd
210 $ZERO_OID ref:refs/heads/main refs/heads/symrefc
211 ref:refs/heads/main ref:refs/heads/branch refs/heads/symrefu
212 committed
213 ref:refs/heads/main $ZERO_OID refs/heads/symref
214 ref:refs/heads/main $ZERO_OID refs/heads/symrefd
215 $ZERO_OID ref:refs/heads/main refs/heads/symrefc
216 ref:refs/heads/main ref:refs/heads/branch refs/heads/symrefu
217 EOF
218
219 git update-ref --no-deref --stdin <<-EOF &&
220 start
221 symref-verify refs/heads/symref refs/heads/main
222 symref-delete refs/heads/symrefd refs/heads/main
223 symref-create refs/heads/symrefc refs/heads/main
224 symref-update refs/heads/symrefu refs/heads/branch ref refs/heads/main
225 prepare
226 commit
227 EOF
228 test_cmp expect actual
229 '
230
231 test_done