Merge pull request #944 from scunz/list_tags Tags: teach git_tag_list not to include the 'refs/tags/' prefix
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 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
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