3 test_description
='Test git check-ref-format'
5 TEST_PASSES_SANITIZE_LEAK
=true
15 desc
="ref name '$1' is valid${2:+ with options $2}"
16 test_expect_success
$prereq "$desc" "
17 git check-ref-format $2 '$1'
27 desc
="ref name '$1' is invalid${2:+ with options $2}"
28 test_expect_success
$prereq "$desc" "
29 test_must_fail git check-ref-format $2 '$1'
34 invalid_ref
!MINGW
'/'
35 invalid_ref
!MINGW
'/' --allow-onelevel
36 invalid_ref
!MINGW
'/' --normalize
37 invalid_ref
!MINGW
'/' '--allow-onelevel --normalize'
38 valid_ref
'foo/bar/baz'
39 valid_ref
'foo/bar/baz' --normalize
40 invalid_ref
'refs///heads/foo'
41 valid_ref
'refs///heads/foo' --normalize
42 invalid_ref
'heads/foo/'
43 invalid_ref
!MINGW
'/heads/foo'
44 valid_ref
!MINGW
'/heads/foo' --normalize
45 invalid_ref
'///heads/foo'
46 valid_ref
'///heads/foo' --normalize
48 invalid_ref
'./foo/bar'
49 invalid_ref
'foo/./bar'
50 invalid_ref
'foo/bar/.'
51 invalid_ref
'.refs/foo'
52 invalid_ref
'refs/heads/foo.'
53 invalid_ref
'heads/foo..bar'
54 invalid_ref
'heads/foo?bar'
56 invalid_ref
'heads/foo.lock'
57 invalid_ref
'heads///foo.lock'
58 invalid_ref
'foo.lock/bar'
59 invalid_ref
'foo.lock///bar'
60 valid_ref
'heads/foo@bar'
61 invalid_ref
'heads/v@{ation'
62 invalid_ref
'heads/foo\bar'
63 invalid_ref
"$(printf 'heads/foo\t')"
64 invalid_ref
"$(printf 'heads/foo\177')"
65 valid_ref
"$(printf 'heads/fu\303\237')"
66 valid_ref
'heads/*foo/bar' --refspec-pattern
67 valid_ref
'heads/foo*/bar' --refspec-pattern
68 valid_ref
'heads/f*o/bar' --refspec-pattern
69 invalid_ref
'heads/f*o*/bar' --refspec-pattern
70 invalid_ref
'heads/foo*/bar*' --refspec-pattern
74 valid_ref
"$ref" --allow-onelevel
75 invalid_ref
"$ref" --refspec-pattern
76 valid_ref
"$ref" '--refspec-pattern --allow-onelevel'
77 invalid_ref
"$ref" --normalize
78 valid_ref
"$ref" '--allow-onelevel --normalize'
82 valid_ref
"$ref" --allow-onelevel
83 valid_ref
"$ref" --refspec-pattern
84 valid_ref
"$ref" '--refspec-pattern --allow-onelevel'
85 valid_ref
"$ref" --normalize
89 invalid_ref
"$ref" --allow-onelevel
90 valid_ref
"$ref" --refspec-pattern
91 valid_ref
"$ref" '--refspec-pattern --allow-onelevel'
95 invalid_ref
"$ref" --allow-onelevel
96 valid_ref
"$ref" --refspec-pattern
97 valid_ref
"$ref" '--refspec-pattern --allow-onelevel'
98 invalid_ref
"$ref" --normalize
99 valid_ref
"$ref" '--refspec-pattern --normalize'
103 invalid_ref
"$ref" --allow-onelevel
104 valid_ref
"$ref" --refspec-pattern
105 valid_ref
"$ref" '--refspec-pattern --allow-onelevel'
109 invalid_ref
"$ref" --allow-onelevel
110 invalid_ref
"$ref" --refspec-pattern
111 valid_ref
"$ref" '--refspec-pattern --allow-onelevel'
114 invalid_ref
"$ref" --refspec-pattern
115 invalid_ref
"$ref" '--refspec-pattern --allow-onelevel'
118 invalid_ref
"$ref" --refspec-pattern
119 invalid_ref
"$ref" '--refspec-pattern --allow-onelevel'
122 invalid_ref
"$ref" --refspec-pattern
123 invalid_ref
"$ref" '--refspec-pattern --allow-onelevel'
126 invalid_ref
!MINGW
"$ref"
127 invalid_ref
!MINGW
"$ref" --allow-onelevel
128 invalid_ref
!MINGW
"$ref" --refspec-pattern
129 invalid_ref
!MINGW
"$ref" '--refspec-pattern --allow-onelevel'
130 invalid_ref
!MINGW
"$ref" --normalize
131 valid_ref
!MINGW
"$ref" '--allow-onelevel --normalize'
132 invalid_ref
!MINGW
"$ref" '--refspec-pattern --normalize'
133 valid_ref
!MINGW
"$ref" '--refspec-pattern --allow-onelevel --normalize'
135 test_expect_success
"check-ref-format --branch @{-1}" '
136 T=$(git write-tree) &&
137 sha1=$(echo A | git commit-tree $T) &&
138 git update-ref refs/heads/main $sha1 &&
139 git update-ref refs/remotes/origin/main $sha1 &&
141 git checkout origin/main &&
143 refname=$(git check-ref-format --branch @{-1}) &&
144 test "$refname" = "$sha1" &&
145 refname2=$(git check-ref-format --branch @{-2}) &&
146 test "$refname2" = main'
148 test_expect_success
'check-ref-format --branch -nain' '
149 test_must_fail git check-ref-format --branch -nain >actual &&
150 test_must_be_empty actual
153 test_expect_success
'check-ref-format --branch from subdir' '
156 T=$(git write-tree) &&
157 sha1=$(echo A | git commit-tree $T) &&
158 git update-ref refs/heads/main $sha1 &&
159 git update-ref refs/remotes/origin/main $sha1 &&
161 git checkout origin/main &&
165 git check-ref-format --branch @{-1}
167 test "$refname" = "$sha1"
170 test_expect_success
'check-ref-format --branch @{-1} from non-repo' '
171 nongit test_must_fail git check-ref-format --branch @{-1} >actual &&
172 test_must_be_empty actual
175 test_expect_success
'check-ref-format --branch main from non-repo' '
177 nongit git check-ref-format --branch main >actual &&
178 test_cmp expect actual
181 valid_ref_normalized
() {
188 test_expect_success
$prereq "ref name '$1' simplifies to '$2'" "
189 refname=\$(git check-ref-format --normalize '$1') &&
190 test \"\$refname\" = '$2'
193 invalid_ref_normalized
() {
200 test_expect_success
$prereq "check-ref-format --normalize rejects '$1'" "
201 test_must_fail git check-ref-format --normalize '$1'
205 valid_ref_normalized
'heads/foo' 'heads/foo'
206 valid_ref_normalized
'refs///heads/foo' 'refs/heads/foo'
207 valid_ref_normalized
!MINGW
'/heads/foo' 'heads/foo'
208 valid_ref_normalized
'///heads/foo' 'heads/foo'
209 invalid_ref_normalized
'foo'
210 invalid_ref_normalized
!MINGW
'/foo'
211 invalid_ref_normalized
'heads/foo/../bar'
212 invalid_ref_normalized
'heads/./foo'
213 invalid_ref_normalized
'heads\foo'
214 invalid_ref_normalized
'heads/foo.lock'
215 invalid_ref_normalized
'heads///foo.lock'
216 invalid_ref_normalized
'foo.lock/bar'
217 invalid_ref_normalized
'foo.lock///bar'