1#!/bin/sh
2
3test_description='Test git check-ref-format'
4
5. ./test-lib.sh
6
7valid_ref() {
8 if test "$#" = 1
9 then
10 test_expect_success "ref name '$1' is valid" \
11 "git check-ref-format '$1'"
12 else
13 test_expect_success "ref name '$1' is valid with options $2" \
14 "git check-ref-format $2 '$1'"
15 fi
16}
17invalid_ref() {
18 if test "$#" = 1
19 then
20 test_expect_success "ref name '$1' is invalid" \
21 "test_must_fail git check-ref-format '$1'"
22 else
23 test_expect_success "ref name '$1' is invalid with options $2" \
24 "test_must_fail git check-ref-format $2 '$1'"
25 fi
26}
27
28invalid_ref ''
29invalid_ref '/'
30invalid_ref '/' --allow-onelevel
31invalid_ref '/' --normalize
32invalid_ref '/' '--allow-onelevel --normalize'
33valid_ref 'foo/bar/baz'
34valid_ref 'foo/bar/baz' --normalize
35invalid_ref 'refs///heads/foo'
36valid_ref 'refs///heads/foo' --normalize
37invalid_ref 'heads/foo/'
38invalid_ref '/heads/foo'
39valid_ref '/heads/foo' --normalize
40invalid_ref '///heads/foo'
41valid_ref '///heads/foo' --normalize
42invalid_ref './foo'
43invalid_ref './foo/bar'
44invalid_ref 'foo/./bar'
45invalid_ref 'foo/bar/.'
46invalid_ref '.refs/foo'
47invalid_ref 'heads/foo..bar'
48invalid_ref 'heads/foo?bar'
49valid_ref 'foo./bar'
50invalid_ref 'heads/foo.lock'
51invalid_ref 'heads///foo.lock'
52invalid_ref 'foo.lock/bar'
53invalid_ref 'foo.lock///bar'
54valid_ref 'heads/foo@bar'
55invalid_ref 'heads/v@{ation'
56invalid_ref 'heads/foo\bar'
57invalid_ref "$(printf 'heads/foo\t')"
58invalid_ref "$(printf 'heads/foo\177')"
59valid_ref "$(printf 'heads/fu\303\237')"
60invalid_ref 'heads/*foo/bar' --refspec-pattern
61invalid_ref 'heads/foo*/bar' --refspec-pattern
62invalid_ref 'heads/f*o/bar' --refspec-pattern
63
64ref='foo'
65invalid_ref "$ref"
66valid_ref "$ref" --allow-onelevel
67invalid_ref "$ref" --refspec-pattern
68valid_ref "$ref" '--refspec-pattern --allow-onelevel'
69invalid_ref "$ref" --normalize
70valid_ref "$ref" '--allow-onelevel --normalize'
71
72ref='foo/bar'
73valid_ref "$ref"
74valid_ref "$ref" --allow-onelevel
75valid_ref "$ref" --refspec-pattern
76valid_ref "$ref" '--refspec-pattern --allow-onelevel'
77valid_ref "$ref" --normalize
78
79ref='foo/*'
80invalid_ref "$ref"
81invalid_ref "$ref" --allow-onelevel
82valid_ref "$ref" --refspec-pattern
83valid_ref "$ref" '--refspec-pattern --allow-onelevel'
84
85ref='*/foo'
86invalid_ref "$ref"
87invalid_ref "$ref" --allow-onelevel
88valid_ref "$ref" --refspec-pattern
89valid_ref "$ref" '--refspec-pattern --allow-onelevel'
90invalid_ref "$ref" --normalize
91valid_ref "$ref" '--refspec-pattern --normalize'
92
93ref='foo/*/bar'
94invalid_ref "$ref"
95invalid_ref "$ref" --allow-onelevel
96valid_ref "$ref" --refspec-pattern
97valid_ref "$ref" '--refspec-pattern --allow-onelevel'
98
99ref='*'
100invalid_ref "$ref"
101invalid_ref "$ref" --allow-onelevel
102invalid_ref "$ref" --refspec-pattern
103valid_ref "$ref" '--refspec-pattern --allow-onelevel'
104
105ref='foo/*/*'
106invalid_ref "$ref" --refspec-pattern
107invalid_ref "$ref" '--refspec-pattern --allow-onelevel'
108
109ref='*/foo/*'
110invalid_ref "$ref" --refspec-pattern
111invalid_ref "$ref" '--refspec-pattern --allow-onelevel'
112
113ref='*/*/foo'
114invalid_ref "$ref" --refspec-pattern
115invalid_ref "$ref" '--refspec-pattern --allow-onelevel'
116
117ref='/foo'
118invalid_ref "$ref"
119invalid_ref "$ref" --allow-onelevel
120invalid_ref "$ref" --refspec-pattern
121invalid_ref "$ref" '--refspec-pattern --allow-onelevel'
122invalid_ref "$ref" --normalize
123valid_ref "$ref" '--allow-onelevel --normalize'
124invalid_ref "$ref" '--refspec-pattern --normalize'
125valid_ref "$ref" '--refspec-pattern --allow-onelevel --normalize'
126
127test_expect_success "check-ref-format --branch @{-1}" '
128 T=$(git write-tree) &&
129 sha1=$(echo A | git commit-tree $T) &&
130 git update-ref refs/heads/master $sha1 &&
131 git update-ref refs/remotes/origin/master $sha1 &&
132 git checkout master &&
133 git checkout origin/master &&
134 git checkout master &&
135 refname=$(git check-ref-format --branch @{-1}) &&
136 test "$refname" = "$sha1" &&
137 refname2=$(git check-ref-format --branch @{-2}) &&
138 test "$refname2" = master'
139
140test_expect_success 'check-ref-format --branch from subdir' '
141 mkdir subdir &&
142
143 T=$(git write-tree) &&
144 sha1=$(echo A | git commit-tree $T) &&
145 git update-ref refs/heads/master $sha1 &&
146 git update-ref refs/remotes/origin/master $sha1 &&
147 git checkout master &&
148 git checkout origin/master &&
149 git checkout master &&
150 refname=$(
151 cd subdir &&
152 git check-ref-format --branch @{-1}
153 ) &&
154 test "$refname" = "$sha1"
155'
156
157valid_ref_normalized() {
158 test_expect_success "ref name '$1' simplifies to '$2'" "
159 refname=\$(git check-ref-format --normalize '$1') &&
160 test \"\$refname\" = '$2'"
161}
162invalid_ref_normalized() {
163 test_expect_success "check-ref-format --normalize rejects '$1'" "
164 test_must_fail git check-ref-format --normalize '$1'"
165}
166
167valid_ref_normalized 'heads/foo' 'heads/foo'
168valid_ref_normalized 'refs///heads/foo' 'refs/heads/foo'
169valid_ref_normalized '/heads/foo' 'heads/foo'
170valid_ref_normalized '///heads/foo' 'heads/foo'
171invalid_ref_normalized 'foo'
172invalid_ref_normalized '/foo'
173invalid_ref_normalized 'heads/foo/../bar'
174invalid_ref_normalized 'heads/./foo'
175invalid_ref_normalized 'heads\foo'
176invalid_ref_normalized 'heads/foo.lock'
177invalid_ref_normalized 'heads///foo.lock'
178invalid_ref_normalized 'foo.lock/bar'
179invalid_ref_normalized 'foo.lock///bar'
180
181test_done