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
67
68ref='foo'
69invalid_ref "$ref"
70valid_ref "$ref" --allow-onelevel
71invalid_ref "$ref" --refspec-pattern
72valid_ref "$ref" '--refspec-pattern --allow-onelevel'
73invalid_ref "$ref" --normalize
74valid_ref "$ref" '--allow-onelevel --normalize'
75
76ref='foo/bar'
77valid_ref "$ref"
78valid_ref "$ref" --allow-onelevel
79valid_ref "$ref" --refspec-pattern
80valid_ref "$ref" '--refspec-pattern --allow-onelevel'
81valid_ref "$ref" --normalize
82
83ref='foo/*'
84invalid_ref "$ref"
85invalid_ref "$ref" --allow-onelevel
86valid_ref "$ref" --refspec-pattern
87valid_ref "$ref" '--refspec-pattern --allow-onelevel'
88
89ref='*/foo'
90invalid_ref "$ref"
91invalid_ref "$ref" --allow-onelevel
92valid_ref "$ref" --refspec-pattern
93valid_ref "$ref" '--refspec-pattern --allow-onelevel'
94invalid_ref "$ref" --normalize
95valid_ref "$ref" '--refspec-pattern --normalize'
96
97ref='foo/*/bar'
98invalid_ref "$ref"
99invalid_ref "$ref" --allow-onelevel
100valid_ref "$ref" --refspec-pattern
101valid_ref "$ref" '--refspec-pattern --allow-onelevel'
102
103ref='*'
104invalid_ref "$ref"
105invalid_ref "$ref" --allow-onelevel
106invalid_ref "$ref" --refspec-pattern
107valid_ref "$ref" '--refspec-pattern --allow-onelevel'
108
109ref='foo/*/*'
110invalid_ref "$ref" --refspec-pattern
111invalid_ref "$ref" '--refspec-pattern --allow-onelevel'
112
113ref='*/foo/*'
114invalid_ref "$ref" --refspec-pattern
115invalid_ref "$ref" '--refspec-pattern --allow-onelevel'
116
117ref='*/*/foo'
118invalid_ref "$ref" --refspec-pattern
119invalid_ref "$ref" '--refspec-pattern --allow-onelevel'
120
121ref='/foo'
122invalid_ref NOT_MINGW "$ref"
123invalid_ref NOT_MINGW "$ref" --allow-onelevel
124invalid_ref NOT_MINGW "$ref" --refspec-pattern
125invalid_ref NOT_MINGW "$ref" '--refspec-pattern --allow-onelevel'
126invalid_ref NOT_MINGW "$ref" --normalize
127valid_ref NOT_MINGW "$ref" '--allow-onelevel --normalize'
128invalid_ref NOT_MINGW "$ref" '--refspec-pattern --normalize'
129valid_ref NOT_MINGW "$ref" '--refspec-pattern --allow-onelevel --normalize'
130
131test_expect_success "check-ref-format --branch @{-1}" '
132 T=$(git write-tree) &&
133 sha1=$(echo A | git commit-tree $T) &&
134 git update-ref refs/heads/master $sha1 &&
135 git update-ref refs/remotes/origin/master $sha1 &&
136 git checkout master &&
137 git checkout origin/master &&
138 git checkout master &&
139 refname=$(git check-ref-format --branch @{-1}) &&
140 test "$refname" = "$sha1" &&
141 refname2=$(git check-ref-format --branch @{-2}) &&
142 test "$refname2" = master'
143
144test_expect_success 'check-ref-format --branch from subdir' '
145 mkdir subdir &&
146
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=$(
155 cd subdir &&
156 git check-ref-format --branch @{-1}
157 ) &&
158 test "$refname" = "$sha1"
159'
160
161valid_ref_normalized() {
162 prereq=
163 case $1 in
164 [A-Z]*)
165 prereq=$1
166 shift
167 esac
168 test_expect_success $prereq "ref name '$1' simplifies to '$2'" "
169 refname=\$(git check-ref-format --normalize '$1') &&
170 test \"\$refname\" = '$2'
171 "
172}
173invalid_ref_normalized() {
174 prereq=
175 case $1 in
176 [A-Z]*)
177 prereq=$1
178 shift
179 esac
180 test_expect_success $prereq "check-ref-format --normalize rejects '$1'" "
181 test_must_fail git check-ref-format --normalize '$1'
182 "
183}
184
185valid_ref_normalized 'heads/foo' 'heads/foo'
186valid_ref_normalized 'refs///heads/foo' 'refs/heads/foo'
187valid_ref_normalized NOT_MINGW '/heads/foo' 'heads/foo'
188valid_ref_normalized '///heads/foo' 'heads/foo'
189invalid_ref_normalized 'foo'
190invalid_ref_normalized NOT_MINGW '/foo'
191invalid_ref_normalized 'heads/foo/../bar'
192invalid_ref_normalized 'heads/./foo'
193invalid_ref_normalized 'heads\foo'
194invalid_ref_normalized 'heads/foo.lock'
195invalid_ref_normalized 'heads///foo.lock'
196invalid_ref_normalized 'foo.lock/bar'
197invalid_ref_normalized 'foo.lock///bar'
198
199test_done