Merge branch 'rs/janitorial'
[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' '
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 &&
45         grep -e "broken\.\.\.ref" output
46 '
47
48 test_expect_success 'branch -d can delete badly named ref' '
49         cp .git/refs/heads/master .git/refs/heads/broken...ref &&
50         test_when_finished "rm -f .git/refs/heads/broken...ref" &&
51         git branch -d broken...ref &&
52         git branch >output &&
53         ! grep -e "broken\.\.\.ref" output
54 '
55
56 test_expect_success 'branch -D can delete badly named ref' '
57         cp .git/refs/heads/master .git/refs/heads/broken...ref &&
58         test_when_finished "rm -f .git/refs/heads/broken...ref" &&
59         git branch -D broken...ref &&
60         git branch >output &&
61         ! grep -e "broken\.\.\.ref" output
62 '
63
64 test_expect_success 'branch -D cannot delete non-ref in .git dir' '
65         echo precious >.git/my-private-file &&
66         echo precious >expect &&
67         test_must_fail git branch -D ../../my-private-file &&
68         test_cmp expect .git/my-private-file
69 '
70
71 test_expect_success 'branch -D cannot delete ref in .git dir' '
72         git rev-parse HEAD >.git/my-private-file &&
73         git rev-parse HEAD >expect &&
74         git branch foo/legit &&
75         test_must_fail git branch -D foo////./././../../../my-private-file &&
76         test_cmp expect .git/my-private-file
77 '
78
79 test_expect_success 'branch -D cannot delete absolute path' '
80         git branch -f extra &&
81         test_must_fail git branch -D "$(pwd)/.git/refs/heads/extra" &&
82         test_cmp_rev HEAD extra
83 '
84
85 test_expect_success 'git branch cannot create a badly named ref' '
86         test_when_finished "rm -f .git/refs/heads/broken...ref" &&
87         test_must_fail git branch broken...ref &&
88         git branch >output &&
89         ! grep -e "broken\.\.\.ref" output
90 '
91
92 test_expect_success 'branch -m cannot rename to a bad ref name' '
93         test_when_finished "rm -f .git/refs/heads/broken...ref" &&
94         test_might_fail git branch -D goodref &&
95         git branch goodref &&
96         test_must_fail git branch -m goodref broken...ref &&
97         test_cmp_rev master goodref &&
98         git branch >output &&
99         ! grep -e "broken\.\.\.ref" output
100 '
101
102 test_expect_failure 'branch -m can rename from a bad ref name' '
103         cp .git/refs/heads/master .git/refs/heads/broken...ref &&
104         test_when_finished "rm -f .git/refs/heads/broken...ref" &&
105         git branch -m broken...ref renamed &&
106         test_cmp_rev master renamed &&
107         git branch >output &&
108         ! grep -e "broken\.\.\.ref" output
109 '
110
111 test_expect_success 'push cannot create a badly named ref' '
112         test_when_finished "rm -f .git/refs/heads/broken...ref" &&
113         test_must_fail git push "file://$(pwd)" HEAD:refs/heads/broken...ref &&
114         git branch >output &&
115         ! grep -e "broken\.\.\.ref" output
116 '
117
118 test_expect_failure 'push --mirror can delete badly named ref' '
119         top=$(pwd) &&
120         git init src &&
121         git init dest &&
122
123         (
124                 cd src &&
125                 test_commit one
126         ) &&
127         (
128                 cd dest &&
129                 test_commit two &&
130                 git checkout --detach &&
131                 cp .git/refs/heads/master .git/refs/heads/broken...ref
132         ) &&
133         git -C src push --mirror "file://$top/dest" &&
134         git -C dest branch >output &&
135         ! grep -e "broken\.\.\.ref" output
136 '
137
138 test_expect_success 'rev-parse skips symref pointing to broken name' '
139         test_when_finished "rm -f .git/refs/heads/broken...ref" &&
140         git branch shadow one &&
141         cp .git/refs/heads/master .git/refs/heads/broken...ref &&
142         git symbolic-ref refs/tags/shadow refs/heads/broken...ref &&
143
144         git rev-parse --verify one >expect &&
145         git rev-parse --verify shadow >actual 2>err &&
146         test_cmp expect actual &&
147         test_i18ngrep "ignoring.*refs/tags/shadow" err
148 '
149
150 test_expect_success 'update-ref --no-deref -d can delete reference to broken name' '
151         git symbolic-ref refs/heads/badname refs/heads/broken...ref &&
152         test_when_finished "rm -f .git/refs/heads/badname" &&
153         test_path_is_file .git/refs/heads/badname &&
154         git update-ref --no-deref -d refs/heads/badname &&
155         test_path_is_missing .git/refs/heads/badname
156 '
157
158 test_expect_success 'update-ref -d can delete broken name' '
159         cp .git/refs/heads/master .git/refs/heads/broken...ref &&
160         test_when_finished "rm -f .git/refs/heads/broken...ref" &&
161         git update-ref -d refs/heads/broken...ref &&
162         git branch >output &&
163         ! grep -e "broken\.\.\.ref" output
164 '
165
166 test_expect_success 'update-ref -d cannot delete non-ref in .git dir' '
167         echo precious >.git/my-private-file &&
168         echo precious >expect &&
169         test_must_fail git update-ref -d my-private-file &&
170         test_cmp expect .git/my-private-file
171 '
172
173 test_expect_success 'update-ref -d cannot delete absolute path' '
174         git branch -f extra &&
175         test_must_fail git update-ref -d "$(pwd)/.git/refs/heads/extra" &&
176         test_cmp_rev HEAD extra
177 '
178
179 test_expect_success 'update-ref --stdin fails create with bad ref name' '
180         echo "create ~a refs/heads/master" >stdin &&
181         test_must_fail git update-ref --stdin <stdin 2>err &&
182         grep "fatal: invalid ref format: ~a" err
183 '
184
185 test_expect_success 'update-ref --stdin fails update with bad ref name' '
186         echo "update ~a refs/heads/master" >stdin &&
187         test_must_fail git update-ref --stdin <stdin 2>err &&
188         grep "fatal: invalid ref format: ~a" err
189 '
190
191 test_expect_success 'update-ref --stdin fails delete with bad ref name' '
192         echo "delete ~a refs/heads/master" >stdin &&
193         test_must_fail git update-ref --stdin <stdin 2>err &&
194         grep "fatal: invalid ref format: ~a" err
195 '
196
197 test_expect_success 'update-ref --stdin -z fails create with bad ref name' '
198         printf "%s\0" "create ~a " refs/heads/master >stdin &&
199         test_must_fail git update-ref -z --stdin <stdin 2>err &&
200         grep "fatal: invalid ref format: ~a " err
201 '
202
203 test_expect_success 'update-ref --stdin -z fails update with bad ref name' '
204         printf "%s\0" "update ~a" refs/heads/master "" >stdin &&
205         test_must_fail git update-ref -z --stdin <stdin 2>err &&
206         grep "fatal: invalid ref format: ~a" err
207 '
208
209 test_expect_success 'update-ref --stdin -z fails delete with bad ref name' '
210         printf "%s\0" "delete ~a" refs/heads/master >stdin &&
211         test_must_fail git update-ref -z --stdin <stdin 2>err &&
212         grep "fatal: invalid ref format: ~a" err
213 '
214
215 test_done