revwalk: Allow changing hide_cb Since git_revwalk objects are encouraged to be reused, a public interface for changing hide_cb is desirable.
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
diff --git a/include/git2/revwalk.h b/include/git2/revwalk.h
index bbc3b2b..901570e 100644
--- a/include/git2/revwalk.h
+++ b/include/git2/revwalk.h
@@ -279,7 +279,7 @@ typedef int(*git_revwalk_hide_cb)(
void *payload);
/**
- * Adds a callback function to hide a commit and its parents
+ * Adds, changes or removes a callback function to hide a commit and its parents
*
* @param walk the revision walker
* @param hide_cb callback function to hide a commit and its parents
diff --git a/src/revwalk.c b/src/revwalk.c
index 4c5a1da..bda5f37 100644
--- a/src/revwalk.c
+++ b/src/revwalk.c
@@ -756,15 +756,11 @@ int git_revwalk_add_hide_cb(
if (walk->walking)
git_revwalk_reset(walk);
- if (walk->hide_cb) {
- /* There is already a callback added */
- giterr_set(GITERR_INVALID, "there is already a callback added to hide commits in revwalk");
- return -1;
- }
-
walk->hide_cb = hide_cb;
walk->hide_cb_payload = payload;
- walk->limited = 1;
+
+ if (hide_cb)
+ walk->limited = 1;
return 0;
}
diff --git a/tests/revwalk/hidecb.c b/tests/revwalk/hidecb.c
index b274ed8..54315bc 100644
--- a/tests/revwalk/hidecb.c
+++ b/tests/revwalk/hidecb.c
@@ -117,13 +117,40 @@ void test_revwalk_hidecb__hide_none_cb(void)
git_revwalk_free(walk);
}
-void test_revwalk_hidecb__add_hide_cb_multiple_times(void)
+void test_revwalk_hidecb__unset_cb_before_walk(void)
{
git_revwalk *walk;
+ git_oid id;
+ int i, error;
+
+ cl_git_pass(git_revwalk_new(&walk, _repo));
+ cl_git_pass(git_revwalk_add_hide_cb(walk, hide_every_commit_cb, NULL));
+ cl_git_pass(git_revwalk_add_hide_cb(walk, NULL, NULL));
+ cl_git_pass(git_revwalk_push(walk, &_head_id));
+
+ /* It should return all 6 commits */
+ i = 0;
+ while ((error = git_revwalk_next(&id, walk)) == 0)
+ i++;
+
+ cl_assert_equal_i(i, 6);
+ cl_assert_equal_i(error, GIT_ITEROVER);
+
+ git_revwalk_free(walk);
+}
+
+void test_revwalk_hidecb__change_cb_before_walk(void)
+{
+ git_revwalk *walk;
+ git_oid id;
cl_git_pass(git_revwalk_new(&walk, _repo));
+ cl_git_pass(git_revwalk_add_hide_cb(walk, hide_none_cb, NULL));
cl_git_pass(git_revwalk_add_hide_cb(walk, hide_every_commit_cb, NULL));
- cl_git_fail(git_revwalk_add_hide_cb(walk, hide_every_commit_cb, NULL));
+ cl_git_pass(git_revwalk_push(walk, &_head_id));
+
+ /* First call to git_revwalk_next should return GIT_ITEROVER */
+ cl_assert_equal_i(GIT_ITEROVER, git_revwalk_next(&id, walk));
git_revwalk_free(walk);
}