Merge branch 'js/emu-write-epipe-on-windows'
[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         grep -e "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         git symbolic-ref refs/tags/shadow refs/heads/broken...ref &&
151
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.*refs/tags/shadow" err
156 '
157
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 &&
163         test_path_is_missing .git/refs/heads/badname
164 '
165
166 test_expect_success 'update-ref -d can delete broken name' '
167         cp .git/refs/heads/master .git/refs/heads/broken...ref &&
168         test_when_finished "rm -f .git/refs/heads/broken...ref" &&
169         git update-ref -d refs/heads/broken...ref &&
170         git branch >output 2>error &&
171         ! grep -e "broken\.\.\.ref" error &&
172         ! grep -e "broken\.\.\.ref" output
173 '
174
175 test_expect_success 'update-ref -d cannot delete non-ref in .git dir' '
176         echo precious >.git/my-private-file &&
177         echo precious >expect &&
178         test_must_fail git update-ref -d my-private-file &&
179         test_cmp expect .git/my-private-file
180 '
181
182 test_expect_success 'update-ref -d cannot delete absolute path' '
183         git branch -f extra &&
184         test_must_fail git update-ref -d "$(pwd)/.git/refs/heads/extra" &&
185         test_cmp_rev HEAD extra
186 '
187
188 test_expect_success 'update-ref --stdin fails create with bad ref name' '
189         echo "create ~a refs/heads/master" >stdin &&
190         test_must_fail git update-ref --stdin <stdin 2>err &&
191         grep "fatal: invalid ref format: ~a" err
192 '
193
194 test_expect_success 'update-ref --stdin fails update with bad ref name' '
195         echo "update ~a refs/heads/master" >stdin &&
196         test_must_fail git update-ref --stdin <stdin 2>err &&
197         grep "fatal: invalid ref format: ~a" err
198 '
199
200 test_expect_success 'update-ref --stdin fails delete with bad ref name' '
201         echo "delete ~a refs/heads/master" >stdin &&
202         test_must_fail git update-ref --stdin <stdin 2>err &&
203         grep "fatal: invalid ref format: ~a" err
204 '
205
206 test_expect_success 'update-ref --stdin -z fails create with bad ref name' '
207         printf "%s\0" "create ~a " refs/heads/master >stdin &&
208         test_must_fail git update-ref -z --stdin <stdin 2>err &&
209         grep "fatal: invalid ref format: ~a " err
210 '
211
212 test_expect_success 'update-ref --stdin -z fails update with bad ref name' '
213         printf "%s\0" "update ~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
216 '
217
218 test_expect_success 'update-ref --stdin -z fails delete with bad ref name' '
219         printf "%s\0" "delete ~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
222 '
223
224 test_done