]> git.ipfire.org Git - thirdparty/git.git/blame - t/t1416-ref-transaction-hooks.sh
refs: skip hooks when deleting uncovered packed refs
[thirdparty/git.git] / t / t1416-ref-transaction-hooks.sh
CommitLineData
67541597
PS
1#!/bin/sh
2
3test_description='reference transaction hooks'
4
06d53148 5GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
334afbc7
JS
6export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
7
67541597
PS
8. ./test-lib.sh
9
10test_expect_success setup '
11 mkdir -p .git/hooks &&
12 test_commit PRE &&
09b2aa30 13 PRE_OID=$(git rev-parse PRE) &&
67541597
PS
14 test_commit POST &&
15 POST_OID=$(git rev-parse POST)
16'
17
18test_expect_success 'hook allows updating ref if successful' '
19 test_when_finished "rm .git/hooks/reference-transaction" &&
20 git reset --hard PRE &&
21 write_script .git/hooks/reference-transaction <<-\EOF &&
22 echo "$*" >>actual
23 EOF
24 cat >expect <<-EOF &&
25 prepared
26 committed
27 EOF
28 git update-ref HEAD POST &&
29 test_cmp expect actual
30'
31
32test_expect_success 'hook aborts updating ref in prepared state' '
33 test_when_finished "rm .git/hooks/reference-transaction" &&
34 git reset --hard PRE &&
35 write_script .git/hooks/reference-transaction <<-\EOF &&
36 if test "$1" = prepared
37 then
38 exit 1
39 fi
40 EOF
41 test_must_fail git update-ref HEAD POST 2>err &&
42 test_i18ngrep "ref updates aborted by hook" err
43'
44
45test_expect_success 'hook gets all queued updates in prepared state' '
46 test_when_finished "rm .git/hooks/reference-transaction actual" &&
47 git reset --hard PRE &&
48 write_script .git/hooks/reference-transaction <<-\EOF &&
49 if test "$1" = prepared
50 then
51 while read -r line
52 do
53 printf "%s\n" "$line"
54 done >actual
55 fi
56 EOF
57 cat >expect <<-EOF &&
58 $ZERO_OID $POST_OID HEAD
06d53148 59 $ZERO_OID $POST_OID refs/heads/main
67541597
PS
60 EOF
61 git update-ref HEAD POST <<-EOF &&
62 update HEAD $ZERO_OID $POST_OID
06d53148 63 update refs/heads/main $ZERO_OID $POST_OID
67541597
PS
64 EOF
65 test_cmp expect actual
66'
67
68test_expect_success 'hook gets all queued updates in committed state' '
69 test_when_finished "rm .git/hooks/reference-transaction actual" &&
70 git reset --hard PRE &&
71 write_script .git/hooks/reference-transaction <<-\EOF &&
72 if test "$1" = committed
73 then
74 while read -r line
75 do
76 printf "%s\n" "$line"
77 done >actual
78 fi
79 EOF
80 cat >expect <<-EOF &&
81 $ZERO_OID $POST_OID HEAD
06d53148 82 $ZERO_OID $POST_OID refs/heads/main
67541597
PS
83 EOF
84 git update-ref HEAD POST &&
85 test_cmp expect actual
86'
87
88test_expect_success 'hook gets all queued updates in aborted state' '
89 test_when_finished "rm .git/hooks/reference-transaction actual" &&
90 git reset --hard PRE &&
91 write_script .git/hooks/reference-transaction <<-\EOF &&
92 if test "$1" = aborted
93 then
94 while read -r line
95 do
96 printf "%s\n" "$line"
97 done >actual
98 fi
99 EOF
100 cat >expect <<-EOF &&
101 $ZERO_OID $POST_OID HEAD
06d53148 102 $ZERO_OID $POST_OID refs/heads/main
67541597
PS
103 EOF
104 git update-ref --stdin <<-EOF &&
105 start
106 update HEAD POST $ZERO_OID
06d53148 107 update refs/heads/main POST $ZERO_OID
67541597
PS
108 abort
109 EOF
110 test_cmp expect actual
111'
112
e5256c82
PS
113test_expect_success 'interleaving hook calls succeed' '
114 test_when_finished "rm -r target-repo.git" &&
115
116 git init --bare target-repo.git &&
117
118 write_script target-repo.git/hooks/reference-transaction <<-\EOF &&
119 echo $0 "$@" >>actual
120 EOF
121
122 write_script target-repo.git/hooks/update <<-\EOF &&
123 echo $0 "$@" >>actual
124 EOF
125
126 cat >expect <<-EOF &&
09b2aa30 127 hooks/update refs/tags/PRE $ZERO_OID $PRE_OID
e5256c82
PS
128 hooks/reference-transaction prepared
129 hooks/reference-transaction committed
09b2aa30 130 hooks/update refs/tags/POST $ZERO_OID $POST_OID
e5256c82
PS
131 hooks/reference-transaction prepared
132 hooks/reference-transaction committed
133 EOF
134
135 git push ./target-repo.git PRE POST &&
136 test_cmp expect target-repo.git/actual
137'
138
2ce82543
PS
139test_expect_success 'hook does not get called on packing refs' '
140 # Pack references first such that we are in a known state.
141 git pack-refs --all &&
142
143 write_script .git/hooks/reference-transaction <<-\EOF &&
144 echo "$@" >>actual
145 cat >>actual
146 EOF
147 rm -f actual &&
148
149 git update-ref refs/heads/unpacked-ref $POST_OID &&
150 git pack-refs --all &&
151
152 # We only expect a single hook invocation, which is the call to
ffad9941 153 # git-update-ref(1).
2ce82543
PS
154 cat >expect <<-EOF &&
155 prepared
156 $ZERO_OID $POST_OID refs/heads/unpacked-ref
157 committed
158 $ZERO_OID $POST_OID refs/heads/unpacked-ref
2ce82543
PS
159 EOF
160
161 test_cmp expect actual
162'
163
164test_expect_success 'deleting packed ref calls hook once' '
165 # Create a reference and pack it.
166 git update-ref refs/heads/to-be-deleted $POST_OID &&
167 git pack-refs --all &&
168
169 write_script .git/hooks/reference-transaction <<-\EOF &&
170 echo "$@" >>actual
171 cat >>actual
172 EOF
173 rm -f actual &&
174
175 git update-ref -d refs/heads/to-be-deleted $POST_OID &&
176
177 # We only expect a single hook invocation, which is the logical
2ed1b64e 178 # deletion.
2ce82543 179 cat >expect <<-EOF &&
2ce82543
PS
180 prepared
181 $POST_OID $ZERO_OID refs/heads/to-be-deleted
182 committed
2ce82543
PS
183 $POST_OID $ZERO_OID refs/heads/to-be-deleted
184 EOF
185
186 test_cmp expect actual
187'
188
67541597 189test_done