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