1 #!/bin/sh
3 test_description='Test git check-ref-format'
5 . ./test-lib.sh
7 valid_ref() {
8 test_expect_success "ref name '$1' is valid" \
9 "git check-ref-format '$1'"
10 }
11 invalid_ref() {
12 test_expect_success "ref name '$1' is not valid" \
13 "test_must_fail git check-ref-format '$1'"
14 }
16 invalid_ref ''
17 invalid_ref '/'
18 valid_ref 'heads/foo'
19 invalid_ref 'foo'
20 valid_ref 'foo/bar/baz'
21 valid_ref 'refs///heads/foo'
22 invalid_ref 'heads/foo/'
23 valid_ref '/heads/foo'
24 valid_ref '///heads/foo'
25 invalid_ref '/foo'
26 invalid_ref './foo'
27 invalid_ref '.refs/foo'
28 invalid_ref 'heads/foo..bar'
29 invalid_ref 'heads/foo?bar'
30 valid_ref 'foo./bar'
31 invalid_ref 'heads/foo.lock'
32 invalid_ref 'heads///foo.lock'
33 valid_ref 'foo.lock/bar'
34 valid_ref 'foo.lock///bar'
35 valid_ref 'heads/foo@bar'
36 invalid_ref 'heads/v@{ation'
37 invalid_ref 'heads/foo\bar'
38 invalid_ref "$(printf 'heads/foo\t')"
39 invalid_ref "$(printf 'heads/foo\177')"
40 valid_ref "$(printf 'heads/fu\303\237')"
42 test_expect_success "check-ref-format --branch @{-1}" '
43 T=$(git write-tree) &&
44 sha1=$(echo A | git commit-tree $T) &&
45 git update-ref refs/heads/master $sha1 &&
46 git update-ref refs/remotes/origin/master $sha1 &&
47 git checkout master &&
48 git checkout origin/master &&
49 git checkout master &&
50 refname=$(git check-ref-format --branch @{-1}) &&
51 test "$refname" = "$sha1" &&
52 refname2=$(git check-ref-format --branch @{-2}) &&
53 test "$refname2" = master'
55 test_expect_success 'check-ref-format --branch from subdir' '
56 mkdir subdir &&
58 T=$(git write-tree) &&
59 sha1=$(echo A | git commit-tree $T) &&
60 git update-ref refs/heads/master $sha1 &&
61 git update-ref refs/remotes/origin/master $sha1 &&
62 git checkout master &&
63 git checkout origin/master &&
64 git checkout master &&
65 refname=$(
66 cd subdir &&
67 git check-ref-format --branch @{-1}
68 ) &&
69 test "$refname" = "$sha1"
70 '
72 valid_ref_normalized() {
73 test_expect_success "ref name '$1' simplifies to '$2'" "
74 refname=\$(git check-ref-format --print '$1') &&
75 test \"\$refname\" = '$2'"
76 }
77 invalid_ref_normalized() {
78 test_expect_success "check-ref-format --print rejects '$1'" "
79 test_must_fail git check-ref-format --print '$1'"
80 }
82 valid_ref_normalized 'heads/foo' 'heads/foo'
83 valid_ref_normalized 'refs///heads/foo' 'refs/heads/foo'
84 valid_ref_normalized '/heads/foo' 'heads/foo'
85 valid_ref_normalized '///heads/foo' 'heads/foo'
86 invalid_ref_normalized 'foo'
87 invalid_ref_normalized '/foo'
88 invalid_ref_normalized 'heads/foo/../bar'
89 invalid_ref_normalized 'heads/./foo'
90 invalid_ref_normalized 'heads\foo'
91 invalid_ref_normalized 'heads/foo.lock'
92 invalid_ref_normalized 'heads///foo.lock'
93 valid_ref_normalized 'foo.lock/bar' 'foo.lock/bar'
94 valid_ref_normalized 'foo.lock///bar' 'foo.lock/bar'
96 test_done