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