t1430: remove refs using test-tool
[alt-git.git] / t / t1430-bad-ref-name.sh
blob84e912777c52bcd0c6f9c37e1b4e7903018f8450
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_i18ngrep -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 printf "ref: refs/heads/broken...ref\n" >.git/refs/tags/shadow &&
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_i18ngrep "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 printf "ref: refs/heads/broken...ref\n" >.git/refs/heads/badname &&
168 test_when_finished "test-tool ref-store main delete-refs REF_NO_DEREF msg refs/heads/badname" &&
169 printf "ref: refs/heads/main\n" >.git/refs/heads/broken...symref &&
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_i18ngrep "ignoring ref with broken name refs/heads/broken\.\.\.ref" error &&
176 test_i18ngrep ! "ignoring broken ref refs/heads/badname" error &&
177 test_i18ngrep "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_i18ngrep "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 printf "ref: refs/heads/broken...ref\n" >.git/refs/heads/badname &&
207 test_when_finished "test-tool ref-store main delete-refs REF_NO_DEREF msg refs/heads/badname" &&
208 git update-ref --no-deref -d refs/heads/badname >output 2>error &&
209 test_path_is_missing .git/refs/heads/badname &&
210 test_must_be_empty output &&
211 test_must_be_empty error
214 test_expect_success 'branch -d can delete symref to broken name' '
215 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 printf "ref: refs/heads/broken...ref\n" >.git/refs/heads/badname &&
219 test_when_finished "test-tool ref-store main delete-refs REF_NO_DEREF msg refs/heads/badname" &&
220 git branch -d badname >output 2>error &&
221 test_path_is_missing .git/refs/heads/badname &&
222 test_i18ngrep "Deleted branch badname (was refs/heads/broken\.\.\.ref)" output &&
223 test_must_be_empty error
226 test_expect_success 'update-ref --no-deref -d can delete dangling symref to broken name' '
227 printf "ref: refs/heads/broken...ref\n" >.git/refs/heads/badname &&
228 test_when_finished "test-tool ref-store main delete-refs REF_NO_DEREF msg refs/heads/badname" &&
229 git update-ref --no-deref -d refs/heads/badname >output 2>error &&
230 test_path_is_missing .git/refs/heads/badname &&
231 test_must_be_empty output &&
232 test_must_be_empty error
235 test_expect_success 'branch -d can delete dangling symref to broken name' '
236 printf "ref: refs/heads/broken...ref\n" >.git/refs/heads/badname &&
237 test_when_finished "test-tool ref-store main delete-refs REF_NO_DEREF msg refs/heads/badname" &&
238 git branch -d badname >output 2>error &&
239 test_path_is_missing .git/refs/heads/badname &&
240 test_i18ngrep "Deleted branch badname (was refs/heads/broken\.\.\.ref)" output &&
241 test_must_be_empty error
244 test_expect_success 'update-ref -d can delete broken name through symref' '
245 test-tool ref-store main update-ref msg "refs/heads/broken...ref" $main_sha1 $ZERO_OID REF_SKIP_REFNAME_VERIFICATION &&
247 test_when_finished "test-tool ref-store main delete-refs REF_NO_DEREF msg refs/heads/broken...ref" &&
248 printf "ref: refs/heads/broken...ref\n" >.git/refs/heads/badname &&
249 test_when_finished "test-tool ref-store main delete-refs REF_NO_DEREF msg refs/heads/badname" &&
250 git update-ref -d refs/heads/badname >output 2>error &&
251 test_path_is_missing .git/refs/heads/broken...ref &&
252 test_must_be_empty output &&
253 test_must_be_empty error
256 test_expect_success 'update-ref --no-deref -d can delete symref with broken name' '
257 printf "ref: refs/heads/main\n" >.git/refs/heads/broken...symref &&
258 test_when_finished "test-tool ref-store main delete-refs REF_NO_DEREF msg refs/heads/broken...symref" &&
259 git update-ref --no-deref -d refs/heads/broken...symref >output 2>error &&
260 test_path_is_missing .git/refs/heads/broken...symref &&
261 test_must_be_empty output &&
262 test_must_be_empty error
265 test_expect_success 'branch -d can delete symref with broken name' '
266 printf "ref: refs/heads/main\n" >.git/refs/heads/broken...symref &&
267 test_when_finished "test-tool ref-store main delete-refs REF_NO_DEREF msg refs/heads/broken...symref" &&
268 git branch -d broken...symref >output 2>error &&
269 test_path_is_missing .git/refs/heads/broken...symref &&
270 test_i18ngrep "Deleted branch broken...symref (was refs/heads/main)" output &&
271 test_must_be_empty error
274 test_expect_success 'update-ref --no-deref -d can delete dangling symref with broken name' '
275 printf "ref: refs/heads/idonotexist\n" >.git/refs/heads/broken...symref &&
276 test_when_finished "test-tool ref-store main delete-refs REF_NO_DEREF msg refs/heads/broken...symref" &&
277 git update-ref --no-deref -d refs/heads/broken...symref >output 2>error &&
278 test_path_is_missing .git/refs/heads/broken...symref &&
279 test_must_be_empty output &&
280 test_must_be_empty error
283 test_expect_success 'branch -d can delete dangling symref with broken name' '
284 printf "ref: refs/heads/idonotexist\n" >.git/refs/heads/broken...symref &&
285 test_when_finished "test-tool ref-store main delete-refs REF_NO_DEREF msg refs/heads/broken...symref" &&
286 git branch -d broken...symref >output 2>error &&
287 test_path_is_missing .git/refs/heads/broken...symref &&
288 test_i18ngrep "Deleted branch broken...symref (was refs/heads/idonotexist)" output &&
289 test_must_be_empty error
292 test_expect_success 'update-ref -d cannot delete non-ref in .git dir' '
293 echo precious >.git/my-private-file &&
294 echo precious >expect &&
295 test_must_fail git update-ref -d my-private-file >output 2>error &&
296 test_must_be_empty output &&
297 test_i18ngrep -e "refusing to update ref with bad name" error &&
298 test_cmp expect .git/my-private-file
301 test_expect_success 'update-ref -d cannot delete absolute path' '
302 git branch -f extra &&
303 test_must_fail git update-ref -d "$(pwd)/.git/refs/heads/extra" &&
304 test_cmp_rev HEAD extra
307 test_expect_success 'update-ref --stdin fails create with bad ref name' '
308 echo "create ~a refs/heads/main" >stdin &&
309 test_must_fail git update-ref --stdin <stdin 2>err &&
310 grep "fatal: invalid ref format: ~a" err
313 test_expect_success 'update-ref --stdin fails update with bad ref name' '
314 echo "update ~a refs/heads/main" >stdin &&
315 test_must_fail git update-ref --stdin <stdin 2>err &&
316 grep "fatal: invalid ref format: ~a" err
319 test_expect_success 'update-ref --stdin fails delete with bad ref name' '
320 echo "delete ~a refs/heads/main" >stdin &&
321 test_must_fail git update-ref --stdin <stdin 2>err &&
322 grep "fatal: invalid ref format: ~a" err
325 test_expect_success 'update-ref --stdin -z fails create with bad ref name' '
326 printf "%s\0" "create ~a " refs/heads/main >stdin &&
327 test_must_fail git update-ref -z --stdin <stdin 2>err &&
328 grep "fatal: invalid ref format: ~a " err
331 test_expect_success 'update-ref --stdin -z fails update with bad ref name' '
332 printf "%s\0" "update ~a" refs/heads/main "" >stdin &&
333 test_must_fail git update-ref -z --stdin <stdin 2>err &&
334 grep "fatal: invalid ref format: ~a" err
337 test_expect_success 'update-ref --stdin -z fails delete with bad ref name' '
338 printf "%s\0" "delete ~a" refs/heads/main >stdin &&
339 test_must_fail git update-ref -z --stdin <stdin 2>err &&
340 grep "fatal: invalid ref format: ~a" err
343 test_expect_success 'branch rejects HEAD as a branch name' '
344 test_must_fail git branch HEAD HEAD^ &&
345 test_must_fail git show-ref refs/heads/HEAD
348 test_expect_success 'checkout -b rejects HEAD as a branch name' '
349 test_must_fail git checkout -B HEAD HEAD^ &&
350 test_must_fail git show-ref refs/heads/HEAD
353 test_expect_success 'update-ref can operate on refs/heads/HEAD' '
354 git update-ref refs/heads/HEAD HEAD^ &&
355 git show-ref refs/heads/HEAD &&
356 git update-ref -d refs/heads/HEAD &&
357 test_must_fail git show-ref refs/heads/HEAD
360 test_expect_success 'branch -d can remove refs/heads/HEAD' '
361 git update-ref refs/heads/HEAD HEAD^ &&
362 git branch -d HEAD &&
363 test_must_fail git show-ref refs/heads/HEAD
366 test_expect_success 'branch -m can rename refs/heads/HEAD' '
367 git update-ref refs/heads/HEAD HEAD^ &&
368 git branch -m HEAD tail &&
369 test_must_fail git show-ref refs/heads/HEAD &&
370 git show-ref refs/heads/tail
373 test_expect_success 'branch -d can remove refs/heads/-dash' '
374 git update-ref refs/heads/-dash HEAD^ &&
375 git branch -d -- -dash &&
376 test_must_fail git show-ref refs/heads/-dash
379 test_expect_success 'branch -m can rename refs/heads/-dash' '
380 git update-ref refs/heads/-dash HEAD^ &&
381 git branch -m -- -dash dash &&
382 test_must_fail git show-ref refs/heads/-dash &&
383 git show-ref refs/heads/dash
386 test_done