From 3af06254d0b54feb25799dc1a5ff4853065a633b Mon Sep 17 00:00:00 2001 From: Sascha Cunz Date: Thu, 20 Sep 2012 22:42:22 +0200 Subject: [PATCH 1/7] Tags: teach git_tag_list not to include the 'refs/tags/' prefix Since quite a while now, git_branch_foreach has learnt to list branches without the 'refs/heads/' or 'refs/remotes' prefixes. This patch teaches git_tag_list to do the same for listing tags. --- src/tag.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/tag.c b/src/tag.c index 6495d470f..ae9d0a895 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; } From f73f760e66194908cb013b1d027960939c0a782c Mon Sep 17 00:00:00 2001 From: Sascha Cunz Date: Thu, 20 Sep 2012 23:49:17 +0200 Subject: [PATCH 2/7] Tests::Object::Tag: move listing tags tests to an own file --- tests-clar/object/tag/list.c | 60 ++++++++++++++++++++++++++++++++++++ tests-clar/object/tag/read.c | 45 --------------------------- 2 files changed, 60 insertions(+), 45 deletions(-) create mode 100644 tests-clar/object/tag/list.c diff --git a/tests-clar/object/tag/list.c b/tests-clar/object/tag/list.c new file mode 100644 index 000000000..ed2cd4f3d --- /dev/null +++ b/tests-clar/object/tag/list.c @@ -0,0 +1,60 @@ +#include "clar_libgit2.h" + +#include "tag.h" + +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_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(tag_list.count == 3); + + git_strarray_free(&tag_list); +} + +void test_object_tag_list__list_by_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); +} diff --git a/tests-clar/object/tag/read.c b/tests-clar/object/tag/read.c index 6a0ad8a23..4dd5cc253 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 From e800bbe80a37468234e4b7412da8e69dff17b5a5 Mon Sep 17 00:00:00 2001 From: Sascha Cunz Date: Fri, 21 Sep 2012 00:32:53 +0200 Subject: [PATCH 3/7] Tests::Object::Tag: Add a mechanism to test which tags were returned This patch changes the tag listing test helper to use a struct as input parameter, which tells what we exactly expect. As I don't think, we can rely on the fact that every os and every filesystem will report the tags in the same order, I made this code independent of the order that the tags are retrieved. --- tests-clar/object/tag/list.c | 60 ++++++++++++++++++++++++++++-------- 1 file changed, 48 insertions(+), 12 deletions(-) diff --git a/tests-clar/object/tag/list.c b/tests-clar/object/tag/list.c index ed2cd4f3d..75feb55e1 100644 --- a/tests-clar/object/tag/list.c +++ b/tests-clar/object/tag/list.c @@ -4,26 +4,56 @@ static git_repository *g_repo; +#define MAX_USED_TAGS 3 + +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 char *pattern, - const size_t expected_matches) + 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; - if ((error = git_tag_list_match(&tag_list, pattern, repo)) < 0) + 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 != expected_matches) + 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(sucessfully_found == data->expected_matches); exit: git_strarray_free(&tag_list); cl_git_pass(error); } - // Fixture setup and teardown void test_object_tag_list__initialize(void) { @@ -47,14 +77,20 @@ void test_object_tag_list__list_all(void) git_strarray_free(&tag_list); } +static const struct pattern_match_t matches[] = { + { "", 3, { "e90810b", "point_to_blob", "test" } }, + { "t*", 1, { "test" } }, + { "*b", 2, { "e90810b", "point_to_blob" } }, + { "e", 0 }, + { "e90810b", 1, { "e90810b" } }, + { "e90810[ab]", 1, { "e90810b" } }, + { NULL } +}; + void test_object_tag_list__list_by_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); + size_t i = 0; + while (matches[i].pattern) + ensure_tag_pattern_match(g_repo, &matches[i++]); } From 7604ddbf701b586b6f6160807cf32fca28398df1 Mon Sep 17 00:00:00 2001 From: Sascha Cunz Date: Fri, 21 Sep 2012 00:57:21 +0200 Subject: [PATCH 4/7] Tests: Add 3 tags to resources/testrepo. Adjusts refs::list test (including the comments) Adjusts objects::tags::list test --- tests-clar/object/tag/list.c | 8 ++++---- tests-clar/refs/list.c | 6 +++--- tests-clar/resources/testrepo/.gitted/packed-refs | 1 + tests-clar/resources/testrepo/.gitted/refs/tags/foo/bar | 1 + .../resources/testrepo/.gitted/refs/tags/foo/foo/bar | 1 + 5 files changed, 10 insertions(+), 7 deletions(-) create mode 100644 tests-clar/resources/testrepo/.gitted/refs/tags/foo/bar create mode 100644 tests-clar/resources/testrepo/.gitted/refs/tags/foo/foo/bar diff --git a/tests-clar/object/tag/list.c b/tests-clar/object/tag/list.c index 75feb55e1..f57a689d5 100644 --- a/tests-clar/object/tag/list.c +++ b/tests-clar/object/tag/list.c @@ -4,7 +4,7 @@ static git_repository *g_repo; -#define MAX_USED_TAGS 3 +#define MAX_USED_TAGS 6 struct pattern_match_t { @@ -47,7 +47,7 @@ static void ensure_tag_pattern_match(git_repository *repo, } } } - cl_assert(sucessfully_found == data->expected_matches); + cl_assert_equal_i((int)sucessfully_found, (int)data->expected_matches); exit: git_strarray_free(&tag_list); @@ -72,13 +72,13 @@ void test_object_tag_list__list_all(void) cl_git_pass(git_tag_list(&tag_list, g_repo)); - cl_assert(tag_list.count == 3); + cl_assert_equal_i((int)tag_list.count, 6); git_strarray_free(&tag_list); } static const struct pattern_match_t matches[] = { - { "", 3, { "e90810b", "point_to_blob", "test" } }, + { "", 6, { "e90810b", "point_to_blob", "test", "packed-tag", "foo/bar", "foo/foo/bar" } }, { "t*", 1, { "test" } }, { "*b", 2, { "e90810b", "point_to_blob" } }, { "e", 0 }, diff --git a/tests-clar/refs/list.c b/tests-clar/refs/list.c index 2daa3941e..3948b2b7a 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 52f5e876f..6018a19d2 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 000000000..6ee952a03 --- /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 000000000..6ee952a03 --- /dev/null +++ b/tests-clar/resources/testrepo/.gitted/refs/tags/foo/foo/bar @@ -0,0 +1 @@ +b25fa35b38051e4ae45d4222e795f9df2e43f1d1 From 45949b378b418d133e5bdf58d84114a6c3175461 Mon Sep 17 00:00:00 2001 From: Sascha Cunz Date: Fri, 21 Sep 2012 01:53:15 +0200 Subject: [PATCH 5/7] Tests::object::tag: also test for a 'foo/*/bar'. --- tests-clar/object/tag/list.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/tests-clar/object/tag/list.c b/tests-clar/object/tag/list.c index f57a689d5..365733557 100644 --- a/tests-clar/object/tag/list.c +++ b/tests-clar/object/tag/list.c @@ -78,12 +78,26 @@ void test_object_tag_list__list_all(void) } 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" } }, + + // End of list { NULL } }; From daa70138fcb7518443bb5053cb10c0ba07fb4494 Mon Sep 17 00:00:00 2001 From: Sascha Cunz Date: Sat, 22 Sep 2012 23:04:45 +0200 Subject: [PATCH 6/7] Tests: reindent object/tag/list.c to use tabs --- tests-clar/object/tag/list.c | 118 +++++++++++++++++------------------ 1 file changed, 59 insertions(+), 59 deletions(-) diff --git a/tests-clar/object/tag/list.c b/tests-clar/object/tag/list.c index 365733557..a3e0b8803 100644 --- a/tests-clar/object/tag/list.c +++ b/tests-clar/object/tag/list.c @@ -8,103 +8,103 @@ static git_repository *g_repo; struct pattern_match_t { - const char* pattern; - const size_t expected_matches; - const char* expected_results[MAX_USED_TAGS]; + 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) + 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; + 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); + cl_assert(data->expected_matches <= MAX_USED_TAGS); - if ((error = git_tag_list_match(&tag_list, data->pattern, repo)) < 0) - goto exit; + 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; - } + 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); + // 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); + 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"); + g_repo = cl_git_sandbox_init("testrepo"); } void test_object_tag_list__cleanup(void) { - cl_git_sandbox_cleanup(); + cl_git_sandbox_cleanup(); } void test_object_tag_list__list_all(void) { - // list all tag names from the repository - git_strarray tag_list; + // list all tag names from the repository + git_strarray tag_list; - cl_git_pass(git_tag_list(&tag_list, g_repo)); + cl_git_pass(git_tag_list(&tag_list, g_repo)); - cl_assert_equal_i((int)tag_list.count, 6); + cl_assert_equal_i((int)tag_list.count, 6); - git_strarray_free(&tag_list); + 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" } }, + // 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" } }, + // beginning with + { "t*", 1, { "test" } }, - // ending with - { "*b", 2, { "e90810b", "point_to_blob" } }, + // ending with + { "*b", 2, { "e90810b", "point_to_blob" } }, - // exact match - { "e", 0 }, - { "e90810b", 1, { "e90810b" } }, + // exact match + { "e", 0 }, + { "e90810b", 1, { "e90810b" } }, - // either or - { "e90810[ab]", 1, { "e90810b" } }, + // either or + { "e90810[ab]", 1, { "e90810b" } }, - // glob in the middle - { "foo/*/bar", 1, { "foo/foo/bar" } }, + // glob in the middle + { "foo/*/bar", 1, { "foo/foo/bar" } }, - // End of list - { NULL } + // 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++]); + // 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++]); } From 8469219e3792fd19d8f6ca5ca6f978928fcb8dc7 Mon Sep 17 00:00:00 2001 From: Sascha Cunz Date: Sat, 22 Sep 2012 23:11:26 +0200 Subject: [PATCH 7/7] Tests: Add test for git_tag_list to check for 'git tag -l "*bar"' --- tests-clar/object/tag/list.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests-clar/object/tag/list.c b/tests-clar/object/tag/list.c index a3e0b8803..6d5a24347 100644 --- a/tests-clar/object/tag/list.c +++ b/tests-clar/object/tag/list.c @@ -97,6 +97,11 @@ static const struct pattern_match_t matches[] = { // 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 } };