]> git.ipfire.org Git - thirdparty/git.git/blame - t/t1402-check-ref-format.sh
hashmap: use *_entry APIs for iteration
[thirdparty/git.git] / t / t1402-check-ref-format.sh
CommitLineData
58a05c74
JN
1#!/bin/sh
2
3test_description='Test git check-ref-format'
4
5. ./test-lib.sh
6
7valid_ref() {
42afe62d
JS
8 prereq=
9 case $1 in
f57a8715 10 [A-Z!]*)
42afe62d
JS
11 prereq=$1
12 shift
13 esac
283b365e
RS
14 desc="ref name '$1' is valid${2:+ with options $2}"
15 test_expect_success $prereq "$desc" "
42afe62d
JS
16 git check-ref-format $2 '$1'
17 "
58a05c74
JN
18}
19invalid_ref() {
42afe62d
JS
20 prereq=
21 case $1 in
f57a8715 22 [A-Z!]*)
42afe62d
JS
23 prereq=$1
24 shift
25 esac
283b365e
RS
26 desc="ref name '$1' is invalid${2:+ with options $2}"
27 test_expect_success $prereq "$desc" "
42afe62d
JS
28 test_must_fail git check-ref-format $2 '$1'
29 "
58a05c74
JN
30}
31
f9b1a5b9 32invalid_ref ''
f57a8715
JH
33invalid_ref !MINGW '/'
34invalid_ref !MINGW '/' --allow-onelevel
35invalid_ref !MINGW '/' --normalize
36invalid_ref !MINGW '/' '--allow-onelevel --normalize'
58a05c74 37valid_ref 'foo/bar/baz'
a40e6fb6
MH
38valid_ref 'foo/bar/baz' --normalize
39invalid_ref 'refs///heads/foo'
40valid_ref 'refs///heads/foo' --normalize
58a05c74 41invalid_ref 'heads/foo/'
f57a8715
JH
42invalid_ref !MINGW '/heads/foo'
43valid_ref !MINGW '/heads/foo' --normalize
a40e6fb6
MH
44invalid_ref '///heads/foo'
45valid_ref '///heads/foo' --normalize
58a05c74 46invalid_ref './foo'
e4ed6105
MH
47invalid_ref './foo/bar'
48invalid_ref 'foo/./bar'
49invalid_ref 'foo/bar/.'
58a05c74 50invalid_ref '.refs/foo'
919eb8ac 51invalid_ref 'refs/heads/foo.'
58a05c74
JN
52invalid_ref 'heads/foo..bar'
53invalid_ref 'heads/foo?bar'
54valid_ref 'foo./bar'
55invalid_ref 'heads/foo.lock'
f9b1a5b9 56invalid_ref 'heads///foo.lock'
7e9d2fe9
MH
57invalid_ref 'foo.lock/bar'
58invalid_ref 'foo.lock///bar'
58a05c74
JN
59valid_ref 'heads/foo@bar'
60invalid_ref 'heads/v@{ation'
61invalid_ref 'heads/foo\bar'
f3738c1c
MH
62invalid_ref "$(printf 'heads/foo\t')"
63invalid_ref "$(printf 'heads/foo\177')"
64valid_ref "$(printf 'heads/fu\303\237')"
cd377f45
JK
65valid_ref 'heads/*foo/bar' --refspec-pattern
66valid_ref 'heads/foo*/bar' --refspec-pattern
67valid_ref 'heads/f*o/bar' --refspec-pattern
68invalid_ref 'heads/f*o*/bar' --refspec-pattern
69invalid_ref 'heads/foo*/bar*' --refspec-pattern
e4ed6105
MH
70
71ref='foo'
72invalid_ref "$ref"
73valid_ref "$ref" --allow-onelevel
74invalid_ref "$ref" --refspec-pattern
75valid_ref "$ref" '--refspec-pattern --allow-onelevel'
a40e6fb6
MH
76invalid_ref "$ref" --normalize
77valid_ref "$ref" '--allow-onelevel --normalize'
e4ed6105
MH
78
79ref='foo/bar'
80valid_ref "$ref"
81valid_ref "$ref" --allow-onelevel
82valid_ref "$ref" --refspec-pattern
83valid_ref "$ref" '--refspec-pattern --allow-onelevel'
a40e6fb6 84valid_ref "$ref" --normalize
e4ed6105
MH
85
86ref='foo/*'
87invalid_ref "$ref"
88invalid_ref "$ref" --allow-onelevel
89valid_ref "$ref" --refspec-pattern
90valid_ref "$ref" '--refspec-pattern --allow-onelevel'
91
92ref='*/foo'
93invalid_ref "$ref"
94invalid_ref "$ref" --allow-onelevel
95valid_ref "$ref" --refspec-pattern
96valid_ref "$ref" '--refspec-pattern --allow-onelevel'
a40e6fb6
MH
97invalid_ref "$ref" --normalize
98valid_ref "$ref" '--refspec-pattern --normalize'
e4ed6105
MH
99
100ref='foo/*/bar'
101invalid_ref "$ref"
102invalid_ref "$ref" --allow-onelevel
103valid_ref "$ref" --refspec-pattern
104valid_ref "$ref" '--refspec-pattern --allow-onelevel'
105
106ref='*'
107invalid_ref "$ref"
8d9c5010 108invalid_ref "$ref" --allow-onelevel
e4ed6105
MH
109invalid_ref "$ref" --refspec-pattern
110valid_ref "$ref" '--refspec-pattern --allow-onelevel'
111
112ref='foo/*/*'
113invalid_ref "$ref" --refspec-pattern
114invalid_ref "$ref" '--refspec-pattern --allow-onelevel'
115
116ref='*/foo/*'
117invalid_ref "$ref" --refspec-pattern
118invalid_ref "$ref" '--refspec-pattern --allow-onelevel'
119
120ref='*/*/foo'
121invalid_ref "$ref" --refspec-pattern
122invalid_ref "$ref" '--refspec-pattern --allow-onelevel'
123
124ref='/foo'
f57a8715
JH
125invalid_ref !MINGW "$ref"
126invalid_ref !MINGW "$ref" --allow-onelevel
127invalid_ref !MINGW "$ref" --refspec-pattern
128invalid_ref !MINGW "$ref" '--refspec-pattern --allow-onelevel'
129invalid_ref !MINGW "$ref" --normalize
130valid_ref !MINGW "$ref" '--allow-onelevel --normalize'
131invalid_ref !MINGW "$ref" '--refspec-pattern --normalize'
132valid_ref !MINGW "$ref" '--refspec-pattern --allow-onelevel --normalize'
58a05c74
JN
133
134test_expect_success "check-ref-format --branch @{-1}" '
135 T=$(git write-tree) &&
136 sha1=$(echo A | git commit-tree $T) &&
137 git update-ref refs/heads/master $sha1 &&
a48fcd83 138 git update-ref refs/remotes/origin/master $sha1 &&
58a05c74
JN
139 git checkout master &&
140 git checkout origin/master &&
141 git checkout master &&
142 refname=$(git check-ref-format --branch @{-1}) &&
143 test "$refname" = "$sha1" &&
144 refname2=$(git check-ref-format --branch @{-2}) &&
145 test "$refname2" = master'
146
7c3f847a
JH
147test_expect_success 'check-ref-format --branch -naster' '
148 test_must_fail git check-ref-format --branch -naster >actual &&
149 test_must_be_empty actual
150'
151
49cc460d
JN
152test_expect_success 'check-ref-format --branch from subdir' '
153 mkdir subdir &&
154
155 T=$(git write-tree) &&
156 sha1=$(echo A | git commit-tree $T) &&
157 git update-ref refs/heads/master $sha1 &&
a48fcd83 158 git update-ref refs/remotes/origin/master $sha1 &&
49cc460d
JN
159 git checkout master &&
160 git checkout origin/master &&
161 git checkout master &&
162 refname=$(
163 cd subdir &&
164 git check-ref-format --branch @{-1}
165 ) &&
166 test "$refname" = "$sha1"
167'
168
7c3f847a
JH
169test_expect_success 'check-ref-format --branch @{-1} from non-repo' '
170 nongit test_must_fail git check-ref-format --branch @{-1} >actual &&
171 test_must_be_empty actual
172'
173
174test_expect_success 'check-ref-format --branch master from non-repo' '
175 echo master >expect &&
176 nongit git check-ref-format --branch master >actual &&
177 test_cmp expect actual
178'
179
38eedc63 180valid_ref_normalized() {
42afe62d
JS
181 prereq=
182 case $1 in
f57a8715 183 [A-Z!]*)
42afe62d
JS
184 prereq=$1
185 shift
186 esac
187 test_expect_success $prereq "ref name '$1' simplifies to '$2'" "
a40e6fb6 188 refname=\$(git check-ref-format --normalize '$1') &&
42afe62d
JS
189 test \"\$refname\" = '$2'
190 "
38eedc63
JH
191}
192invalid_ref_normalized() {
42afe62d
JS
193 prereq=
194 case $1 in
f57a8715 195 [A-Z!]*)
42afe62d
JS
196 prereq=$1
197 shift
198 esac
199 test_expect_success $prereq "check-ref-format --normalize rejects '$1'" "
200 test_must_fail git check-ref-format --normalize '$1'
201 "
38eedc63
JH
202}
203
204valid_ref_normalized 'heads/foo' 'heads/foo'
205valid_ref_normalized 'refs///heads/foo' 'refs/heads/foo'
f57a8715 206valid_ref_normalized !MINGW '/heads/foo' 'heads/foo'
2f633f41 207valid_ref_normalized '///heads/foo' 'heads/foo'
38eedc63 208invalid_ref_normalized 'foo'
f57a8715 209invalid_ref_normalized !MINGW '/foo'
38eedc63
JH
210invalid_ref_normalized 'heads/foo/../bar'
211invalid_ref_normalized 'heads/./foo'
212invalid_ref_normalized 'heads\foo'
f9b1a5b9
MH
213invalid_ref_normalized 'heads/foo.lock'
214invalid_ref_normalized 'heads///foo.lock'
7e9d2fe9
MH
215invalid_ref_normalized 'foo.lock/bar'
216invalid_ref_normalized 'foo.lock///bar'
38eedc63 217
58a05c74 218test_done