The second batch
[git] / t / t1506-rev-parse-diagnosis.sh
1 #!/bin/sh
2
3 test_description='test git rev-parse diagnosis for invalid argument'
4
5 exec </dev/null
6
7 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
8 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
9
10 . ./test-lib.sh
11
12 test_did_you_mean ()
13 {
14         cat >expected <<-EOF &&
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}?
17         EOF
18         test_cmp expected error
19 }
20
21 HASH_file=
22
23 test_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
35 test_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
46 test_expect_success 'correct relative file objects (0)' '
47         git rev-parse :file.txt >expected &&
48         git rev-parse :./file.txt >result &&
49         test_cmp expected result &&
50         git rev-parse :0:./file.txt >result &&
51         test_cmp expected result
52 '
53
54 test_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
60 test_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
68 test_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
76 test_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
85 test_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
96 test_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
107 test_expect_success 'incorrect revision id' '
108         test_must_fail git rev-parse foobar:file.txt 2>error &&
109         test_i18ngrep "invalid object name .foobar." error &&
110         test_must_fail git rev-parse foobar 2>error &&
111         test_i18ngrep "unknown revision or path not in the working tree." error
112 '
113
114 test_expect_success 'incorrect file in sha1:path' '
115         test_must_fail git rev-parse HEAD:nothing.txt 2>error &&
116         test_i18ngrep "path .nothing.txt. does not exist in .HEAD." error &&
117         test_must_fail git rev-parse HEAD:index-only.txt 2>error &&
118         test_i18ngrep "path .index-only.txt. exists on disk, but not in .HEAD." error &&
119         (cd subdir &&
120          test_must_fail git rev-parse HEAD:file2.txt 2>error &&
121          test_did_you_mean HEAD subdir/ file2.txt exists )
122 '
123
124 test_expect_success 'incorrect file in :path and :N:path' '
125         test_must_fail git rev-parse :nothing.txt 2>error &&
126         test_i18ngrep "path .nothing.txt. does not exist (neither on disk nor in the index)" error &&
127         test_must_fail git rev-parse :1:nothing.txt 2>error &&
128         test_i18ngrep "path .nothing.txt. does not exist (neither on disk nor in the index)" error &&
129         test_must_fail git rev-parse :1:file.txt 2>error &&
130         test_did_you_mean ":0" "" file.txt "is in the index" "at stage 1" &&
131         (cd subdir &&
132          test_must_fail git rev-parse :1:file.txt 2>error &&
133          test_did_you_mean ":0" "" file.txt "is in the index" "at stage 1" &&
134          test_must_fail git rev-parse :file2.txt 2>error &&
135          test_did_you_mean ":0" subdir/ file2.txt "is in the index" &&
136          test_must_fail git rev-parse :2:file2.txt 2>error &&
137          test_did_you_mean :0 subdir/ file2.txt "is in the index") &&
138         test_must_fail git rev-parse :disk-only.txt 2>error &&
139         test_i18ngrep "path .disk-only.txt. exists on disk, but not in the index" error
140 '
141
142 test_expect_success 'invalid @{n} reference' '
143         test_must_fail git rev-parse main@{99999} >output 2>error &&
144         test_must_be_empty output &&
145         test_i18ngrep "log for [^ ]* only has [0-9][0-9]* entries" error  &&
146         test_must_fail git rev-parse --verify main@{99999} >output 2>error &&
147         test_must_be_empty output &&
148         test_i18ngrep "log for [^ ]* only has [0-9][0-9]* entries" error
149 '
150
151 test_expect_success 'relative path not found' '
152         (
153                 cd subdir &&
154                 test_must_fail git rev-parse HEAD:./nonexistent.txt 2>error &&
155                 test_i18ngrep subdir/nonexistent.txt error
156         )
157 '
158
159 test_expect_success 'relative path outside worktree' '
160         test_must_fail git rev-parse HEAD:../file.txt >output 2>error &&
161         test_must_be_empty output &&
162         test_i18ngrep "outside repository" error
163 '
164
165 test_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 &&
167         test_must_be_empty output &&
168         test_i18ngrep "relative path syntax can.t be used outside working tree" error
169 '
170
171 test_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 &&
174         test_i18ngrep "no such path in the working tree" error &&
175         cat >expect <<-\EOF &&
176         file.txt
177         HEAD:file.txt
178         EOF
179         test_cmp expect actual
180 '
181
182 test_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
196 test_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
214 test_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
219 test_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
225 test_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
231                 git show-ref -s refs/heads/foobar &&
232                 printf "%s\n" --
233         } >expect &&
234         git rev-parse foobar -- >actual &&
235         test_cmp expect actual
236 '
237
238 test_expect_success 'reject Nth parent if N is too high' '
239         test_must_fail git rev-parse HEAD^100000000000000000000000000000000
240 '
241
242 test_expect_success 'reject Nth ancestor if N is too high' '
243         test_must_fail git rev-parse HEAD~100000000000000000000000000000000
244 '
245
246 test_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
252 test_expect_success 'backslash does not trigger wildcard rule' '
253         test_must_fail git rev-parse "foo\\bar"
254 '
255
256 test_expect_success 'escaped char does not trigger wildcard rule' '
257         test_must_fail git rev-parse "foo\\*bar"
258 '
259
260 test_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
269 test_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
274 test_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
285 test_done