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