Commit e7873eb2cc0e96c9eebe653b6a5ba41daea4a28e

Patrick Steinhardt 2018-11-29T08:00:31

Merge pull request #4888 from TheBB/add-cb revwalk: Allow changing hide_cb

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 85d42e2..597e666 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);
 }