Merge branch 'jn/maint-notes-avoid-va-args' into maint
[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         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 }
15
16 valid_ref 'heads/foo'
17 invalid_ref 'foo'
18 valid_ref 'foo/bar/baz'
19 valid_ref 'refs///heads/foo'
20 invalid_ref 'heads/foo/'
21 valid_ref '/heads/foo'
22 valid_ref '///heads/foo'
23 invalid_ref '/foo'
24 invalid_ref './foo'
25 invalid_ref '.refs/foo'
26 invalid_ref 'heads/foo..bar'
27 invalid_ref 'heads/foo?bar'
28 valid_ref 'foo./bar'
29 invalid_ref 'heads/foo.lock'
30 valid_ref 'heads/foo@bar'
31 invalid_ref 'heads/v@{ation'
32 invalid_ref 'heads/foo\bar'
33 invalid_ref "$(printf 'heads/foo\t')"
34 invalid_ref "$(printf 'heads/foo\177')"
35 valid_ref "$(printf 'heads/fu\303\237')"
36
37 test_expect_success "check-ref-format --branch @{-1}" '
38         T=$(git write-tree) &&
39         sha1=$(echo A | git commit-tree $T) &&
40         git update-ref refs/heads/master $sha1 &&
41         git update-ref refs/remotes/origin/master $sha1 &&
42         git checkout master &&
43         git checkout origin/master &&
44         git checkout master &&
45         refname=$(git check-ref-format --branch @{-1}) &&
46         test "$refname" = "$sha1" &&
47         refname2=$(git check-ref-format --branch @{-2}) &&
48         test "$refname2" = master'
49
50 test_expect_success 'check-ref-format --branch from subdir' '
51         mkdir subdir &&
52
53         T=$(git write-tree) &&
54         sha1=$(echo A | git commit-tree $T) &&
55         git update-ref refs/heads/master $sha1 &&
56         git update-ref refs/remotes/origin/master $sha1 &&
57         git checkout master &&
58         git checkout origin/master &&
59         git checkout master &&
60         refname=$(
61                 cd subdir &&
62                 git check-ref-format --branch @{-1}
63         ) &&
64         test "$refname" = "$sha1"
65 '
66
67 valid_ref_normalized() {
68         test_expect_success "ref name '$1' simplifies to '$2'" "
69                 refname=\$(git check-ref-format --print '$1') &&
70                 test \"\$refname\" = '$2'"
71 }
72 invalid_ref_normalized() {
73         test_expect_success "check-ref-format --print rejects '$1'" "
74                 test_must_fail git check-ref-format --print '$1'"
75 }
76
77 valid_ref_normalized 'heads/foo' 'heads/foo'
78 valid_ref_normalized 'refs///heads/foo' 'refs/heads/foo'
79 valid_ref_normalized '/heads/foo' 'heads/foo'
80 valid_ref_normalized '///heads/foo' 'heads/foo'
81 invalid_ref_normalized 'foo'
82 invalid_ref_normalized '/foo'
83 invalid_ref_normalized 'heads/foo/../bar'
84 invalid_ref_normalized 'heads/./foo'
85 invalid_ref_normalized 'heads\foo'
86
87 test_done