1#!/bin/sh
2
3test_description='test git rev-parse diagnosis for invalid argument'
4
5exec </dev/null
6
7. ./test-lib.sh
8
9test_did_you_mean ()
10{
11 cat >expected <<-EOF &&
12 fatal: Path '$2$3' $4, but not ${5:-$SQ$3$SQ}.
13 Did you mean '$1:$2$3'${2:+ aka $SQ$1:./$3$SQ}?
14 EOF
15 test_cmp expected error
16}
17
18HASH_file=
19
20test_expect_success 'set up basic repo' '
21 echo one > file.txt &&
22 mkdir subdir &&
23 echo two > subdir/file.txt &&
24 echo three > subdir/file2.txt &&
25 git add . &&
26 git commit -m init &&
27 echo four > index-only.txt &&
28 git add index-only.txt &&
29 echo five > disk-only.txt
30'
31
32test_expect_success 'correct file objects' '
33 HASH_file=$(git rev-parse HEAD:file.txt) &&
34 git rev-parse HEAD:subdir/file.txt &&
35 git rev-parse :index-only.txt &&
36 (cd subdir &&
37 git rev-parse HEAD:subdir/file2.txt &&
38 test $HASH_file = $(git rev-parse HEAD:file.txt) &&
39 test $HASH_file = $(git rev-parse :file.txt) &&
40 test $HASH_file = $(git rev-parse :0:file.txt) )
41'
42
43test_expect_success 'correct relative file objects (0)' '
44 git rev-parse :file.txt >expected &&
45 git rev-parse :./file.txt >result &&
46 test_cmp expected result &&
47 git rev-parse :0:./file.txt >result &&
48 test_cmp expected result
49'
50
51test_expect_success 'correct relative file objects (1)' '
52 git rev-parse HEAD:file.txt >expected &&
53 git rev-parse HEAD:./file.txt >result &&
54 test_cmp expected result
55'
56
57test_expect_success 'correct relative file objects (2)' '
58 (
59 cd subdir &&
60 git rev-parse HEAD:../file.txt >result &&
61 test_cmp ../expected result
62 )
63'
64
65test_expect_success 'correct relative file objects (3)' '
66 (
67 cd subdir &&
68 git rev-parse HEAD:../subdir/../file.txt >result &&
69 test_cmp ../expected result
70 )
71'
72
73test_expect_success 'correct relative file objects (4)' '
74 git rev-parse HEAD:subdir/file.txt >expected &&
75 (
76 cd subdir &&
77 git rev-parse HEAD:./file.txt >result &&
78 test_cmp ../expected result
79 )
80'
81
82test_expect_success 'correct relative file objects (5)' '
83 git rev-parse :subdir/file.txt >expected &&
84 (
85 cd subdir &&
86 git rev-parse :./file.txt >result &&
87 test_cmp ../expected result &&
88 git rev-parse :0:./file.txt >result &&
89 test_cmp ../expected result
90 )
91'
92
93test_expect_success 'correct relative file objects (6)' '
94 git rev-parse :file.txt >expected &&
95 (
96 cd subdir &&
97 git rev-parse :../file.txt >result &&
98 test_cmp ../expected result &&
99 git rev-parse :0:../file.txt >result &&
100 test_cmp ../expected result
101 )
102'
103
104test_expect_success 'incorrect revision id' '
105 test_must_fail git rev-parse foobar:file.txt 2>error &&
106 grep "Invalid object name '"'"'foobar'"'"'." error &&
107 test_must_fail git rev-parse foobar 2> error &&
108 test_i18ngrep "unknown revision or path not in the working tree." error
109'
110
111test_expect_success 'incorrect file in sha1:path' '
112 test_must_fail git rev-parse HEAD:nothing.txt 2> error &&
113 grep "fatal: Path '"'"'nothing.txt'"'"' does not exist in '"'"'HEAD'"'"'" error &&
114 test_must_fail git rev-parse HEAD:index-only.txt 2> error &&
115 grep "fatal: Path '"'"'index-only.txt'"'"' exists on disk, but not in '"'"'HEAD'"'"'." error &&
116 (cd subdir &&
117 test_must_fail git rev-parse HEAD:file2.txt 2> error &&
118 test_did_you_mean HEAD subdir/ file2.txt exists )
119'
120
121test_expect_success 'incorrect file in :path and :N:path' '
122 test_must_fail git rev-parse :nothing.txt 2> error &&
123 grep "fatal: Path '"'"'nothing.txt'"'"' does not exist (neither on disk nor in the index)." error &&
124 test_must_fail git rev-parse :1:nothing.txt 2> error &&
125 grep "Path '"'"'nothing.txt'"'"' does not exist (neither on disk nor in the index)." error &&
126 test_must_fail git rev-parse :1:file.txt 2> error &&
127 test_did_you_mean ":0" "" file.txt "is in the index" "at stage 1" &&
128 (cd subdir &&
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 test_must_fail git rev-parse :file2.txt 2> error &&
132 test_did_you_mean ":0" subdir/ file2.txt "is in the index" &&
133 test_must_fail git rev-parse :2:file2.txt 2> error &&
134 test_did_you_mean :0 subdir/ file2.txt "is in the index") &&
135 test_must_fail git rev-parse :disk-only.txt 2> error &&
136 grep "fatal: Path '"'"'disk-only.txt'"'"' exists on disk, but not in the index." error
137'
138
139test_expect_success 'invalid @{n} reference' '
140 test_must_fail git rev-parse master@{99999} >output 2>error &&
141 test -z "$(cat output)" &&
142 grep "fatal: Log for [^ ]* only has [0-9][0-9]* entries." error &&
143 test_must_fail git rev-parse --verify master@{99999} >output 2>error &&
144 test -z "$(cat output)" &&
145 grep "fatal: Log for [^ ]* only has [0-9][0-9]* entries." error
146'
147
148test_expect_success 'relative path not found' '
149 (
150 cd subdir &&
151 test_must_fail git rev-parse HEAD:./nonexistent.txt 2>error &&
152 grep subdir/nonexistent.txt error
153 )
154'
155
156test_expect_success 'relative path outside worktree' '
157 test_must_fail git rev-parse HEAD:../file.txt >output 2>error &&
158 test -z "$(cat output)" &&
159 test_i18ngrep "outside repository" error
160'
161
162test_expect_success 'relative path when cwd is outside worktree' '
163 test_must_fail git --git-dir=.git --work-tree=subdir rev-parse HEAD:./file.txt >output 2>error &&
164 test -z "$(cat output)" &&
165 grep "relative path syntax can.t be used outside working tree." error
166'
167
168test_expect_success '<commit>:file correctly diagnosed after a pathname' '
169 test_must_fail git rev-parse file.txt HEAD:file.txt 1>actual 2>error &&
170 test_i18ngrep ! "exists on disk" error &&
171 test_i18ngrep "no such path in the working tree" error &&
172 cat >expect <<-\EOF &&
173 file.txt
174 HEAD:file.txt
175 EOF
176 test_cmp expect actual
177'
178
179test_expect_success 'dotdot is not an empty set' '
180 ( H=$(git rev-parse HEAD) && echo $H && echo ^$H ) >expect &&
181
182 git rev-parse HEAD.. >actual &&
183 test_cmp expect actual &&
184
185 git rev-parse ..HEAD >actual &&
186 test_cmp expect actual &&
187
188 echo .. >expect &&
189 git rev-parse .. >actual &&
190 test_cmp expect actual
191'
192
193test_expect_success 'arg before dashdash must be a revision (missing)' '
194 test_must_fail git rev-parse foobar -- 2>stderr &&
195 test_i18ngrep "bad revision" stderr
196'
197
198test_expect_success 'arg before dashdash must be a revision (file)' '
199 >foobar &&
200 test_must_fail git rev-parse foobar -- 2>stderr &&
201 test_i18ngrep "bad revision" stderr
202'
203
204test_expect_success 'arg before dashdash must be a revision (ambiguous)' '
205 >foobar &&
206 git update-ref refs/heads/foobar HEAD &&
207 {
208 # we do not want to use rev-parse here, because
209 # we are testing it
210 cat .git/refs/heads/foobar &&
211 printf "%s\n" --
212 } >expect &&
213 git rev-parse foobar -- >actual &&
214 test_cmp expect actual
215'
216
217test_done