peel.c 3.87 KB
Newer Older
1 2 3
#include "clar_libgit2.h"

static git_repository *g_repo;
4
static git_repository *g_peel_repo;
5 6 7 8

void test_refs_peel__initialize(void)
{
	cl_git_pass(git_repository_open(&g_repo, cl_fixture("testrepo.git")));
9
	cl_git_pass(git_repository_open(&g_peel_repo, cl_fixture("peeled.git")));
10 11 12 13 14
}

void test_refs_peel__cleanup(void)
{
	git_repository_free(g_repo);
15
	g_repo = NULL;
16 17
	git_repository_free(g_peel_repo);
	g_peel_repo = NULL;
18 19
}

20 21
static void assert_peel_generic(
	git_repository *repo,
22
	const char *ref_name,
23
	git_object_t requested_type,
24
	const char* expected_sha,
25
	git_object_t expected_type)
26 27 28 29 30
{
	git_oid expected_oid;
	git_reference *ref;
	git_object *peeled;

31
	cl_git_pass(git_reference_lookup(&ref, repo, ref_name));
32

33 34 35
	cl_git_pass(git_reference_peel(&peeled, ref, requested_type));

	cl_git_pass(git_oid_fromstr(&expected_oid, expected_sha));
36
	cl_assert_equal_oid(&expected_oid, git_object_id(peeled));
37 38 39 40 41 42 43

	cl_assert_equal_i(expected_type, git_object_type(peeled));

	git_object_free(peeled);
	git_reference_free(ref);
}

44 45
static void assert_peel(
	const char *ref_name,
46
	git_object_t requested_type,
47
	const char* expected_sha,
48
	git_object_t expected_type)
49 50 51 52 53
{
	assert_peel_generic(g_repo, ref_name, requested_type,
			    expected_sha, expected_type);
}

54
static void assert_peel_error(int error, const char *ref_name, git_object_t requested_type)
55 56 57 58 59
{
	git_reference *ref;
	git_object *peeled;

	cl_git_pass(git_reference_lookup(&ref, g_repo, ref_name));
60

61 62 63 64 65 66 67
	cl_assert_equal_i(error, git_reference_peel(&peeled, ref, requested_type));

	git_reference_free(ref);
}

void test_refs_peel__can_peel_a_tag(void)
{
68 69 70 71 72 73 74 75
	assert_peel("refs/tags/test", GIT_OBJECT_TAG,
		"b25fa35b38051e4ae45d4222e795f9df2e43f1d1", GIT_OBJECT_TAG);
	assert_peel("refs/tags/test", GIT_OBJECT_COMMIT,
		"e90810b8df3e80c413d903f631643c716887138d", GIT_OBJECT_COMMIT);
	assert_peel("refs/tags/test", GIT_OBJECT_TREE,
		"53fc32d17276939fc79ed05badaef2db09990016", GIT_OBJECT_TREE);
	assert_peel("refs/tags/point_to_blob", GIT_OBJECT_BLOB,
		"1385f264afb75a56a5bec74243be9b367ba4ca08", GIT_OBJECT_BLOB);
76 77 78 79
}

void test_refs_peel__can_peel_a_branch(void)
{
80 81 82 83
	assert_peel("refs/heads/master", GIT_OBJECT_COMMIT,
		"a65fedf39aefe402d3bb6e24df4d4f5fe4547750", GIT_OBJECT_COMMIT);
	assert_peel("refs/heads/master", GIT_OBJECT_TREE,
		"944c0f6e4dfa41595e6eb3ceecdb14f50fe18162", GIT_OBJECT_TREE);
84 85 86 87
}

void test_refs_peel__can_peel_a_symbolic_reference(void)
{
88 89 90 91
	assert_peel("HEAD", GIT_OBJECT_COMMIT,
		"a65fedf39aefe402d3bb6e24df4d4f5fe4547750", GIT_OBJECT_COMMIT);
	assert_peel("HEAD", GIT_OBJECT_TREE,
		"944c0f6e4dfa41595e6eb3ceecdb14f50fe18162", GIT_OBJECT_TREE);
92 93 94 95
}

void test_refs_peel__cannot_peel_into_a_non_existing_target(void)
{
96
	assert_peel_error(GIT_EINVALIDSPEC, "refs/tags/point_to_blob", GIT_OBJECT_TAG);
97 98 99 100
}

void test_refs_peel__can_peel_into_any_non_tag_object(void)
{
101 102 103 104 105 106
	assert_peel("refs/heads/master", GIT_OBJECT_ANY,
		"a65fedf39aefe402d3bb6e24df4d4f5fe4547750", GIT_OBJECT_COMMIT);
	assert_peel("refs/tags/point_to_blob", GIT_OBJECT_ANY,
		"1385f264afb75a56a5bec74243be9b367ba4ca08", GIT_OBJECT_BLOB);
	assert_peel("refs/tags/test", GIT_OBJECT_ANY,
		"e90810b8df3e80c413d903f631643c716887138d", GIT_OBJECT_COMMIT);
107
}
108 109 110 111

void test_refs_peel__can_peel_fully_peeled_packed_refs(void)
{
	assert_peel_generic(g_peel_repo,
112
			    "refs/tags/tag-inside-tags", GIT_OBJECT_ANY,
113
			    "0df1a5865c8abfc09f1f2182e6a31be550e99f07",
114
			    GIT_OBJECT_COMMIT);
115
	assert_peel_generic(g_peel_repo,
116
			    "refs/foo/tag-outside-tags", GIT_OBJECT_ANY,
117
			    "0df1a5865c8abfc09f1f2182e6a31be550e99f07",
118
			    GIT_OBJECT_COMMIT);
119
}
120 121 122 123

void test_refs_peel__can_peel_fully_peeled_tag_to_tag(void)
{
	assert_peel_generic(g_peel_repo,
124
			    "refs/tags/tag-inside-tags", GIT_OBJECT_TAG,
125
			    "c2596aa0151888587ec5c0187f261e63412d9e11",
126
			    GIT_OBJECT_TAG);
127
	assert_peel_generic(g_peel_repo,
128
			    "refs/foo/tag-outside-tags", GIT_OBJECT_TAG,
129
			    "c2596aa0151888587ec5c0187f261e63412d9e11",
130
			    GIT_OBJECT_TAG);
131
}