Commit | Line | Data |
---|---|---|
58a05c74 JN |
1 | #!/bin/sh |
2 | ||
3 | test_description='Test git check-ref-format' | |
4 | ||
5 | . ./test-lib.sh | |
6 | ||
7 | valid_ref() { | |
e4ed6105 MH |
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 | |
58a05c74 JN |
16 | } |
17 | invalid_ref() { | |
e4ed6105 MH |
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 | |
58a05c74 JN |
26 | } |
27 | ||
f9b1a5b9 MH |
28 | invalid_ref '' |
29 | invalid_ref '/' | |
e4ed6105 | 30 | invalid_ref '/' --allow-onelevel |
58a05c74 JN |
31 | valid_ref 'foo/bar/baz' |
32 | valid_ref 'refs///heads/foo' | |
33 | invalid_ref 'heads/foo/' | |
2f633f41 MH |
34 | valid_ref '/heads/foo' |
35 | valid_ref '///heads/foo' | |
58a05c74 | 36 | invalid_ref './foo' |
e4ed6105 MH |
37 | invalid_ref './foo/bar' |
38 | invalid_ref 'foo/./bar' | |
39 | invalid_ref 'foo/bar/.' | |
58a05c74 JN |
40 | invalid_ref '.refs/foo' |
41 | invalid_ref 'heads/foo..bar' | |
42 | invalid_ref 'heads/foo?bar' | |
43 | valid_ref 'foo./bar' | |
44 | invalid_ref 'heads/foo.lock' | |
f9b1a5b9 MH |
45 | invalid_ref 'heads///foo.lock' |
46 | valid_ref 'foo.lock/bar' | |
47 | valid_ref 'foo.lock///bar' | |
58a05c74 JN |
48 | valid_ref 'heads/foo@bar' |
49 | invalid_ref 'heads/v@{ation' | |
50 | invalid_ref 'heads/foo\bar' | |
f3738c1c MH |
51 | invalid_ref "$(printf 'heads/foo\t')" |
52 | invalid_ref "$(printf 'heads/foo\177')" | |
53 | valid_ref "$(printf 'heads/fu\303\237')" | |
e4ed6105 MH |
54 | invalid_ref 'heads/*foo/bar' --refspec-pattern |
55 | invalid_ref 'heads/foo*/bar' --refspec-pattern | |
56 | invalid_ref 'heads/f*o/bar' --refspec-pattern | |
57 | ||
58 | ref='foo' | |
59 | invalid_ref "$ref" | |
60 | valid_ref "$ref" --allow-onelevel | |
61 | invalid_ref "$ref" --refspec-pattern | |
62 | valid_ref "$ref" '--refspec-pattern --allow-onelevel' | |
63 | ||
64 | ref='foo/bar' | |
65 | valid_ref "$ref" | |
66 | valid_ref "$ref" --allow-onelevel | |
67 | valid_ref "$ref" --refspec-pattern | |
68 | valid_ref "$ref" '--refspec-pattern --allow-onelevel' | |
69 | ||
70 | ref='foo/*' | |
71 | invalid_ref "$ref" | |
72 | invalid_ref "$ref" --allow-onelevel | |
73 | valid_ref "$ref" --refspec-pattern | |
74 | valid_ref "$ref" '--refspec-pattern --allow-onelevel' | |
75 | ||
76 | ref='*/foo' | |
77 | invalid_ref "$ref" | |
78 | invalid_ref "$ref" --allow-onelevel | |
79 | valid_ref "$ref" --refspec-pattern | |
80 | valid_ref "$ref" '--refspec-pattern --allow-onelevel' | |
81 | ||
82 | ref='foo/*/bar' | |
83 | invalid_ref "$ref" | |
84 | invalid_ref "$ref" --allow-onelevel | |
85 | valid_ref "$ref" --refspec-pattern | |
86 | valid_ref "$ref" '--refspec-pattern --allow-onelevel' | |
87 | ||
88 | ref='*' | |
89 | invalid_ref "$ref" | |
8d9c5010 | 90 | invalid_ref "$ref" --allow-onelevel |
e4ed6105 MH |
91 | invalid_ref "$ref" --refspec-pattern |
92 | valid_ref "$ref" '--refspec-pattern --allow-onelevel' | |
93 | ||
94 | ref='foo/*/*' | |
95 | invalid_ref "$ref" --refspec-pattern | |
96 | invalid_ref "$ref" '--refspec-pattern --allow-onelevel' | |
97 | ||
98 | ref='*/foo/*' | |
99 | invalid_ref "$ref" --refspec-pattern | |
100 | invalid_ref "$ref" '--refspec-pattern --allow-onelevel' | |
101 | ||
102 | ref='*/*/foo' | |
103 | invalid_ref "$ref" --refspec-pattern | |
104 | invalid_ref "$ref" '--refspec-pattern --allow-onelevel' | |
105 | ||
106 | ref='/foo' | |
107 | invalid_ref "$ref" | |
108 | valid_ref "$ref" --allow-onelevel | |
109 | invalid_ref "$ref" --refspec-pattern | |
110 | valid_ref "$ref" '--refspec-pattern --allow-onelevel' | |
58a05c74 JN |
111 | |
112 | test_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 && | |
a48fcd83 | 116 | git update-ref refs/remotes/origin/master $sha1 && |
58a05c74 JN |
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 | ||
49cc460d JN |
125 | test_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 && | |
a48fcd83 | 131 | git update-ref refs/remotes/origin/master $sha1 && |
49cc460d JN |
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 | ||
38eedc63 JH |
142 | valid_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 | } | |
147 | invalid_ref_normalized() { | |
148 | test_expect_success "check-ref-format --print rejects '$1'" " | |
149 | test_must_fail git check-ref-format --print '$1'" | |
150 | } | |
151 | ||
152 | valid_ref_normalized 'heads/foo' 'heads/foo' | |
153 | valid_ref_normalized 'refs///heads/foo' 'refs/heads/foo' | |
2f633f41 MH |
154 | valid_ref_normalized '/heads/foo' 'heads/foo' |
155 | valid_ref_normalized '///heads/foo' 'heads/foo' | |
38eedc63 | 156 | invalid_ref_normalized 'foo' |
2f633f41 | 157 | invalid_ref_normalized '/foo' |
38eedc63 JH |
158 | invalid_ref_normalized 'heads/foo/../bar' |
159 | invalid_ref_normalized 'heads/./foo' | |
160 | invalid_ref_normalized 'heads\foo' | |
f9b1a5b9 MH |
161 | invalid_ref_normalized 'heads/foo.lock' |
162 | invalid_ref_normalized 'heads///foo.lock' | |
163 | valid_ref_normalized 'foo.lock/bar' 'foo.lock/bar' | |
164 | valid_ref_normalized 'foo.lock///bar' 'foo.lock/bar' | |
38eedc63 | 165 | |
58a05c74 | 166 | test_done |