add-patch: plug a leak handling the '/' command
[alt-git.git] / t / t1430-bad-ref-name.sh
blob0c00118c2b3f798777bad27a7082e5c0a2f29ea4
1 #!/bin/sh
3 test_description='Test handling of ref names that check-ref-format rejects'
4 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
5 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
7 TEST_PASSES_SANITIZE_LEAK=true
8 . ./test-lib.sh
10 test_expect_success setup '
11 test_commit one &&
12 test_commit two &&
13 main_sha1=$(git rev-parse refs/heads/main)
16 test_expect_success 'fast-import: fail on invalid branch name ".badbranchname"' '
17 test_when_finished "rm -f .git/objects/pack_* .git/objects/index_*" &&
18 cat >input <<-INPUT_END &&
19 commit .badbranchname
20 committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
21 data <<COMMIT
22 corrupt
23 COMMIT
25 from refs/heads/main
27 INPUT_END
28 test_must_fail git fast-import <input
31 test_expect_success 'fast-import: fail on invalid branch name "bad[branch]name"' '
32 test_when_finished "rm -f .git/objects/pack_* .git/objects/index_*" &&
33 cat >input <<-INPUT_END &&
34 commit bad[branch]name
35 committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
36 data <<COMMIT
37 corrupt
38 COMMIT
40 from refs/heads/main
42 INPUT_END
43 test_must_fail git fast-import <input
46 test_expect_success 'git branch shows badly named ref as warning' '
47 test-tool ref-store main update-ref msg "refs/heads/broken...ref" $main_sha1 $ZERO_OID REF_SKIP_REFNAME_VERIFICATION &&
48 test_when_finished "test-tool ref-store main delete-refs REF_NO_DEREF msg refs/heads/broken...ref" &&
49 git branch >output 2>error &&
50 test_grep -e "ignoring ref with broken name refs/heads/broken\.\.\.ref" error &&
51 ! grep -e "broken\.\.\.ref" output
54 test_expect_success 'branch -d can delete badly named ref' '
55 test-tool ref-store main update-ref msg "refs/heads/broken...ref" $main_sha1 $ZERO_OID REF_SKIP_REFNAME_VERIFICATION &&
56 test_when_finished "test-tool ref-store main delete-refs REF_NO_DEREF msg refs/heads/broken...ref" &&
57 git branch -d broken...ref &&
58 git branch >output 2>error &&
59 ! grep -e "broken\.\.\.ref" error &&
60 ! grep -e "broken\.\.\.ref" output
63 test_expect_success 'branch -D can delete badly named ref' '
64 test-tool ref-store main update-ref msg "refs/heads/broken...ref" $main_sha1 $ZERO_OID REF_SKIP_REFNAME_VERIFICATION &&
65 test_when_finished "test-tool ref-store main delete-refs REF_NO_DEREF msg refs/heads/broken...ref" &&
66 git branch -D broken...ref &&
67 git branch >output 2>error &&
68 ! grep -e "broken\.\.\.ref" error &&
69 ! grep -e "broken\.\.\.ref" output
72 test_expect_success 'branch -D cannot delete non-ref in .git dir' '
73 echo precious >.git/my-private-file &&
74 echo precious >expect &&
75 test_must_fail git branch -D ../../my-private-file &&
76 test_cmp expect .git/my-private-file
79 test_expect_success 'branch -D cannot delete ref in .git dir' '
80 git rev-parse HEAD >.git/my-private-file &&
81 git rev-parse HEAD >expect &&
82 git branch foo/legit &&
83 test_must_fail git branch -D foo////./././../../../my-private-file &&
84 test_cmp expect .git/my-private-file
87 test_expect_success 'branch -D cannot delete absolute path' '
88 git branch -f extra &&
89 test_must_fail git branch -D "$(pwd)/.git/refs/heads/extra" &&
90 test_cmp_rev HEAD extra
93 test_expect_success 'git branch cannot create a badly named ref' '
94 test_when_finished "test-tool ref-store main delete-refs REF_NO_DEREF msg refs/heads/broken...ref" &&
95 test_must_fail git branch broken...ref &&
96 git branch >output 2>error &&
97 ! grep -e "broken\.\.\.ref" error &&
98 ! grep -e "broken\.\.\.ref" output
101 test_expect_success 'branch -m cannot rename to a bad ref name' '
102 test_when_finished "test-tool ref-store main delete-refs REF_NO_DEREF msg refs/heads/broken...ref" &&
103 test_might_fail git branch -D goodref &&
104 git branch goodref &&
105 test_must_fail git branch -m goodref broken...ref &&
106 test_cmp_rev main goodref &&
107 git branch >output 2>error &&
108 ! grep -e "broken\.\.\.ref" error &&
109 ! grep -e "broken\.\.\.ref" output
112 test_expect_failure 'branch -m can rename from a bad ref name' '
113 test-tool ref-store main update-ref msg "refs/heads/broken...ref" $main_sha1 $ZERO_OID REF_SKIP_REFNAME_VERIFICATION &&
115 test_when_finished "test-tool ref-store main delete-refs REF_NO_DEREF msg refs/heads/broken...ref" &&
116 git branch -m broken...ref renamed &&
117 test_cmp_rev main renamed &&
118 git branch >output 2>error &&
119 ! grep -e "broken\.\.\.ref" error &&
120 ! grep -e "broken\.\.\.ref" output
123 test_expect_success 'push cannot create a badly named ref' '
124 test_when_finished "test-tool ref-store main delete-refs REF_NO_DEREF msg refs/heads/broken...ref" &&
125 test_must_fail git push "file://$(pwd)" HEAD:refs/heads/broken...ref &&
126 git branch >output 2>error &&
127 ! grep -e "broken\.\.\.ref" error &&
128 ! grep -e "broken\.\.\.ref" output
131 test_expect_failure 'push --mirror can delete badly named ref' '
132 top=$(pwd) &&
133 git init src &&
134 git init dest &&
137 cd src &&
138 test_commit one
139 ) &&
141 cd dest &&
142 test_commit two &&
143 git checkout --detach &&
144 test-tool ref-store main update-ref msg "refs/heads/broken...ref" $main_sha1 $ZERO_OID REF_SKIP_REFNAME_VERIFICATION
145 ) &&
146 git -C src push --mirror "file://$top/dest" &&
147 git -C dest branch >output 2>error &&
148 ! grep -e "broken\.\.\.ref" error &&
149 ! grep -e "broken\.\.\.ref" output
152 test_expect_success 'rev-parse skips symref pointing to broken name' '
153 test_when_finished "test-tool ref-store main delete-refs REF_NO_DEREF msg refs/heads/broken...ref" &&
154 git branch shadow one &&
155 test-tool ref-store main update-ref msg "refs/heads/broken...ref" $main_sha1 $ZERO_OID REF_SKIP_REFNAME_VERIFICATION &&
156 test-tool ref-store main create-symref refs/tags/shadow refs/heads/broken...ref msg &&
157 test_when_finished "test-tool ref-store main delete-refs REF_NO_DEREF msg refs/tags/shadow" &&
158 git rev-parse --verify one >expect &&
159 git rev-parse --verify shadow >actual 2>err &&
160 test_cmp expect actual &&
161 test_grep "ignoring dangling symref refs/tags/shadow" err
164 test_expect_success 'for-each-ref emits warnings for broken names' '
165 test-tool ref-store main update-ref msg "refs/heads/broken...ref" $main_sha1 $ZERO_OID REF_SKIP_REFNAME_VERIFICATION &&
166 test_when_finished "test-tool ref-store main delete-refs REF_NO_DEREF msg refs/heads/broken...ref" &&
167 test-tool ref-store main create-symref refs/heads/badname refs/heads/broken...ref &&
168 test_when_finished "test-tool ref-store main delete-refs REF_NO_DEREF msg refs/heads/badname" &&
169 test-tool ref-store main create-symref refs/heads/broken...symref refs/heads/main &&
170 test_when_finished "test-tool ref-store main delete-refs REF_NO_DEREF msg refs/heads/broken...symref" &&
171 git for-each-ref >output 2>error &&
172 ! grep -e "broken\.\.\.ref" output &&
173 ! grep -e "badname" output &&
174 ! grep -e "broken\.\.\.symref" output &&
175 test_grep "ignoring ref with broken name refs/heads/broken\.\.\.ref" error &&
176 test_grep ! "ignoring broken ref refs/heads/badname" error &&
177 test_grep "ignoring ref with broken name refs/heads/broken\.\.\.symref" error
180 test_expect_success 'update-ref -d can delete broken name' '
181 test-tool ref-store main update-ref msg "refs/heads/broken...ref" $main_sha1 $ZERO_OID REF_SKIP_REFNAME_VERIFICATION &&
182 test_when_finished "test-tool ref-store main delete-refs REF_NO_DEREF msg refs/heads/broken...ref" &&
183 git update-ref -d refs/heads/broken...ref >output 2>error &&
184 test_must_be_empty output &&
185 test_must_be_empty error &&
186 git branch >output 2>error &&
187 ! grep -e "broken\.\.\.ref" error &&
188 ! grep -e "broken\.\.\.ref" output
191 test_expect_success 'branch -d can delete broken name' '
192 test-tool ref-store main update-ref msg "refs/heads/broken...ref" $main_sha1 $ZERO_OID REF_SKIP_REFNAME_VERIFICATION &&
193 test_when_finished "test-tool ref-store main delete-refs REF_NO_DEREF msg refs/heads/broken...ref" &&
194 git branch -d broken...ref >output 2>error &&
195 test_grep "Deleted branch broken...ref (was broken)" output &&
196 test_must_be_empty error &&
197 git branch >output 2>error &&
198 ! grep -e "broken\.\.\.ref" error &&
199 ! grep -e "broken\.\.\.ref" output
202 test_expect_success 'update-ref --no-deref -d can delete symref to broken name' '
203 test-tool ref-store main update-ref msg "refs/heads/broken...ref" $main_sha1 $ZERO_OID REF_SKIP_REFNAME_VERIFICATION &&
205 test_when_finished "test-tool ref-store main delete-refs REF_NO_DEREF msg refs/heads/broken...ref" &&
206 test-tool ref-store main create-symref refs/heads/badname refs/heads/broken...ref msg &&
207 test_when_finished "test-tool ref-store main delete-refs REF_NO_DEREF msg refs/heads/badname" &&
208 test_ref_exists refs/heads/badname &&
209 git update-ref --no-deref -d refs/heads/badname >output 2>error &&
210 test_ref_missing refs/heads/badname &&
211 test_must_be_empty output &&
212 test_must_be_empty error
215 test_expect_success 'branch -d can delete symref to broken name' '
216 test-tool ref-store main update-ref msg "refs/heads/broken...ref" $main_sha1 $ZERO_OID REF_SKIP_REFNAME_VERIFICATION &&
217 test_when_finished "test-tool ref-store main delete-refs REF_NO_DEREF msg refs/heads/broken...ref" &&
218 test-tool ref-store main create-symref refs/heads/badname refs/heads/broken...ref msg &&
219 test_when_finished "test-tool ref-store main delete-refs REF_NO_DEREF msg refs/heads/badname" &&
220 test_ref_exists refs/heads/badname &&
221 git branch -d badname >output 2>error &&
222 test_ref_missing refs/heads/badname &&
223 test_grep "Deleted branch badname (was refs/heads/broken\.\.\.ref)" output &&
224 test_must_be_empty error
227 test_expect_success 'update-ref --no-deref -d can delete dangling symref to broken name' '
228 test-tool ref-store main create-symref refs/heads/badname refs/heads/broken...ref msg &&
229 test_when_finished "test-tool ref-store main delete-refs REF_NO_DEREF msg refs/heads/badname" &&
230 test_ref_exists refs/heads/badname &&
231 git update-ref --no-deref -d refs/heads/badname >output 2>error &&
232 test_ref_missing refs/heads/badname &&
233 test_must_be_empty output &&
234 test_must_be_empty error
237 test_expect_success 'branch -d can delete dangling symref to broken name' '
238 test-tool ref-store main create-symref refs/heads/badname refs/heads/broken...ref msg &&
239 test_when_finished "test-tool ref-store main delete-refs REF_NO_DEREF msg refs/heads/badname" &&
240 test_ref_exists refs/heads/badname &&
241 git branch -d badname >output 2>error &&
242 test_ref_missing refs/heads/badname &&
243 test_grep "Deleted branch badname (was refs/heads/broken\.\.\.ref)" output &&
244 test_must_be_empty error
247 test_expect_success 'update-ref -d can delete broken name through symref' '
248 test-tool ref-store main update-ref msg "refs/heads/broken...ref" $main_sha1 $ZERO_OID REF_SKIP_REFNAME_VERIFICATION &&
249 test_when_finished "test-tool ref-store main delete-refs REF_NO_DEREF msg refs/heads/broken...ref" &&
250 test-tool ref-store main create-symref refs/heads/badname refs/heads/broken...ref msg &&
251 test_when_finished "test-tool ref-store main delete-refs REF_NO_DEREF msg refs/heads/badname" &&
252 test_ref_exists refs/heads/broken...ref &&
253 git update-ref -d refs/heads/badname >output 2>error &&
254 test_ref_missing refs/heads/broken...ref &&
255 test_must_be_empty output &&
256 test_must_be_empty error
259 test_expect_success 'update-ref --no-deref -d can delete symref with broken name' '
260 test-tool ref-store main create-symref refs/heads/broken...symref refs/heads/main &&
261 test_when_finished "test-tool ref-store main delete-refs REF_NO_DEREF msg refs/heads/broken...symref" &&
262 test_ref_exists refs/heads/broken...symref &&
263 git update-ref --no-deref -d refs/heads/broken...symref >output 2>error &&
264 test_ref_missing refs/heads/broken...symref &&
265 test_must_be_empty output &&
266 test_must_be_empty error
269 test_expect_success 'branch -d can delete symref with broken name' '
270 test-tool ref-store main create-symref refs/heads/broken...symref refs/heads/main &&
271 test_when_finished "test-tool ref-store main delete-refs REF_NO_DEREF msg refs/heads/broken...symref" &&
272 test_ref_exists refs/heads/broken...symref &&
273 git branch -d broken...symref >output 2>error &&
274 test_ref_missing refs/heads/broken...symref &&
275 test_grep "Deleted branch broken...symref (was refs/heads/main)" output &&
276 test_must_be_empty error
279 test_expect_success 'update-ref --no-deref -d can delete dangling symref with broken name' '
280 test-tool ref-store main create-symref refs/heads/broken...symref refs/heads/idonotexist &&
281 test_when_finished "test-tool ref-store main delete-refs REF_NO_DEREF msg refs/heads/broken...symref" &&
282 test_ref_exists refs/heads/broken...symref &&
283 git update-ref --no-deref -d refs/heads/broken...symref >output 2>error &&
284 test_ref_missing refs/heads/broken...symref &&
285 test_must_be_empty output &&
286 test_must_be_empty error
289 test_expect_success 'branch -d can delete dangling symref with broken name' '
290 test-tool ref-store main create-symref refs/heads/broken...symref refs/heads/idonotexist &&
291 test_when_finished "test-tool ref-store main delete-refs REF_NO_DEREF msg refs/heads/broken...symref" &&
292 test_ref_exists refs/heads/broken...symref &&
293 git branch -d broken...symref >output 2>error &&
294 test_ref_missing refs/heads/broken...symref &&
295 test_grep "Deleted branch broken...symref (was refs/heads/idonotexist)" output &&
296 test_must_be_empty error
299 test_expect_success 'update-ref -d cannot delete non-ref in .git dir' '
300 echo precious >.git/my-private-file &&
301 echo precious >expect &&
302 test_must_fail git update-ref -d my-private-file >output 2>error &&
303 test_must_be_empty output &&
304 test_grep -e "refusing to update ref with bad name" error &&
305 test_cmp expect .git/my-private-file
308 test_expect_success 'update-ref -d cannot delete absolute path' '
309 git branch -f extra &&
310 test_must_fail git update-ref -d "$(pwd)/.git/refs/heads/extra" &&
311 test_cmp_rev HEAD extra
314 test_expect_success 'update-ref --stdin fails create with bad ref name' '
315 echo "create ~a refs/heads/main" >stdin &&
316 test_must_fail git update-ref --stdin <stdin 2>err &&
317 grep "fatal: invalid ref format: ~a" err
320 test_expect_success 'update-ref --stdin fails update with bad ref name' '
321 echo "update ~a refs/heads/main" >stdin &&
322 test_must_fail git update-ref --stdin <stdin 2>err &&
323 grep "fatal: invalid ref format: ~a" err
326 test_expect_success 'update-ref --stdin fails delete with bad ref name' '
327 echo "delete ~a refs/heads/main" >stdin &&
328 test_must_fail git update-ref --stdin <stdin 2>err &&
329 grep "fatal: invalid ref format: ~a" err
332 test_expect_success 'update-ref --stdin -z fails create with bad ref name' '
333 printf "%s\0" "create ~a " refs/heads/main >stdin &&
334 test_must_fail git update-ref -z --stdin <stdin 2>err &&
335 grep "fatal: invalid ref format: ~a " err
338 test_expect_success 'update-ref --stdin -z fails update with bad ref name' '
339 printf "%s\0" "update ~a" refs/heads/main "" >stdin &&
340 test_must_fail git update-ref -z --stdin <stdin 2>err &&
341 grep "fatal: invalid ref format: ~a" err
344 test_expect_success 'update-ref --stdin -z fails delete with bad ref name' '
345 printf "%s\0" "delete ~a" refs/heads/main >stdin &&
346 test_must_fail git update-ref -z --stdin <stdin 2>err &&
347 grep "fatal: invalid ref format: ~a" err
350 test_expect_success 'branch rejects HEAD as a branch name' '
351 test_must_fail git branch HEAD HEAD^ &&
352 test_must_fail git show-ref refs/heads/HEAD
355 test_expect_success 'checkout -b rejects HEAD as a branch name' '
356 test_must_fail git checkout -B HEAD HEAD^ &&
357 test_must_fail git show-ref refs/heads/HEAD
360 test_expect_success 'update-ref can operate on refs/heads/HEAD' '
361 git update-ref refs/heads/HEAD HEAD^ &&
362 git show-ref refs/heads/HEAD &&
363 git update-ref -d refs/heads/HEAD &&
364 test_must_fail git show-ref refs/heads/HEAD
367 test_expect_success 'branch -d can remove refs/heads/HEAD' '
368 git update-ref refs/heads/HEAD HEAD^ &&
369 git branch -d HEAD &&
370 test_must_fail git show-ref refs/heads/HEAD
373 test_expect_success 'branch -m can rename refs/heads/HEAD' '
374 git update-ref refs/heads/HEAD HEAD^ &&
375 git branch -m HEAD tail &&
376 test_must_fail git show-ref refs/heads/HEAD &&
377 git show-ref refs/heads/tail
380 test_expect_success 'branch -d can remove refs/heads/-dash' '
381 git update-ref refs/heads/-dash HEAD^ &&
382 git branch -d -- -dash &&
383 test_must_fail git show-ref refs/heads/-dash
386 test_expect_success 'branch -m can rename refs/heads/-dash' '
387 git update-ref refs/heads/-dash HEAD^ &&
388 git branch -m -- -dash dash &&
389 test_must_fail git show-ref refs/heads/-dash &&
390 git show-ref refs/heads/dash
393 test_done