Commit 316659489a97e8e93f88dd3610320c8ae5b35e4a

nulltoken 2012-08-24T21:30:45

refs: introduce git_reference_peel() Fix #530

diff --git a/include/git2/refs.h b/include/git2/refs.h
index 660b48b..73b32a9 100644
--- a/include/git2/refs.h
+++ b/include/git2/refs.h
@@ -434,6 +434,26 @@ GIT_EXTERN(int) git_reference_normalize_name(
 	const char *name,
 	unsigned int flags);
 
+/**
+ * Recursively peel an reference until an object of the
+ * specified type is met.
+ *
+ * The retrieved `peeled` object is owned by the repository
+ * and should be closed with the `git_object_free` method.
+ *
+ * If you pass `GIT_OBJ_ANY` as the target type, then the object
+ * will be peeled until a non-tag object is met.
+ *
+ * @param peeled Pointer to the peeled git_object
+ * @param ref The reference to be processed
+ * @param target_type The type of the requested object
+ * @return 0 or an error code
+ */
+GIT_EXTERN(int) git_reference_peel(
+	git_object **out,
+	git_reference *ref,
+	git_otype type);
+
 /** @} */
 GIT_END_DECL
 #endif
diff --git a/src/refs.c b/src/refs.c
index 211a587..cdf3cb9 100644
--- a/src/refs.c
+++ b/src/refs.c
@@ -1844,3 +1844,50 @@ int git_reference_is_remote(git_reference *ref)
 	assert(ref);
 	return git__prefixcmp(ref->name, GIT_REFS_REMOTES_DIR) == 0;
 }
+
+static int peel_error(int error, git_reference *ref, const char* msg)
+{
+	giterr_set(
+		GITERR_INVALID,
+		"The reference '%s' cannot be peeled - %s", git_reference_name(ref), msg);
+	return error;
+}
+
+static int reference_target(git_object **object, git_reference *ref)
+{
+	const git_oid *oid;
+
+	oid = git_reference_oid(ref);
+
+	return git_object_lookup(object, git_reference_owner(ref), oid, GIT_OBJ_ANY);
+}
+
+int git_reference_peel(
+		git_object **peeled,
+		git_reference *ref,
+		git_otype target_type)
+{
+	git_reference *resolved = NULL;
+	git_object *target = NULL;
+	int error;
+
+	assert(ref);
+
+	if ((error = git_reference_resolve(&resolved, ref)) < 0)
+		return peel_error(error, ref, "Cannot resolve reference");
+
+	if ((error = reference_target(&target, resolved)) < 0) {
+		peel_error(error, ref, "Cannot retrieve reference target");
+		goto cleanup;
+	}
+	
+	if (target_type == GIT_OBJ_ANY && git_object_type(target) != GIT_OBJ_TAG)
+		error = git_object__dup(peeled, target);
+	else 
+		error = git_object_peel(peeled, target, target_type);
+
+cleanup:
+	git_object_free(target);
+	git_reference_free(resolved);
+	return error;
+}
diff --git a/tests-clar/refs/peel.c b/tests-clar/refs/peel.c
new file mode 100644
index 0000000..35a290b
--- /dev/null
+++ b/tests-clar/refs/peel.c
@@ -0,0 +1,91 @@
+#include "clar_libgit2.h"
+
+static git_repository *g_repo;
+
+void test_refs_peel__initialize(void)
+{
+	cl_git_pass(git_repository_open(&g_repo, cl_fixture("testrepo.git")));
+}
+
+void test_refs_peel__cleanup(void)
+{
+	git_repository_free(g_repo);
+}
+
+static void assert_peel(
+	const char *ref_name,
+	git_otype requested_type,
+	const char* expected_sha,
+	git_otype expected_type)
+{
+	git_oid expected_oid;
+	git_reference *ref;
+	git_object *peeled;
+
+	cl_git_pass(git_reference_lookup(&ref, g_repo, ref_name));
+	
+	cl_git_pass(git_reference_peel(&peeled, ref, requested_type));
+
+	cl_git_pass(git_oid_fromstr(&expected_oid, expected_sha));
+	cl_assert_equal_i(0, git_oid_cmp(&expected_oid, git_object_id(peeled)));
+
+	cl_assert_equal_i(expected_type, git_object_type(peeled));
+
+	git_object_free(peeled);
+	git_reference_free(ref);
+}
+
+static void assert_peel_error(int error, const char *ref_name, git_otype requested_type)
+{
+	git_reference *ref;
+	git_object *peeled;
+
+	cl_git_pass(git_reference_lookup(&ref, g_repo, ref_name));
+	
+	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)
+{
+	assert_peel("refs/tags/test", GIT_OBJ_TAG,
+		"b25fa35b38051e4ae45d4222e795f9df2e43f1d1", GIT_OBJ_TAG);
+	assert_peel("refs/tags/test", GIT_OBJ_COMMIT,
+		"e90810b8df3e80c413d903f631643c716887138d", GIT_OBJ_COMMIT);
+	assert_peel("refs/tags/test", GIT_OBJ_TREE,
+		"53fc32d17276939fc79ed05badaef2db09990016", GIT_OBJ_TREE);
+	assert_peel("refs/tags/point_to_blob", GIT_OBJ_BLOB,
+		"1385f264afb75a56a5bec74243be9b367ba4ca08", GIT_OBJ_BLOB);
+}
+
+void test_refs_peel__can_peel_a_branch(void)
+{
+	assert_peel("refs/heads/master", GIT_OBJ_COMMIT,
+		"a65fedf39aefe402d3bb6e24df4d4f5fe4547750", GIT_OBJ_COMMIT);
+	assert_peel("refs/heads/master", GIT_OBJ_TREE,
+		"944c0f6e4dfa41595e6eb3ceecdb14f50fe18162", GIT_OBJ_TREE);
+}
+
+void test_refs_peel__can_peel_a_symbolic_reference(void)
+{
+	assert_peel("HEAD", GIT_OBJ_COMMIT,
+		"a65fedf39aefe402d3bb6e24df4d4f5fe4547750", GIT_OBJ_COMMIT);
+	assert_peel("HEAD", GIT_OBJ_TREE,
+		"944c0f6e4dfa41595e6eb3ceecdb14f50fe18162", GIT_OBJ_TREE);
+}
+
+void test_refs_peel__cannot_peel_into_a_non_existing_target(void)
+{
+	assert_peel_error(GIT_ERROR, "refs/tags/point_to_blob", GIT_OBJ_TAG);
+}
+
+void test_refs_peel__can_peel_into_any_non_tag_object(void)
+{
+	assert_peel("refs/heads/master", GIT_OBJ_ANY,
+		"a65fedf39aefe402d3bb6e24df4d4f5fe4547750", GIT_OBJ_COMMIT);
+	assert_peel("refs/tags/point_to_blob", GIT_OBJ_ANY,
+		"1385f264afb75a56a5bec74243be9b367ba4ca08", GIT_OBJ_BLOB);
+	assert_peel("refs/tags/test", GIT_OBJ_ANY,
+		"e90810b8df3e80c413d903f631643c716887138d", GIT_OBJ_COMMIT);
+}