The eleventh batch
[git/gitster.git] / t / t1416-ref-transaction-hooks.sh
blob5a812ca3c0b5d3f7de60dc999de6aafaf1f384a6
1 #!/bin/sh
3 test_description='reference transaction hooks'
5 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
6 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
8 TEST_PASSES_SANITIZE_LEAK=true
9 . ./test-lib.sh
11 test_expect_success setup '
12 test_commit PRE &&
13 PRE_OID=$(git rev-parse PRE) &&
14 test_commit POST &&
15 POST_OID=$(git rev-parse POST)
18 test_expect_success 'hook allows updating ref if successful' '
19 git reset --hard PRE &&
20 test_hook reference-transaction <<-\EOF &&
21 echo "$*" >>actual
22 EOF
23 cat >expect <<-EOF &&
24 prepared
25 committed
26 EOF
27 git update-ref HEAD POST &&
28 test_cmp expect actual
31 test_expect_success 'hook aborts updating ref in prepared state' '
32 git reset --hard PRE &&
33 test_hook reference-transaction <<-\EOF &&
34 if test "$1" = prepared
35 then
36 exit 1
38 EOF
39 test_must_fail git update-ref HEAD POST 2>err &&
40 test_grep "ref updates aborted by hook" err
43 test_expect_success 'hook gets all queued updates in prepared state' '
44 test_when_finished "rm actual" &&
45 git reset --hard PRE &&
46 test_hook reference-transaction <<-\EOF &&
47 if test "$1" = prepared
48 then
49 while read -r line
51 printf "%s\n" "$line"
52 done >actual
54 EOF
55 cat >expect <<-EOF &&
56 $ZERO_OID $POST_OID HEAD
57 $ZERO_OID $POST_OID refs/heads/main
58 EOF
59 git update-ref HEAD POST <<-EOF &&
60 update HEAD $ZERO_OID $POST_OID
61 update refs/heads/main $ZERO_OID $POST_OID
62 EOF
63 test_cmp expect actual
66 test_expect_success 'hook gets all queued updates in committed state' '
67 test_when_finished "rm actual" &&
68 git reset --hard PRE &&
69 test_hook reference-transaction <<-\EOF &&
70 if test "$1" = committed
71 then
72 while read -r line
74 printf "%s\n" "$line"
75 done >actual
77 EOF
78 cat >expect <<-EOF &&
79 $ZERO_OID $POST_OID HEAD
80 $ZERO_OID $POST_OID refs/heads/main
81 EOF
82 git update-ref HEAD POST &&
83 test_cmp expect actual
86 test_expect_success 'hook gets all queued updates in aborted state' '
87 test_when_finished "rm actual" &&
88 git reset --hard PRE &&
89 test_hook reference-transaction <<-\EOF &&
90 if test "$1" = aborted
91 then
92 while read -r line
94 printf "%s\n" "$line"
95 done >actual
97 EOF
98 cat >expect <<-EOF &&
99 $ZERO_OID $POST_OID HEAD
100 $ZERO_OID $POST_OID refs/heads/main
102 git update-ref --stdin <<-EOF &&
103 start
104 update HEAD POST $ZERO_OID
105 update refs/heads/main POST $ZERO_OID
106 abort
108 test_cmp expect actual
111 test_expect_success 'interleaving hook calls succeed' '
112 test_when_finished "rm -r target-repo.git" &&
114 git init --bare target-repo.git &&
116 test_hook -C target-repo.git reference-transaction <<-\EOF &&
117 echo $0 "$@" >>actual
120 test_hook -C target-repo.git update <<-\EOF &&
121 echo $0 "$@" >>actual
124 cat >expect <<-EOF &&
125 hooks/update refs/tags/PRE $ZERO_OID $PRE_OID
126 hooks/reference-transaction prepared
127 hooks/reference-transaction committed
128 hooks/update refs/tags/POST $ZERO_OID $POST_OID
129 hooks/reference-transaction prepared
130 hooks/reference-transaction committed
133 git push ./target-repo.git PRE POST &&
134 test_cmp expect target-repo.git/actual
137 test_expect_success 'hook captures git-symbolic-ref updates' '
138 test_when_finished "rm actual" &&
140 test_hook reference-transaction <<-\EOF &&
141 echo "$*" >>actual
142 while read -r line
144 printf "%s\n" "$line"
145 done >>actual
148 git symbolic-ref refs/heads/symref refs/heads/main &&
150 cat >expect <<-EOF &&
151 prepared
152 $ZERO_OID ref:refs/heads/main refs/heads/symref
153 committed
154 $ZERO_OID ref:refs/heads/main refs/heads/symref
157 test_cmp expect actual
160 test_expect_success 'hook gets all queued symref updates' '
161 test_when_finished "rm actual" &&
163 git update-ref refs/heads/branch $POST_OID &&
164 git symbolic-ref refs/heads/symref refs/heads/main &&
165 git symbolic-ref refs/heads/symrefd refs/heads/main &&
166 git symbolic-ref refs/heads/symrefu refs/heads/main &&
168 test_hook reference-transaction <<-\EOF &&
169 echo "$*" >>actual
170 while read -r line
172 printf "%s\n" "$line"
173 done >>actual
176 # In the files backend, "delete" also triggers an additional transaction
177 # update on the packed-refs backend, which constitutes additional reflog
178 # entries.
179 if test_have_prereq REFFILES
180 then
181 cat >expect <<-EOF
182 aborted
183 $ZERO_OID $ZERO_OID refs/heads/symrefd
185 else
186 >expect
187 fi &&
189 cat >>expect <<-EOF &&
190 prepared
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 committed
196 ref:refs/heads/main $ZERO_OID refs/heads/symref
197 ref:refs/heads/main $ZERO_OID refs/heads/symrefd
198 $ZERO_OID ref:refs/heads/main refs/heads/symrefc
199 ref:refs/heads/main ref:refs/heads/branch refs/heads/symrefu
202 git update-ref --no-deref --stdin <<-EOF &&
203 start
204 symref-verify refs/heads/symref refs/heads/main
205 symref-delete refs/heads/symrefd refs/heads/main
206 symref-create refs/heads/symrefc refs/heads/main
207 symref-update refs/heads/symrefu refs/heads/branch ref refs/heads/main
208 prepare
209 commit
211 test_cmp expect actual
214 test_done