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
10 test_expect_success setup
'
11 test_commit --annotate A &&
12 git checkout -b side &&
13 test_commit --annotate B &&
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
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 --branches, --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.branches &&
129 git show-ref --branches >actual &&
130 test_cmp expect.branches actual &&
134 echo $(git rev-parse refs/tags/$tag) refs/tags/$tag || return 1
136 git show-ref --tags >actual &&
137 test_cmp expect.tags actual &&
139 cat expect.branches expect.tags >expect &&
140 git show-ref --branches --tags >actual &&
141 test_cmp expect actual &&
144 echo $(git rev-parse HEAD) HEAD &&
145 cat expect.branches expect.tags
147 git show-ref --branches --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
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^{}"
164 git show-ref --head -d B >actual &&
165 test_cmp expect actual
168 test_expect_success
'show-ref --heads is deprecated and hidden' '
169 test_expect_code 129 git show-ref -h >short-help &&
170 test_grep ! -e --heads short-help &&
171 git show-ref --heads >actual 2>warning &&
172 test_grep ! deprecated warning &&
173 test_cmp expect.branches actual
176 test_expect_success
'show-ref --verify HEAD' '
177 echo $(git rev-parse HEAD) HEAD >expect &&
178 git show-ref --verify HEAD >actual &&
179 test_cmp expect actual &&
181 git show-ref --verify -q HEAD >actual &&
182 test_must_be_empty actual
185 test_expect_success
'show-ref --verify pseudorefs' '
186 git update-ref CHERRY_PICK_HEAD HEAD $ZERO_OID &&
187 test_when_finished "git update-ref -d CHERRY_PICK_HEAD" &&
188 git show-ref -s --verify HEAD >actual &&
189 git show-ref -s --verify CHERRY_PICK_HEAD >expect &&
190 test_cmp actual expect
193 test_expect_success
'show-ref --verify with dangling ref' '
195 echo "$*" | sed "s#..#.git/objects/&/#"
199 file=$(sha1_file "$*") &&
204 test_when_finished "rm -rf dangling" &&
208 test_commit dangling &&
209 sha=$(git rev-parse refs/tags/dangling) &&
210 remove_object $sha &&
211 test_must_fail git show-ref --verify refs/tags/dangling
215 test_expect_success
'show-ref sub-modes are mutually exclusive' '
216 test_must_fail git show-ref --verify --exclude-existing 2>err &&
218 grep "exclude-existing" err &&
219 grep "cannot be used together" err &&
221 test_must_fail git show-ref --verify --exists 2>err &&
224 grep "cannot be used together" err &&
226 test_must_fail git show-ref --exclude-existing --exists 2>err &&
227 grep "exclude-existing" err &&
229 grep "cannot be used together" err
232 test_expect_success
'--exists with existing reference' '
233 git show-ref --exists refs/heads/$GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
236 test_expect_success
'--exists with missing reference' '
237 test_expect_code 2 git show-ref --exists refs/heads/does-not-exist
240 test_expect_success
'--exists does not use DWIM' '
241 test_expect_code 2 git show-ref --exists $GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME 2>err &&
242 grep "reference does not exist" err
245 test_expect_success
'--exists with HEAD' '
246 git show-ref --exists HEAD
249 test_expect_success
'--exists with bad reference name' '
250 test_when_finished "git update-ref -d refs/heads/bad...name" &&
251 new_oid=$(git rev-parse HEAD) &&
252 test-tool ref-store main update-ref msg refs/heads/bad...name $new_oid $ZERO_OID REF_SKIP_REFNAME_VERIFICATION &&
253 git show-ref --exists refs/heads/bad...name
256 test_expect_success
'--exists with arbitrary symref' '
257 test_when_finished "git symbolic-ref -d refs/symref" &&
258 git symbolic-ref refs/symref refs/heads/$GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME &&
259 git show-ref --exists refs/symref
262 test_expect_success
'--exists with dangling symref' '
263 test_when_finished "git symbolic-ref -d refs/heads/dangling" &&
264 git symbolic-ref refs/heads/dangling refs/heads/does-not-exist &&
265 git show-ref --exists refs/heads/dangling
268 test_expect_success
'--exists with nonexistent object ID' '
269 test-tool ref-store main update-ref msg refs/heads/missing-oid $(test_oid 001) $ZERO_OID REF_SKIP_OID_VERIFICATION &&
270 git show-ref --exists refs/heads/missing-oid
273 test_expect_success
'--exists with non-commit object' '
274 tree_oid=$(git rev-parse HEAD^{tree}) &&
275 test-tool ref-store main update-ref msg refs/heads/tree ${tree_oid} $ZERO_OID REF_SKIP_OID_VERIFICATION &&
276 git show-ref --exists refs/heads/tree
279 test_expect_success
'--exists with directory fails with generic error' '
280 cat >expect <<-EOF &&
281 error: reference does not exist
283 test_expect_code 2 git show-ref --exists refs/heads 2>err &&
287 test_expect_success
'--exists with non-existent special ref' '
288 test_expect_code 2 git show-ref --exists FETCH_HEAD
291 test_expect_success
'--exists with existing special ref' '
292 test_when_finished "rm .git/FETCH_HEAD" &&
293 git rev-parse HEAD >.git/FETCH_HEAD &&
294 git show-ref --exists FETCH_HEAD