Merge branch 'nd/diffstat-gramnum'
[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         test_expect_success $prereq "ref name '$1' is valid${2:+ with options $2}" "
15                 git check-ref-format $2 '$1'
16         "
17 }
18 invalid_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
30 invalid_ref ''
31 invalid_ref NOT_MINGW '/'
32 invalid_ref NOT_MINGW '/' --allow-onelevel
33 invalid_ref NOT_MINGW '/' --normalize
34 invalid_ref NOT_MINGW '/' '--allow-onelevel --normalize'
35 valid_ref 'foo/bar/baz'
36 valid_ref 'foo/bar/baz' --normalize
37 invalid_ref 'refs///heads/foo'
38 valid_ref 'refs///heads/foo' --normalize
39 invalid_ref 'heads/foo/'
40 invalid_ref NOT_MINGW '/heads/foo'
41 valid_ref NOT_MINGW '/heads/foo' --normalize
42 invalid_ref '///heads/foo'
43 valid_ref '///heads/foo' --normalize
44 invalid_ref './foo'
45 invalid_ref './foo/bar'
46 invalid_ref 'foo/./bar'
47 invalid_ref 'foo/bar/.'
48 invalid_ref '.refs/foo'
49 invalid_ref 'heads/foo..bar'
50 invalid_ref 'heads/foo?bar'
51 valid_ref 'foo./bar'
52 invalid_ref 'heads/foo.lock'
53 invalid_ref 'heads///foo.lock'
54 invalid_ref 'foo.lock/bar'
55 invalid_ref 'foo.lock///bar'
56 valid_ref 'heads/foo@bar'
57 invalid_ref 'heads/v@{ation'
58 invalid_ref 'heads/foo\bar'
59 invalid_ref "$(printf 'heads/foo\t')"
60 invalid_ref "$(printf 'heads/foo\177')"
61 valid_ref "$(printf 'heads/fu\303\237')"
62 invalid_ref 'heads/*foo/bar' --refspec-pattern
63 invalid_ref 'heads/foo*/bar' --refspec-pattern
64 invalid_ref 'heads/f*o/bar' --refspec-pattern
65
66 ref='foo'
67 invalid_ref "$ref"
68 valid_ref "$ref" --allow-onelevel
69 invalid_ref "$ref" --refspec-pattern
70 valid_ref "$ref" '--refspec-pattern --allow-onelevel'
71 invalid_ref "$ref" --normalize
72 valid_ref "$ref" '--allow-onelevel --normalize'
73
74 ref='foo/bar'
75 valid_ref "$ref"
76 valid_ref "$ref" --allow-onelevel
77 valid_ref "$ref" --refspec-pattern
78 valid_ref "$ref" '--refspec-pattern --allow-onelevel'
79 valid_ref "$ref" --normalize
80
81 ref='foo/*'
82 invalid_ref "$ref"
83 invalid_ref "$ref" --allow-onelevel
84 valid_ref "$ref" --refspec-pattern
85 valid_ref "$ref" '--refspec-pattern --allow-onelevel'
86
87 ref='*/foo'
88 invalid_ref "$ref"
89 invalid_ref "$ref" --allow-onelevel
90 valid_ref "$ref" --refspec-pattern
91 valid_ref "$ref" '--refspec-pattern --allow-onelevel'
92 invalid_ref "$ref" --normalize
93 valid_ref "$ref" '--refspec-pattern --normalize'
94
95 ref='foo/*/bar'
96 invalid_ref "$ref"
97 invalid_ref "$ref" --allow-onelevel
98 valid_ref "$ref" --refspec-pattern
99 valid_ref "$ref" '--refspec-pattern --allow-onelevel'
100
101 ref='*'
102 invalid_ref "$ref"
103 invalid_ref "$ref" --allow-onelevel
104 invalid_ref "$ref" --refspec-pattern
105 valid_ref "$ref" '--refspec-pattern --allow-onelevel'
106
107 ref='foo/*/*'
108 invalid_ref "$ref" --refspec-pattern
109 invalid_ref "$ref" '--refspec-pattern --allow-onelevel'
110
111 ref='*/foo/*'
112 invalid_ref "$ref" --refspec-pattern
113 invalid_ref "$ref" '--refspec-pattern --allow-onelevel'
114
115 ref='*/*/foo'
116 invalid_ref "$ref" --refspec-pattern
117 invalid_ref "$ref" '--refspec-pattern --allow-onelevel'
118
119 ref='/foo'
120 invalid_ref NOT_MINGW "$ref"
121 invalid_ref NOT_MINGW "$ref" --allow-onelevel
122 invalid_ref NOT_MINGW "$ref" --refspec-pattern
123 invalid_ref NOT_MINGW "$ref" '--refspec-pattern --allow-onelevel'
124 invalid_ref NOT_MINGW "$ref" --normalize
125 valid_ref NOT_MINGW "$ref" '--allow-onelevel --normalize'
126 invalid_ref NOT_MINGW "$ref" '--refspec-pattern --normalize'
127 valid_ref NOT_MINGW "$ref" '--refspec-pattern --allow-onelevel --normalize'
128
129 test_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
142 test_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
159 valid_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 }
171 invalid_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
183 valid_ref_normalized 'heads/foo' 'heads/foo'
184 valid_ref_normalized 'refs///heads/foo' 'refs/heads/foo'
185 valid_ref_normalized NOT_MINGW '/heads/foo' 'heads/foo'
186 valid_ref_normalized '///heads/foo' 'heads/foo'
187 invalid_ref_normalized 'foo'
188 invalid_ref_normalized NOT_MINGW '/foo'
189 invalid_ref_normalized 'heads/foo/../bar'
190 invalid_ref_normalized 'heads/./foo'
191 invalid_ref_normalized 'heads\foo'
192 invalid_ref_normalized 'heads/foo.lock'
193 invalid_ref_normalized 'heads///foo.lock'
194 invalid_ref_normalized 'foo.lock/bar'
195 invalid_ref_normalized 'foo.lock///bar'
196
197 test_done