Merge branch 'ew/daemon-socket-keepalive' into maint
[git] / t / t1430-bad-ref-name.sh
1 #!/bin/sh
2
3 test_description='Test handling of ref names that check-ref-format rejects'
4 . ./test-lib.sh
5
6 test_expect_success setup '
7         test_commit one &&
8         test_commit two
9 '
10
11 test_expect_success 'fast-import: fail on invalid branch name ".badbranchname"' '
12         test_when_finished "rm -f .git/objects/pack_* .git/objects/index_*" &&
13         cat >input <<-INPUT_END &&
14                 commit .badbranchname
15                 committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
16                 data <<COMMIT
17                 corrupt
18                 COMMIT
19
20                 from refs/heads/master
21
22         INPUT_END
23         test_must_fail git fast-import <input
24 '
25
26 test_expect_success 'fast-import: fail on invalid branch name "bad[branch]name"' '
27         test_when_finished "rm -f .git/objects/pack_* .git/objects/index_*" &&
28         cat >input <<-INPUT_END &&
29                 commit bad[branch]name
30                 committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
31                 data <<COMMIT
32                 corrupt
33                 COMMIT
34
35                 from refs/heads/master
36
37         INPUT_END
38         test_must_fail git fast-import <input
39 '
40
41 test_expect_success 'git branch shows badly named ref as warning' '
42         cp .git/refs/heads/master .git/refs/heads/broken...ref &&
43         test_when_finished "rm -f .git/refs/heads/broken...ref" &&
44         git branch >output 2>error &&
45         test_i18ngrep -e "ignoring ref with broken name refs/heads/broken\.\.\.ref" error &&
46         ! grep -e "broken\.\.\.ref" output
47 '
48
49 test_expect_success 'branch -d can delete badly named ref' '
50         cp .git/refs/heads/master .git/refs/heads/broken...ref &&
51         test_when_finished "rm -f .git/refs/heads/broken...ref" &&
52         git branch -d broken...ref &&
53         git branch >output 2>error &&
54         ! grep -e "broken\.\.\.ref" error &&
55         ! grep -e "broken\.\.\.ref" output
56 '
57
58 test_expect_success 'branch -D can delete badly named ref' '
59         cp .git/refs/heads/master .git/refs/heads/broken...ref &&
60         test_when_finished "rm -f .git/refs/heads/broken...ref" &&
61         git branch -D broken...ref &&
62         git branch >output 2>error &&
63         ! grep -e "broken\.\.\.ref" error &&
64         ! grep -e "broken\.\.\.ref" output
65 '
66
67 test_expect_success 'branch -D cannot delete non-ref in .git dir' '
68         echo precious >.git/my-private-file &&
69         echo precious >expect &&
70         test_must_fail git branch -D ../../my-private-file &&
71         test_cmp expect .git/my-private-file
72 '
73
74 test_expect_success 'branch -D cannot delete ref in .git dir' '
75         git rev-parse HEAD >.git/my-private-file &&
76         git rev-parse HEAD >expect &&
77         git branch foo/legit &&
78         test_must_fail git branch -D foo////./././../../../my-private-file &&
79         test_cmp expect .git/my-private-file
80 '
81
82 test_expect_success 'branch -D cannot delete absolute path' '
83         git branch -f extra &&
84         test_must_fail git branch -D "$(pwd)/.git/refs/heads/extra" &&
85         test_cmp_rev HEAD extra
86 '
87
88 test_expect_success 'git branch cannot create a badly named ref' '
89         test_when_finished "rm -f .git/refs/heads/broken...ref" &&
90         test_must_fail git branch broken...ref &&
91         git branch >output 2>error &&
92         ! grep -e "broken\.\.\.ref" error &&
93         ! grep -e "broken\.\.\.ref" output
94 '
95
96 test_expect_success 'branch -m cannot rename to a bad ref name' '
97         test_when_finished "rm -f .git/refs/heads/broken...ref" &&
98         test_might_fail git branch -D goodref &&
99         git branch goodref &&
100         test_must_fail git branch -m goodref broken...ref &&
101         test_cmp_rev master goodref &&
102         git branch >output 2>error &&
103         ! grep -e "broken\.\.\.ref" error &&
104         ! grep -e "broken\.\.\.ref" output
105 '
106
107 test_expect_failure 'branch -m can rename from a bad ref name' '
108         cp .git/refs/heads/master .git/refs/heads/broken...ref &&
109         test_when_finished "rm -f .git/refs/heads/broken...ref" &&
110         git branch -m broken...ref renamed &&
111         test_cmp_rev master renamed &&
112         git branch >output 2>error &&
113         ! grep -e "broken\.\.\.ref" error &&
114         ! grep -e "broken\.\.\.ref" output
115 '
116
117 test_expect_success 'push cannot create a badly named ref' '
118         test_when_finished "rm -f .git/refs/heads/broken...ref" &&
119         test_must_fail git push "file://$(pwd)" HEAD:refs/heads/broken...ref &&
120         git branch >output 2>error &&
121         ! grep -e "broken\.\.\.ref" error &&
122         ! grep -e "broken\.\.\.ref" output
123 '
124
125 test_expect_failure 'push --mirror can delete badly named ref' '
126         top=$(pwd) &&
127         git init src &&
128         git init dest &&
129
130         (
131                 cd src &&
132                 test_commit one
133         ) &&
134         (
135                 cd dest &&
136                 test_commit two &&
137                 git checkout --detach &&
138                 cp .git/refs/heads/master .git/refs/heads/broken...ref
139         ) &&
140         git -C src push --mirror "file://$top/dest" &&
141         git -C dest branch >output 2>error &&
142         ! grep -e "broken\.\.\.ref" error &&
143         ! grep -e "broken\.\.\.ref" output
144 '
145
146 test_expect_success 'rev-parse skips symref pointing to broken name' '
147         test_when_finished "rm -f .git/refs/heads/broken...ref" &&
148         git branch shadow one &&
149         cp .git/refs/heads/master .git/refs/heads/broken...ref &&
150         printf "ref: refs/heads/broken...ref\n" >.git/refs/tags/shadow &&
151         test_when_finished "rm -f .git/refs/tags/shadow" &&
152         git rev-parse --verify one >expect &&
153         git rev-parse --verify shadow >actual 2>err &&
154         test_cmp expect actual &&
155         test_i18ngrep "ignoring dangling symref refs/tags/shadow" err
156 '
157
158 test_expect_success 'for-each-ref emits warnings for broken names' '
159         cp .git/refs/heads/master .git/refs/heads/broken...ref &&
160         test_when_finished "rm -f .git/refs/heads/broken...ref" &&
161         printf "ref: refs/heads/broken...ref\n" >.git/refs/heads/badname &&
162         test_when_finished "rm -f .git/refs/heads/badname" &&
163         printf "ref: refs/heads/master\n" >.git/refs/heads/broken...symref &&
164         test_when_finished "rm -f .git/refs/heads/broken...symref" &&
165         git for-each-ref >output 2>error &&
166         ! grep -e "broken\.\.\.ref" output &&
167         ! grep -e "badname" output &&
168         ! grep -e "broken\.\.\.symref" output &&
169         test_i18ngrep "ignoring ref with broken name refs/heads/broken\.\.\.ref" error &&
170         test_i18ngrep "ignoring broken ref refs/heads/badname" error &&
171         test_i18ngrep "ignoring ref with broken name refs/heads/broken\.\.\.symref" error
172 '
173
174 test_expect_success 'update-ref -d can delete broken name' '
175         cp .git/refs/heads/master .git/refs/heads/broken...ref &&
176         test_when_finished "rm -f .git/refs/heads/broken...ref" &&
177         git update-ref -d refs/heads/broken...ref >output 2>error &&
178         test_must_be_empty output &&
179         test_must_be_empty error &&
180         git branch >output 2>error &&
181         ! grep -e "broken\.\.\.ref" error &&
182         ! grep -e "broken\.\.\.ref" output
183 '
184
185 test_expect_success 'branch -d can delete broken name' '
186         cp .git/refs/heads/master .git/refs/heads/broken...ref &&
187         test_when_finished "rm -f .git/refs/heads/broken...ref" &&
188         git branch -d broken...ref >output 2>error &&
189         test_i18ngrep "Deleted branch broken...ref (was broken)" output &&
190         test_must_be_empty error &&
191         git branch >output 2>error &&
192         ! grep -e "broken\.\.\.ref" error &&
193         ! grep -e "broken\.\.\.ref" output
194 '
195
196 test_expect_success 'update-ref --no-deref -d can delete symref to broken name' '
197         cp .git/refs/heads/master .git/refs/heads/broken...ref &&
198         test_when_finished "rm -f .git/refs/heads/broken...ref" &&
199         printf "ref: refs/heads/broken...ref\n" >.git/refs/heads/badname &&
200         test_when_finished "rm -f .git/refs/heads/badname" &&
201         git update-ref --no-deref -d refs/heads/badname >output 2>error &&
202         test_path_is_missing .git/refs/heads/badname &&
203         test_must_be_empty output &&
204         test_must_be_empty error
205 '
206
207 test_expect_success 'branch -d can delete symref to broken name' '
208         cp .git/refs/heads/master .git/refs/heads/broken...ref &&
209         test_when_finished "rm -f .git/refs/heads/broken...ref" &&
210         printf "ref: refs/heads/broken...ref\n" >.git/refs/heads/badname &&
211         test_when_finished "rm -f .git/refs/heads/badname" &&
212         git branch -d badname >output 2>error &&
213         test_path_is_missing .git/refs/heads/badname &&
214         test_i18ngrep "Deleted branch badname (was refs/heads/broken\.\.\.ref)" output &&
215         test_must_be_empty error
216 '
217
218 test_expect_success 'update-ref --no-deref -d can delete dangling symref to broken name' '
219         printf "ref: refs/heads/broken...ref\n" >.git/refs/heads/badname &&
220         test_when_finished "rm -f .git/refs/heads/badname" &&
221         git update-ref --no-deref -d refs/heads/badname >output 2>error &&
222         test_path_is_missing .git/refs/heads/badname &&
223         test_must_be_empty output &&
224         test_must_be_empty error
225 '
226
227 test_expect_success 'branch -d can delete dangling symref to broken name' '
228         printf "ref: refs/heads/broken...ref\n" >.git/refs/heads/badname &&
229         test_when_finished "rm -f .git/refs/heads/badname" &&
230         git branch -d badname >output 2>error &&
231         test_path_is_missing .git/refs/heads/badname &&
232         test_i18ngrep "Deleted branch badname (was refs/heads/broken\.\.\.ref)" output &&
233         test_must_be_empty error
234 '
235
236 test_expect_success 'update-ref -d can delete broken name through symref' '
237         cp .git/refs/heads/master .git/refs/heads/broken...ref &&
238         test_when_finished "rm -f .git/refs/heads/broken...ref" &&
239         printf "ref: refs/heads/broken...ref\n" >.git/refs/heads/badname &&
240         test_when_finished "rm -f .git/refs/heads/badname" &&
241         git update-ref -d refs/heads/badname >output 2>error &&
242         test_path_is_missing .git/refs/heads/broken...ref &&
243         test_must_be_empty output &&
244         test_must_be_empty error
245 '
246
247 test_expect_success 'update-ref --no-deref -d can delete symref with broken name' '
248         printf "ref: refs/heads/master\n" >.git/refs/heads/broken...symref &&
249         test_when_finished "rm -f .git/refs/heads/broken...symref" &&
250         git update-ref --no-deref -d refs/heads/broken...symref >output 2>error &&
251         test_path_is_missing .git/refs/heads/broken...symref &&
252         test_must_be_empty output &&
253         test_must_be_empty error
254 '
255
256 test_expect_success 'branch -d can delete symref with broken name' '
257         printf "ref: refs/heads/master\n" >.git/refs/heads/broken...symref &&
258         test_when_finished "rm -f .git/refs/heads/broken...symref" &&
259         git branch -d broken...symref >output 2>error &&
260         test_path_is_missing .git/refs/heads/broken...symref &&
261         test_i18ngrep "Deleted branch broken...symref (was refs/heads/master)" output &&
262         test_must_be_empty error
263 '
264
265 test_expect_success 'update-ref --no-deref -d can delete dangling symref with broken name' '
266         printf "ref: refs/heads/idonotexist\n" >.git/refs/heads/broken...symref &&
267         test_when_finished "rm -f .git/refs/heads/broken...symref" &&
268         git update-ref --no-deref -d refs/heads/broken...symref >output 2>error &&
269         test_path_is_missing .git/refs/heads/broken...symref &&
270         test_must_be_empty output &&
271         test_must_be_empty error
272 '
273
274 test_expect_success 'branch -d can delete dangling symref with broken name' '
275         printf "ref: refs/heads/idonotexist\n" >.git/refs/heads/broken...symref &&
276         test_when_finished "rm -f .git/refs/heads/broken...symref" &&
277         git branch -d broken...symref >output 2>error &&
278         test_path_is_missing .git/refs/heads/broken...symref &&
279         test_i18ngrep "Deleted branch broken...symref (was refs/heads/idonotexist)" output &&
280         test_must_be_empty error
281 '
282
283 test_expect_success 'update-ref -d cannot delete non-ref in .git dir' '
284         echo precious >.git/my-private-file &&
285         echo precious >expect &&
286         test_must_fail git update-ref -d my-private-file >output 2>error &&
287         test_must_be_empty output &&
288         test_i18ngrep -e "cannot lock .*: unable to resolve reference" error &&
289         test_cmp expect .git/my-private-file
290 '
291
292 test_expect_success 'update-ref -d cannot delete absolute path' '
293         git branch -f extra &&
294         test_must_fail git update-ref -d "$(pwd)/.git/refs/heads/extra" &&
295         test_cmp_rev HEAD extra
296 '
297
298 test_expect_success 'update-ref --stdin fails create with bad ref name' '
299         echo "create ~a refs/heads/master" >stdin &&
300         test_must_fail git update-ref --stdin <stdin 2>err &&
301         grep "fatal: invalid ref format: ~a" err
302 '
303
304 test_expect_success 'update-ref --stdin fails update with bad ref name' '
305         echo "update ~a refs/heads/master" >stdin &&
306         test_must_fail git update-ref --stdin <stdin 2>err &&
307         grep "fatal: invalid ref format: ~a" err
308 '
309
310 test_expect_success 'update-ref --stdin fails delete with bad ref name' '
311         echo "delete ~a refs/heads/master" >stdin &&
312         test_must_fail git update-ref --stdin <stdin 2>err &&
313         grep "fatal: invalid ref format: ~a" err
314 '
315
316 test_expect_success 'update-ref --stdin -z fails create with bad ref name' '
317         printf "%s\0" "create ~a " refs/heads/master >stdin &&
318         test_must_fail git update-ref -z --stdin <stdin 2>err &&
319         grep "fatal: invalid ref format: ~a " err
320 '
321
322 test_expect_success 'update-ref --stdin -z fails update with bad ref name' '
323         printf "%s\0" "update ~a" refs/heads/master "" >stdin &&
324         test_must_fail git update-ref -z --stdin <stdin 2>err &&
325         grep "fatal: invalid ref format: ~a" err
326 '
327
328 test_expect_success 'update-ref --stdin -z fails delete with bad ref name' '
329         printf "%s\0" "delete ~a" refs/heads/master >stdin &&
330         test_must_fail git update-ref -z --stdin <stdin 2>err &&
331         grep "fatal: invalid ref format: ~a" err
332 '
333
334 test_done