blob: 98e4a8613ba8ab4aebe99360e89c21be2ce3473d [file] [log] [blame]
Jonathan Nieder58a05c72009-10-12 00:27:04 -05001#!/bin/sh
2
3test_description='Test git check-ref-format'
4
5. ./test-lib.sh
6
7valid_ref() {
Johannes Sixt42afe622011-10-13 10:06:20 +02008 prereq=
9 case $1 in
Junio C Hamanof57a8712014-07-21 15:09:27 -070010 [A-Z!]*)
Johannes Sixt42afe622011-10-13 10:06:20 +020011 prereq=$1
12 shift
13 esac
René Scharfe283b3652013-01-08 21:23:01 +010014 desc="ref name '$1' is valid${2:+ with options $2}"
15 test_expect_success $prereq "$desc" "
Johannes Sixt42afe622011-10-13 10:06:20 +020016 git check-ref-format $2 '$1'
17 "
Jonathan Nieder58a05c72009-10-12 00:27:04 -050018}
19invalid_ref() {
Johannes Sixt42afe622011-10-13 10:06:20 +020020 prereq=
21 case $1 in
Junio C Hamanof57a8712014-07-21 15:09:27 -070022 [A-Z!]*)
Johannes Sixt42afe622011-10-13 10:06:20 +020023 prereq=$1
24 shift
25 esac
René Scharfe283b3652013-01-08 21:23:01 +010026 desc="ref name '$1' is invalid${2:+ with options $2}"
27 test_expect_success $prereq "$desc" "
Johannes Sixt42afe622011-10-13 10:06:20 +020028 test_must_fail git check-ref-format $2 '$1'
29 "
Jonathan Nieder58a05c72009-10-12 00:27:04 -050030}
31
Michael Haggertyf9b1a5b2011-09-15 23:10:22 +020032invalid_ref ''
Junio C Hamanof57a8712014-07-21 15:09:27 -070033invalid_ref !MINGW '/'
34invalid_ref !MINGW '/' --allow-onelevel
35invalid_ref !MINGW '/' --normalize
36invalid_ref !MINGW '/' '--allow-onelevel --normalize'
Jonathan Nieder58a05c72009-10-12 00:27:04 -050037valid_ref 'foo/bar/baz'
Michael Haggertya40e6fb2011-09-15 23:10:30 +020038valid_ref 'foo/bar/baz' --normalize
39invalid_ref 'refs///heads/foo'
40valid_ref 'refs///heads/foo' --normalize
Jonathan Nieder58a05c72009-10-12 00:27:04 -050041invalid_ref 'heads/foo/'
Junio C Hamanof57a8712014-07-21 15:09:27 -070042invalid_ref !MINGW '/heads/foo'
43valid_ref !MINGW '/heads/foo' --normalize
Michael Haggertya40e6fb2011-09-15 23:10:30 +020044invalid_ref '///heads/foo'
45valid_ref '///heads/foo' --normalize
Jonathan Nieder58a05c72009-10-12 00:27:04 -050046invalid_ref './foo'
Michael Haggertye4ed6102011-09-15 23:10:23 +020047invalid_ref './foo/bar'
48invalid_ref 'foo/./bar'
49invalid_ref 'foo/bar/.'
Jonathan Nieder58a05c72009-10-12 00:27:04 -050050invalid_ref '.refs/foo'
Jeff King919eb8a2014-07-28 11:48:11 -040051invalid_ref 'refs/heads/foo.'
Jonathan Nieder58a05c72009-10-12 00:27:04 -050052invalid_ref 'heads/foo..bar'
53invalid_ref 'heads/foo?bar'
54valid_ref 'foo./bar'
55invalid_ref 'heads/foo.lock'
Michael Haggertyf9b1a5b2011-09-15 23:10:22 +020056invalid_ref 'heads///foo.lock'
Michael Haggerty7e9d2fe2011-09-15 23:10:27 +020057invalid_ref 'foo.lock/bar'
58invalid_ref 'foo.lock///bar'
Jonathan Nieder58a05c72009-10-12 00:27:04 -050059valid_ref 'heads/foo@bar'
60invalid_ref 'heads/v@{ation'
61invalid_ref 'heads/foo\bar'
Michael Haggertyf3738c12011-08-27 06:12:44 +020062invalid_ref "$(printf 'heads/foo\t')"
63invalid_ref "$(printf 'heads/foo\177')"
64valid_ref "$(printf 'heads/fu\303\237')"
Jacob Kellercd377f42015-07-22 14:05:33 -070065valid_ref 'heads/*foo/bar' --refspec-pattern
66valid_ref 'heads/foo*/bar' --refspec-pattern
67valid_ref 'heads/f*o/bar' --refspec-pattern
68invalid_ref 'heads/f*o*/bar' --refspec-pattern
69invalid_ref 'heads/foo*/bar*' --refspec-pattern
Michael Haggertye4ed6102011-09-15 23:10:23 +020070
71ref='foo'
72invalid_ref "$ref"
73valid_ref "$ref" --allow-onelevel
74invalid_ref "$ref" --refspec-pattern
75valid_ref "$ref" '--refspec-pattern --allow-onelevel'
Michael Haggertya40e6fb2011-09-15 23:10:30 +020076invalid_ref "$ref" --normalize
77valid_ref "$ref" '--allow-onelevel --normalize'
Michael Haggertye4ed6102011-09-15 23:10:23 +020078
79ref='foo/bar'
80valid_ref "$ref"
81valid_ref "$ref" --allow-onelevel
82valid_ref "$ref" --refspec-pattern
83valid_ref "$ref" '--refspec-pattern --allow-onelevel'
Michael Haggertya40e6fb2011-09-15 23:10:30 +020084valid_ref "$ref" --normalize
Michael Haggertye4ed6102011-09-15 23:10:23 +020085
86ref='foo/*'
87invalid_ref "$ref"
88invalid_ref "$ref" --allow-onelevel
89valid_ref "$ref" --refspec-pattern
90valid_ref "$ref" '--refspec-pattern --allow-onelevel'
91
92ref='*/foo'
93invalid_ref "$ref"
94invalid_ref "$ref" --allow-onelevel
95valid_ref "$ref" --refspec-pattern
96valid_ref "$ref" '--refspec-pattern --allow-onelevel'
Michael Haggertya40e6fb2011-09-15 23:10:30 +020097invalid_ref "$ref" --normalize
98valid_ref "$ref" '--refspec-pattern --normalize'
Michael Haggertye4ed6102011-09-15 23:10:23 +020099
100ref='foo/*/bar'
101invalid_ref "$ref"
102invalid_ref "$ref" --allow-onelevel
103valid_ref "$ref" --refspec-pattern
104valid_ref "$ref" '--refspec-pattern --allow-onelevel'
105
106ref='*'
107invalid_ref "$ref"
Michael Haggerty8d9c5012011-09-15 23:10:25 +0200108invalid_ref "$ref" --allow-onelevel
Michael Haggertye4ed6102011-09-15 23:10:23 +0200109invalid_ref "$ref" --refspec-pattern
110valid_ref "$ref" '--refspec-pattern --allow-onelevel'
111
112ref='foo/*/*'
113invalid_ref "$ref" --refspec-pattern
114invalid_ref "$ref" '--refspec-pattern --allow-onelevel'
115
116ref='*/foo/*'
117invalid_ref "$ref" --refspec-pattern
118invalid_ref "$ref" '--refspec-pattern --allow-onelevel'
119
120ref='*/*/foo'
121invalid_ref "$ref" --refspec-pattern
122invalid_ref "$ref" '--refspec-pattern --allow-onelevel'
123
124ref='/foo'
Junio C Hamanof57a8712014-07-21 15:09:27 -0700125invalid_ref !MINGW "$ref"
126invalid_ref !MINGW "$ref" --allow-onelevel
127invalid_ref !MINGW "$ref" --refspec-pattern
128invalid_ref !MINGW "$ref" '--refspec-pattern --allow-onelevel'
129invalid_ref !MINGW "$ref" --normalize
130valid_ref !MINGW "$ref" '--allow-onelevel --normalize'
131invalid_ref !MINGW "$ref" '--refspec-pattern --normalize'
132valid_ref !MINGW "$ref" '--refspec-pattern --allow-onelevel --normalize'
Jonathan Nieder58a05c72009-10-12 00:27:04 -0500133
134test_expect_success "check-ref-format --branch @{-1}" '
135 T=$(git write-tree) &&
136 sha1=$(echo A | git commit-tree $T) &&
137 git update-ref refs/heads/master $sha1 &&
Jonathan Niedera48fcd82010-10-30 20:46:54 -0500138 git update-ref refs/remotes/origin/master $sha1 &&
Jonathan Nieder58a05c72009-10-12 00:27:04 -0500139 git checkout master &&
140 git checkout origin/master &&
141 git checkout master &&
142 refname=$(git check-ref-format --branch @{-1}) &&
143 test "$refname" = "$sha1" &&
144 refname2=$(git check-ref-format --branch @{-2}) &&
145 test "$refname2" = master'
146
Junio C Hamano7c3f8472017-10-17 00:08:08 -0700147test_expect_success 'check-ref-format --branch -naster' '
148 test_must_fail git check-ref-format --branch -naster >actual &&
149 test_must_be_empty actual
150'
151
Jonathan Nieder49cc4602010-08-05 22:39:22 -0500152test_expect_success 'check-ref-format --branch from subdir' '
153 mkdir subdir &&
154
155 T=$(git write-tree) &&
156 sha1=$(echo A | git commit-tree $T) &&
157 git update-ref refs/heads/master $sha1 &&
Jonathan Niedera48fcd82010-10-30 20:46:54 -0500158 git update-ref refs/remotes/origin/master $sha1 &&
Jonathan Nieder49cc4602010-08-05 22:39:22 -0500159 git checkout master &&
160 git checkout origin/master &&
161 git checkout master &&
162 refname=$(
163 cd subdir &&
164 git check-ref-format --branch @{-1}
165 ) &&
166 test "$refname" = "$sha1"
167'
168
Junio C Hamano7c3f8472017-10-17 00:08:08 -0700169test_expect_success 'check-ref-format --branch @{-1} from non-repo' '
170 nongit test_must_fail git check-ref-format --branch @{-1} >actual &&
171 test_must_be_empty actual
172'
173
174test_expect_success 'check-ref-format --branch master from non-repo' '
175 echo master >expect &&
176 nongit git check-ref-format --branch master >actual &&
177 test_cmp expect actual
178'
179
Junio C Hamano38eedc62009-10-12 16:39:43 -0700180valid_ref_normalized() {
Johannes Sixt42afe622011-10-13 10:06:20 +0200181 prereq=
182 case $1 in
Junio C Hamanof57a8712014-07-21 15:09:27 -0700183 [A-Z!]*)
Johannes Sixt42afe622011-10-13 10:06:20 +0200184 prereq=$1
185 shift
186 esac
187 test_expect_success $prereq "ref name '$1' simplifies to '$2'" "
Michael Haggertya40e6fb2011-09-15 23:10:30 +0200188 refname=\$(git check-ref-format --normalize '$1') &&
Johannes Sixt42afe622011-10-13 10:06:20 +0200189 test \"\$refname\" = '$2'
190 "
Junio C Hamano38eedc62009-10-12 16:39:43 -0700191}
192invalid_ref_normalized() {
Johannes Sixt42afe622011-10-13 10:06:20 +0200193 prereq=
194 case $1 in
Junio C Hamanof57a8712014-07-21 15:09:27 -0700195 [A-Z!]*)
Johannes Sixt42afe622011-10-13 10:06:20 +0200196 prereq=$1
197 shift
198 esac
199 test_expect_success $prereq "check-ref-format --normalize rejects '$1'" "
200 test_must_fail git check-ref-format --normalize '$1'
201 "
Junio C Hamano38eedc62009-10-12 16:39:43 -0700202}
203
204valid_ref_normalized 'heads/foo' 'heads/foo'
205valid_ref_normalized 'refs///heads/foo' 'refs/heads/foo'
Junio C Hamanof57a8712014-07-21 15:09:27 -0700206valid_ref_normalized !MINGW '/heads/foo' 'heads/foo'
Michael Haggerty2f633f42011-08-25 21:19:24 +0200207valid_ref_normalized '///heads/foo' 'heads/foo'
Junio C Hamano38eedc62009-10-12 16:39:43 -0700208invalid_ref_normalized 'foo'
Junio C Hamanof57a8712014-07-21 15:09:27 -0700209invalid_ref_normalized !MINGW '/foo'
Junio C Hamano38eedc62009-10-12 16:39:43 -0700210invalid_ref_normalized 'heads/foo/../bar'
211invalid_ref_normalized 'heads/./foo'
212invalid_ref_normalized 'heads\foo'
Michael Haggertyf9b1a5b2011-09-15 23:10:22 +0200213invalid_ref_normalized 'heads/foo.lock'
214invalid_ref_normalized 'heads///foo.lock'
Michael Haggerty7e9d2fe2011-09-15 23:10:27 +0200215invalid_ref_normalized 'foo.lock/bar'
216invalid_ref_normalized 'foo.lock///bar'
Junio C Hamano38eedc62009-10-12 16:39:43 -0700217
Jonathan Nieder58a05c72009-10-12 00:27:04 -0500218test_done