1#!/bin/sh
2
3test_description='tests for the peel_ref optimization of packed-refs'
4GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
5export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
6
7TEST_PASSES_SANITIZE_LEAK=true
8. ./test-lib.sh
9
10test_expect_success 'create annotated tag in refs/tags' '
11	test_commit base &&
12	git tag -m annotated foo
13'
14
15test_expect_success 'create annotated tag outside of refs/tags' '
16	git update-ref refs/outside/foo refs/tags/foo
17'
18
19# This matches show-ref's output
20print_ref() {
21	echo "$(git rev-parse "$1") $1"
22}
23
24test_expect_success 'set up expected show-ref output' '
25	{
26		print_ref "refs/heads/main" &&
27		print_ref "refs/outside/foo" &&
28		print_ref "refs/outside/foo^{}" &&
29		print_ref "refs/tags/base" &&
30		print_ref "refs/tags/foo" &&
31		print_ref "refs/tags/foo^{}"
32	} >expect
33'
34
35test_expect_success 'refs are peeled outside of refs/tags (loose)' '
36	git show-ref -d >actual &&
37	test_cmp expect actual
38'
39
40test_expect_success 'refs are peeled outside of refs/tags (packed)' '
41	git pack-refs --all &&
42	git show-ref -d >actual &&
43	test_cmp expect actual
44'
45
46test_expect_success 'create old-style pack-refs without fully-peeled' '
47	# Git no longer writes without fully-peeled, so we just write our own
48	# from scratch; we could also munge the existing file to remove the
49	# fully-peeled bits, but that seems even more prone to failure,
50	# especially if the format ever changes again. At least this way we
51	# know we are emulating exactly what an older git would have written.
52	{
53		echo "# pack-refs with: peeled " &&
54		print_ref "refs/heads/main" &&
55		print_ref "refs/outside/foo" &&
56		print_ref "refs/tags/base" &&
57		print_ref "refs/tags/foo" &&
58		echo "^$(git rev-parse "refs/tags/foo^{}")"
59	} >tmp &&
60	mv tmp .git/packed-refs
61'
62
63test_expect_success 'refs are peeled outside of refs/tags (old packed)' '
64	git show-ref -d >actual &&
65	test_cmp expect actual
66'
67
68test_expect_success 'peeled refs survive deletion of packed ref' '
69	git pack-refs --all &&
70	cp .git/packed-refs fully-peeled &&
71	git branch yadda &&
72	git pack-refs --all &&
73	git branch -d yadda &&
74	test_cmp fully-peeled .git/packed-refs
75'
76
77test_done
78