Add `git_tag_list` Lists all the tag references in a repository using a custom callback. Includes unit tests courtesy of Emeric Fermas <3
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
diff --git a/include/git2/tag.h b/include/git2/tag.h
index c751a13..3fc6b44 100644
--- a/include/git2/tag.h
+++ b/include/git2/tag.h
@@ -275,6 +275,23 @@ GIT_EXTERN(int) git_tag_delete(
git_repository *repo,
const char *tag_name);
+/**
+ * Fill a list with all the tags in the Repository
+ *
+ * The string array will be filled with the names of the
+ * matching tags; these values are owned by the user and
+ * should be free'd manually when no longer needed, using
+ * `git_strarray_free`.
+ *
+ * @param array Pointer to a git_strarray structure where
+ * the tag names will be stored
+ * @param repo Repository where to find the tags
+ * @return 0 on success; error code otherwise
+ */
+GIT_EXTERN(int) git_tag_list(
+ git_strarray *tag_names,
+ git_repository *repo);
+
/** @} */
GIT_END_DECL
#endif
diff --git a/src/tag.c b/src/tag.c
index a340095..148eb28 100644
--- a/src/tag.c
+++ b/src/tag.c
@@ -377,3 +377,29 @@ int git_tag__parse(git_tag *tag, git_odb_object *obj)
return parse_tag_buffer(tag, obj->raw.data, (char *)obj->raw.data + obj->raw.len);
}
+static int tag_list_cb(const char *tag_name, void *payload)
+{
+ if (git__prefixcmp(tag_name, GIT_REFS_TAGS_DIR) == 0)
+ return git_vector_insert((git_vector *)payload, git__strdup(tag_name));
+
+ return GIT_SUCCESS;
+}
+
+int git_tag_list(git_strarray *tag_names, git_repository *repo)
+{
+ int error;
+ git_vector taglist;
+
+ if (git_vector_init(&taglist, 8, NULL) < GIT_SUCCESS)
+ return GIT_ENOMEM;
+
+ error = git_reference_listcb(repo, GIT_REF_OID|GIT_REF_PACKED, &tag_list_cb, (void *)&taglist);
+ if (error < GIT_SUCCESS) {
+ git_vector_free(&taglist);
+ return error;
+ }
+
+ tag_names->strings = (char **)taglist.contents;
+ tag_names->count = taglist.length;
+ return GIT_SUCCESS;
+}
diff --git a/tests/t08-tag.c b/tests/t08-tag.c
index de67fdd..fae2e99 100644
--- a/tests/t08-tag.c
+++ b/tests/t08-tag.c
@@ -64,6 +64,19 @@ BEGIN_TEST(read0, "read and parse a tag from the repository")
git_repository_free(repo);
END_TEST
+BEGIN_TEST(read1, "list all tag names from the repository")
+ git_repository *repo;
+ git_strarray tag_list;
+
+ must_pass(git_repository_open(&repo, REPOSITORY_FOLDER));
+ must_pass(git_tag_list(&tag_list, repo));
+
+ must_be_true(tag_list.count == 3);
+
+ git_strarray_free(&tag_list);
+ git_repository_free(repo);
+END_TEST
+
#define TAGGER_NAME "Vicent Marti"
#define TAGGER_EMAIL "vicent@github.com"
@@ -227,6 +240,7 @@ END_TEST
BEGIN_SUITE(tag)
ADD_TEST(read0);
+ ADD_TEST(read1);
ADD_TEST(write0);
ADD_TEST(write1);
ADD_TEST(write2);