Add git_tag_delete()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
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);