Raw
1 #!/bin/sh
2
3 test_description='git refs update'
4
5 . ./test-lib.sh
6
7 setup_repo () {
8 git init "$1" &&
9 test_commit -C "$1" A &&
10 test_commit -C "$1" B
11 }
12
13 test_ref_matches () {
14 git rev-parse "$1" >expect &&
15 echo "$2" >actual &&
16 test_cmp expect actual
17 }
18
19 test_expect_success 'update creates a new reference' '
20 test_when_finished "rm -rf repo" &&
21 setup_repo repo &&
22 (
23 cd repo &&
24 A=$(git rev-parse A) &&
25 git refs update refs/heads/foo $A &&
26 test_ref_matches refs/heads/foo "$A"
27 )
28 '
29
30 test_expect_success 'update an existing reference without oldvalue' '
31 test_when_finished "rm -rf repo" &&
32 setup_repo repo &&
33 (
34 cd repo &&
35 A=$(git rev-parse A) &&
36 B=$(git rev-parse B) &&
37 git refs update refs/heads/foo $A &&
38 git refs update refs/heads/foo $B &&
39 test_ref_matches refs/heads/foo $B
40 )
41 '
42
43 test_expect_success 'update with matching oldvalue' '
44 test_when_finished "rm -rf repo" &&
45 setup_repo repo &&
46 (
47 cd repo &&
48 A=$(git rev-parse A) &&
49 B=$(git rev-parse B) &&
50 git refs update refs/heads/foo $A &&
51 git refs update refs/heads/foo $B $A &&
52 test_ref_matches refs/heads/foo $B
53 )
54 '
55
56 test_expect_success 'update with stale oldvalue fails' '
57 test_when_finished "rm -rf repo" &&
58 setup_repo repo &&
59 (
60 cd repo &&
61 A=$(git rev-parse A) &&
62 B=$(git rev-parse B) &&
63 git refs update refs/heads/foo $A &&
64 test_must_fail git refs update refs/heads/foo $B $B 2>err &&
65 test_grep " but expected " err &&
66 test_ref_matches refs/heads/foo $A
67 )
68 '
69
70 test_expect_success 'update can create a new branch with oldvalue' '
71 test_when_finished "rm -rf repo" &&
72 setup_repo repo &&
73 (
74 cd repo &&
75 A=$(git rev-parse A) &&
76 git refs update refs/heads/foo $A $ZERO_OID 2>err &&
77 test_ref_matches refs/heads/foo $A
78 )
79 '
80
81 test_expect_success 'update can create a new branch without oldvalue' '
82 test_when_finished "rm -rf repo" &&
83 setup_repo repo &&
84 (
85 cd repo &&
86 A=$(git rev-parse A) &&
87 git refs update refs/heads/foo $A 2>err &&
88 test_ref_matches refs/heads/foo $A
89 )
90 '
91
92 test_expect_success 'update refuses to create preexisting branch' '
93 test_when_finished "rm -rf repo" &&
94 setup_repo repo &&
95 (
96 cd repo &&
97 A=$(git rev-parse A) &&
98 B=$(git rev-parse B) &&
99 git refs update refs/heads/foo $A &&
100 test_must_fail git refs update refs/heads/foo $B $ZERO_OID 2>err &&
101 test_grep "reference already exists" err &&
102 test_ref_matches refs/heads/foo $A
103 )
104 '
105
106 test_expect_success 'update can delete a branch with oldvalue' '
107 test_when_finished "rm -rf repo" &&
108 setup_repo repo &&
109 (
110 cd repo &&
111 A=$(git rev-parse A) &&
112 git refs update refs/heads/foo $A 2>err &&
113 git refs update refs/heads/foo $ZERO_OID $A 2>err &&
114 test_must_fail git refs exists refs/heads/foo
115 )
116 '
117
118 test_expect_success 'update can delete a branch without oldvalue' '
119 test_when_finished "rm -rf repo" &&
120 setup_repo repo &&
121 (
122 cd repo &&
123 A=$(git rev-parse A) &&
124 git refs update refs/heads/foo $A 2>err &&
125 git refs update refs/heads/foo $ZERO_OID 2>err &&
126 test_must_fail git refs exists refs/heads/foo
127 )
128 '
129
130 test_expect_success 'update refuses to delete a branch with mismatching value' '
131 test_when_finished "rm -rf repo" &&
132 setup_repo repo &&
133 (
134 cd repo &&
135 A=$(git rev-parse A) &&
136 B=$(git rev-parse B) &&
137 git refs update refs/heads/foo $A 2>err &&
138 test_must_fail git refs update refs/heads/foo $ZERO_OID $B 2>err &&
139 test_grep " but expected " err &&
140 git refs exists refs/heads/foo
141 )
142 '
143
144 test_expect_success 'update refuses to create preexisting branch' '
145 test_when_finished "rm -rf repo" &&
146 setup_repo repo &&
147 (
148 cd repo &&
149 A=$(git rev-parse A) &&
150 B=$(git rev-parse B) &&
151 git refs update refs/heads/foo $A &&
152 test_must_fail git refs update refs/heads/foo $B $ZERO_OID 2>err &&
153 test_grep "reference already exists" err &&
154 test_ref_matches refs/heads/foo $A
155 )
156 '
157
158
159 test_expect_success 'update with invalid new value fails' '
160 test_when_finished "rm -rf repo" &&
161 setup_repo repo &&
162 (
163 cd repo &&
164 test_must_fail git refs update refs/heads/foo invalid-oid 2>err &&
165 test_grep "invalid new object ID" err &&
166 test_must_fail git refs exists refs/heads/foo
167 )
168 '
169
170 test_expect_success 'update with invalid old value fails' '
171 test_when_finished "rm -rf repo" &&
172 setup_repo repo &&
173 (
174 cd repo &&
175 A=$(git rev-parse A) &&
176 B=$(git rev-parse B) &&
177 git refs update refs/heads/foo $A &&
178 test_must_fail git refs update refs/heads/foo $B invalid-oid 2>err &&
179 test_grep "invalid old object ID" err &&
180 test_ref_matches refs/heads/foo $A
181 )
182 '
183
184 test_expect_success 'update --no-deref rewrites the symref itself' '
185 test_when_finished "rm -rf repo" &&
186 setup_repo repo &&
187 (
188 cd repo &&
189 A=$(git rev-parse A) &&
190 B=$(git rev-parse B) &&
191 git refs update refs/heads/foo $A &&
192 git symbolic-ref refs/heads/symref refs/heads/foo &&
193 git refs update --no-deref refs/heads/symref $B &&
194 test_must_fail git symbolic-ref refs/heads/symref &&
195 test_ref_matches refs/heads/symref $B &&
196 test_ref_matches refs/heads/foo $A
197 )
198 '
199
200 test_expect_success 'update does not create a reflog by default' '
201 test_when_finished "rm -rf repo" &&
202 setup_repo repo &&
203 (
204 cd repo &&
205 A=$(git rev-parse A) &&
206 git refs update refs/foo $A &&
207 test_must_fail git reflog exists refs/foo
208 )
209 '
210
211 test_expect_success 'update creates a reflog with --create-reflog' '
212 test_when_finished "rm -rf repo" &&
213 setup_repo repo &&
214 (
215 cd repo &&
216 A=$(git rev-parse A) &&
217 git refs update --create-reflog refs/foo $A &&
218 git reflog exists refs/foo
219 )
220 '
221
222 test_expect_success 'update with message records reason in reflog' '
223 test_when_finished "rm -rf repo" &&
224 setup_repo repo &&
225 (
226 cd repo &&
227 A=$(git rev-parse A) &&
228 B=$(git rev-parse B) &&
229 git refs update refs/heads/foo $A &&
230 git refs update --message=update-reason refs/heads/foo $B &&
231 git reflog show refs/heads/foo >actual &&
232 test_grep "update-reason$" actual
233 )
234 '
235
236 test_expect_success 'update with empty message fails' '
237 test_when_finished "rm -rf repo" &&
238 setup_repo repo &&
239 (
240 cd repo &&
241 A=$(git rev-parse A) &&
242 B=$(git rev-parse B) &&
243 git refs update refs/heads/foo $A &&
244 test_must_fail git refs update --message= refs/heads/foo $B 2>err &&
245 test_grep "empty message" err
246 )
247 '
248
249 test_expect_success 'update with too few arguments fails' '
250 test_when_finished "rm -rf repo" &&
251 setup_repo repo &&
252 test_must_fail git -C repo refs update refs/heads/foo 2>err &&
253 test_grep "requires reference name, new value" err
254 '
255
256 test_expect_success 'update with too many arguments fails' '
257 test_when_finished "rm -rf repo" &&
258 setup_repo repo &&
259 (
260 cd repo &&
261 A=$(git rev-parse A) &&
262 B=$(git rev-parse B) &&
263 test_must_fail git refs update refs/heads/foo $A $B extra 2>err &&
264 test_grep "requires reference name, new value" err
265 )
266 '
267
268 test_done