Git 2.45
[git/gitster.git] / t / t1506-rev-parse-diagnosis.sh
blobef40511d8972924fed8abc1e8cb29f9bcf0cda77
1 #!/bin/sh
3 test_description='test git rev-parse diagnosis for invalid argument'
5 exec </dev/null
7 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
8 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
10 TEST_PASSES_SANITIZE_LEAK=true
11 . ./test-lib.sh
13 test_did_you_mean ()
15 cat >expected <<-EOF &&
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}?
18 EOF
19 test_cmp expected error
22 HASH_file=
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
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) )
47 test_expect_success 'correct relative file objects (0)' '
48 git rev-parse :file.txt >expected &&
49 git rev-parse :./file.txt >result &&
50 test_cmp expected result &&
51 git rev-parse :0:./file.txt >result &&
52 test_cmp expected result
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
61 test_expect_success 'correct relative file objects (2)' '
63 cd subdir &&
64 git rev-parse HEAD:../file.txt >result &&
65 test_cmp ../expected result
69 test_expect_success 'correct relative file objects (3)' '
71 cd subdir &&
72 git rev-parse HEAD:../subdir/../file.txt >result &&
73 test_cmp ../expected result
77 test_expect_success 'correct relative file objects (4)' '
78 git rev-parse HEAD:subdir/file.txt >expected &&
80 cd subdir &&
81 git rev-parse HEAD:./file.txt >result &&
82 test_cmp ../expected result
86 test_expect_success 'correct relative file objects (5)' '
87 git rev-parse :subdir/file.txt >expected &&
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
97 test_expect_success 'correct relative file objects (6)' '
98 git rev-parse :file.txt >expected &&
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
108 test_expect_success 'incorrect revision id' '
109 test_must_fail git rev-parse foobar:file.txt 2>error &&
110 test_grep "invalid object name .foobar." error &&
111 test_must_fail git rev-parse foobar 2>error &&
112 test_grep "unknown revision or path not in the working tree." error
115 test_expect_success 'incorrect file in sha1:path' '
116 test_must_fail git rev-parse HEAD:nothing.txt 2>error &&
117 test_grep "path .nothing.txt. does not exist in .HEAD." error &&
118 test_must_fail git rev-parse HEAD:index-only.txt 2>error &&
119 test_grep "path .index-only.txt. exists on disk, but not in .HEAD." error &&
120 (cd subdir &&
121 test_must_fail git rev-parse HEAD:file2.txt 2>error &&
122 test_did_you_mean HEAD subdir/ file2.txt exists )
125 test_expect_success 'incorrect file in :path and :N:path' '
126 test_must_fail git rev-parse :nothing.txt 2>error &&
127 test_grep "path .nothing.txt. does not exist (neither on disk nor in the index)" error &&
128 test_must_fail git rev-parse :1:nothing.txt 2>error &&
129 test_grep "path .nothing.txt. does not exist (neither on disk nor in the index)" error &&
130 test_must_fail git rev-parse :1:file.txt 2>error &&
131 test_did_you_mean ":0" "" file.txt "is in the index" "at stage 1" &&
132 (cd subdir &&
133 test_must_fail git rev-parse :1:file.txt 2>error &&
134 test_did_you_mean ":0" "" file.txt "is in the index" "at stage 1" &&
135 test_must_fail git rev-parse :file2.txt 2>error &&
136 test_did_you_mean ":0" subdir/ file2.txt "is in the index" &&
137 test_must_fail git rev-parse :2:file2.txt 2>error &&
138 test_did_you_mean :0 subdir/ file2.txt "is in the index") &&
139 test_must_fail git rev-parse :disk-only.txt 2>error &&
140 test_grep "path .disk-only.txt. exists on disk, but not in the index" error
143 test_expect_success 'invalid @{n} reference' '
144 test_must_fail git rev-parse main@{99999} >output 2>error &&
145 test_must_be_empty output &&
146 test_grep "log for [^ ]* only has [0-9][0-9]* entries" error &&
147 test_must_fail git rev-parse --verify main@{99999} >output 2>error &&
148 test_must_be_empty output &&
149 test_grep "log for [^ ]* only has [0-9][0-9]* entries" error
152 test_expect_success 'relative path not found' '
154 cd subdir &&
155 test_must_fail git rev-parse HEAD:./nonexistent.txt 2>error &&
156 test_grep subdir/nonexistent.txt error
160 test_expect_success 'relative path outside worktree' '
161 test_must_fail git rev-parse HEAD:../file.txt >output 2>error &&
162 test_must_be_empty output &&
163 test_grep "outside repository" error
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 &&
168 test_must_be_empty output &&
169 test_grep "relative path syntax can.t be used outside working tree" error
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 &&
174 test_grep ! "exists on disk" error &&
175 test_grep "no such path in the working tree" error &&
176 cat >expect <<-\EOF &&
177 file.txt
178 HEAD:file.txt
180 test_cmp expect actual
183 test_expect_success 'dotdot is not an empty set' '
184 ( H=$(git rev-parse HEAD) && echo $H && echo ^$H ) >expect &&
186 git rev-parse HEAD.. >actual &&
187 test_cmp expect actual &&
189 git rev-parse ..HEAD >actual &&
190 test_cmp expect actual &&
192 echo .. >expect &&
193 git rev-parse .. >actual &&
194 test_cmp expect actual
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) &&
202 echo $A && echo ^$A
203 } >expect-with-two-dots &&
205 echo $A && echo $A && echo ^$H
206 } >expect-with-merge-base &&
208 git rev-parse annotated..annotated >actual-with-two-dots &&
209 test_cmp expect-with-two-dots actual-with-two-dots &&
211 git rev-parse annotated...annotated >actual-with-merge-base &&
212 test_cmp expect-with-merge-base actual-with-merge-base
215 test_expect_success 'arg before dashdash must be a revision (missing)' '
216 test_must_fail git rev-parse foobar -- 2>stderr &&
217 test_grep "bad revision" stderr
220 test_expect_success 'arg before dashdash must be a revision (file)' '
221 >foobar &&
222 test_must_fail git rev-parse foobar -- 2>stderr &&
223 test_grep "bad revision" stderr
226 test_expect_success 'arg before dashdash must be a revision (ambiguous)' '
227 >foobar &&
228 git update-ref refs/heads/foobar HEAD &&
230 # we do not want to use rev-parse here, because
231 # we are testing it
232 git show-ref -s refs/heads/foobar &&
233 printf "%s\n" --
234 } >expect &&
235 git rev-parse foobar -- >actual &&
236 test_cmp expect actual
239 test_expect_success 'reject Nth parent if N is too high' '
240 test_must_fail git rev-parse HEAD^100000000000000000000000000000000
243 test_expect_success 'reject Nth ancestor if N is too high' '
244 test_must_fail git rev-parse HEAD~100000000000000000000000000000000
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
253 test_expect_success 'backslash does not trigger wildcard rule' '
254 test_must_fail git rev-parse "foo\\bar"
257 test_expect_success 'escaped char does not trigger wildcard rule' '
258 test_must_fail git rev-parse "foo\\*bar"
261 test_expect_success 'arg after dashdash not interpreted as option' '
262 cat >expect <<-\EOF &&
264 --local-env-vars
266 git rev-parse -- --local-env-vars >actual &&
267 test_cmp expect actual
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 &&
272 test_grep bad.revision.*--not-real err
275 test_expect_success 'end-of-options still allows --' '
276 cat >expect <<-EOF &&
277 --end-of-options
278 $(git rev-parse --verify HEAD)
280 path
282 git rev-parse --end-of-options HEAD -- path >actual &&
283 test_cmp expect actual
286 test_done