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
'
15 test_expect_success
'fast-import: fail on invalid branch name ".badbranchname"' '
16 test_when_finished "rm -f .git/objects/pack_* .git/objects/index_*" &&
17 cat >input <<-INPUT_END &&
19 committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
27 test_must_fail git fast-import <input
30 test_expect_success
'fast-import: fail on invalid branch name "bad[branch]name"' '
31 test_when_finished "rm -f .git/objects/pack_* .git/objects/index_*" &&
32 cat >input <<-INPUT_END &&
33 commit bad[branch]name
34 committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
42 test_must_fail git fast-import <input
45 test_expect_success
'git branch shows badly named ref as warning' '
46 cp .git/refs/heads/main .git/refs/heads/broken...ref &&
47 test_when_finished "rm -f .git/refs/heads/broken...ref" &&
48 git branch >output 2>error &&
49 test_i18ngrep -e "ignoring ref with broken name refs/heads/broken\.\.\.ref" error &&
50 ! grep -e "broken\.\.\.ref" output
53 test_expect_success
'branch -d can delete badly named ref' '
54 cp .git/refs/heads/main .git/refs/heads/broken...ref &&
55 test_when_finished "rm -f .git/refs/heads/broken...ref" &&
56 git branch -d broken...ref &&
57 git branch >output 2>error &&
58 ! grep -e "broken\.\.\.ref" error &&
59 ! grep -e "broken\.\.\.ref" output
62 test_expect_success
'branch -D can delete badly named ref' '
63 cp .git/refs/heads/main .git/refs/heads/broken...ref &&
64 test_when_finished "rm -f .git/refs/heads/broken...ref" &&
65 git branch -D broken...ref &&
66 git branch >output 2>error &&
67 ! grep -e "broken\.\.\.ref" error &&
68 ! grep -e "broken\.\.\.ref" output
71 test_expect_success
'branch -D cannot delete non-ref in .git dir' '
72 echo precious >.git/my-private-file &&
73 echo precious >expect &&
74 test_must_fail git branch -D ../../my-private-file &&
75 test_cmp expect .git/my-private-file
78 test_expect_success
'branch -D cannot delete ref in .git dir' '
79 git rev-parse HEAD >.git/my-private-file &&
80 git rev-parse HEAD >expect &&
81 git branch foo/legit &&
82 test_must_fail git branch -D foo////./././../../../my-private-file &&
83 test_cmp expect .git/my-private-file
86 test_expect_success
'branch -D cannot delete absolute path' '
87 git branch -f extra &&
88 test_must_fail git branch -D "$(pwd)/.git/refs/heads/extra" &&
89 test_cmp_rev HEAD extra
92 test_expect_success
'git branch cannot create a badly named ref' '
93 test_when_finished "rm -f .git/refs/heads/broken...ref" &&
94 test_must_fail git branch broken...ref &&
95 git branch >output 2>error &&
96 ! grep -e "broken\.\.\.ref" error &&
97 ! grep -e "broken\.\.\.ref" output
100 test_expect_success
'branch -m cannot rename to a bad ref name' '
101 test_when_finished "rm -f .git/refs/heads/broken...ref" &&
102 test_might_fail git branch -D goodref &&
103 git branch goodref &&
104 test_must_fail git branch -m goodref broken...ref &&
105 test_cmp_rev main goodref &&
106 git branch >output 2>error &&
107 ! grep -e "broken\.\.\.ref" error &&
108 ! grep -e "broken\.\.\.ref" output
111 test_expect_failure
'branch -m can rename from a bad ref name' '
112 cp .git/refs/heads/main .git/refs/heads/broken...ref &&
113 test_when_finished "rm -f .git/refs/heads/broken...ref" &&
114 git branch -m broken...ref renamed &&
115 test_cmp_rev main renamed &&
116 git branch >output 2>error &&
117 ! grep -e "broken\.\.\.ref" error &&
118 ! grep -e "broken\.\.\.ref" output
121 test_expect_success
'push cannot create a badly named ref' '
122 test_when_finished "rm -f .git/refs/heads/broken...ref" &&
123 test_must_fail git push "file://$(pwd)" HEAD:refs/heads/broken...ref &&
124 git branch >output 2>error &&
125 ! grep -e "broken\.\.\.ref" error &&
126 ! grep -e "broken\.\.\.ref" output
129 test_expect_failure
'push --mirror can delete badly named ref' '
141 git checkout --detach &&
142 cp .git/refs/heads/main .git/refs/heads/broken...ref
144 git -C src push --mirror "file://$top/dest" &&
145 git -C dest branch >output 2>error &&
146 ! grep -e "broken\.\.\.ref" error &&
147 ! grep -e "broken\.\.\.ref" output
150 test_expect_success
'rev-parse skips symref pointing to broken name' '
151 test_when_finished "rm -f .git/refs/heads/broken...ref" &&
152 git branch shadow one &&
153 cp .git/refs/heads/main .git/refs/heads/broken...ref &&
154 printf "ref: refs/heads/broken...ref\n" >.git/refs/tags/shadow &&
155 test_when_finished "rm -f .git/refs/tags/shadow" &&
156 git rev-parse --verify one >expect &&
157 git rev-parse --verify shadow >actual 2>err &&
158 test_cmp expect actual &&
159 test_i18ngrep "ignoring dangling symref refs/tags/shadow" err
162 test_expect_success
'for-each-ref emits warnings for broken names' '
163 cp .git/refs/heads/main .git/refs/heads/broken...ref &&
164 test_when_finished "rm -f .git/refs/heads/broken...ref" &&
165 printf "ref: refs/heads/broken...ref\n" >.git/refs/heads/badname &&
166 test_when_finished "rm -f .git/refs/heads/badname" &&
167 printf "ref: refs/heads/main\n" >.git/refs/heads/broken...symref &&
168 test_when_finished "rm -f .git/refs/heads/broken...symref" &&
169 git for-each-ref >output 2>error &&
170 ! grep -e "broken\.\.\.ref" output &&
171 ! grep -e "badname" output &&
172 ! grep -e "broken\.\.\.symref" output &&
173 test_i18ngrep "ignoring ref with broken name refs/heads/broken\.\.\.ref" error &&
174 test_i18ngrep ! "ignoring broken ref refs/heads/badname" error &&
175 test_i18ngrep "ignoring ref with broken name refs/heads/broken\.\.\.symref" error
178 test_expect_success
'update-ref -d can delete broken name' '
179 cp .git/refs/heads/main .git/refs/heads/broken...ref &&
180 test_when_finished "rm -f .git/refs/heads/broken...ref" &&
181 git update-ref -d refs/heads/broken...ref >output 2>error &&
182 test_must_be_empty output &&
183 test_must_be_empty error &&
184 git branch >output 2>error &&
185 ! grep -e "broken\.\.\.ref" error &&
186 ! grep -e "broken\.\.\.ref" output
189 test_expect_success
'branch -d can delete broken name' '
190 cp .git/refs/heads/main .git/refs/heads/broken...ref &&
191 test_when_finished "rm -f .git/refs/heads/broken...ref" &&
192 git branch -d broken...ref >output 2>error &&
193 test_i18ngrep "Deleted branch broken...ref (was broken)" output &&
194 test_must_be_empty error &&
195 git branch >output 2>error &&
196 ! grep -e "broken\.\.\.ref" error &&
197 ! grep -e "broken\.\.\.ref" output
200 test_expect_success
'update-ref --no-deref -d can delete symref to broken name' '
201 cp .git/refs/heads/main .git/refs/heads/broken...ref &&
202 test_when_finished "rm -f .git/refs/heads/broken...ref" &&
203 printf "ref: refs/heads/broken...ref\n" >.git/refs/heads/badname &&
204 test_when_finished "rm -f .git/refs/heads/badname" &&
205 git update-ref --no-deref -d refs/heads/badname >output 2>error &&
206 test_path_is_missing .git/refs/heads/badname &&
207 test_must_be_empty output &&
208 test_must_be_empty error
211 test_expect_success
'branch -d can delete symref to broken name' '
212 cp .git/refs/heads/main .git/refs/heads/broken...ref &&
213 test_when_finished "rm -f .git/refs/heads/broken...ref" &&
214 printf "ref: refs/heads/broken...ref\n" >.git/refs/heads/badname &&
215 test_when_finished "rm -f .git/refs/heads/badname" &&
216 git branch -d badname >output 2>error &&
217 test_path_is_missing .git/refs/heads/badname &&
218 test_i18ngrep "Deleted branch badname (was refs/heads/broken\.\.\.ref)" output &&
219 test_must_be_empty error
222 test_expect_success
'update-ref --no-deref -d can delete dangling symref to broken name' '
223 printf "ref: refs/heads/broken...ref\n" >.git/refs/heads/badname &&
224 test_when_finished "rm -f .git/refs/heads/badname" &&
225 git update-ref --no-deref -d refs/heads/badname >output 2>error &&
226 test_path_is_missing .git/refs/heads/badname &&
227 test_must_be_empty output &&
228 test_must_be_empty error
231 test_expect_success
'branch -d can delete dangling symref to broken name' '
232 printf "ref: refs/heads/broken...ref\n" >.git/refs/heads/badname &&
233 test_when_finished "rm -f .git/refs/heads/badname" &&
234 git branch -d badname >output 2>error &&
235 test_path_is_missing .git/refs/heads/badname &&
236 test_i18ngrep "Deleted branch badname (was refs/heads/broken\.\.\.ref)" output &&
237 test_must_be_empty error
240 test_expect_success
'update-ref -d can delete broken name through symref' '
241 cp .git/refs/heads/main .git/refs/heads/broken...ref &&
242 test_when_finished "rm -f .git/refs/heads/broken...ref" &&
243 printf "ref: refs/heads/broken...ref\n" >.git/refs/heads/badname &&
244 test_when_finished "rm -f .git/refs/heads/badname" &&
245 git update-ref -d refs/heads/badname >output 2>error &&
246 test_path_is_missing .git/refs/heads/broken...ref &&
247 test_must_be_empty output &&
248 test_must_be_empty error
251 test_expect_success
'update-ref --no-deref -d can delete symref with broken name' '
252 printf "ref: refs/heads/main\n" >.git/refs/heads/broken...symref &&
253 test_when_finished "rm -f .git/refs/heads/broken...symref" &&
254 git update-ref --no-deref -d refs/heads/broken...symref >output 2>error &&
255 test_path_is_missing .git/refs/heads/broken...symref &&
256 test_must_be_empty output &&
257 test_must_be_empty error
260 test_expect_success
'branch -d can delete symref with broken name' '
261 printf "ref: refs/heads/main\n" >.git/refs/heads/broken...symref &&
262 test_when_finished "rm -f .git/refs/heads/broken...symref" &&
263 git branch -d broken...symref >output 2>error &&
264 test_path_is_missing .git/refs/heads/broken...symref &&
265 test_i18ngrep "Deleted branch broken...symref (was refs/heads/main)" output &&
266 test_must_be_empty error
269 test_expect_success
'update-ref --no-deref -d can delete dangling symref with broken name' '
270 printf "ref: refs/heads/idonotexist\n" >.git/refs/heads/broken...symref &&
271 test_when_finished "rm -f .git/refs/heads/broken...symref" &&
272 git update-ref --no-deref -d refs/heads/broken...symref >output 2>error &&
273 test_path_is_missing .git/refs/heads/broken...symref &&
274 test_must_be_empty output &&
275 test_must_be_empty error
278 test_expect_success
'branch -d can delete dangling symref with broken name' '
279 printf "ref: refs/heads/idonotexist\n" >.git/refs/heads/broken...symref &&
280 test_when_finished "rm -f .git/refs/heads/broken...symref" &&
281 git branch -d broken...symref >output 2>error &&
282 test_path_is_missing .git/refs/heads/broken...symref &&
283 test_i18ngrep "Deleted branch broken...symref (was refs/heads/idonotexist)" output &&
284 test_must_be_empty error
287 test_expect_success
'update-ref -d cannot delete non-ref in .git dir' '
288 echo precious >.git/my-private-file &&
289 echo precious >expect &&
290 test_must_fail git update-ref -d my-private-file >output 2>error &&
291 test_must_be_empty output &&
292 test_i18ngrep -e "refusing to update ref with bad name" error &&
293 test_cmp expect .git/my-private-file
296 test_expect_success
'update-ref -d cannot delete absolute path' '
297 git branch -f extra &&
298 test_must_fail git update-ref -d "$(pwd)/.git/refs/heads/extra" &&
299 test_cmp_rev HEAD extra
302 test_expect_success
'update-ref --stdin fails create with bad ref name' '
303 echo "create ~a refs/heads/main" >stdin &&
304 test_must_fail git update-ref --stdin <stdin 2>err &&
305 grep "fatal: invalid ref format: ~a" err
308 test_expect_success
'update-ref --stdin fails update with bad ref name' '
309 echo "update ~a refs/heads/main" >stdin &&
310 test_must_fail git update-ref --stdin <stdin 2>err &&
311 grep "fatal: invalid ref format: ~a" err
314 test_expect_success
'update-ref --stdin fails delete with bad ref name' '
315 echo "delete ~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 -z fails create with bad ref name' '
321 printf "%s\0" "create ~a " refs/heads/main >stdin &&
322 test_must_fail git update-ref -z --stdin <stdin 2>err &&
323 grep "fatal: invalid ref format: ~a " err
326 test_expect_success
'update-ref --stdin -z fails update with bad ref name' '
327 printf "%s\0" "update ~a" refs/heads/main "" >stdin &&
328 test_must_fail git update-ref -z --stdin <stdin 2>err &&
329 grep "fatal: invalid ref format: ~a" err
332 test_expect_success
'update-ref --stdin -z fails delete with bad ref name' '
333 printf "%s\0" "delete ~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
'branch rejects HEAD as a branch name' '
339 test_must_fail git branch HEAD HEAD^ &&
340 test_must_fail git show-ref refs/heads/HEAD
343 test_expect_success
'checkout -b rejects HEAD as a branch name' '
344 test_must_fail git checkout -B HEAD HEAD^ &&
345 test_must_fail git show-ref refs/heads/HEAD
348 test_expect_success
'update-ref can operate on refs/heads/HEAD' '
349 git update-ref refs/heads/HEAD HEAD^ &&
350 git show-ref refs/heads/HEAD &&
351 git update-ref -d refs/heads/HEAD &&
352 test_must_fail git show-ref refs/heads/HEAD
355 test_expect_success
'branch -d can remove refs/heads/HEAD' '
356 git update-ref refs/heads/HEAD HEAD^ &&
357 git branch -d HEAD &&
358 test_must_fail git show-ref refs/heads/HEAD
361 test_expect_success
'branch -m can rename refs/heads/HEAD' '
362 git update-ref refs/heads/HEAD HEAD^ &&
363 git branch -m HEAD tail &&
364 test_must_fail git show-ref refs/heads/HEAD &&
365 git show-ref refs/heads/tail
368 test_expect_success
'branch -d can remove refs/heads/-dash' '
369 git update-ref refs/heads/-dash HEAD^ &&
370 git branch -d -- -dash &&
371 test_must_fail git show-ref refs/heads/-dash
374 test_expect_success
'branch -m can rename refs/heads/-dash' '
375 git update-ref refs/heads/-dash HEAD^ &&
376 git branch -m -- -dash dash &&
377 test_must_fail git show-ref refs/heads/-dash &&
378 git show-ref refs/heads/dash