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
31valid_ref 'foo/bar/baz'
32valid_ref 'refs///heads/foo'
33invalid_ref 'heads/foo/'
34valid_ref '/heads/foo'
35valid_ref '///heads/foo'
36invalid_ref './foo'
37invalid_ref './foo/bar'
38invalid_ref 'foo/./bar'
39invalid_ref 'foo/bar/.'
40invalid_ref '.refs/foo'
41invalid_ref 'heads/foo..bar'
42invalid_ref 'heads/foo?bar'
43valid_ref 'foo./bar'
44invalid_ref 'heads/foo.lock'
45invalid_ref 'heads///foo.lock'
46valid_ref 'foo.lock/bar'
47valid_ref 'foo.lock///bar'
48valid_ref 'heads/foo@bar'
49invalid_ref 'heads/v@{ation'
50invalid_ref 'heads/foo\bar'
51invalid_ref "$(printf 'heads/foo\t')"
52invalid_ref "$(printf 'heads/foo\177')"
53valid_ref "$(printf 'heads/fu\303\237')"
54invalid_ref 'heads/*foo/bar' --refspec-pattern
55invalid_ref 'heads/foo*/bar' --refspec-pattern
56invalid_ref 'heads/f*o/bar' --refspec-pattern
57
58ref='foo'
59invalid_ref "$ref"
60valid_ref "$ref" --allow-onelevel
61invalid_ref "$ref" --refspec-pattern
62valid_ref "$ref" '--refspec-pattern --allow-onelevel'
63
64ref='foo/bar'
65valid_ref "$ref"
66valid_ref "$ref" --allow-onelevel
67valid_ref "$ref" --refspec-pattern
68valid_ref "$ref" '--refspec-pattern --allow-onelevel'
69
70ref='foo/*'
71invalid_ref "$ref"
72invalid_ref "$ref" --allow-onelevel
73valid_ref "$ref" --refspec-pattern
74valid_ref "$ref" '--refspec-pattern --allow-onelevel'
75
76ref='*/foo'
77invalid_ref "$ref"
78invalid_ref "$ref" --allow-onelevel
79valid_ref "$ref" --refspec-pattern
80valid_ref "$ref" '--refspec-pattern --allow-onelevel'
81
82ref='foo/*/bar'
83invalid_ref "$ref"
84invalid_ref "$ref" --allow-onelevel
85valid_ref "$ref" --refspec-pattern
86valid_ref "$ref" '--refspec-pattern --allow-onelevel'
87
88ref='*'
89invalid_ref "$ref"
90invalid_ref "$ref" --allow-onelevel
91invalid_ref "$ref" --refspec-pattern
92valid_ref "$ref" '--refspec-pattern --allow-onelevel'
93
94ref='foo/*/*'
95invalid_ref "$ref" --refspec-pattern
96invalid_ref "$ref" '--refspec-pattern --allow-onelevel'
97
98ref='*/foo/*'
99invalid_ref "$ref" --refspec-pattern
100invalid_ref "$ref" '--refspec-pattern --allow-onelevel'
101
102ref='*/*/foo'
103invalid_ref "$ref" --refspec-pattern
104invalid_ref "$ref" '--refspec-pattern --allow-onelevel'
105
106ref='/foo'
107invalid_ref "$ref"
108valid_ref "$ref" --allow-onelevel
109invalid_ref "$ref" --refspec-pattern
110valid_ref "$ref" '--refspec-pattern --allow-onelevel'
111
112test_expect_success "check-ref-format --branch @{-1}" '
113 T=$(git write-tree) &&
114 sha1=$(echo A | git commit-tree $T) &&
115 git update-ref refs/heads/master $sha1 &&
116 git update-ref refs/remotes/origin/master $sha1 &&
117 git checkout master &&
118 git checkout origin/master &&
119 git checkout master &&
120 refname=$(git check-ref-format --branch @{-1}) &&
121 test "$refname" = "$sha1" &&
122 refname2=$(git check-ref-format --branch @{-2}) &&
123 test "$refname2" = master'
124
125test_expect_success 'check-ref-format --branch from subdir' '
126 mkdir subdir &&
127
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=$(
136 cd subdir &&
137 git check-ref-format --branch @{-1}
138 ) &&
139 test "$refname" = "$sha1"
140'
141
142valid_ref_normalized() {
143 test_expect_success "ref name '$1' simplifies to '$2'" "
144 refname=\$(git check-ref-format --print '$1') &&
145 test \"\$refname\" = '$2'"
146}
147invalid_ref_normalized() {
148 test_expect_success "check-ref-format --print rejects '$1'" "
149 test_must_fail git check-ref-format --print '$1'"
150}
151
152valid_ref_normalized 'heads/foo' 'heads/foo'
153valid_ref_normalized 'refs///heads/foo' 'refs/heads/foo'
154valid_ref_normalized '/heads/foo' 'heads/foo'
155valid_ref_normalized '///heads/foo' 'heads/foo'
156invalid_ref_normalized 'foo'
157invalid_ref_normalized '/foo'
158invalid_ref_normalized 'heads/foo/../bar'
159invalid_ref_normalized 'heads/./foo'
160invalid_ref_normalized 'heads\foo'
161invalid_ref_normalized 'heads/foo.lock'
162invalid_ref_normalized 'heads///foo.lock'
163valid_ref_normalized 'foo.lock/bar' 'foo.lock/bar'
164valid_ref_normalized 'foo.lock///bar' 'foo.lock/bar'
165
166test_done