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
'
11 mkdir -p .git/hooks &&
13 PRE_OID=$(git rev-parse PRE) &&
15 POST_OID=$(git rev-parse POST)
18 test_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 &&
28 git update-ref HEAD POST &&
29 test_cmp expect actual
32 test_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
41 test_must_fail git update-ref HEAD POST 2>err &&
42 test_i18ngrep "ref updates aborted by hook" err
45 test_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
58 $ZERO_OID $POST_OID HEAD
59 $ZERO_OID $POST_OID refs/heads/main
61 git update-ref HEAD POST <<-EOF &&
62 update HEAD $ZERO_OID $POST_OID
63 update refs/heads/main $ZERO_OID $POST_OID
65 test_cmp expect actual
68 test_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
81 $ZERO_OID $POST_OID HEAD
82 $ZERO_OID $POST_OID refs/heads/main
84 git update-ref HEAD POST &&
85 test_cmp expect actual
88 test_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
100 cat >expect <<-EOF &&
101 $ZERO_OID $POST_OID HEAD
102 $ZERO_OID $POST_OID refs/heads/main
104 git update-ref --stdin <<-EOF &&
106 update HEAD POST $ZERO_OID
107 update refs/heads/main POST $ZERO_OID
110 test_cmp expect actual
113 test_expect_success
'interleaving hook calls succeed' '
114 test_when_finished "rm -r target-repo.git" &&
116 git init --bare target-repo.git &&
118 write_script target-repo.git/hooks/reference-transaction <<-\EOF &&
119 echo $0 "$@" >>actual
122 write_script target-repo.git/hooks/update <<-\EOF &&
123 echo $0 "$@" >>actual
126 cat >expect <<-EOF &&
127 hooks/update refs/tags/PRE $ZERO_OID $PRE_OID
128 hooks/reference-transaction prepared
129 hooks/reference-transaction committed
130 hooks/update refs/tags/POST $ZERO_OID $POST_OID
131 hooks/reference-transaction prepared
132 hooks/reference-transaction committed
135 git push ./target-repo.git PRE POST &&
136 test_cmp expect target-repo.git/actual