refs/files: skip creation of "refs/{heads,tags}" for worktrees
[git.git] / t / t1403-show-ref.sh
blobb50ae6fcf115abe384444db1ef0ddbf04ce247d3
1 #!/bin/sh
3 test_description='show-ref'
4 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
5 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
7 TEST_PASSES_SANITIZE_LEAK=true
8 . ./test-lib.sh
10 test_expect_success setup '
11 test_commit --annotate A &&
12 git checkout -b side &&
13 test_commit --annotate B &&
14 git checkout main &&
15 test_commit C &&
16 git branch B A^0
19 test_expect_success 'show-ref' '
20 echo $(git rev-parse refs/tags/A) refs/tags/A >expect &&
22 git show-ref A >actual &&
23 test_cmp expect actual &&
25 git show-ref tags/A >actual &&
26 test_cmp expect actual &&
28 git show-ref refs/tags/A >actual &&
29 test_cmp expect actual &&
31 test_must_fail git show-ref D >actual &&
32 test_must_be_empty actual
35 test_expect_success 'show-ref -q' '
36 git show-ref -q A >actual &&
37 test_must_be_empty actual &&
39 git show-ref -q tags/A >actual &&
40 test_must_be_empty actual &&
42 git show-ref -q refs/tags/A >actual &&
43 test_must_be_empty actual &&
45 test_must_fail git show-ref -q D >actual &&
46 test_must_be_empty actual
49 test_expect_success 'show-ref --verify' '
50 echo $(git rev-parse refs/tags/A) refs/tags/A >expect &&
52 git show-ref --verify refs/tags/A >actual &&
53 test_cmp expect actual &&
55 test_must_fail git show-ref --verify A >actual &&
56 test_must_be_empty actual &&
58 test_must_fail git show-ref --verify tags/A >actual &&
59 test_must_be_empty actual &&
61 test_must_fail git show-ref --verify D >actual &&
62 test_must_be_empty actual
65 test_expect_success 'show-ref --verify -q' '
66 git show-ref --verify -q refs/tags/A >actual &&
67 test_must_be_empty actual &&
69 test_must_fail git show-ref --verify -q A >actual &&
70 test_must_be_empty actual &&
72 test_must_fail git show-ref --verify -q tags/A >actual &&
73 test_must_be_empty actual &&
75 test_must_fail git show-ref --verify -q D >actual &&
76 test_must_be_empty actual
79 test_expect_success 'show-ref -d' '
81 echo $(git rev-parse refs/tags/A) refs/tags/A &&
82 echo $(git rev-parse refs/tags/A^0) "refs/tags/A^{}" &&
83 echo $(git rev-parse refs/tags/C) refs/tags/C
84 } >expect &&
85 git show-ref -d A C >actual &&
86 test_cmp expect actual &&
88 git show-ref -d tags/A tags/C >actual &&
89 test_cmp expect actual &&
91 git show-ref -d refs/tags/A refs/tags/C >actual &&
92 test_cmp expect actual &&
94 git show-ref --verify -d refs/tags/A refs/tags/C >actual &&
95 test_cmp expect actual &&
97 echo $(git rev-parse refs/heads/main) refs/heads/main >expect &&
98 git show-ref -d main >actual &&
99 test_cmp expect actual &&
101 git show-ref -d heads/main >actual &&
102 test_cmp expect actual &&
104 git show-ref -d refs/heads/main >actual &&
105 test_cmp expect actual &&
107 git show-ref -d --verify refs/heads/main >actual &&
108 test_cmp expect actual &&
110 test_must_fail git show-ref -d --verify main >actual &&
111 test_must_be_empty actual &&
113 test_must_fail git show-ref -d --verify heads/main >actual &&
114 test_must_be_empty actual &&
116 test_must_fail git show-ref --verify -d A C >actual &&
117 test_must_be_empty actual &&
119 test_must_fail git show-ref --verify -d tags/A tags/C >actual &&
120 test_must_be_empty actual
124 test_expect_success 'show-ref --heads, --tags, --head, pattern' '
125 for branch in B main side
127 echo $(git rev-parse refs/heads/$branch) refs/heads/$branch || return 1
128 done >expect.heads &&
129 git show-ref --heads >actual &&
130 test_cmp expect.heads actual &&
132 for tag in A B C
134 echo $(git rev-parse refs/tags/$tag) refs/tags/$tag || return 1
135 done >expect.tags &&
136 git show-ref --tags >actual &&
137 test_cmp expect.tags actual &&
139 cat expect.heads expect.tags >expect &&
140 git show-ref --heads --tags >actual &&
141 test_cmp expect actual &&
144 echo $(git rev-parse HEAD) HEAD &&
145 cat expect.heads expect.tags
146 } >expect &&
147 git show-ref --heads --tags --head >actual &&
148 test_cmp expect actual &&
151 echo $(git rev-parse HEAD) HEAD &&
152 echo $(git rev-parse refs/heads/B) refs/heads/B &&
153 echo $(git rev-parse refs/tags/B) refs/tags/B
154 } >expect &&
155 git show-ref --head B >actual &&
156 test_cmp expect actual &&
159 echo $(git rev-parse HEAD) HEAD &&
160 echo $(git rev-parse refs/heads/B) refs/heads/B &&
161 echo $(git rev-parse refs/tags/B) refs/tags/B &&
162 echo $(git rev-parse refs/tags/B^0) "refs/tags/B^{}"
163 } >expect &&
164 git show-ref --head -d B >actual &&
165 test_cmp expect actual
168 test_expect_success 'show-ref --verify HEAD' '
169 echo $(git rev-parse HEAD) HEAD >expect &&
170 git show-ref --verify HEAD >actual &&
171 test_cmp expect actual &&
173 git show-ref --verify -q HEAD >actual &&
174 test_must_be_empty actual
177 test_expect_success 'show-ref --verify with dangling ref' '
178 sha1_file() {
179 echo "$*" | sed "s#..#.git/objects/&/#"
180 } &&
182 remove_object() {
183 file=$(sha1_file "$*") &&
184 test -e "$file" &&
185 rm -f "$file"
186 } &&
188 test_when_finished "rm -rf dangling" &&
190 git init dangling &&
191 cd dangling &&
192 test_commit dangling &&
193 sha=$(git rev-parse refs/tags/dangling) &&
194 remove_object $sha &&
195 test_must_fail git show-ref --verify refs/tags/dangling
199 test_expect_success 'show-ref sub-modes are mutually exclusive' '
200 cat >expect <<-EOF &&
201 fatal: only one of ${SQ}--exclude-existing${SQ}, ${SQ}--verify${SQ} or ${SQ}--exists${SQ} can be given
204 test_must_fail git show-ref --verify --exclude-existing 2>err &&
205 test_cmp expect err &&
207 test_must_fail git show-ref --verify --exists 2>err &&
208 test_cmp expect err &&
210 test_must_fail git show-ref --exclude-existing --exists 2>err &&
211 test_cmp expect err
214 test_expect_success '--exists with existing reference' '
215 git show-ref --exists refs/heads/$GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
218 test_expect_success '--exists with missing reference' '
219 test_expect_code 2 git show-ref --exists refs/heads/does-not-exist
222 test_expect_success '--exists does not use DWIM' '
223 test_expect_code 2 git show-ref --exists $GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME 2>err &&
224 grep "reference does not exist" err
227 test_expect_success '--exists with HEAD' '
228 git show-ref --exists HEAD
231 test_expect_success '--exists with bad reference name' '
232 test_when_finished "git update-ref -d refs/heads/bad...name" &&
233 new_oid=$(git rev-parse HEAD) &&
234 test-tool ref-store main update-ref msg refs/heads/bad...name $new_oid $ZERO_OID REF_SKIP_REFNAME_VERIFICATION &&
235 git show-ref --exists refs/heads/bad...name
238 test_expect_success '--exists with arbitrary symref' '
239 test_when_finished "git symbolic-ref -d refs/symref" &&
240 git symbolic-ref refs/symref refs/heads/$GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME &&
241 git show-ref --exists refs/symref
244 test_expect_success '--exists with dangling symref' '
245 test_when_finished "git symbolic-ref -d refs/heads/dangling" &&
246 git symbolic-ref refs/heads/dangling refs/heads/does-not-exist &&
247 git show-ref --exists refs/heads/dangling
250 test_expect_success '--exists with nonexistent object ID' '
251 test-tool ref-store main update-ref msg refs/heads/missing-oid $(test_oid 001) $ZERO_OID REF_SKIP_OID_VERIFICATION &&
252 git show-ref --exists refs/heads/missing-oid
255 test_expect_success '--exists with non-commit object' '
256 tree_oid=$(git rev-parse HEAD^{tree}) &&
257 test-tool ref-store main update-ref msg refs/heads/tree ${tree_oid} $ZERO_OID REF_SKIP_OID_VERIFICATION &&
258 git show-ref --exists refs/heads/tree
261 test_expect_success '--exists with directory fails with generic error' '
262 cat >expect <<-EOF &&
263 error: failed to look up reference: Is a directory
265 test_expect_code 1 git show-ref --exists refs/heads 2>err &&
266 test_cmp expect err
269 test_done