Commit 31d22037a63443c415462bed76dffda5d3c2de2c

Vicent Martí 2012-09-25T14:52:24

Merge pull request #944 from scunz/list_tags Tags: teach git_tag_list not to include the 'refs/tags/' prefix

diff --git a/src/tag.c b/src/tag.c
index 6495d47..ae9d0a8 100644
--- a/src/tag.c
+++ b/src/tag.c
@@ -408,7 +408,7 @@ static int tag_list_cb(const char *tag_name, void *payload)
 
 	filter = (tag_filter_data *)payload;
 	if (!*filter->pattern || p_fnmatch(filter->pattern, tag_name + GIT_REFS_TAGS_DIR_LEN, 0) == 0)
-		return git_vector_insert(filter->taglist, git__strdup(tag_name));
+		return git_vector_insert(filter->taglist, git__strdup(tag_name + GIT_REFS_TAGS_DIR_LEN));
 
 	return 0;
 }
diff --git a/tests-clar/object/tag/list.c b/tests-clar/object/tag/list.c
new file mode 100644
index 0000000..6d5a243
--- /dev/null
+++ b/tests-clar/object/tag/list.c
@@ -0,0 +1,115 @@
+#include "clar_libgit2.h"
+
+#include "tag.h"
+
+static git_repository *g_repo;
+
+#define MAX_USED_TAGS 6
+
+struct pattern_match_t
+{
+	const char* pattern;
+	const size_t expected_matches;
+	const char* expected_results[MAX_USED_TAGS];
+};
+
+// Helpers
+static void ensure_tag_pattern_match(git_repository *repo,
+									 const struct pattern_match_t* data)
+{
+	int already_found[MAX_USED_TAGS] = { 0 };
+	git_strarray tag_list;
+	int error = 0;
+	size_t sucessfully_found = 0;
+	size_t i, j;
+
+	cl_assert(data->expected_matches <= MAX_USED_TAGS);
+
+	if ((error = git_tag_list_match(&tag_list, data->pattern, repo)) < 0)
+		goto exit;
+
+	if (tag_list.count != data->expected_matches)
+	{
+		error = GIT_ERROR;
+		goto exit;
+	}
+
+	// we have to be prepared that tags come in any order.
+	for (i = 0; i < tag_list.count; i++)
+	{
+		for (j = 0; j < data->expected_matches; j++)
+		{
+			if (!already_found[j] && !strcmp(data->expected_results[j], tag_list.strings[i]))
+			{
+				already_found[j] = 1;
+				sucessfully_found++;
+				break;
+			}
+		}
+	}
+	cl_assert_equal_i((int)sucessfully_found, (int)data->expected_matches);
+
+exit:
+	git_strarray_free(&tag_list);
+	cl_git_pass(error);
+}
+
+// Fixture setup and teardown
+void test_object_tag_list__initialize(void)
+{
+	g_repo = cl_git_sandbox_init("testrepo");
+}
+
+void test_object_tag_list__cleanup(void)
+{
+	cl_git_sandbox_cleanup();
+}
+
+void test_object_tag_list__list_all(void)
+{
+	// list all tag names from the repository
+	git_strarray tag_list;
+
+	cl_git_pass(git_tag_list(&tag_list, g_repo));
+
+	cl_assert_equal_i((int)tag_list.count, 6);
+
+	git_strarray_free(&tag_list);
+}
+
+static const struct pattern_match_t matches[] = {
+	// All tags, including a packed one and two namespaced ones.
+	{ "", 6, { "e90810b", "point_to_blob", "test", "packed-tag", "foo/bar", "foo/foo/bar" } },
+
+	// beginning with
+	{ "t*", 1, { "test" } },
+
+	// ending with
+	{ "*b", 2, { "e90810b", "point_to_blob" } },
+
+	// exact match
+	{ "e", 0 },
+	{ "e90810b", 1, { "e90810b" } },
+
+	// either or
+	{ "e90810[ab]", 1, { "e90810b" } },
+
+	// glob in the middle
+	{ "foo/*/bar", 1, { "foo/foo/bar" } },
+
+	// The matching of '*' is based on plain string matching analog to the regular expression ".*"
+	// => a '/' in the tag name has no special meaning.
+	// Compare to `git tag -l "*bar"`
+	{ "*bar", 2, { "foo/bar", "foo/foo/bar" } },
+
+	// End of list
+	{ NULL }
+};
+
+void test_object_tag_list__list_by_pattern(void)
+{
+	// list all tag names from the repository matching a specified pattern
+	size_t i = 0;
+	while (matches[i].pattern)
+		ensure_tag_pattern_match(g_repo, &matches[i++]);
+}
diff --git a/tests-clar/object/tag/read.c b/tests-clar/object/tag/read.c
index 6a0ad8a..4dd5cc2 100644
--- a/tests-clar/object/tag/read.c
+++ b/tests-clar/object/tag/read.c
@@ -10,27 +10,6 @@ static const char *badly_tagged_commit = "e90810b8df3e80c413d903f631643c71688713
 
 static git_repository *g_repo;
 
-
-// Helpers
-static void ensure_tag_pattern_match(git_repository *repo,
-                                     const char *pattern,
-                                     const size_t expected_matches)
-{
-   git_strarray tag_list;
-   int error = 0;
-
-   if ((error = git_tag_list_match(&tag_list, pattern, repo)) < 0)
-      goto exit;
-
-   if (tag_list.count != expected_matches)
-      error = GIT_ERROR;
-
-exit:
-   git_strarray_free(&tag_list);
-   cl_git_pass(error);
-}
-
-
 // Fixture setup and teardown
 void test_object_tag_read__initialize(void)
 {
@@ -74,30 +53,6 @@ void test_object_tag_read__parse(void)
    git_commit_free(commit);
 }
 
-void test_object_tag_read__list(void)
-{
-   // list all tag names from the repository
-   git_strarray tag_list;
-
-   cl_git_pass(git_tag_list(&tag_list, g_repo));
-
-   cl_assert(tag_list.count == 3);
-
-   git_strarray_free(&tag_list);
-}
-
-void test_object_tag_read__list_pattern(void)
-{
-   // list all tag names from the repository matching a specified pattern
-   ensure_tag_pattern_match(g_repo, "", 3);
-   ensure_tag_pattern_match(g_repo, "*", 3);
-   ensure_tag_pattern_match(g_repo, "t*", 1);
-   ensure_tag_pattern_match(g_repo, "*b", 2);
-   ensure_tag_pattern_match(g_repo, "e", 0);
-   ensure_tag_pattern_match(g_repo, "e90810b", 1);
-   ensure_tag_pattern_match(g_repo, "e90810[ab]", 1);
-}
-
 void test_object_tag_read__parse_without_tagger(void)
 {
    // read and parse a tag without a tagger field
diff --git a/tests-clar/refs/list.c b/tests-clar/refs/list.c
index 2daa394..3948b2b 100644
--- a/tests-clar/refs/list.c
+++ b/tests-clar/refs/list.c
@@ -33,10 +33,10 @@ void test_refs_list__all(void)
 			printf("# %s\n", ref_list.strings[i]);
 	}*/
 
-	/* We have exactly 9 refs in total if we include the packed ones:
+	/* We have exactly 12 refs in total if we include the packed ones:
 	 * there is a reference that exists both in the packfile and as
 	 * loose, but we only list it once */
-	cl_assert_equal_i((int)ref_list.count, 10);
+	cl_assert_equal_i((int)ref_list.count, 13);
 
 	git_strarray_free(&ref_list);
 }
@@ -62,7 +62,7 @@ void test_refs_list__do_not_retrieve_references_which_name_end_with_a_lock_exten
 		"144344043ba4d4a405da03de3844aa829ae8be0e\n");
 
 	cl_git_pass(git_reference_list(&ref_list, g_repo, GIT_REF_LISTALL));
-	cl_assert_equal_i((int)ref_list.count, 10);
+	cl_assert_equal_i((int)ref_list.count, 13);
 
 	git_strarray_free(&ref_list);
 }
diff --git a/tests-clar/resources/testrepo/.gitted/packed-refs b/tests-clar/resources/testrepo/.gitted/packed-refs
index 52f5e87..6018a19 100644
--- a/tests-clar/resources/testrepo/.gitted/packed-refs
+++ b/tests-clar/resources/testrepo/.gitted/packed-refs
@@ -1,3 +1,4 @@
 # pack-refs with: peeled 
 41bc8c69075bbdb46c5c6f0566cc8cc5b46e8bd9 refs/heads/packed
 5b5b025afb0b4c913b4c338a42934a3863bf3644 refs/heads/packed-test
+b25fa35b38051e4ae45d4222e795f9df2e43f1d1 refs/tags/packed-tag
diff --git a/tests-clar/resources/testrepo/.gitted/refs/tags/foo/bar b/tests-clar/resources/testrepo/.gitted/refs/tags/foo/bar
new file mode 100644
index 0000000..6ee952a
--- /dev/null
+++ b/tests-clar/resources/testrepo/.gitted/refs/tags/foo/bar
@@ -0,0 +1 @@
+b25fa35b38051e4ae45d4222e795f9df2e43f1d1
diff --git a/tests-clar/resources/testrepo/.gitted/refs/tags/foo/foo/bar b/tests-clar/resources/testrepo/.gitted/refs/tags/foo/foo/bar
new file mode 100644
index 0000000..6ee952a
--- /dev/null
+++ b/tests-clar/resources/testrepo/.gitted/refs/tags/foo/foo/bar
@@ -0,0 +1 @@
+b25fa35b38051e4ae45d4222e795f9df2e43f1d1