peel.c 4.41 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
#include "clar_libgit2.h"

static git_repository *g_repo;

void test_object_peel__initialize(void)
{
	cl_git_pass(git_repository_open(&g_repo, cl_fixture("testrepo.git")));
}

void test_object_peel__cleanup(void)
{
	git_repository_free(g_repo);
13
	g_repo = NULL;
14 15
}

16 17 18 19 20
static void assert_peel(
	const char *sha,
	git_otype requested_type,
	const char* expected_sha,
	git_otype expected_type)
21 22 23 24 25 26 27 28 29 30 31
{
	git_oid oid, expected_oid;
	git_object *obj;
	git_object *peeled;

	cl_git_pass(git_oid_fromstr(&oid, sha));
	cl_git_pass(git_object_lookup(&obj, g_repo, &oid, GIT_OBJ_ANY));
	
	cl_git_pass(git_object_peel(&peeled, obj, requested_type));

	cl_git_pass(git_oid_fromstr(&expected_oid, expected_sha));
32
	cl_assert_equal_oid(&expected_oid, git_object_id(peeled));
33

34 35
	cl_assert_equal_i(expected_type, git_object_type(peeled));

36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
	git_object_free(peeled);
	git_object_free(obj);
}

static void assert_peel_error(int error, const char *sha, git_otype requested_type)
{
	git_oid oid;
	git_object *obj;
	git_object *peeled;

	cl_git_pass(git_oid_fromstr(&oid, sha));
	cl_git_pass(git_object_lookup(&obj, g_repo, &oid, GIT_OBJ_ANY));
	
	cl_assert_equal_i(error, git_object_peel(&peeled, obj, requested_type));

	git_object_free(obj);
}

void test_object_peel__peeling_an_object_into_its_own_type_returns_another_instance_of_it(void)
{
56 57 58 59 60 61 62 63
	assert_peel("e90810b8df3e80c413d903f631643c716887138d", GIT_OBJ_COMMIT,
		"e90810b8df3e80c413d903f631643c716887138d", GIT_OBJ_COMMIT);
	assert_peel("7b4384978d2493e851f9cca7858815fac9b10980", GIT_OBJ_TAG,
		"7b4384978d2493e851f9cca7858815fac9b10980", GIT_OBJ_TAG);
	assert_peel("53fc32d17276939fc79ed05badaef2db09990016", GIT_OBJ_TREE,
		"53fc32d17276939fc79ed05badaef2db09990016", GIT_OBJ_TREE);
	assert_peel("0266163a49e280c4f5ed1e08facd36a2bd716bcf", GIT_OBJ_BLOB,
		"0266163a49e280c4f5ed1e08facd36a2bd716bcf", GIT_OBJ_BLOB);
64 65
}

66
void test_object_peel__tag(void)
67
{
68 69 70 71
	assert_peel("7b4384978d2493e851f9cca7858815fac9b10980", GIT_OBJ_COMMIT,
		"e90810b8df3e80c413d903f631643c716887138d", GIT_OBJ_COMMIT);
	assert_peel("7b4384978d2493e851f9cca7858815fac9b10980", GIT_OBJ_TREE,
		"53fc32d17276939fc79ed05badaef2db09990016", GIT_OBJ_TREE);
72 73 74
	assert_peel_error(GIT_EPEEL, "7b4384978d2493e851f9cca7858815fac9b10980", GIT_OBJ_BLOB);
	assert_peel("7b4384978d2493e851f9cca7858815fac9b10980", GIT_OBJ_ANY,
		    "e90810b8df3e80c413d903f631643c716887138d", GIT_OBJ_COMMIT);
75 76
}

77
void test_object_peel__commit(void)
78
{
79
	assert_peel_error(GIT_EINVALIDSPEC, "e90810b8df3e80c413d903f631643c716887138d", GIT_OBJ_BLOB);
80
	assert_peel("e90810b8df3e80c413d903f631643c716887138d", GIT_OBJ_TREE,
81 82 83 84 85 86
		    "53fc32d17276939fc79ed05badaef2db09990016", GIT_OBJ_TREE);
	assert_peel("e90810b8df3e80c413d903f631643c716887138d", GIT_OBJ_COMMIT,
		    "e90810b8df3e80c413d903f631643c716887138d", GIT_OBJ_COMMIT);
	assert_peel_error(GIT_EINVALIDSPEC, "e90810b8df3e80c413d903f631643c716887138d", GIT_OBJ_TAG);
	assert_peel("e90810b8df3e80c413d903f631643c716887138d", GIT_OBJ_ANY,
		    "53fc32d17276939fc79ed05badaef2db09990016", GIT_OBJ_TREE);
87 88
}

89
void test_object_peel__tree(void)
90
{
91 92 93 94 95 96
	assert_peel_error(GIT_EINVALIDSPEC, "53fc32d17276939fc79ed05badaef2db09990016", GIT_OBJ_BLOB);
	assert_peel("53fc32d17276939fc79ed05badaef2db09990016", GIT_OBJ_TREE,
		    "53fc32d17276939fc79ed05badaef2db09990016", GIT_OBJ_TREE);
	assert_peel_error(GIT_EINVALIDSPEC, "53fc32d17276939fc79ed05badaef2db09990016", GIT_OBJ_COMMIT);
	assert_peel_error(GIT_EINVALIDSPEC, "53fc32d17276939fc79ed05badaef2db09990016", GIT_OBJ_TAG);
	assert_peel_error(GIT_EINVALIDSPEC, "53fc32d17276939fc79ed05badaef2db09990016", GIT_OBJ_ANY);
97 98
}

99
void test_object_peel__blob(void)
100
{
101 102 103 104 105 106
	assert_peel("0266163a49e280c4f5ed1e08facd36a2bd716bcf", GIT_OBJ_BLOB,
		    "0266163a49e280c4f5ed1e08facd36a2bd716bcf", GIT_OBJ_BLOB);
	assert_peel_error(GIT_EINVALIDSPEC, "0266163a49e280c4f5ed1e08facd36a2bd716bcf", GIT_OBJ_TREE);
	assert_peel_error(GIT_EINVALIDSPEC, "0266163a49e280c4f5ed1e08facd36a2bd716bcf", GIT_OBJ_COMMIT);
	assert_peel_error(GIT_EINVALIDSPEC, "0266163a49e280c4f5ed1e08facd36a2bd716bcf", GIT_OBJ_TAG);
	assert_peel_error(GIT_EINVALIDSPEC, "0266163a49e280c4f5ed1e08facd36a2bd716bcf", GIT_OBJ_ANY);
107 108
}

109
void test_object_peel__target_any_object_for_type_change(void)
110
{
111
	/* tag to commit */
112 113
	assert_peel("7b4384978d2493e851f9cca7858815fac9b10980", GIT_OBJ_ANY,
		"e90810b8df3e80c413d903f631643c716887138d", GIT_OBJ_COMMIT);
114 115

	/* commit to tree */
116 117
	assert_peel("e90810b8df3e80c413d903f631643c716887138d", GIT_OBJ_ANY,
		"53fc32d17276939fc79ed05badaef2db09990016", GIT_OBJ_TREE);
118
}