]>
Commit | Line | Data |
---|---|---|
009fee47 MM |
1 | #!/bin/sh |
2 | ||
3 | test_description='test git rev-parse diagnosis for invalid argument' | |
4 | ||
5 | exec </dev/null | |
6 | ||
7 | . ./test-lib.sh | |
8 | ||
34df9abb MG |
9 | test_did_you_mean () |
10 | { | |
365c2aaa | 11 | cat >expected <<-EOF && |
b0418303 JK |
12 | fatal: path '$2$3' $4, but not ${5:-$SQ$3$SQ} |
13 | hint: Did you mean '$1:$2$3'${2:+ aka $SQ$1:./$3$SQ}? | |
365c2aaa | 14 | EOF |
b0418303 | 15 | test_i18ncmp expected error |
34df9abb MG |
16 | } |
17 | ||
009fee47 MM |
18 | HASH_file= |
19 | ||
20 | test_expect_success 'set up basic repo' ' | |
21 | echo one > file.txt && | |
22 | mkdir subdir && | |
23 | echo two > subdir/file.txt && | |
24 | echo three > subdir/file2.txt && | |
25 | git add . && | |
26 | git commit -m init && | |
27 | echo four > index-only.txt && | |
28 | git add index-only.txt && | |
29 | echo five > disk-only.txt | |
30 | ' | |
31 | ||
32 | test_expect_success 'correct file objects' ' | |
33 | HASH_file=$(git rev-parse HEAD:file.txt) && | |
34 | git rev-parse HEAD:subdir/file.txt && | |
35 | git rev-parse :index-only.txt && | |
36 | (cd subdir && | |
37 | git rev-parse HEAD:subdir/file2.txt && | |
38 | test $HASH_file = $(git rev-parse HEAD:file.txt) && | |
39 | test $HASH_file = $(git rev-parse :file.txt) && | |
40 | test $HASH_file = $(git rev-parse :0:file.txt) ) | |
41 | ' | |
42 | ||
979f7929 NTND |
43 | test_expect_success 'correct relative file objects (0)' ' |
44 | git rev-parse :file.txt >expected && | |
45 | git rev-parse :./file.txt >result && | |
3d6e0f74 JH |
46 | test_cmp expected result && |
47 | git rev-parse :0:./file.txt >result && | |
979f7929 NTND |
48 | test_cmp expected result |
49 | ' | |
50 | ||
51 | test_expect_success 'correct relative file objects (1)' ' | |
52 | git rev-parse HEAD:file.txt >expected && | |
53 | git rev-parse HEAD:./file.txt >result && | |
54 | test_cmp expected result | |
55 | ' | |
56 | ||
57 | test_expect_success 'correct relative file objects (2)' ' | |
58 | ( | |
59 | cd subdir && | |
60 | git rev-parse HEAD:../file.txt >result && | |
61 | test_cmp ../expected result | |
62 | ) | |
63 | ' | |
64 | ||
65 | test_expect_success 'correct relative file objects (3)' ' | |
66 | ( | |
67 | cd subdir && | |
68 | git rev-parse HEAD:../subdir/../file.txt >result && | |
69 | test_cmp ../expected result | |
70 | ) | |
71 | ' | |
72 | ||
73 | test_expect_success 'correct relative file objects (4)' ' | |
74 | git rev-parse HEAD:subdir/file.txt >expected && | |
75 | ( | |
76 | cd subdir && | |
77 | git rev-parse HEAD:./file.txt >result && | |
78 | test_cmp ../expected result | |
79 | ) | |
80 | ' | |
81 | ||
3d6e0f74 JH |
82 | test_expect_success 'correct relative file objects (5)' ' |
83 | git rev-parse :subdir/file.txt >expected && | |
84 | ( | |
85 | cd subdir && | |
86 | git rev-parse :./file.txt >result && | |
87 | test_cmp ../expected result && | |
88 | git rev-parse :0:./file.txt >result && | |
89 | test_cmp ../expected result | |
90 | ) | |
91 | ' | |
92 | ||
93 | test_expect_success 'correct relative file objects (6)' ' | |
94 | git rev-parse :file.txt >expected && | |
95 | ( | |
96 | cd subdir && | |
97 | git rev-parse :../file.txt >result && | |
98 | test_cmp ../expected result && | |
99 | git rev-parse :0:../file.txt >result && | |
100 | test_cmp ../expected result | |
101 | ) | |
102 | ' | |
103 | ||
009fee47 MM |
104 | test_expect_success 'incorrect revision id' ' |
105 | test_must_fail git rev-parse foobar:file.txt 2>error && | |
b0418303 | 106 | test_i18ngrep "invalid object name .foobar." error && |
bc3f657f | 107 | test_must_fail git rev-parse foobar 2>error && |
ab33a76e | 108 | test_i18ngrep "unknown revision or path not in the working tree." error |
009fee47 MM |
109 | ' |
110 | ||
111 | test_expect_success 'incorrect file in sha1:path' ' | |
bc3f657f | 112 | test_must_fail git rev-parse HEAD:nothing.txt 2>error && |
b0418303 | 113 | test_i18ngrep "path .nothing.txt. does not exist in .HEAD." error && |
bc3f657f | 114 | test_must_fail git rev-parse HEAD:index-only.txt 2>error && |
b0418303 | 115 | test_i18ngrep "path .index-only.txt. exists on disk, but not in .HEAD." error && |
009fee47 | 116 | (cd subdir && |
bc3f657f | 117 | test_must_fail git rev-parse HEAD:file2.txt 2>error && |
34df9abb | 118 | test_did_you_mean HEAD subdir/ file2.txt exists ) |
009fee47 MM |
119 | ' |
120 | ||
121 | test_expect_success 'incorrect file in :path and :N:path' ' | |
bc3f657f | 122 | test_must_fail git rev-parse :nothing.txt 2>error && |
b0418303 | 123 | test_i18ngrep "path .nothing.txt. does not exist (neither on disk nor in the index)" error && |
bc3f657f | 124 | test_must_fail git rev-parse :1:nothing.txt 2>error && |
b0418303 | 125 | test_i18ngrep "path .nothing.txt. does not exist (neither on disk nor in the index)" error && |
bc3f657f | 126 | test_must_fail git rev-parse :1:file.txt 2>error && |
34df9abb | 127 | test_did_you_mean ":0" "" file.txt "is in the index" "at stage 1" && |
009fee47 | 128 | (cd subdir && |
bc3f657f | 129 | test_must_fail git rev-parse :1:file.txt 2>error && |
34df9abb | 130 | test_did_you_mean ":0" "" file.txt "is in the index" "at stage 1" && |
bc3f657f | 131 | test_must_fail git rev-parse :file2.txt 2>error && |
34df9abb | 132 | test_did_you_mean ":0" subdir/ file2.txt "is in the index" && |
bc3f657f | 133 | test_must_fail git rev-parse :2:file2.txt 2>error && |
34df9abb | 134 | test_did_you_mean :0 subdir/ file2.txt "is in the index") && |
bc3f657f | 135 | test_must_fail git rev-parse :disk-only.txt 2>error && |
b0418303 | 136 | test_i18ngrep "path .disk-only.txt. exists on disk, but not in the index" error |
009fee47 MM |
137 | ' |
138 | ||
9c46c054 JS |
139 | test_expect_success 'invalid @{n} reference' ' |
140 | test_must_fail git rev-parse master@{99999} >output 2>error && | |
213dabf4 | 141 | test_must_be_empty output && |
b0418303 | 142 | test_i18ngrep "log for [^ ]* only has [0-9][0-9]* entries" error && |
9c46c054 | 143 | test_must_fail git rev-parse --verify master@{99999} >output 2>error && |
213dabf4 | 144 | test_must_be_empty output && |
b0418303 | 145 | test_i18ngrep "log for [^ ]* only has [0-9][0-9]* entries" error |
9c46c054 JS |
146 | ' |
147 | ||
979f7929 NTND |
148 | test_expect_success 'relative path not found' ' |
149 | ( | |
150 | cd subdir && | |
151 | test_must_fail git rev-parse HEAD:./nonexistent.txt 2>error && | |
b0418303 | 152 | test_i18ngrep subdir/nonexistent.txt error |
979f7929 NTND |
153 | ) |
154 | ' | |
155 | ||
156 | test_expect_success 'relative path outside worktree' ' | |
157 | test_must_fail git rev-parse HEAD:../file.txt >output 2>error && | |
213dabf4 | 158 | test_must_be_empty output && |
fc045fe7 | 159 | test_i18ngrep "outside repository" error |
979f7929 NTND |
160 | ' |
161 | ||
162 | test_expect_success 'relative path when cwd is outside worktree' ' | |
163 | test_must_fail git --git-dir=.git --work-tree=subdir rev-parse HEAD:./file.txt >output 2>error && | |
213dabf4 | 164 | test_must_be_empty output && |
b0418303 | 165 | test_i18ngrep "relative path syntax can.t be used outside working tree" error |
979f7929 NTND |
166 | ' |
167 | ||
d7236c43 MM |
168 | test_expect_success '<commit>:file correctly diagnosed after a pathname' ' |
169 | test_must_fail git rev-parse file.txt HEAD:file.txt 1>actual 2>error && | |
170 | test_i18ngrep ! "exists on disk" error && | |
023e37c3 | 171 | test_i18ngrep "no such path in the working tree" error && |
d7236c43 MM |
172 | cat >expect <<-\EOF && |
173 | file.txt | |
174 | HEAD:file.txt | |
175 | EOF | |
176 | test_cmp expect actual | |
177 | ' | |
178 | ||
003c84f6 JH |
179 | test_expect_success 'dotdot is not an empty set' ' |
180 | ( H=$(git rev-parse HEAD) && echo $H && echo ^$H ) >expect && | |
181 | ||
182 | git rev-parse HEAD.. >actual && | |
183 | test_cmp expect actual && | |
184 | ||
185 | git rev-parse ..HEAD >actual && | |
186 | test_cmp expect actual && | |
187 | ||
188 | echo .. >expect && | |
189 | git rev-parse .. >actual && | |
190 | test_cmp expect actual | |
191 | ' | |
192 | ||
9f0be821 JH |
193 | test_expect_success 'dotdot does not peel endpoints' ' |
194 | git tag -a -m "annote" annotated HEAD && | |
195 | A=$(git rev-parse annotated) && | |
196 | H=$(git rev-parse annotated^0) && | |
197 | { | |
198 | echo $A && echo ^$A | |
199 | } >expect-with-two-dots && | |
200 | { | |
201 | echo $A && echo $A && echo ^$H | |
202 | } >expect-with-merge-base && | |
203 | ||
204 | git rev-parse annotated..annotated >actual-with-two-dots && | |
205 | test_cmp expect-with-two-dots actual-with-two-dots && | |
206 | ||
207 | git rev-parse annotated...annotated >actual-with-merge-base && | |
208 | test_cmp expect-with-merge-base actual-with-merge-base | |
209 | ' | |
210 | ||
14185673 JK |
211 | test_expect_success 'arg before dashdash must be a revision (missing)' ' |
212 | test_must_fail git rev-parse foobar -- 2>stderr && | |
213 | test_i18ngrep "bad revision" stderr | |
214 | ' | |
215 | ||
216 | test_expect_success 'arg before dashdash must be a revision (file)' ' | |
217 | >foobar && | |
218 | test_must_fail git rev-parse foobar -- 2>stderr && | |
219 | test_i18ngrep "bad revision" stderr | |
220 | ' | |
221 | ||
222 | test_expect_success 'arg before dashdash must be a revision (ambiguous)' ' | |
223 | >foobar && | |
224 | git update-ref refs/heads/foobar HEAD && | |
225 | { | |
226 | # we do not want to use rev-parse here, because | |
227 | # we are testing it | |
cdb73ca5 | 228 | git show-ref -s refs/heads/foobar && |
14185673 JK |
229 | printf "%s\n" -- |
230 | } >expect && | |
231 | git rev-parse foobar -- >actual && | |
232 | test_cmp expect actual | |
233 | ' | |
234 | ||
59fa5f5a | 235 | test_expect_success 'reject Nth parent if N is too high' ' |
a678df1b RS |
236 | test_must_fail git rev-parse HEAD^100000000000000000000000000000000 |
237 | ' | |
238 | ||
59fa5f5a | 239 | test_expect_success 'reject Nth ancestor if N is too high' ' |
a678df1b RS |
240 | test_must_fail git rev-parse HEAD~100000000000000000000000000000000 |
241 | ' | |
242 | ||
39e21c6e JK |
243 | test_expect_success 'pathspecs with wildcards are not ambiguous' ' |
244 | echo "*.c" >expect && | |
245 | git rev-parse "*.c" >actual && | |
246 | test_cmp expect actual | |
247 | ' | |
248 | ||
249 | test_expect_success 'backslash does not trigger wildcard rule' ' | |
250 | test_must_fail git rev-parse "foo\\bar" | |
251 | ' | |
252 | ||
253 | test_expect_success 'escaped char does not trigger wildcard rule' ' | |
254 | test_must_fail git rev-parse "foo\\*bar" | |
255 | ' | |
256 | ||
e05e2ae8 JK |
257 | test_expect_success 'arg after dashdash not interpreted as option' ' |
258 | cat >expect <<-\EOF && | |
259 | -- | |
260 | --local-env-vars | |
261 | EOF | |
262 | git rev-parse -- --local-env-vars >actual && | |
263 | test_cmp expect actual | |
264 | ' | |
265 | ||
3a1f91cf JK |
266 | test_expect_success 'arg after end-of-options not interpreted as option' ' |
267 | test_must_fail git rev-parse --end-of-options --not-real -- 2>err && | |
268 | test_i18ngrep bad.revision.*--not-real err | |
269 | ' | |
270 | ||
271 | test_expect_success 'end-of-options still allows --' ' | |
272 | cat >expect <<-EOF && | |
273 | --end-of-options | |
274 | $(git rev-parse --verify HEAD) | |
275 | -- | |
276 | path | |
277 | EOF | |
278 | git rev-parse --end-of-options HEAD -- path >actual && | |
279 | test_cmp expect actual | |
280 | ' | |
281 | ||
009fee47 | 282 | test_done |