setup: fix leaking repository format
[alt-git.git] / t / t1402-check-ref-format.sh
blob5ed9d7318e0cc97435952039c7e8c1af842a24a9
1 #!/bin/sh
3 test_description='Test git check-ref-format'
5 TEST_PASSES_SANITIZE_LEAK=true
6 . ./test-lib.sh
8 valid_ref() {
9 prereq=
10 case $1 in
11 [A-Z!]*)
12 prereq=$1
13 shift
14 esac
15 desc="ref name '$1' is valid${2:+ with options $2}"
16 test_expect_success $prereq "$desc" "
17 git check-ref-format $2 '$1'
20 invalid_ref() {
21 prereq=
22 case $1 in
23 [A-Z!]*)
24 prereq=$1
25 shift
26 esac
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'
33 invalid_ref ''
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
47 invalid_ref './foo'
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'
55 valid_ref '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
72 ref='foo'
73 invalid_ref "$ref"
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'
80 ref='foo/bar'
81 valid_ref "$ref"
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
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'
93 ref='*/foo'
94 invalid_ref "$ref"
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'
101 ref='foo/*/bar'
102 invalid_ref "$ref"
103 invalid_ref "$ref" --allow-onelevel
104 valid_ref "$ref" --refspec-pattern
105 valid_ref "$ref" '--refspec-pattern --allow-onelevel'
107 ref='*'
108 invalid_ref "$ref"
109 invalid_ref "$ref" --allow-onelevel
110 invalid_ref "$ref" --refspec-pattern
111 valid_ref "$ref" '--refspec-pattern --allow-onelevel'
113 ref='foo/*/*'
114 invalid_ref "$ref" --refspec-pattern
115 invalid_ref "$ref" '--refspec-pattern --allow-onelevel'
117 ref='*/foo/*'
118 invalid_ref "$ref" --refspec-pattern
119 invalid_ref "$ref" '--refspec-pattern --allow-onelevel'
121 ref='*/*/foo'
122 invalid_ref "$ref" --refspec-pattern
123 invalid_ref "$ref" '--refspec-pattern --allow-onelevel'
125 ref='/foo'
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 &&
140 git checkout main &&
141 git checkout origin/main &&
142 git checkout 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' '
154 mkdir 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 &&
160 git checkout main &&
161 git checkout origin/main &&
162 git checkout main &&
163 refname=$(
164 cd subdir &&
165 git check-ref-format --branch @{-1}
166 ) &&
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' '
176 echo main >expect &&
177 nongit git check-ref-format --branch main >actual &&
178 test_cmp expect actual
181 valid_ref_normalized() {
182 prereq=
183 case $1 in
184 [A-Z!]*)
185 prereq=$1
186 shift
187 esac
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() {
194 prereq=
195 case $1 in
196 [A-Z!]*)
197 prereq=$1
198 shift
199 esac
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'
219 test_done