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