checkout: fix "branch info" memory leaks
[git/debian.git] / t / t1506-rev-parse-diagnosis.sh
blob65a154a8a20f8df27a3f0eaee9ff9b65d5132639
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-lib.sh
12 test_did_you_mean ()
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
21 HASH_file=
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
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) )
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
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
60 test_expect_success 'correct relative file objects (2)' '
62 cd subdir &&
63 git rev-parse HEAD:../file.txt >result &&
64 test_cmp ../expected result
68 test_expect_success 'correct relative file objects (3)' '
70 cd subdir &&
71 git rev-parse HEAD:../subdir/../file.txt >result &&
72 test_cmp ../expected result
76 test_expect_success 'correct relative file objects (4)' '
77 git rev-parse HEAD:subdir/file.txt >expected &&
79 cd subdir &&
80 git rev-parse HEAD:./file.txt >result &&
81 test_cmp ../expected result
85 test_expect_success 'correct relative file objects (5)' '
86 git rev-parse :subdir/file.txt >expected &&
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
96 test_expect_success 'correct relative file objects (6)' '
97 git rev-parse :file.txt >expected &&
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
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
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 )
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
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
151 test_expect_success 'relative path not found' '
153 cd subdir &&
154 test_must_fail git rev-parse HEAD:./nonexistent.txt 2>error &&
155 test_i18ngrep subdir/nonexistent.txt error
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
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
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
179 test_cmp expect actual
182 test_expect_success 'dotdot is not an empty set' '
183 ( H=$(git rev-parse HEAD) && echo $H && echo ^$H ) >expect &&
185 git rev-parse HEAD.. >actual &&
186 test_cmp expect actual &&
188 git rev-parse ..HEAD >actual &&
189 test_cmp expect actual &&
191 echo .. >expect &&
192 git rev-parse .. >actual &&
193 test_cmp expect actual
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) &&
201 echo $A && echo ^$A
202 } >expect-with-two-dots &&
204 echo $A && echo $A && echo ^$H
205 } >expect-with-merge-base &&
207 git rev-parse annotated..annotated >actual-with-two-dots &&
208 test_cmp expect-with-two-dots actual-with-two-dots &&
210 git rev-parse annotated...annotated >actual-with-merge-base &&
211 test_cmp expect-with-merge-base actual-with-merge-base
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
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
225 test_expect_success 'arg before dashdash must be a revision (ambiguous)' '
226 >foobar &&
227 git update-ref refs/heads/foobar HEAD &&
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
238 test_expect_success 'reject Nth parent if N is too high' '
239 test_must_fail git rev-parse HEAD^100000000000000000000000000000000
242 test_expect_success 'reject Nth ancestor if N is too high' '
243 test_must_fail git rev-parse HEAD~100000000000000000000000000000000
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
252 test_expect_success 'backslash does not trigger wildcard rule' '
253 test_must_fail git rev-parse "foo\\bar"
256 test_expect_success 'escaped char does not trigger wildcard rule' '
257 test_must_fail git rev-parse "foo\\*bar"
260 test_expect_success 'arg after dashdash not interpreted as option' '
261 cat >expect <<-\EOF &&
263 --local-env-vars
265 git rev-parse -- --local-env-vars >actual &&
266 test_cmp expect actual
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
274 test_expect_success 'end-of-options still allows --' '
275 cat >expect <<-EOF &&
276 --end-of-options
277 $(git rev-parse --verify HEAD)
279 path
281 git rev-parse --end-of-options HEAD -- path >actual &&
282 test_cmp expect actual
285 test_done