3 test_description='Test handling of ref names that check-ref-format rejects'
6 test_expect_success setup '
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 &&
15 committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
20 from refs/heads/master
23 test_must_fail git fast-import <input
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
35 from refs/heads/master
38 test_must_fail git fast-import <input
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
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
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
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
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
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
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
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 &&
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
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
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
125 test_expect_failure 'push --mirror can delete badly named ref' '
137 git checkout --detach &&
138 cp .git/refs/heads/master .git/refs/heads/broken...ref
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
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 git symbolic-ref refs/tags/shadow refs/heads/broken...ref &&
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
158 test_expect_success 'update-ref --no-deref -d can delete reference to broken name' '
159 git symbolic-ref refs/heads/badname refs/heads/broken...ref &&
160 test_when_finished "rm -f .git/refs/heads/badname" &&
161 test_path_is_file .git/refs/heads/badname &&
162 git update-ref --no-deref -d refs/heads/badname >output 2>error &&
163 test_path_is_missing .git/refs/heads/badname &&
164 test_must_be_empty output &&
165 test_must_be_empty error
168 test_expect_success 'update-ref -d can delete broken name' '
169 cp .git/refs/heads/master .git/refs/heads/broken...ref &&
170 test_when_finished "rm -f .git/refs/heads/broken...ref" &&
171 git update-ref -d refs/heads/broken...ref >output 2>error &&
172 test_must_be_empty output &&
173 test_must_be_empty error &&
174 git branch >output 2>error &&
175 ! grep -e "broken\.\.\.ref" error &&
176 ! grep -e "broken\.\.\.ref" output
179 test_expect_success 'update-ref -d cannot delete non-ref in .git dir' '
180 echo precious >.git/my-private-file &&
181 echo precious >expect &&
182 test_must_fail git update-ref -d my-private-file >output 2>error &&
183 test_must_be_empty output &&
184 test_i18ngrep -e "cannot lock .*: unable to resolve reference" error &&
185 test_cmp expect .git/my-private-file
188 test_expect_success 'update-ref -d cannot delete absolute path' '
189 git branch -f extra &&
190 test_must_fail git update-ref -d "$(pwd)/.git/refs/heads/extra" &&
191 test_cmp_rev HEAD extra
194 test_expect_success 'update-ref --stdin fails create with bad ref name' '
195 echo "create ~a refs/heads/master" >stdin &&
196 test_must_fail git update-ref --stdin <stdin 2>err &&
197 grep "fatal: invalid ref format: ~a" err
200 test_expect_success 'update-ref --stdin fails update with bad ref name' '
201 echo "update ~a refs/heads/master" >stdin &&
202 test_must_fail git update-ref --stdin <stdin 2>err &&
203 grep "fatal: invalid ref format: ~a" err
206 test_expect_success 'update-ref --stdin fails delete with bad ref name' '
207 echo "delete ~a refs/heads/master" >stdin &&
208 test_must_fail git update-ref --stdin <stdin 2>err &&
209 grep "fatal: invalid ref format: ~a" err
212 test_expect_success 'update-ref --stdin -z fails create with bad ref name' '
213 printf "%s\0" "create ~a " refs/heads/master >stdin &&
214 test_must_fail git update-ref -z --stdin <stdin 2>err &&
215 grep "fatal: invalid ref format: ~a " err
218 test_expect_success 'update-ref --stdin -z fails update with bad ref name' '
219 printf "%s\0" "update ~a" refs/heads/master "" >stdin &&
220 test_must_fail git update-ref -z --stdin <stdin 2>err &&
221 grep "fatal: invalid ref format: ~a" err
224 test_expect_success 'update-ref --stdin -z fails delete with bad ref name' '
225 printf "%s\0" "delete ~a" refs/heads/master >stdin &&
226 test_must_fail git update-ref -z --stdin <stdin 2>err &&
227 grep "fatal: invalid ref format: ~a" err