]>
Commit | Line | Data |
---|---|---|
67541597 PS |
1 | #!/bin/sh |
2 | ||
3 | test_description='reference transaction hooks' | |
4 | ||
06d53148 | 5 | GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main |
334afbc7 JS |
6 | export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME |
7 | ||
67541597 PS |
8 | . ./test-lib.sh |
9 | ||
10 | test_expect_success setup ' | |
67541597 | 11 | test_commit PRE && |
09b2aa30 | 12 | PRE_OID=$(git rev-parse PRE) && |
67541597 PS |
13 | test_commit POST && |
14 | POST_OID=$(git rev-parse POST) | |
15 | ' | |
16 | ||
17 | test_expect_success 'hook allows updating ref if successful' ' | |
67541597 | 18 | git reset --hard PRE && |
7da7f63c | 19 | test_hook reference-transaction <<-\EOF && |
67541597 PS |
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 | ||
30 | test_expect_success 'hook aborts updating ref in prepared state' ' | |
67541597 | 31 | git reset --hard PRE && |
7da7f63c | 32 | test_hook reference-transaction <<-\EOF && |
67541597 PS |
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_i18ngrep "ref updates aborted by hook" err | |
40 | ' | |
41 | ||
42 | test_expect_success 'hook gets all queued updates in prepared state' ' | |
7da7f63c | 43 | test_when_finished "rm actual" && |
67541597 | 44 | git reset --hard PRE && |
7da7f63c | 45 | test_hook reference-transaction <<-\EOF && |
67541597 PS |
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 HEAD | |
06d53148 | 56 | $ZERO_OID $POST_OID refs/heads/main |
67541597 PS |
57 | EOF |
58 | git update-ref HEAD POST <<-EOF && | |
59 | update HEAD $ZERO_OID $POST_OID | |
06d53148 | 60 | update refs/heads/main $ZERO_OID $POST_OID |
67541597 PS |
61 | EOF |
62 | test_cmp expect actual | |
63 | ' | |
64 | ||
65 | test_expect_success 'hook gets all queued updates in committed state' ' | |
7da7f63c | 66 | test_when_finished "rm actual" && |
67541597 | 67 | git reset --hard PRE && |
7da7f63c | 68 | test_hook reference-transaction <<-\EOF && |
67541597 PS |
69 | if test "$1" = committed |
70 | then | |
71 | while read -r line | |
72 | do | |
73 | printf "%s\n" "$line" | |
74 | done >actual | |
75 | fi | |
76 | EOF | |
77 | cat >expect <<-EOF && | |
78 | $ZERO_OID $POST_OID HEAD | |
06d53148 | 79 | $ZERO_OID $POST_OID refs/heads/main |
67541597 PS |
80 | EOF |
81 | git update-ref HEAD POST && | |
82 | test_cmp expect actual | |
83 | ' | |
84 | ||
85 | test_expect_success 'hook gets all queued updates in aborted state' ' | |
7da7f63c | 86 | test_when_finished "rm actual" && |
67541597 | 87 | git reset --hard PRE && |
7da7f63c | 88 | test_hook reference-transaction <<-\EOF && |
67541597 PS |
89 | if test "$1" = aborted |
90 | then | |
91 | while read -r line | |
92 | do | |
93 | printf "%s\n" "$line" | |
94 | done >actual | |
95 | fi | |
96 | EOF | |
97 | cat >expect <<-EOF && | |
98 | $ZERO_OID $POST_OID HEAD | |
06d53148 | 99 | $ZERO_OID $POST_OID refs/heads/main |
67541597 PS |
100 | EOF |
101 | git update-ref --stdin <<-EOF && | |
102 | start | |
103 | update HEAD POST $ZERO_OID | |
06d53148 | 104 | update refs/heads/main POST $ZERO_OID |
67541597 PS |
105 | abort |
106 | EOF | |
107 | test_cmp expect actual | |
108 | ' | |
109 | ||
e5256c82 PS |
110 | test_expect_success 'interleaving hook calls succeed' ' |
111 | test_when_finished "rm -r target-repo.git" && | |
112 | ||
113 | git init --bare target-repo.git && | |
114 | ||
7da7f63c | 115 | test_hook -C target-repo.git reference-transaction <<-\EOF && |
e5256c82 PS |
116 | echo $0 "$@" >>actual |
117 | EOF | |
118 | ||
7da7f63c | 119 | test_hook -C target-repo.git update <<-\EOF && |
e5256c82 PS |
120 | echo $0 "$@" >>actual |
121 | EOF | |
122 | ||
123 | cat >expect <<-EOF && | |
09b2aa30 | 124 | hooks/update refs/tags/PRE $ZERO_OID $PRE_OID |
e5256c82 PS |
125 | hooks/reference-transaction prepared |
126 | hooks/reference-transaction committed | |
09b2aa30 | 127 | hooks/update refs/tags/POST $ZERO_OID $POST_OID |
e5256c82 PS |
128 | hooks/reference-transaction prepared |
129 | hooks/reference-transaction committed | |
130 | EOF | |
131 | ||
132 | git push ./target-repo.git PRE POST && | |
133 | test_cmp expect target-repo.git/actual | |
134 | ' | |
135 | ||
2ce82543 PS |
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 && | |
139 | ||
7da7f63c | 140 | test_hook reference-transaction <<-\EOF && |
2ce82543 PS |
141 | echo "$@" >>actual |
142 | cat >>actual | |
143 | EOF | |
144 | rm -f actual && | |
145 | ||
146 | git update-ref refs/heads/unpacked-ref $POST_OID && | |
147 | git pack-refs --all && | |
148 | ||
149 | # We only expect a single hook invocation, which is the call to | |
ffad9941 | 150 | # git-update-ref(1). |
2ce82543 PS |
151 | cat >expect <<-EOF && |
152 | prepared | |
153 | $ZERO_OID $POST_OID refs/heads/unpacked-ref | |
154 | committed | |
155 | $ZERO_OID $POST_OID refs/heads/unpacked-ref | |
2ce82543 PS |
156 | EOF |
157 | ||
158 | test_cmp expect actual | |
159 | ' | |
160 | ||
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 && | |
165 | ||
7da7f63c | 166 | test_hook reference-transaction <<-\EOF && |
2ce82543 PS |
167 | echo "$@" >>actual |
168 | cat >>actual | |
169 | EOF | |
170 | rm -f actual && | |
171 | ||
172 | git update-ref -d refs/heads/to-be-deleted $POST_OID && | |
173 | ||
174 | # We only expect a single hook invocation, which is the logical | |
2ed1b64e | 175 | # deletion. |
2ce82543 | 176 | cat >expect <<-EOF && |
2ce82543 PS |
177 | prepared |
178 | $POST_OID $ZERO_OID refs/heads/to-be-deleted | |
179 | committed | |
2ce82543 PS |
180 | $POST_OID $ZERO_OID refs/heads/to-be-deleted |
181 | EOF | |
182 | ||
183 | test_cmp expect actual | |
184 | ' | |
185 | ||
67541597 | 186 | test_done |