Commit 8b2670b1ce9cee2d8434265ac1af4023ad435645

Patrick Steinhardt 2018-12-19T10:34:00

Merge pull request #4910 from libgit2/cmn/annotated-from-tag Support creating annotated commits from annotated tags

diff --git a/include/git2/refs.h b/include/git2/refs.h
index ec67a8f..5504c30 100644
--- a/include/git2/refs.h
+++ b/include/git2/refs.h
@@ -710,7 +710,7 @@ GIT_EXTERN(int) git_reference_normalize_name(
  */
 GIT_EXTERN(int) git_reference_peel(
 	git_object **out,
-	git_reference *ref,
+	const git_reference *ref,
 	git_object_t type);
 
 /**
diff --git a/src/annotated_commit.c b/src/annotated_commit.c
index 3d982cb..d95d440 100644
--- a/src/annotated_commit.c
+++ b/src/annotated_commit.c
@@ -123,19 +123,19 @@ int git_annotated_commit_from_ref(
 	git_repository *repo,
 	const git_reference *ref)
 {
-	git_reference *resolved;
+	git_object *peeled;
 	int error = 0;
 
 	assert(out && repo && ref);
 
 	*out = NULL;
 
-	if ((error = git_reference_resolve(&resolved, ref)) < 0)
+	if ((error = git_reference_peel(&peeled, ref, GIT_OBJ_COMMIT)) < 0)
 		return error;
 
 	error = annotated_commit_init_from_id(out,
 		repo,
-		git_reference_target(resolved),
+		git_object_id(peeled),
 		git_reference_name(ref));
 
 	if (!error) {
@@ -143,7 +143,7 @@ int git_annotated_commit_from_ref(
 		GITERR_CHECK_ALLOC((*out)->ref_name);
 	}
 
-	git_reference_free(resolved);
+	git_object_free(peeled);
 	return error;
 }
 
diff --git a/src/refs.c b/src/refs.c
index ee2718d..50afb26 100644
--- a/src/refs.c
+++ b/src/refs.c
@@ -1337,7 +1337,7 @@ int git_reference_is_note(const git_reference *ref)
 	return git_reference__is_note(ref->name);
 }
 
-static int peel_error(int error, git_reference *ref, const char* msg)
+static int peel_error(int error, const git_reference *ref, const char* msg)
 {
 	giterr_set(
 		GITERR_INVALID,
@@ -1347,10 +1347,11 @@ static int peel_error(int error, git_reference *ref, const char* msg)
 
 int git_reference_peel(
 	git_object **peeled,
-	git_reference *ref,
+	const git_reference *ref,
 	git_object_t target_type)
 {
-	git_reference *resolved = NULL;
+	const git_reference *resolved = NULL;
+	git_reference *allocated = NULL;
 	git_object *target = NULL;
 	int error;
 
@@ -1359,8 +1360,10 @@ int git_reference_peel(
 	if (ref->type == GIT_REF_OID) {
 		resolved = ref;
 	} else {
-		if ((error = git_reference_resolve(&resolved, ref)) < 0)
+		if ((error = git_reference_resolve(&allocated, ref)) < 0)
 			return peel_error(error, ref, "Cannot resolve reference");
+
+		resolved = allocated;
 	}
 
 	/*
@@ -1389,9 +1392,7 @@ int git_reference_peel(
 
 cleanup:
 	git_object_free(target);
-
-	if (resolved != ref)
-		git_reference_free(resolved);
+	git_reference_free(allocated);
 
 	return error;
 }
diff --git a/tests/merge/annotated_commit.c b/tests/merge/annotated_commit.c
new file mode 100644
index 0000000..cfdf849
--- /dev/null
+++ b/tests/merge/annotated_commit.c
@@ -0,0 +1,26 @@
+#include "clar_libgit2.h"
+
+
+static git_repository *g_repo;
+
+void test_merge_annotated_commit__initialize(void)
+{
+	g_repo = cl_git_sandbox_init("testrepo");
+}
+
+void test_merge_annotated_commit__cleanup(void)
+{
+	cl_git_sandbox_cleanup();
+}
+
+void test_merge_annotated_commit__lookup_annotated_tag(void)
+{
+	git_annotated_commit *commit;
+	git_reference *ref;
+
+	cl_git_pass(git_reference_lookup(&ref, g_repo, "refs/tags/test"));
+	cl_git_pass(git_annotated_commit_from_ref(&commit, g_repo, ref));
+
+	git_annotated_commit_free(commit);
+	git_reference_free(ref);
+}