Merge branch 'jc/apply-trailing-blank-removal' into maint
[git] / t / t1402-check-ref-format.sh
1 #!/bin/sh
2
3 test_description='Test git check-ref-format'
4
5 . ./test-lib.sh
6
7 valid_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 }
19 invalid_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
32 invalid_ref ''
33 invalid_ref NOT_MINGW '/'
34 invalid_ref NOT_MINGW '/' --allow-onelevel
35 invalid_ref NOT_MINGW '/' --normalize
36 invalid_ref NOT_MINGW '/' '--allow-onelevel --normalize'
37 valid_ref 'foo/bar/baz'
38 valid_ref 'foo/bar/baz' --normalize
39 invalid_ref 'refs///heads/foo'
40 valid_ref 'refs///heads/foo' --normalize
41 invalid_ref 'heads/foo/'
42 invalid_ref NOT_MINGW '/heads/foo'
43 valid_ref NOT_MINGW '/heads/foo' --normalize
44 invalid_ref '///heads/foo'
45 valid_ref '///heads/foo' --normalize
46 invalid_ref './foo'
47 invalid_ref './foo/bar'
48 invalid_ref 'foo/./bar'
49 invalid_ref 'foo/bar/.'
50 invalid_ref '.refs/foo'
51 invalid_ref 'heads/foo..bar'
52 invalid_ref 'heads/foo?bar'
53 valid_ref 'foo./bar'
54 invalid_ref 'heads/foo.lock'
55 invalid_ref 'heads///foo.lock'
56 invalid_ref 'foo.lock/bar'
57 invalid_ref 'foo.lock///bar'
58 valid_ref 'heads/foo@bar'
59 invalid_ref 'heads/v@{ation'
60 invalid_ref 'heads/foo\bar'
61 invalid_ref "$(printf 'heads/foo\t')"
62 invalid_ref "$(printf 'heads/foo\177')"
63 valid_ref "$(printf 'heads/fu\303\237')"
64 invalid_ref 'heads/*foo/bar' --refspec-pattern
65 invalid_ref 'heads/foo*/bar' --refspec-pattern
66 invalid_ref 'heads/f*o/bar' --refspec-pattern
67
68 ref='foo'
69 invalid_ref "$ref"
70 valid_ref "$ref" --allow-onelevel
71 invalid_ref "$ref" --refspec-pattern
72 valid_ref "$ref" '--refspec-pattern --allow-onelevel'
73 invalid_ref "$ref" --normalize
74 valid_ref "$ref" '--allow-onelevel --normalize'
75
76 ref='foo/bar'
77 valid_ref "$ref"
78 valid_ref "$ref" --allow-onelevel
79 valid_ref "$ref" --refspec-pattern
80 valid_ref "$ref" '--refspec-pattern --allow-onelevel'
81 valid_ref "$ref" --normalize
82
83 ref='foo/*'
84 invalid_ref "$ref"
85 invalid_ref "$ref" --allow-onelevel
86 valid_ref "$ref" --refspec-pattern
87 valid_ref "$ref" '--refspec-pattern --allow-onelevel'
88
89 ref='*/foo'
90 invalid_ref "$ref"
91 invalid_ref "$ref" --allow-onelevel
92 valid_ref "$ref" --refspec-pattern
93 valid_ref "$ref" '--refspec-pattern --allow-onelevel'
94 invalid_ref "$ref" --normalize
95 valid_ref "$ref" '--refspec-pattern --normalize'
96
97 ref='foo/*/bar'
98 invalid_ref "$ref"
99 invalid_ref "$ref" --allow-onelevel
100 valid_ref "$ref" --refspec-pattern
101 valid_ref "$ref" '--refspec-pattern --allow-onelevel'
102
103 ref='*'
104 invalid_ref "$ref"
105 invalid_ref "$ref" --allow-onelevel
106 invalid_ref "$ref" --refspec-pattern
107 valid_ref "$ref" '--refspec-pattern --allow-onelevel'
108
109 ref='foo/*/*'
110 invalid_ref "$ref" --refspec-pattern
111 invalid_ref "$ref" '--refspec-pattern --allow-onelevel'
112
113 ref='*/foo/*'
114 invalid_ref "$ref" --refspec-pattern
115 invalid_ref "$ref" '--refspec-pattern --allow-onelevel'
116
117 ref='*/*/foo'
118 invalid_ref "$ref" --refspec-pattern
119 invalid_ref "$ref" '--refspec-pattern --allow-onelevel'
120
121 ref='/foo'
122 invalid_ref NOT_MINGW "$ref"
123 invalid_ref NOT_MINGW "$ref" --allow-onelevel
124 invalid_ref NOT_MINGW "$ref" --refspec-pattern
125 invalid_ref NOT_MINGW "$ref" '--refspec-pattern --allow-onelevel'
126 invalid_ref NOT_MINGW "$ref" --normalize
127 valid_ref NOT_MINGW "$ref" '--allow-onelevel --normalize'
128 invalid_ref NOT_MINGW "$ref" '--refspec-pattern --normalize'
129 valid_ref NOT_MINGW "$ref" '--refspec-pattern --allow-onelevel --normalize'
130
131 test_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
144 test_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
161 valid_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 }
173 invalid_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
185 valid_ref_normalized 'heads/foo' 'heads/foo'
186 valid_ref_normalized 'refs///heads/foo' 'refs/heads/foo'
187 valid_ref_normalized NOT_MINGW '/heads/foo' 'heads/foo'
188 valid_ref_normalized '///heads/foo' 'heads/foo'
189 invalid_ref_normalized 'foo'
190 invalid_ref_normalized NOT_MINGW '/foo'
191 invalid_ref_normalized 'heads/foo/../bar'
192 invalid_ref_normalized 'heads/./foo'
193 invalid_ref_normalized 'heads\foo'
194 invalid_ref_normalized 'heads/foo.lock'
195 invalid_ref_normalized 'heads///foo.lock'
196 invalid_ref_normalized 'foo.lock/bar'
197 invalid_ref_normalized 'foo.lock///bar'
198
199 test_done