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
10 test_expect_success setup
'
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 &&
20 committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
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
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' '
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
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_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 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 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 &&
216 test_when_finished "test-tool ref-store main delete-refs REF_NO_DEREF msg refs/heads/broken...ref" &&
217 test-tool ref-store main create-symref refs/heads/badname refs/heads/broken...ref msg &&
218 test_when_finished "test-tool ref-store main delete-refs REF_NO_DEREF msg refs/heads/badname" &&
219 git branch -d badname >output 2>error &&
220 test_path_is_missing .git/refs/heads/badname &&
221 test_i18ngrep "Deleted branch badname (was refs/heads/broken\.\.\.ref)" output &&
222 test_must_be_empty error
225 test_expect_success
'update-ref --no-deref -d can delete dangling symref to broken name' '
226 test-tool ref-store main create-symref refs/heads/badname refs/heads/broken...ref msg &&
227 test_when_finished "test-tool ref-store main delete-refs REF_NO_DEREF msg refs/heads/badname" &&
228 git update-ref --no-deref -d refs/heads/badname >output 2>error &&
229 test_path_is_missing .git/refs/heads/badname &&
230 test_must_be_empty output &&
231 test_must_be_empty error
234 test_expect_success
'branch -d can delete dangling symref to broken name' '
235 test-tool ref-store main create-symref refs/heads/badname refs/heads/broken...ref msg &&
236 test_when_finished "test-tool ref-store main delete-refs REF_NO_DEREF msg refs/heads/badname" &&
237 git branch -d badname >output 2>error &&
238 test_path_is_missing .git/refs/heads/badname &&
239 test_i18ngrep "Deleted branch badname (was refs/heads/broken\.\.\.ref)" output &&
240 test_must_be_empty error
243 test_expect_success
'update-ref -d can delete broken name through symref' '
244 test-tool ref-store main update-ref msg "refs/heads/broken...ref" $main_sha1 $ZERO_OID REF_SKIP_REFNAME_VERIFICATION &&
245 test_when_finished "test-tool ref-store main delete-refs REF_NO_DEREF msg refs/heads/broken...ref" &&
246 test-tool ref-store main create-symref refs/heads/badname refs/heads/broken...ref msg &&
247 test_when_finished "test-tool ref-store main delete-refs REF_NO_DEREF msg refs/heads/badname" &&
248 git update-ref -d refs/heads/badname >output 2>error &&
249 test_path_is_missing .git/refs/heads/broken...ref &&
250 test_must_be_empty output &&
251 test_must_be_empty error
254 test_expect_success
'update-ref --no-deref -d can delete symref with broken name' '
255 printf "ref: refs/heads/main\n" >.git/refs/heads/broken...symref &&
256 test_when_finished "test-tool ref-store main delete-refs REF_NO_DEREF msg refs/heads/broken...symref" &&
257 git update-ref --no-deref -d refs/heads/broken...symref >output 2>error &&
258 test_path_is_missing .git/refs/heads/broken...symref &&
259 test_must_be_empty output &&
260 test_must_be_empty error
263 test_expect_success
'branch -d can delete symref with broken name' '
264 printf "ref: refs/heads/main\n" >.git/refs/heads/broken...symref &&
265 test_when_finished "test-tool ref-store main delete-refs REF_NO_DEREF msg refs/heads/broken...symref" &&
266 git branch -d broken...symref >output 2>error &&
267 test_path_is_missing .git/refs/heads/broken...symref &&
268 test_i18ngrep "Deleted branch broken...symref (was refs/heads/main)" output &&
269 test_must_be_empty error
272 test_expect_success
'update-ref --no-deref -d can delete dangling symref with broken name' '
273 printf "ref: refs/heads/idonotexist\n" >.git/refs/heads/broken...symref &&
274 test_when_finished "test-tool ref-store main delete-refs REF_NO_DEREF msg refs/heads/broken...symref" &&
275 git update-ref --no-deref -d refs/heads/broken...symref >output 2>error &&
276 test_path_is_missing .git/refs/heads/broken...symref &&
277 test_must_be_empty output &&
278 test_must_be_empty error
281 test_expect_success
'branch -d can delete dangling symref with broken name' '
282 printf "ref: refs/heads/idonotexist\n" >.git/refs/heads/broken...symref &&
283 test_when_finished "test-tool ref-store main delete-refs REF_NO_DEREF msg refs/heads/broken...symref" &&
284 git branch -d broken...symref >output 2>error &&
285 test_path_is_missing .git/refs/heads/broken...symref &&
286 test_i18ngrep "Deleted branch broken...symref (was refs/heads/idonotexist)" output &&
287 test_must_be_empty error
290 test_expect_success
'update-ref -d cannot delete non-ref in .git dir' '
291 echo precious >.git/my-private-file &&
292 echo precious >expect &&
293 test_must_fail git update-ref -d my-private-file >output 2>error &&
294 test_must_be_empty output &&
295 test_i18ngrep -e "refusing to update ref with bad name" error &&
296 test_cmp expect .git/my-private-file
299 test_expect_success
'update-ref -d cannot delete absolute path' '
300 git branch -f extra &&
301 test_must_fail git update-ref -d "$(pwd)/.git/refs/heads/extra" &&
302 test_cmp_rev HEAD extra
305 test_expect_success
'update-ref --stdin fails create with bad ref name' '
306 echo "create ~a refs/heads/main" >stdin &&
307 test_must_fail git update-ref --stdin <stdin 2>err &&
308 grep "fatal: invalid ref format: ~a" err
311 test_expect_success
'update-ref --stdin fails update with bad ref name' '
312 echo "update ~a refs/heads/main" >stdin &&
313 test_must_fail git update-ref --stdin <stdin 2>err &&
314 grep "fatal: invalid ref format: ~a" err
317 test_expect_success
'update-ref --stdin fails delete with bad ref name' '
318 echo "delete ~a refs/heads/main" >stdin &&
319 test_must_fail git update-ref --stdin <stdin 2>err &&
320 grep "fatal: invalid ref format: ~a" err
323 test_expect_success
'update-ref --stdin -z fails create with bad ref name' '
324 printf "%s\0" "create ~a " refs/heads/main >stdin &&
325 test_must_fail git update-ref -z --stdin <stdin 2>err &&
326 grep "fatal: invalid ref format: ~a " err
329 test_expect_success
'update-ref --stdin -z fails update with bad ref name' '
330 printf "%s\0" "update ~a" refs/heads/main "" >stdin &&
331 test_must_fail git update-ref -z --stdin <stdin 2>err &&
332 grep "fatal: invalid ref format: ~a" err
335 test_expect_success
'update-ref --stdin -z fails delete with bad ref name' '
336 printf "%s\0" "delete ~a" refs/heads/main >stdin &&
337 test_must_fail git update-ref -z --stdin <stdin 2>err &&
338 grep "fatal: invalid ref format: ~a" err
341 test_expect_success
'branch rejects HEAD as a branch name' '
342 test_must_fail git branch HEAD HEAD^ &&
343 test_must_fail git show-ref refs/heads/HEAD
346 test_expect_success
'checkout -b rejects HEAD as a branch name' '
347 test_must_fail git checkout -B HEAD HEAD^ &&
348 test_must_fail git show-ref refs/heads/HEAD
351 test_expect_success
'update-ref can operate on refs/heads/HEAD' '
352 git update-ref refs/heads/HEAD HEAD^ &&
353 git show-ref refs/heads/HEAD &&
354 git update-ref -d refs/heads/HEAD &&
355 test_must_fail git show-ref refs/heads/HEAD
358 test_expect_success
'branch -d can remove refs/heads/HEAD' '
359 git update-ref refs/heads/HEAD HEAD^ &&
360 git branch -d HEAD &&
361 test_must_fail git show-ref refs/heads/HEAD
364 test_expect_success
'branch -m can rename refs/heads/HEAD' '
365 git update-ref refs/heads/HEAD HEAD^ &&
366 git branch -m HEAD tail &&
367 test_must_fail git show-ref refs/heads/HEAD &&
368 git show-ref refs/heads/tail
371 test_expect_success
'branch -d can remove refs/heads/-dash' '
372 git update-ref refs/heads/-dash HEAD^ &&
373 git branch -d -- -dash &&
374 test_must_fail git show-ref refs/heads/-dash
377 test_expect_success
'branch -m can rename refs/heads/-dash' '
378 git update-ref refs/heads/-dash HEAD^ &&
379 git branch -m -- -dash dash &&
380 test_must_fail git show-ref refs/heads/-dash &&
381 git show-ref refs/heads/dash