From 8f7be6ca8a79f04620ca6cf280a3a04d1d96f2c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn=20Nieto?= Date: Mon, 27 Feb 2012 21:30:22 +0100 Subject: [PATCH 1/5] Move revwalk test to clar --- tests-clar/revwalk/basic.c | 119 +++++++++++++++++++++++++++++++++++++ 1 file changed, 119 insertions(+) create mode 100644 tests-clar/revwalk/basic.c diff --git a/tests-clar/revwalk/basic.c b/tests-clar/revwalk/basic.c new file mode 100644 index 000000000..5907c2443 --- /dev/null +++ b/tests-clar/revwalk/basic.c @@ -0,0 +1,119 @@ +#include "clar_libgit2.h" + +/* + $ git log --oneline --graph --decorate + * a4a7dce (HEAD, br2) Merge branch 'master' into br2 + |\ + | * 9fd738e (master) a fourth commit + | * 4a202b3 a third commit + * | c47800c branch commit one + |/ + * 5b5b025 another commit + * 8496071 testing +*/ +static const char *commit_head = "a4a7dce85cf63874e984719f4fdd239f5145052f"; + +static const char *commit_ids[] = { + "a4a7dce85cf63874e984719f4fdd239f5145052f", /* 0 */ + "9fd738e8f7967c078dceed8190330fc8648ee56a", /* 1 */ + "4a202b346bb0fb0db7eff3cffeb3c70babbd2045", /* 2 */ + "c47800c7266a2be04c571c04d5a6614691ea99bd", /* 3 */ + "8496071c1b46c854b31185ea97743be6a8774479", /* 4 */ + "5b5b025afb0b4c913b4c338a42934a3863bf3644", /* 5 */ +}; + +/* Careful: there are two possible topological sorts */ +static const int commit_sorting_topo[][6] = { + {0, 1, 2, 3, 5, 4}, {0, 3, 1, 2, 5, 4} +}; + +static const int commit_sorting_time[][6] = { + {0, 3, 1, 2, 5, 4} +}; + +static const int commit_sorting_topo_reverse[][6] = { + {4, 5, 3, 2, 1, 0}, {4, 5, 2, 1, 3, 0} +}; + +static const int commit_sorting_time_reverse[][6] = { + {4, 5, 2, 1, 3, 0} +}; + +#define commit_count 6 +static const int result_bytes = 24; + + +static int get_commit_index(git_oid *raw_oid) +{ + int i; + char oid[40]; + + git_oid_fmt(oid, raw_oid); + + for (i = 0; i < commit_count; ++i) + if (memcmp(oid, commit_ids[i], 40) == 0) + return i; + + return -1; +} + +static int test_walk(git_revwalk *walk, const git_oid *root, + int flags, const int possible_results[][6], int results_count) +{ + git_oid oid; + + int i; + int result_array[commit_count]; + + git_revwalk_sorting(walk, flags); + git_revwalk_push(walk, root); + + for (i = 0; i < commit_count; ++i) + result_array[i] = -1; + + i = 0; + + while (git_revwalk_next(&oid, walk) == GIT_SUCCESS) { + result_array[i++] = get_commit_index(&oid); + /*{ + char str[41]; + git_oid_fmt(str, &oid); + str[40] = 0; + printf(" %d) %s\n", i, str); + }*/ + } + + for (i = 0; i < results_count; ++i) + if (memcmp(possible_results[i], + result_array, result_bytes) == 0) + return GIT_SUCCESS; + + return GIT_ERROR; +} + +static git_repository *_repo; +static git_revwalk *_walk; + +void test_revwalk_basic__initialize(void) +{ + cl_git_pass(git_repository_open(&_repo, cl_fixture("testrepo.git"))); + cl_git_pass(git_revwalk_new(&_walk, _repo)); +} + +void test_revwalk_basic__cleanup(void) +{ + git_revwalk_free(_walk); + git_repository_free(_repo); +} + +void test_revwalk_basic__sorting_modes(void) +{ + git_oid id; + + git_oid_fromstr(&id, commit_head); + + cl_git_pass(test_walk(_walk, &id, GIT_SORT_TIME, commit_sorting_time, 1)); + cl_git_pass(test_walk(_walk, &id, GIT_SORT_TOPOLOGICAL, commit_sorting_topo, 2)); + cl_git_pass(test_walk(_walk, &id, GIT_SORT_TIME | GIT_SORT_REVERSE, commit_sorting_time_reverse, 1)); + cl_git_pass(test_walk(_walk, &id, GIT_SORT_TOPOLOGICAL | GIT_SORT_REVERSE, commit_sorting_topo_reverse, 2)); +} From 155aca2da79c8cae650c4e4f387a40d8f0a66527 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn=20Nieto?= Date: Mon, 27 Feb 2012 21:17:13 +0100 Subject: [PATCH 2/5] revwalk: introduce pushing and hiding by glob git_revwalk_{push,hide}_glob() lets you push the OIDs of references that match the specified glob. This is the basics for what git.git does with the rev-list options --branches, --tags, --remotes and --glob. --- include/git2/revwalk.h | 30 +++++++++++++++ src/revwalk.c | 87 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 117 insertions(+) diff --git a/include/git2/revwalk.h b/include/git2/revwalk.h index 1af0e4291..020c898ca 100644 --- a/include/git2/revwalk.h +++ b/include/git2/revwalk.h @@ -101,6 +101,20 @@ GIT_EXTERN(void) git_revwalk_reset(git_revwalk *walker); */ GIT_EXTERN(int) git_revwalk_push(git_revwalk *walk, const git_oid *oid); +/** + * Push matching references + * + * The OIDs pinted to by the references that match the given glob + * pattern will be pushed to the revision walker. + * + * A leading 'refs/' is implied it not present as well as a trailing + * '/ *' if the glob lacks '?', '*' or '['. + * + * @param walk the walker being used for the traversal + * @param glob the glob pattern references should match + * @return GIT_SUCCESS or an error code + */ +GIT_EXTERN(int) git_revwalk_push_glob(git_revwalk *walk, const char *glob); /** * Mark a commit (and its ancestors) uninteresting for the output. @@ -117,6 +131,22 @@ GIT_EXTERN(int) git_revwalk_push(git_revwalk *walk, const git_oid *oid); */ GIT_EXTERN(int) git_revwalk_hide(git_revwalk *walk, const git_oid *oid); +/** + * Hide matching references. + * + * The OIDs pinted to by the references that match the given glob + * pattern and their ancestors will be hidden from the output on the + * revision walk. + * + * A leading 'refs/' is implied it not present as well as a trailing + * '/ *' if the glob lacks '?', '*' or '['. + * + * @param walk the walker being used for the traversal + * @param glob the glob pattern references should match + * @return GIT_SUCCESS or an error code + */ +GIT_EXTERN(int) git_revwalk_hide_glob(git_revwalk *walk, const char *glob); + /** * Get the next commit from the revision walk. * diff --git a/src/revwalk.c b/src/revwalk.c index 49d4b7236..8f818b814 100644 --- a/src/revwalk.c +++ b/src/revwalk.c @@ -13,6 +13,8 @@ #include "git2/revwalk.h" +#include + typedef struct commit_object { git_oid oid; uint32_t time; @@ -298,12 +300,97 @@ int git_revwalk_push(git_revwalk *walk, const git_oid *oid) return push_commit(walk, oid, 0); } + int git_revwalk_hide(git_revwalk *walk, const git_oid *oid) { assert(walk && oid); return push_commit(walk, oid, 1); } +struct push_cb_data { + git_revwalk *walk; + const char *glob; + int hide; +}; + +static int push_glob_cb(const char *refname, void *data_) +{ + struct push_cb_data *data = (struct push_cb_data *)data_; + + if (!git__fnmatch(data->glob, refname, 0)) { + git_reference *ref, *resolved; + int error; + + error = git_reference_lookup(&ref, data->walk->repo, refname); + if (error < GIT_SUCCESS) + return error; + error = git_reference_resolve(&resolved, ref); + git_reference_free(ref); + if (error < GIT_SUCCESS) + return error; + error = push_commit(data->walk, git_reference_oid(resolved), data->hide); + git_reference_free(resolved); + return error; + } + + return GIT_SUCCESS; +} + +static int push_glob(git_revwalk *walk, const char *glob, int hide) +{ + git_buf buf = GIT_BUF_INIT; + struct push_cb_data data; + int error; + regex_t preg; + + assert(walk && glob); + + /* refs/ is implied if not given in the glob */ + if (strncmp(glob, GIT_REFS_DIR, strlen(GIT_REFS_DIR))) { + git_buf_printf(&buf, GIT_REFS_DIR "%s", glob); + } else { + git_buf_puts(&buf, glob); + } + + /* If no '?', '*' or '[' exist, we append '/ *' to the glob */ + memset(&preg, 0x0, sizeof(regex_t)); + if (regcomp(&preg, "[?*[]", REG_EXTENDED)) { + error = git__throw(GIT_EOSERR, "Regex failed to compile"); + goto cleanup; + } + + if (regexec(&preg, glob, 0, NULL, 0)) + git_buf_puts(&buf, "/*"); + + if (git_buf_oom(&buf)) { + error = GIT_ENOMEM; + goto cleanup; + } + + data.walk = walk; + data.glob = git_buf_cstr(&buf); + data.hide = hide; + + error = git_reference_foreach(walk->repo, GIT_REF_LISTALL, push_glob_cb, &data); + +cleanup: + regfree(&preg); + git_buf_free(&buf); + return error; +} + +int git_revwalk_push_glob(git_revwalk *walk, const char *glob) +{ + assert(walk && glob); + return push_glob(walk, glob, 0); +} + +int git_revwalk_hide_glob(git_revwalk *walk, const char *glob) +{ + assert(walk && glob); + return push_glob(walk, glob, 1); +} + static int revwalk_enqueue_timesort(git_revwalk *walk, commit_object *commit) { return git_pqueue_insert(&walk->iterator_time, commit); From f0fa1c1a73f5a19a9799c0f8b90dcd533154cb3a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn=20Nieto?= Date: Mon, 27 Feb 2012 22:00:49 +0100 Subject: [PATCH 3/5] Add revwalk glob test --- tests-clar/revwalk/basic.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/tests-clar/revwalk/basic.c b/tests-clar/revwalk/basic.c index 5907c2443..f013945ff 100644 --- a/tests-clar/revwalk/basic.c +++ b/tests-clar/revwalk/basic.c @@ -117,3 +117,18 @@ void test_revwalk_basic__sorting_modes(void) cl_git_pass(test_walk(_walk, &id, GIT_SORT_TIME | GIT_SORT_REVERSE, commit_sorting_time_reverse, 1)); cl_git_pass(test_walk(_walk, &id, GIT_SORT_TOPOLOGICAL | GIT_SORT_REVERSE, commit_sorting_topo_reverse, 2)); } + +void test_revwalk_basic__glob_heads(void) +{ + int i = 0; + git_oid oid; + + cl_git_pass(git_revwalk_push_glob(_walk, "heads")); + + while (git_revwalk_next(&oid, _walk) == GIT_SUCCESS) { + i++; + } + + /* git log --branches --oneline | wc -l => 13 */ + cl_assert(i == 13); +} From f7367993cba7b6a3c72da0b4a09c0ae88c7446c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn=20Nieto?= Date: Mon, 27 Feb 2012 22:22:45 +0100 Subject: [PATCH 4/5] revwalk: add convenience function to push/hide HEAD It's not unusual to want the walker to act on HEAD, so add a convencience function for the case that the user doesn't already have a resolved HEAD reference. --- include/git2/revwalk.h | 16 ++++++++++++++++ src/revwalk.c | 33 +++++++++++++++++++++++++++++++++ tests-clar/revwalk/basic.c | 15 +++++++++++++++ 3 files changed, 64 insertions(+) diff --git a/include/git2/revwalk.h b/include/git2/revwalk.h index 020c898ca..e7ec2abf3 100644 --- a/include/git2/revwalk.h +++ b/include/git2/revwalk.h @@ -116,6 +116,14 @@ GIT_EXTERN(int) git_revwalk_push(git_revwalk *walk, const git_oid *oid); */ GIT_EXTERN(int) git_revwalk_push_glob(git_revwalk *walk, const char *glob); +/** + * Push the repository's HEAD + * + * @param walk the walker being used for the traversal + * @return GIT_SUCCESS or an error code + */ +GIT_EXTERN(int) git_revwalk_push_head(git_revwalk *walk); + /** * Mark a commit (and its ancestors) uninteresting for the output. * @@ -147,6 +155,14 @@ GIT_EXTERN(int) git_revwalk_hide(git_revwalk *walk, const git_oid *oid); */ GIT_EXTERN(int) git_revwalk_hide_glob(git_revwalk *walk, const char *glob); +/** + * Hide the repository's HEAD + * + * @param walk the walker being used for the traversal + * @return GIT_SUCCESS or an error code + */ +GIT_EXTERN(int) git_revwalk_hide_head(git_revwalk *walk); + /** * Get the next commit from the revision walk. * diff --git a/src/revwalk.c b/src/revwalk.c index 8f818b814..cd971b5d9 100644 --- a/src/revwalk.c +++ b/src/revwalk.c @@ -391,6 +391,39 @@ int git_revwalk_hide_glob(git_revwalk *walk, const char *glob) return push_glob(walk, glob, 1); } +static int push_head(git_revwalk *walk, int hide) +{ + git_reference *ref, *resolved; + int error; + + error = git_reference_lookup(&ref, walk->repo, "HEAD"); + if (error < GIT_SUCCESS) { + return error; + } + error = git_reference_resolve(&resolved, ref); + if (error < GIT_SUCCESS) { + return error; + } + git_reference_free(ref); + + error = push_commit(walk, git_reference_oid(resolved), hide); + + git_reference_free(resolved); + return error; +} + +int git_revwalk_push_head(git_revwalk *walk) +{ + assert(walk); + return push_head(walk, 0); +} + +int git_revwalk_hide_head(git_revwalk *walk) +{ + assert(walk); + return push_head(walk, 1); +} + static int revwalk_enqueue_timesort(git_revwalk *walk, commit_object *commit) { return git_pqueue_insert(&walk->iterator_time, commit); diff --git a/tests-clar/revwalk/basic.c b/tests-clar/revwalk/basic.c index f013945ff..fff93ec93 100644 --- a/tests-clar/revwalk/basic.c +++ b/tests-clar/revwalk/basic.c @@ -132,3 +132,18 @@ void test_revwalk_basic__glob_heads(void) /* git log --branches --oneline | wc -l => 13 */ cl_assert(i == 13); } + +void test_revwalk_basic__push_head(void) +{ + int i = 0; + git_oid oid; + + cl_git_pass(git_revwalk_push_head(_walk)); + + while (git_revwalk_next(&oid, _walk) == GIT_SUCCESS) { + i++; + } + + /* git log HEAD --oneline | wc -l => 7 */ + cl_assert(i == 7); +} From a4a910dd9c485989cecdf8af19750a11f0d2653d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn=20Nieto?= Date: Mon, 27 Feb 2012 22:46:45 +0100 Subject: [PATCH 5/5] Simple test for pushing HEAD and hiding a branch --- tests-clar/revwalk/basic.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/tests-clar/revwalk/basic.c b/tests-clar/revwalk/basic.c index fff93ec93..cc88ec65b 100644 --- a/tests-clar/revwalk/basic.c +++ b/tests-clar/revwalk/basic.c @@ -147,3 +147,20 @@ void test_revwalk_basic__push_head(void) /* git log HEAD --oneline | wc -l => 7 */ cl_assert(i == 7); } + +void test_revwalk_basic__push_head_hide_glob(void) +{ + int i = 0; + git_oid oid; + + cl_git_pass(git_revwalk_push_head(_walk)); + /* This is a hack, as we know this will only match the packed-test branch */ + cl_git_pass(git_revwalk_hide_glob(_walk, "heads/packed-test*")); + + while (git_revwalk_next(&oid, _walk) == GIT_SUCCESS) { + i++; + } + + /* git log HEAD --oneline --not refs/heads/packed-test | wc -l => 4 */ + cl_assert(i == 4); +}