3 test_description
='reference transaction hooks'
5 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
=main
6 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
10 test_expect_success setup
'
12 PRE_OID=$(git rev-parse PRE) &&
14 POST_OID=$(git rev-parse POST)
17 test_expect_success
'hook allows updating ref if successful' '
18 git reset --hard PRE &&
19 test_hook reference-transaction <<-\EOF &&
26 git update-ref HEAD POST &&
27 test_cmp expect actual
30 test_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
38 test_must_fail git update-ref HEAD POST 2>err &&
39 test_i18ngrep "ref updates aborted by hook" err
42 test_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
55 $ZERO_OID $POST_OID HEAD
56 $ZERO_OID $POST_OID refs/heads/main
58 git update-ref HEAD POST <<-EOF &&
59 update HEAD $ZERO_OID $POST_OID
60 update refs/heads/main $ZERO_OID $POST_OID
62 test_cmp expect actual
65 test_expect_success
'hook gets all queued updates in committed state' '
66 test_when_finished "rm actual" &&
67 git reset --hard PRE &&
68 test_hook reference-transaction <<-\EOF &&
69 if test "$1" = committed
78 $ZERO_OID $POST_OID HEAD
79 $ZERO_OID $POST_OID refs/heads/main
81 git update-ref HEAD POST &&
82 test_cmp expect actual
85 test_expect_success
'hook gets all queued updates in aborted state' '
86 test_when_finished "rm actual" &&
87 git reset --hard PRE &&
88 test_hook reference-transaction <<-\EOF &&
89 if test "$1" = aborted
98 $ZERO_OID $POST_OID HEAD
99 $ZERO_OID $POST_OID refs/heads/main
101 git update-ref --stdin <<-EOF &&
103 update HEAD POST $ZERO_OID
104 update refs/heads/main POST $ZERO_OID
107 test_cmp expect actual
110 test_expect_success
'interleaving hook calls succeed' '
111 test_when_finished "rm -r target-repo.git" &&
113 git init --bare target-repo.git &&
115 test_hook -C target-repo.git reference-transaction <<-\EOF &&
116 echo $0 "$@" >>actual
119 test_hook -C target-repo.git update <<-\EOF &&
120 echo $0 "$@" >>actual
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
132 git push ./target-repo.git PRE POST &&
133 test_cmp expect target-repo.git/actual
136 test_expect_success
'hook does not get called on packing refs' '
137 # Pack references first such that we are in a known state.
138 git pack-refs --all &&
140 test_hook reference-transaction <<-\EOF &&
146 git update-ref refs/heads/unpacked-ref $POST_OID &&
147 git pack-refs --all &&
149 # We only expect a single hook invocation, which is the call to
151 cat >expect <<-EOF &&
153 $ZERO_OID $POST_OID refs/heads/unpacked-ref
155 $ZERO_OID $POST_OID refs/heads/unpacked-ref
158 test_cmp expect actual
161 test_expect_success
'deleting packed ref calls hook once' '
162 # Create a reference and pack it.
163 git update-ref refs/heads/to-be-deleted $POST_OID &&
164 git pack-refs --all &&
166 test_hook reference-transaction <<-\EOF &&
172 git update-ref -d refs/heads/to-be-deleted $POST_OID &&
174 # We only expect a single hook invocation, which is the logical
176 cat >expect <<-EOF &&
178 $POST_OID $ZERO_OID refs/heads/to-be-deleted
180 $POST_OID $ZERO_OID refs/heads/to-be-deleted
183 test_cmp expect actual