Merge branch 'jk/pull-rebase-with-work-tree'
[git] / t / t1402-check-ref-format.sh
1 #!/bin/sh
2
3 test_description='Test git check-ref-format'
4
5 . ./test-lib.sh
6
7 valid_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 }
17 invalid_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
28 invalid_ref ''
29 invalid_ref '/'
30 invalid_ref '/' --allow-onelevel
31 invalid_ref '/' --normalize
32 invalid_ref '/' '--allow-onelevel --normalize'
33 valid_ref 'foo/bar/baz'
34 valid_ref 'foo/bar/baz' --normalize
35 invalid_ref 'refs///heads/foo'
36 valid_ref 'refs///heads/foo' --normalize
37 invalid_ref 'heads/foo/'
38 invalid_ref '/heads/foo'
39 valid_ref '/heads/foo' --normalize
40 invalid_ref '///heads/foo'
41 valid_ref '///heads/foo' --normalize
42 invalid_ref './foo'
43 invalid_ref './foo/bar'
44 invalid_ref 'foo/./bar'
45 invalid_ref 'foo/bar/.'
46 invalid_ref '.refs/foo'
47 invalid_ref 'heads/foo..bar'
48 invalid_ref 'heads/foo?bar'
49 valid_ref 'foo./bar'
50 invalid_ref 'heads/foo.lock'
51 invalid_ref 'heads///foo.lock'
52 invalid_ref 'foo.lock/bar'
53 invalid_ref 'foo.lock///bar'
54 valid_ref 'heads/foo@bar'
55 invalid_ref 'heads/v@{ation'
56 invalid_ref 'heads/foo\bar'
57 invalid_ref "$(printf 'heads/foo\t')"
58 invalid_ref "$(printf 'heads/foo\177')"
59 valid_ref "$(printf 'heads/fu\303\237')"
60 invalid_ref 'heads/*foo/bar' --refspec-pattern
61 invalid_ref 'heads/foo*/bar' --refspec-pattern
62 invalid_ref 'heads/f*o/bar' --refspec-pattern
63
64 ref='foo'
65 invalid_ref "$ref"
66 valid_ref "$ref" --allow-onelevel
67 invalid_ref "$ref" --refspec-pattern
68 valid_ref "$ref" '--refspec-pattern --allow-onelevel'
69 invalid_ref "$ref" --normalize
70 valid_ref "$ref" '--allow-onelevel --normalize'
71
72 ref='foo/bar'
73 valid_ref "$ref"
74 valid_ref "$ref" --allow-onelevel
75 valid_ref "$ref" --refspec-pattern
76 valid_ref "$ref" '--refspec-pattern --allow-onelevel'
77 valid_ref "$ref" --normalize
78
79 ref='foo/*'
80 invalid_ref "$ref"
81 invalid_ref "$ref" --allow-onelevel
82 valid_ref "$ref" --refspec-pattern
83 valid_ref "$ref" '--refspec-pattern --allow-onelevel'
84
85 ref='*/foo'
86 invalid_ref "$ref"
87 invalid_ref "$ref" --allow-onelevel
88 valid_ref "$ref" --refspec-pattern
89 valid_ref "$ref" '--refspec-pattern --allow-onelevel'
90 invalid_ref "$ref" --normalize
91 valid_ref "$ref" '--refspec-pattern --normalize'
92
93 ref='foo/*/bar'
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
99 ref='*'
100 invalid_ref "$ref"
101 invalid_ref "$ref" --allow-onelevel
102 invalid_ref "$ref" --refspec-pattern
103 valid_ref "$ref" '--refspec-pattern --allow-onelevel'
104
105 ref='foo/*/*'
106 invalid_ref "$ref" --refspec-pattern
107 invalid_ref "$ref" '--refspec-pattern --allow-onelevel'
108
109 ref='*/foo/*'
110 invalid_ref "$ref" --refspec-pattern
111 invalid_ref "$ref" '--refspec-pattern --allow-onelevel'
112
113 ref='*/*/foo'
114 invalid_ref "$ref" --refspec-pattern
115 invalid_ref "$ref" '--refspec-pattern --allow-onelevel'
116
117 ref='/foo'
118 invalid_ref "$ref"
119 invalid_ref "$ref" --allow-onelevel
120 invalid_ref "$ref" --refspec-pattern
121 invalid_ref "$ref" '--refspec-pattern --allow-onelevel'
122 invalid_ref "$ref" --normalize
123 valid_ref "$ref" '--allow-onelevel --normalize'
124 invalid_ref "$ref" '--refspec-pattern --normalize'
125 valid_ref "$ref" '--refspec-pattern --allow-onelevel --normalize'
126
127 test_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
140 test_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
157 valid_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 }
162 invalid_ref_normalized() {
163         test_expect_success "check-ref-format --normalize rejects '$1'" "
164                 test_must_fail git check-ref-format --normalize '$1'"
165 }
166
167 valid_ref_normalized 'heads/foo' 'heads/foo'
168 valid_ref_normalized 'refs///heads/foo' 'refs/heads/foo'
169 valid_ref_normalized '/heads/foo' 'heads/foo'
170 valid_ref_normalized '///heads/foo' 'heads/foo'
171 invalid_ref_normalized 'foo'
172 invalid_ref_normalized '/foo'
173 invalid_ref_normalized 'heads/foo/../bar'
174 invalid_ref_normalized 'heads/./foo'
175 invalid_ref_normalized 'heads\foo'
176 invalid_ref_normalized 'heads/foo.lock'
177 invalid_ref_normalized 'heads///foo.lock'
178 invalid_ref_normalized 'foo.lock/bar'
179 invalid_ref_normalized 'foo.lock///bar'
180
181 test_done