t1430: test for-each-ref in the presence of badly-named refs
[git/git.git] / t / t1430-bad-ref-name.sh
CommitLineData
8159f4af
RS
1#!/bin/sh
2
3test_description='Test handling of ref names that check-ref-format rejects'
4. ./test-lib.sh
5
6test_expect_success setup '
d0f810f0
RS
7 test_commit one &&
8 test_commit two
8159f4af
RS
9'
10
11test_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
26test_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
aedcb7dc 41test_expect_success 'git branch shows badly named ref as warning' '
d0f810f0
RS
42 cp .git/refs/heads/master .git/refs/heads/broken...ref &&
43 test_when_finished "rm -f .git/refs/heads/broken...ref" &&
aedcb7dc 44 git branch >output 2>error &&
f86d8350 45 test_i18ngrep -e "ignoring ref with broken name refs/heads/broken\.\.\.ref" error &&
aedcb7dc 46 ! grep -e "broken\.\.\.ref" output
d0f810f0
RS
47'
48
49test_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 &&
aedcb7dc
KN
53 git branch >output 2>error &&
54 ! grep -e "broken\.\.\.ref" error &&
d0f810f0
RS
55 ! grep -e "broken\.\.\.ref" output
56'
57
58test_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 &&
aedcb7dc
KN
62 git branch >output 2>error &&
63 ! grep -e "broken\.\.\.ref" error &&
d0f810f0
RS
64 ! grep -e "broken\.\.\.ref" output
65'
66
67test_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
a3372926
JK
74test_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
d0f810f0
RS
82test_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
88test_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 &&
aedcb7dc
KN
91 git branch >output 2>error &&
92 ! grep -e "broken\.\.\.ref" error &&
d0f810f0
RS
93 ! grep -e "broken\.\.\.ref" output
94'
95
96test_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 &&
aedcb7dc
KN
102 git branch >output 2>error &&
103 ! grep -e "broken\.\.\.ref" error &&
d0f810f0
RS
104 ! grep -e "broken\.\.\.ref" output
105'
106
107test_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 &&
aedcb7dc
KN
112 git branch >output 2>error &&
113 ! grep -e "broken\.\.\.ref" error &&
d0f810f0
RS
114 ! grep -e "broken\.\.\.ref" output
115'
116
117test_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 &&
aedcb7dc
KN
120 git branch >output 2>error &&
121 ! grep -e "broken\.\.\.ref" error &&
d0f810f0
RS
122 ! grep -e "broken\.\.\.ref" output
123'
124
125test_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" &&
aedcb7dc
KN
141 git -C dest branch >output 2>error &&
142 ! grep -e "broken\.\.\.ref" error &&
d0f810f0
RS
143 ! grep -e "broken\.\.\.ref" output
144'
145
146test_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 &&
6141a6dc 150 printf "ref: refs/heads/broken...ref\n" >.git/refs/tags/shadow &&
45669a79 151 test_when_finished "rm -f .git/refs/tags/shadow" &&
d0f810f0
RS
152 git rev-parse --verify one >expect &&
153 git rev-parse --verify shadow >actual 2>err &&
154 test_cmp expect actual &&
f86d8350 155 test_i18ngrep "ignoring dangling symref refs/tags/shadow" err
d0f810f0
RS
156'
157
b78ceced
MH
158test_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
8159f4af 174test_expect_success 'update-ref --no-deref -d can delete reference to broken name' '
6141a6dc 175 printf "ref: refs/heads/broken...ref\n" >.git/refs/heads/badname &&
8159f4af
RS
176 test_when_finished "rm -f .git/refs/heads/badname" &&
177 test_path_is_file .git/refs/heads/badname &&
f86d8350
MH
178 git update-ref --no-deref -d refs/heads/badname >output 2>error &&
179 test_path_is_missing .git/refs/heads/badname &&
180 test_must_be_empty output &&
181 test_must_be_empty error
8159f4af
RS
182'
183
d0f810f0
RS
184test_expect_success 'update-ref -d can delete broken name' '
185 cp .git/refs/heads/master .git/refs/heads/broken...ref &&
186 test_when_finished "rm -f .git/refs/heads/broken...ref" &&
f86d8350
MH
187 git update-ref -d refs/heads/broken...ref >output 2>error &&
188 test_must_be_empty output &&
189 test_must_be_empty error &&
aedcb7dc
KN
190 git branch >output 2>error &&
191 ! grep -e "broken\.\.\.ref" error &&
d0f810f0
RS
192 ! grep -e "broken\.\.\.ref" output
193'
194
195test_expect_success 'update-ref -d cannot delete non-ref in .git dir' '
196 echo precious >.git/my-private-file &&
197 echo precious >expect &&
f86d8350
MH
198 test_must_fail git update-ref -d my-private-file >output 2>error &&
199 test_must_be_empty output &&
200 test_i18ngrep -e "cannot lock .*: unable to resolve reference" error &&
d0f810f0
RS
201 test_cmp expect .git/my-private-file
202'
203
204test_expect_success 'update-ref -d cannot delete absolute path' '
205 git branch -f extra &&
206 test_must_fail git update-ref -d "$(pwd)/.git/refs/heads/extra" &&
207 test_cmp_rev HEAD extra
208'
209
8159f4af
RS
210test_expect_success 'update-ref --stdin fails create with bad ref name' '
211 echo "create ~a refs/heads/master" >stdin &&
212 test_must_fail git update-ref --stdin <stdin 2>err &&
213 grep "fatal: invalid ref format: ~a" err
214'
215
216test_expect_success 'update-ref --stdin fails update with bad ref name' '
217 echo "update ~a refs/heads/master" >stdin &&
218 test_must_fail git update-ref --stdin <stdin 2>err &&
219 grep "fatal: invalid ref format: ~a" err
220'
221
222test_expect_success 'update-ref --stdin fails delete with bad ref name' '
223 echo "delete ~a refs/heads/master" >stdin &&
224 test_must_fail git update-ref --stdin <stdin 2>err &&
225 grep "fatal: invalid ref format: ~a" err
226'
227
228test_expect_success 'update-ref --stdin -z fails create with bad ref name' '
229 printf "%s\0" "create ~a " refs/heads/master >stdin &&
230 test_must_fail git update-ref -z --stdin <stdin 2>err &&
231 grep "fatal: invalid ref format: ~a " err
232'
233
234test_expect_success 'update-ref --stdin -z fails update with bad ref name' '
235 printf "%s\0" "update ~a" refs/heads/master "" >stdin &&
236 test_must_fail git update-ref -z --stdin <stdin 2>err &&
237 grep "fatal: invalid ref format: ~a" err
238'
239
240test_expect_success 'update-ref --stdin -z fails delete with bad ref name' '
241 printf "%s\0" "delete ~a" refs/heads/master >stdin &&
242 test_must_fail git update-ref -z --stdin <stdin 2>err &&
243 grep "fatal: invalid ref format: ~a" err
244'
245
246test_done