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