interplane.Get Omega
Read in your language

git

[Ported from github.com/git/git] Sync with Git 2.56.1

Brought by drakestapleton

branch master Branches Tags History

git / t / t1402-check-ref-format.sh

223 lines · 6111 bytes Raw Blame History
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 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}
19invalid_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
32invalid_ref ''
33invalid_ref !MINGW '/'
34invalid_ref !MINGW '/' --allow-onelevel
35invalid_ref !MINGW '/' --normalize
36invalid_ref !MINGW '/' '--allow-onelevel --normalize'
37valid_ref 'foo/bar/baz'
38valid_ref 'foo/bar/baz' --normalize
39invalid_ref 'refs///heads/foo'
40valid_ref 'refs///heads/foo' --normalize
41invalid_ref 'heads/foo/'
42invalid_ref !MINGW '/heads/foo'
43valid_ref !MINGW '/heads/foo' --normalize
44invalid_ref '///heads/foo'
45valid_ref '///heads/foo' --normalize
46invalid_ref './foo'
47invalid_ref './foo/bar'
48invalid_ref 'foo/./bar'
49invalid_ref 'foo/bar/.'
50invalid_ref '.refs/foo'
51invalid_ref 'refs/heads/foo.'
52for c in '?' '~' '^' ':' '*' '[' ' ' '\' '..'
53do
54 invalid_ref "heads/foo${c}bar"
55done
56valid_ref 'foo./bar'
57invalid_ref 'heads/foo.lock'
58invalid_ref 'heads///foo.lock'
59invalid_ref 'foo.lock/bar'
60invalid_ref 'foo.lock///bar'
61valid_ref 'heads/foo@bar'
62valid_ref 'refs/@'
63invalid_ref '@' --allow-onelevel
64invalid_ref 'heads/v@{ation'
65invalid_ref "$(printf 'heads/foo\t')"
66invalid_ref "$(printf 'heads/foo\177')"
67valid_ref "$(printf 'heads/fu\303\237')"
68valid_ref 'heads/*foo/bar' --refspec-pattern
69valid_ref 'heads/foo*/bar' --refspec-pattern
70valid_ref 'heads/f*o/bar' --refspec-pattern
71invalid_ref 'heads/f*o*/bar' --refspec-pattern
72invalid_ref 'heads/foo*/bar*' --refspec-pattern
73
74ref='foo'
75invalid_ref "$ref"
76valid_ref "$ref" --allow-onelevel
77invalid_ref "$ref" '--allow-onelevel --no-allow-onelevel'
78valid_ref "$ref" '--no-allow-onelevel --allow-onelevel'
79invalid_ref "$ref" --refspec-pattern
80valid_ref "$ref" '--refspec-pattern --allow-onelevel'
81invalid_ref "$ref" --normalize
82valid_ref "$ref" '--allow-onelevel --normalize'
83
84ref='foo/bar'
85valid_ref "$ref"
86valid_ref "$ref" --allow-onelevel
87valid_ref "$ref" --refspec-pattern
88valid_ref "$ref" '--refspec-pattern --allow-onelevel'
89valid_ref "$ref" --normalize
90
91ref='foo/*'
92invalid_ref "$ref"
93invalid_ref "$ref" --allow-onelevel
94valid_ref "$ref" --refspec-pattern
95valid_ref "$ref" '--refspec-pattern --allow-onelevel'
96
97ref='*/foo'
98invalid_ref "$ref"
99invalid_ref "$ref" --allow-onelevel
100valid_ref "$ref" --refspec-pattern
101valid_ref "$ref" '--refspec-pattern --allow-onelevel'
102invalid_ref "$ref" --normalize
103valid_ref "$ref" '--refspec-pattern --normalize'
104
105ref='foo/*/bar'
106invalid_ref "$ref"
107invalid_ref "$ref" --allow-onelevel
108valid_ref "$ref" --refspec-pattern
109valid_ref "$ref" '--refspec-pattern --allow-onelevel'
110
111ref='*'
112invalid_ref "$ref"
113invalid_ref "$ref" --allow-onelevel
114invalid_ref "$ref" --refspec-pattern
115valid_ref "$ref" '--refspec-pattern --allow-onelevel'
116
117ref='foo/*/*'
118invalid_ref "$ref" --refspec-pattern
119invalid_ref "$ref" '--refspec-pattern --allow-onelevel'
120
121ref='*/foo/*'
122invalid_ref "$ref" --refspec-pattern
123invalid_ref "$ref" '--refspec-pattern --allow-onelevel'
124
125ref='*/*/foo'
126invalid_ref "$ref" --refspec-pattern
127invalid_ref "$ref" '--refspec-pattern --allow-onelevel'
128
129ref='/foo'
130invalid_ref !MINGW "$ref"
131invalid_ref !MINGW "$ref" --allow-onelevel
132invalid_ref !MINGW "$ref" --refspec-pattern
133invalid_ref !MINGW "$ref" '--refspec-pattern --allow-onelevel'
134invalid_ref !MINGW "$ref" --normalize
135valid_ref !MINGW "$ref" '--allow-onelevel --normalize'
136invalid_ref !MINGW "$ref" '--refspec-pattern --normalize'
137valid_ref !MINGW "$ref" '--refspec-pattern --allow-onelevel --normalize'
138
139test_expect_success "check-ref-format --branch @{-1}" '
140 T=$(git write-tree) &&
141 sha1=$(echo A | git commit-tree $T) &&
142 git update-ref refs/heads/main $sha1 &&
143 git update-ref refs/remotes/origin/main $sha1 &&
144 git checkout main &&
145 git checkout origin/main &&
146 git checkout main &&
147 refname=$(git check-ref-format --branch @{-1}) &&
148 test "$refname" = "$sha1" &&
149 refname2=$(git check-ref-format --branch @{-2}) &&
150 test "$refname2" = main'
151
152test_expect_success 'check-ref-format --branch -nain' '
153 test_must_fail git check-ref-format --branch -nain >actual &&
154 test_must_be_empty actual
155'
156
157test_expect_success 'check-ref-format --branch from subdir' '
158 mkdir subdir &&
159
160 T=$(git write-tree) &&
161 sha1=$(echo A | git commit-tree $T) &&
162 git update-ref refs/heads/main $sha1 &&
163 git update-ref refs/remotes/origin/main $sha1 &&
164 git checkout main &&
165 git checkout origin/main &&
166 git checkout main &&
167 refname=$(
168 cd subdir &&
169 git check-ref-format --branch @{-1}
170 ) &&
171 test "$refname" = "$sha1"
172'
173
174test_expect_success 'check-ref-format --branch @{-1} from non-repo' '
175 nongit test_must_fail git check-ref-format --branch @{-1} >actual &&
176 test_must_be_empty actual
177'
178
179test_expect_success 'check-ref-format --branch main from non-repo' '
180 echo main >expect &&
181 nongit git check-ref-format --branch main >actual &&
182 test_cmp expect actual
183'
184
185valid_ref_normalized() {
186 prereq=
187 case $1 in
188 [A-Z!]*)
189 prereq=$1
190 shift
191 esac
192 test_expect_success $prereq "ref name '$1' simplifies to '$2'" "
193 refname=\$(git check-ref-format --normalize '$1') &&
194 test \"\$refname\" = '$2'
195 "
196}
197invalid_ref_normalized() {
198 prereq=
199 case $1 in
200 [A-Z!]*)
201 prereq=$1
202 shift
203 esac
204 test_expect_success $prereq "check-ref-format --normalize rejects '$1'" "
205 test_must_fail git check-ref-format --normalize '$1'
206 "
207}
208
209valid_ref_normalized 'heads/foo' 'heads/foo'
210valid_ref_normalized 'refs///heads/foo' 'refs/heads/foo'
211valid_ref_normalized !MINGW '/heads/foo' 'heads/foo'
212valid_ref_normalized '///heads/foo' 'heads/foo'
213invalid_ref_normalized 'foo'
214invalid_ref_normalized !MINGW '/foo'
215invalid_ref_normalized 'heads/foo/../bar'
216invalid_ref_normalized 'heads/./foo'
217invalid_ref_normalized 'heads\foo'
218invalid_ref_normalized 'heads/foo.lock'
219invalid_ref_normalized 'heads///foo.lock'
220invalid_ref_normalized 'foo.lock/bar'
221invalid_ref_normalized 'foo.lock///bar'
222
223test_done