Commit 9e680bcc002af8f230bfce8dbfee3fef4001fb38

nulltoken 2011-03-30T23:26:36

Add git_tag_delete()

diff --git a/include/git2/tag.h b/include/git2/tag.h
index 6468cfd..1ee1e0c 100644
--- a/include/git2/tag.h
+++ b/include/git2/tag.h
@@ -247,6 +247,20 @@ GIT_EXTERN(int) git_tag_create_o_f(
 		const git_signature *tagger,
 		const char *message);
 
+/**
+* Delete an existing tag reference.
+*
+* @param repo Repository where lives the tag
+*
+* @param tag_name Name of the tag to be deleted;
+* this name is validated for consistency.
+*
+* @return 0 on success; error code otherwise.
+*/
+GIT_EXTERN(int) git_tag_delete(
+		git_repository *repo,
+		const char *tag_name);
+
 /** @} */
 GIT_END_DECL
 #endif
diff --git a/src/tag.c b/src/tag.c
index e75c469..ff2d0ab 100644
--- a/src/tag.c
+++ b/src/tag.c
@@ -153,6 +153,21 @@ static int parse_tag_buffer(git_tag *tag, char *buffer, const char *buffer_end)
 	return GIT_SUCCESS;
 }
 
+static int retreive_tag_reference(git_reference **tag_reference_out, char *ref_name_out, git_repository *repo, const char *tag_name)
+{
+	git_reference *tag_ref;
+	int error;
+
+	git__joinpath(ref_name_out, GIT_REFS_TAGS_DIR, tag_name);
+	error = git_reference_lookup(&tag_ref, repo, ref_name_out);
+	if (error < GIT_SUCCESS)
+		return error;
+
+		*tag_reference_out = tag_ref;;
+
+	return GIT_SUCCESS;
+}
+
 static int tag_create(
 		git_oid *oid,
 		git_repository *repo,
@@ -177,8 +192,7 @@ static int tag_create(
 
 	/** Ensure the tag name doesn't conflict with an already existing 
 	    reference unless overwriting has explictly been requested **/
-	git__joinpath(ref_name, GIT_REFS_TAGS_DIR, tag_name);
-	error = git_reference_lookup(&new_ref, repo, ref_name);
+	error = retreive_tag_reference(&new_ref, ref_name, repo, tag_name);
 
 	switch (error) {
 	case GIT_SUCCESS:
@@ -305,6 +319,19 @@ int git_tag_create_f(
 		tagger, message, 1);
 }
 
+int git_tag_delete(git_repository *repo, const char *tag_name)
+{
+	int error;
+	git_reference *tag_ref;
+	char ref_name[MAX_GITDIR_TREE_STRUCTURE_PATH_LENGTH];
+
+	error = retreive_tag_reference(&tag_ref, ref_name, repo, tag_name);
+	if (error < GIT_SUCCESS)
+		return error;
+
+	return git_reference_delete(tag_ref);
+}
+
 int git_tag__parse(git_tag *tag, git_odb_object *obj)
 {
 	assert(tag);
diff --git a/tests/t08-tag.c b/tests/t08-tag.c
index 2bea4bc..ebb7a1f 100644
--- a/tests/t08-tag.c
+++ b/tests/t08-tag.c
@@ -225,6 +225,20 @@ BEGIN_TEST(write3, "Replace an already existing tag")
 
 END_TEST
 
+BEGIN_TEST(write4, "Delete an already existing tag")
+	git_repository *repo;
+	git_reference *ref_tag;
+
+	must_pass(open_temp_repo(&repo, REPOSITORY_FOLDER));
+
+	must_pass(git_tag_delete(repo,"very-simple"));
+
+	must_fail(git_reference_lookup(&ref_tag, repo, "refs/tags/very-simple"));
+
+	close_temp_repo(repo);
+
+END_TEST
+
 BEGIN_SUITE(tag)
 	ADD_TEST(read0);
 	ADD_TEST(write0);