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