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