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 printf "fatal: Path '$2$3' $4, but not ${5:-'$3'}.\n" >expected &&
12 printf "Did you mean '$1:$2$3'${2:+ aka '$1:./$3'}?\n" >>expected &&
13 test_cmp expected error
14}
15
16HASH_file=
17
18test_expect_success 'set up basic repo' '
19 echo one > file.txt &&
20 mkdir subdir &&
21 echo two > subdir/file.txt &&
22 echo three > subdir/file2.txt &&
23 git add . &&
24 git commit -m init &&
25 echo four > index-only.txt &&
26 git add index-only.txt &&
27 echo five > disk-only.txt
28'
29
30test_expect_success 'correct file objects' '
31 HASH_file=$(git rev-parse HEAD:file.txt) &&
32 git rev-parse HEAD:subdir/file.txt &&
33 git rev-parse :index-only.txt &&
34 (cd subdir &&
35 git rev-parse HEAD:subdir/file2.txt &&
36 test $HASH_file = $(git rev-parse HEAD:file.txt) &&
37 test $HASH_file = $(git rev-parse :file.txt) &&
38 test $HASH_file = $(git rev-parse :0:file.txt) )
39'
40
41test_expect_success 'correct relative file objects (0)' '
42 git rev-parse :file.txt >expected &&
43 git rev-parse :./file.txt >result &&
44 test_cmp expected result &&
45 git rev-parse :0:./file.txt >result &&
46 test_cmp expected result
47'
48
49test_expect_success 'correct relative file objects (1)' '
50 git rev-parse HEAD:file.txt >expected &&
51 git rev-parse HEAD:./file.txt >result &&
52 test_cmp expected result
53'
54
55test_expect_success 'correct relative file objects (2)' '
56 (
57 cd subdir &&
58 git rev-parse HEAD:../file.txt >result &&
59 test_cmp ../expected result
60 )
61'
62
63test_expect_success 'correct relative file objects (3)' '
64 (
65 cd subdir &&
66 git rev-parse HEAD:../subdir/../file.txt >result &&
67 test_cmp ../expected result
68 )
69'
70
71test_expect_success 'correct relative file objects (4)' '
72 git rev-parse HEAD:subdir/file.txt >expected &&
73 (
74 cd subdir &&
75 git rev-parse HEAD:./file.txt >result &&
76 test_cmp ../expected result
77 )
78'
79
80test_expect_success 'correct relative file objects (5)' '
81 git rev-parse :subdir/file.txt >expected &&
82 (
83 cd subdir &&
84 git rev-parse :./file.txt >result &&
85 test_cmp ../expected result &&
86 git rev-parse :0:./file.txt >result &&
87 test_cmp ../expected result
88 )
89'
90
91test_expect_success 'correct relative file objects (6)' '
92 git rev-parse :file.txt >expected &&
93 (
94 cd subdir &&
95 git rev-parse :../file.txt >result &&
96 test_cmp ../expected result &&
97 git rev-parse :0:../file.txt >result &&
98 test_cmp ../expected result
99 )
100'
101
102test_expect_success 'incorrect revision id' '
103 test_must_fail git rev-parse foobar:file.txt 2>error &&
104 grep "Invalid object name '"'"'foobar'"'"'." error &&
105 test_must_fail git rev-parse foobar 2> error &&
106 grep "unknown revision or path not in the working tree." error
107'
108
109test_expect_success 'incorrect file in sha1:path' '
110 test_must_fail git rev-parse HEAD:nothing.txt 2> error &&
111 grep "fatal: Path '"'"'nothing.txt'"'"' does not exist in '"'"'HEAD'"'"'" error &&
112 test_must_fail git rev-parse HEAD:index-only.txt 2> error &&
113 grep "fatal: Path '"'"'index-only.txt'"'"' exists on disk, but not in '"'"'HEAD'"'"'." error &&
114 (cd subdir &&
115 test_must_fail git rev-parse HEAD:file2.txt 2> error &&
116 test_did_you_mean HEAD subdir/ file2.txt exists )
117'
118
119test_expect_success 'incorrect file in :path and :N:path' '
120 test_must_fail git rev-parse :nothing.txt 2> error &&
121 grep "fatal: Path '"'"'nothing.txt'"'"' does not exist (neither on disk nor in the index)." error &&
122 test_must_fail git rev-parse :1:nothing.txt 2> error &&
123 grep "Path '"'"'nothing.txt'"'"' does not exist (neither on disk nor in the index)." error &&
124 test_must_fail git rev-parse :1:file.txt 2> error &&
125 test_did_you_mean ":0" "" file.txt "is in the index" "at stage 1" &&
126 (cd subdir &&
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 test_must_fail git rev-parse :file2.txt 2> error &&
130 test_did_you_mean ":0" subdir/ file2.txt "is in the index" &&
131 test_must_fail git rev-parse :2:file2.txt 2> error &&
132 test_did_you_mean :0 subdir/ file2.txt "is in the index") &&
133 test_must_fail git rev-parse :disk-only.txt 2> error &&
134 grep "fatal: Path '"'"'disk-only.txt'"'"' exists on disk, but not in the index." error
135'
136
137test_expect_success 'invalid @{n} reference' '
138 test_must_fail git rev-parse master@{99999} >output 2>error &&
139 test -z "$(cat output)" &&
140 grep "fatal: Log for [^ ]* only has [0-9][0-9]* entries." error &&
141 test_must_fail git rev-parse --verify master@{99999} >output 2>error &&
142 test -z "$(cat output)" &&
143 grep "fatal: Log for [^ ]* only has [0-9][0-9]* entries." error
144'
145
146test_expect_success 'relative path not found' '
147 (
148 cd subdir &&
149 test_must_fail git rev-parse HEAD:./nonexistent.txt 2>error &&
150 grep subdir/nonexistent.txt error
151 )
152'
153
154test_expect_success 'relative path outside worktree' '
155 test_must_fail git rev-parse HEAD:../file.txt >output 2>error &&
156 test -z "$(cat output)" &&
157 grep "outside repository" error
158'
159
160test_expect_success 'relative path when cwd is outside worktree' '
161 test_must_fail git --git-dir=.git --work-tree=subdir rev-parse HEAD:./file.txt >output 2>error &&
162 test -z "$(cat output)" &&
163 grep "relative path syntax can.t be used outside working tree." error
164'
165
166test_expect_success 'relative path when startup_info is NULL' '
167 test_must_fail test-match-trees HEAD:./file.txt HEAD:./file.txt 2>error &&
168 grep "BUG: startup_info struct is not initialized." error
169'
170
171test_done