1#!/bin/sh
2
3test_description='Test git check-ref-format'
4
5. ./test-lib.sh
6
7valid_ref() {
8 prereq=
9 case $1 in
10 [A-Z]*)
11 prereq=$1
12 shift
13 esac
14 desc="ref name '$1' is valid${2:+ with options $2}"
15 test_expect_success $prereq "$desc" "
16 git check-ref-format $2 '$1'
17 "
18}
19invalid_ref() {
20 prereq=
21 case $1 in
22 [A-Z]*)
23 prereq=$1
24 shift
25 esac
26 desc="ref name '$1' is invalid${2:+ with options $2}"
27 test_expect_success $prereq "$desc" "
28 test_must_fail git check-ref-format $2 '$1'
29 "
30}
31
32invalid_ref ''
33invalid_ref NOT_MINGW '/'
34invalid_ref NOT_MINGW '/' --allow-onelevel
35invalid_ref NOT_MINGW '/' --normalize
36invalid_ref NOT_MINGW '/' '--allow-onelevel --normalize'
37valid_ref 'foo/bar/baz'
38valid_ref 'foo/bar/baz' --normalize
39invalid_ref 'refs///heads/foo'
40valid_ref 'refs///heads/foo' --normalize
41invalid_ref 'heads/foo/'
42invalid_ref NOT_MINGW '/heads/foo'
43valid_ref NOT_MINGW '/heads/foo' --normalize
44invalid_ref '///heads/foo'
45valid_ref '///heads/foo' --normalize
46invalid_ref './foo'
47invalid_ref './foo/bar'
48invalid_ref 'foo/./bar'
49invalid_ref 'foo/bar/.'
50invalid_ref '.refs/foo'
51invalid_ref 'heads/foo..bar'
52invalid_ref 'heads/foo?bar'
53valid_ref 'foo./bar'
54invalid_ref 'heads/foo.lock'
55invalid_ref 'heads///foo.lock'
56invalid_ref 'foo.lock/bar'
57invalid_ref 'foo.lock///bar'
58valid_ref 'heads/foo@bar'
59invalid_ref 'heads/v@{ation'
60invalid_ref 'heads/foo\bar'
61invalid_ref "$(printf 'heads/foo\t')"
62invalid_ref "$(printf 'heads/foo\177')"
63valid_ref "$(printf 'heads/fu\303\237')"
64invalid_ref 'heads/*foo/bar' --refspec-pattern
65invalid_ref 'heads/foo*/bar' --refspec-pattern
66invalid_ref 'heads/f*o/bar' --refspec-pattern
67invalid_ref 'heads/foo*//bar' --refspec-pattern
68
69ref='foo'
70invalid_ref "$ref"
71valid_ref "$ref" --allow-onelevel
72invalid_ref "$ref" --refspec-pattern
73valid_ref "$ref" '--refspec-pattern --allow-onelevel'
74invalid_ref "$ref" --normalize
75valid_ref "$ref" '--allow-onelevel --normalize'
76
77ref='foo/bar'
78valid_ref "$ref"
79valid_ref "$ref" --allow-onelevel
80valid_ref "$ref" --refspec-pattern
81valid_ref "$ref" '--refspec-pattern --allow-onelevel'
82valid_ref "$ref" --normalize
83
84ref='foo/*'
85invalid_ref "$ref"
86invalid_ref "$ref" --allow-onelevel
87valid_ref "$ref" --refspec-pattern
88valid_ref "$ref" '--refspec-pattern --allow-onelevel'
89
90ref='*/foo'
91invalid_ref "$ref"
92invalid_ref "$ref" --allow-onelevel
93valid_ref "$ref" --refspec-pattern
94valid_ref "$ref" '--refspec-pattern --allow-onelevel'
95invalid_ref "$ref" --normalize
96valid_ref "$ref" '--refspec-pattern --normalize'
97
98ref='foo/*/bar'
99invalid_ref "$ref"
100invalid_ref "$ref" --allow-onelevel
101valid_ref "$ref" --refspec-pattern
102valid_ref "$ref" '--refspec-pattern --allow-onelevel'
103
104ref='*'
105invalid_ref "$ref"
106invalid_ref "$ref" --allow-onelevel
107invalid_ref "$ref" --refspec-pattern
108valid_ref "$ref" '--refspec-pattern --allow-onelevel'
109
110ref='foo/*/*'
111invalid_ref "$ref" --refspec-pattern
112invalid_ref "$ref" '--refspec-pattern --allow-onelevel'
113
114ref='*/foo/*'
115invalid_ref "$ref" --refspec-pattern
116invalid_ref "$ref" '--refspec-pattern --allow-onelevel'
117
118ref='*/*/foo'
119invalid_ref "$ref" --refspec-pattern
120invalid_ref "$ref" '--refspec-pattern --allow-onelevel'
121
122ref='/foo'
123invalid_ref NOT_MINGW "$ref"
124invalid_ref NOT_MINGW "$ref" --allow-onelevel
125invalid_ref NOT_MINGW "$ref" --refspec-pattern
126invalid_ref NOT_MINGW "$ref" '--refspec-pattern --allow-onelevel'
127invalid_ref NOT_MINGW "$ref" --normalize
128valid_ref NOT_MINGW "$ref" '--allow-onelevel --normalize'
129invalid_ref NOT_MINGW "$ref" '--refspec-pattern --normalize'
130valid_ref NOT_MINGW "$ref" '--refspec-pattern --allow-onelevel --normalize'
131
132
133valid_ref 'refs/heads/a-very-long-refname'
134invalid_ref 'refs/heads/.a-very-long-refname'
135invalid_ref 'refs/heads/abcdefgh0123..'
136invalid_ref 'refs/heads/abcdefgh01234..'
137invalid_ref 'refs/heads/abcdefgh012345..'
138invalid_ref 'refs/heads/abcdefgh0123456..'
139invalid_ref 'refs/heads/abcdefgh01234567..'
140valid_ref 'refs/heads/abcdefgh0123.a'
141valid_ref 'refs/heads/abcdefgh01234.a'
142valid_ref 'refs/heads/abcdefgh012345.a'
143valid_ref 'refs/heads/abcdefgh0123456.a'
144valid_ref 'refs/heads/abcdefgh01234567.a'
145
146test_expect_success "check-ref-format --branch @{-1}" '
147 T=$(git write-tree) &&
148 sha1=$(echo A | git commit-tree $T) &&
149 git update-ref refs/heads/master $sha1 &&
150 git update-ref refs/remotes/origin/master $sha1 &&
151 git checkout master &&
152 git checkout origin/master &&
153 git checkout master &&
154 refname=$(git check-ref-format --branch @{-1}) &&
155 test "$refname" = "$sha1" &&
156 refname2=$(git check-ref-format --branch @{-2}) &&
157 test "$refname2" = master'
158
159test_expect_success 'check-ref-format --branch from subdir' '
160 mkdir subdir &&
161
162 T=$(git write-tree) &&
163 sha1=$(echo A | git commit-tree $T) &&
164 git update-ref refs/heads/master $sha1 &&
165 git update-ref refs/remotes/origin/master $sha1 &&
166 git checkout master &&
167 git checkout origin/master &&
168 git checkout master &&
169 refname=$(
170 cd subdir &&
171 git check-ref-format --branch @{-1}
172 ) &&
173 test "$refname" = "$sha1"
174'
175
176valid_ref_normalized() {
177 prereq=
178 case $1 in
179 [A-Z]*)
180 prereq=$1
181 shift
182 esac
183 test_expect_success $prereq "ref name '$1' simplifies to '$2'" "
184 refname=\$(git check-ref-format --normalize '$1') &&
185 test \"\$refname\" = '$2'
186 "
187}
188invalid_ref_normalized() {
189 prereq=
190 case $1 in
191 [A-Z]*)
192 prereq=$1
193 shift
194 esac
195 test_expect_success $prereq "check-ref-format --normalize rejects '$1'" "
196 test_must_fail git check-ref-format --normalize '$1'
197 "
198}
199
200valid_ref_normalized 'heads/foo' 'heads/foo'
201valid_ref_normalized 'refs///heads/foo' 'refs/heads/foo'
202valid_ref_normalized NOT_MINGW '/heads/foo' 'heads/foo'
203valid_ref_normalized '///heads/foo' 'heads/foo'
204invalid_ref_normalized 'foo'
205invalid_ref_normalized NOT_MINGW '/foo'
206invalid_ref_normalized 'heads/foo/../bar'
207invalid_ref_normalized 'heads/./foo'
208invalid_ref_normalized 'heads\foo'
209invalid_ref_normalized 'heads/foo.lock'
210invalid_ref_normalized 'heads///foo.lock'
211invalid_ref_normalized 'foo.lock/bar'
212invalid_ref_normalized 'foo.lock///bar'
213
214test_done