Merge branch 'jk/ref-filter-segfault-fix'
[git] / t / t1403-show-ref.sh
1 #!/bin/sh
2
3 test_description='show-ref'
4 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
5 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
6
7 . ./test-lib.sh
8
9 test_expect_success setup '
10         test_commit A &&
11         git tag -f -a -m "annotated A" A &&
12         git checkout -b side &&
13         test_commit B &&
14         git tag -f -a -m "annotated B" B &&
15         git checkout main &&
16         test_commit C &&
17         git branch B A^0
18 '
19
20 test_expect_success 'show-ref' '
21         echo $(git rev-parse refs/tags/A) refs/tags/A >expect &&
22
23         git show-ref A >actual &&
24         test_cmp expect actual &&
25
26         git show-ref tags/A >actual &&
27         test_cmp expect actual &&
28
29         git show-ref refs/tags/A >actual &&
30         test_cmp expect actual &&
31
32         test_must_fail git show-ref D >actual &&
33         test_must_be_empty actual
34 '
35
36 test_expect_success 'show-ref -q' '
37         git show-ref -q A >actual &&
38         test_must_be_empty actual &&
39
40         git show-ref -q tags/A >actual &&
41         test_must_be_empty actual &&
42
43         git show-ref -q refs/tags/A >actual &&
44         test_must_be_empty actual &&
45
46         test_must_fail git show-ref -q D >actual &&
47         test_must_be_empty actual
48 '
49
50 test_expect_success 'show-ref --verify' '
51         echo $(git rev-parse refs/tags/A) refs/tags/A >expect &&
52
53         git show-ref --verify refs/tags/A >actual &&
54         test_cmp expect actual &&
55
56         test_must_fail git show-ref --verify A >actual &&
57         test_must_be_empty actual &&
58
59         test_must_fail git show-ref --verify tags/A >actual &&
60         test_must_be_empty actual &&
61
62         test_must_fail git show-ref --verify D >actual &&
63         test_must_be_empty actual
64 '
65
66 test_expect_success 'show-ref --verify -q' '
67         git show-ref --verify -q refs/tags/A >actual &&
68         test_must_be_empty actual &&
69
70         test_must_fail git show-ref --verify -q A >actual &&
71         test_must_be_empty actual &&
72
73         test_must_fail git show-ref --verify -q tags/A >actual &&
74         test_must_be_empty actual &&
75
76         test_must_fail git show-ref --verify -q D >actual &&
77         test_must_be_empty actual
78 '
79
80 test_expect_success 'show-ref -d' '
81         {
82                 echo $(git rev-parse refs/tags/A) refs/tags/A &&
83                 echo $(git rev-parse refs/tags/A^0) "refs/tags/A^{}"
84                 echo $(git rev-parse refs/tags/C) refs/tags/C
85         } >expect &&
86         git show-ref -d A C >actual &&
87         test_cmp expect actual &&
88
89         git show-ref -d tags/A tags/C >actual &&
90         test_cmp expect actual &&
91
92         git show-ref -d refs/tags/A refs/tags/C >actual &&
93         test_cmp expect actual &&
94
95         git show-ref --verify -d refs/tags/A refs/tags/C >actual &&
96         test_cmp expect actual &&
97
98         echo $(git rev-parse refs/heads/main) refs/heads/main >expect &&
99         git show-ref -d main >actual &&
100         test_cmp expect actual &&
101
102         git show-ref -d heads/main >actual &&
103         test_cmp expect actual &&
104
105         git show-ref -d refs/heads/main >actual &&
106         test_cmp expect actual &&
107
108         git show-ref -d --verify refs/heads/main >actual &&
109         test_cmp expect actual &&
110
111         test_must_fail git show-ref -d --verify main >actual &&
112         test_must_be_empty actual &&
113
114         test_must_fail git show-ref -d --verify heads/main >actual &&
115         test_must_be_empty actual &&
116
117         test_must_fail git show-ref --verify -d A C >actual &&
118         test_must_be_empty actual &&
119
120         test_must_fail git show-ref --verify -d tags/A tags/C >actual &&
121         test_must_be_empty actual
122
123 '
124
125 test_expect_success 'show-ref --heads, --tags, --head, pattern' '
126         for branch in B main side
127         do
128                 echo $(git rev-parse refs/heads/$branch) refs/heads/$branch
129         done >expect.heads &&
130         git show-ref --heads >actual &&
131         test_cmp expect.heads actual &&
132
133         for tag in A B C
134         do
135                 echo $(git rev-parse refs/tags/$tag) refs/tags/$tag
136         done >expect.tags &&
137         git show-ref --tags >actual &&
138         test_cmp expect.tags actual &&
139
140         cat expect.heads expect.tags >expect &&
141         git show-ref --heads --tags >actual &&
142         test_cmp expect actual &&
143
144         {
145                 echo $(git rev-parse HEAD) HEAD &&
146                 cat expect.heads expect.tags
147         } >expect &&
148         git show-ref --heads --tags --head >actual &&
149         test_cmp expect actual &&
150
151         {
152                 echo $(git rev-parse HEAD) HEAD &&
153                 echo $(git rev-parse refs/heads/B) refs/heads/B
154                 echo $(git rev-parse refs/tags/B) refs/tags/B
155         } >expect &&
156         git show-ref --head B >actual &&
157         test_cmp expect actual &&
158
159         {
160                 echo $(git rev-parse HEAD) HEAD &&
161                 echo $(git rev-parse refs/heads/B) refs/heads/B
162                 echo $(git rev-parse refs/tags/B) refs/tags/B
163                 echo $(git rev-parse refs/tags/B^0) "refs/tags/B^{}"
164         } >expect &&
165         git show-ref --head -d B >actual &&
166         test_cmp expect actual
167 '
168
169 test_expect_success 'show-ref --verify HEAD' '
170         echo $(git rev-parse HEAD) HEAD >expect &&
171         git show-ref --verify HEAD >actual &&
172         test_cmp expect actual &&
173
174         git show-ref --verify -q HEAD >actual &&
175         test_must_be_empty actual
176 '
177
178 test_expect_success 'show-ref --verify with dangling ref' '
179         sha1_file() {
180                 echo "$*" | sed "s#..#.git/objects/&/#"
181         } &&
182
183         remove_object() {
184                 file=$(sha1_file "$*") &&
185                 test -e "$file" &&
186                 rm -f "$file"
187         } &&
188
189         test_when_finished "rm -rf dangling" &&
190         (
191                 git init dangling &&
192                 cd dangling &&
193                 test_commit dangling &&
194                 sha=$(git rev-parse refs/tags/dangling) &&
195                 remove_object $sha &&
196                 test_must_fail git show-ref --verify refs/tags/dangling
197         )
198 '
199
200 test_done