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