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