dc431710042bcc5c3905da4462b673b1cd37456b
1#!/bin/sh
2
3test_description='Test git check-ref-format'
4
5. ./test-lib.sh
6
7valid_ref() {
8 test_expect_success "ref name '$1' is valid" \
9 "git check-ref-format '$1'"
10}
11invalid_ref() {
12 test_expect_success "ref name '$1' is not valid" \
13 "test_must_fail git check-ref-format '$1'"
14}
15
16invalid_ref ''
17invalid_ref '/'
18valid_ref 'heads/foo'
19invalid_ref 'foo'
20valid_ref 'foo/bar/baz'
21valid_ref 'refs///heads/foo'
22invalid_ref 'heads/foo/'
23valid_ref '/heads/foo'
24valid_ref '///heads/foo'
25invalid_ref '/foo'
26invalid_ref './foo'
27invalid_ref '.refs/foo'
28invalid_ref 'heads/foo..bar'
29invalid_ref 'heads/foo?bar'
30valid_ref 'foo./bar'
31invalid_ref 'heads/foo.lock'
32invalid_ref 'heads///foo.lock'
33valid_ref 'foo.lock/bar'
34valid_ref 'foo.lock///bar'
35valid_ref 'heads/foo@bar'
36invalid_ref 'heads/v@{ation'
37invalid_ref 'heads/foo\bar'
38invalid_ref "$(printf 'heads/foo\t')"
39invalid_ref "$(printf 'heads/foo\177')"
40valid_ref "$(printf 'heads/fu\303\237')"
41
42test_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'
54
55test_expect_success 'check-ref-format --branch from subdir' '
56 mkdir subdir &&
57
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'
71
72valid_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}
77invalid_ref_normalized() {
78 test_expect_success "check-ref-format --print rejects '$1'" "
79 test_must_fail git check-ref-format --print '$1'"
80}
81
82valid_ref_normalized 'heads/foo' 'heads/foo'
83valid_ref_normalized 'refs///heads/foo' 'refs/heads/foo'
84valid_ref_normalized '/heads/foo' 'heads/foo'
85valid_ref_normalized '///heads/foo' 'heads/foo'
86invalid_ref_normalized 'foo'
87invalid_ref_normalized '/foo'
88invalid_ref_normalized 'heads/foo/../bar'
89invalid_ref_normalized 'heads/./foo'
90invalid_ref_normalized 'heads\foo'
91invalid_ref_normalized 'heads/foo.lock'
92invalid_ref_normalized 'heads///foo.lock'
93valid_ref_normalized 'foo.lock/bar' 'foo.lock/bar'
94valid_ref_normalized 'foo.lock///bar' 'foo.lock/bar'
95
96test_done