t1500: avoid setting configuration options outside of tests
[git/raj.git] / t / t1506-rev-parse-diagnosis.sh
blob86c2ff255d43d644caf81e6258d29dcea1db8e53
1 #!/bin/sh
3 test_description='test git rev-parse diagnosis for invalid argument'
5 exec </dev/null
7 . ./test-lib.sh
9 test_did_you_mean ()
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
19 HASH_file=
21 test_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
33 test_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) )
44 test_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
52 test_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
58 test_expect_success 'correct relative file objects (2)' '
60 cd subdir &&
61 git rev-parse HEAD:../file.txt >result &&
62 test_cmp ../expected result
66 test_expect_success 'correct relative file objects (3)' '
68 cd subdir &&
69 git rev-parse HEAD:../subdir/../file.txt >result &&
70 test_cmp ../expected result
74 test_expect_success 'correct relative file objects (4)' '
75 git rev-parse HEAD:subdir/file.txt >expected &&
77 cd subdir &&
78 git rev-parse HEAD:./file.txt >result &&
79 test_cmp ../expected result
83 test_expect_success 'correct relative file objects (5)' '
84 git rev-parse :subdir/file.txt >expected &&
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
94 test_expect_success 'correct relative file objects (6)' '
95 git rev-parse :file.txt >expected &&
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
105 test_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
112 test_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 )
122 test_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
140 test_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
149 test_expect_success 'relative path not found' '
151 cd subdir &&
152 test_must_fail git rev-parse HEAD:./nonexistent.txt 2>error &&
153 grep subdir/nonexistent.txt error
157 test_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
163 test_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
169 test_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
177 test_cmp expect actual
180 test_expect_success 'dotdot is not an empty set' '
181 ( H=$(git rev-parse HEAD) && echo $H && echo ^$H ) >expect &&
183 git rev-parse HEAD.. >actual &&
184 test_cmp expect actual &&
186 git rev-parse ..HEAD >actual &&
187 test_cmp expect actual &&
189 echo .. >expect &&
190 git rev-parse .. >actual &&
191 test_cmp expect actual
194 test_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
199 test_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
205 test_expect_success 'arg before dashdash must be a revision (ambiguous)' '
206 >foobar &&
207 git update-ref refs/heads/foobar HEAD &&
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
218 test_done