Commit 892aa808e2e879562c45f3d0886668f86265f1cf

Anurag Gupta 2014-03-10T12:00:33

Callback to hide commits in revision walker.

diff --git a/include/git2/revwalk.h b/include/git2/revwalk.h
index aef0b5f..7ab0827 100644
--- a/include/git2/revwalk.h
+++ b/include/git2/revwalk.h
@@ -261,6 +261,30 @@ GIT_EXTERN(void) git_revwalk_free(git_revwalk *walk);
  */
 GIT_EXTERN(git_repository *) git_revwalk_repository(git_revwalk *walk);
 
+/**
+* This is a callback function that user can provide to hide a
+* commit and its parents. If the callback function returns true,
+* then this commit and its parents will be hidden.
+*
+* @param commit_id oid of Commit
+* @param payload User-specified pointer to data to be passed as data payload
+*/
+typedef int(*git_revwalk_hide_cb)(
+	const git_oid *commit_id,
+	void *payload);
+
+/**
+* Adds a callback function to hide a commit
+*
+* @param walk the revision walker
+* @param hide_cb  callback function to hide a commit and its parents
+* @param payload  data payload to be passed to callback function
+*/
+GIT_EXTERN(int) git_revwalk_add_hide_cb(
+	git_revwalk *walk,
+	git_revwalk_hide_cb hide_cb,
+	void *payload);
+
 /** @} */
 GIT_END_DECL
 #endif
diff --git a/src/revwalk.c b/src/revwalk.c
index f037ee6..4e2e033 100644
--- a/src/revwalk.c
+++ b/src/revwalk.c
@@ -81,6 +81,11 @@ static int process_commit(git_revwalk *walk, git_commit_list_node *commit, int h
 {
 	int error;
 
+	if (!hide && walk->hide_cb)
+	{
+		hide = walk->hide_cb(&commit->oid, walk->hide_cb_payload);
+	}
+
 	if (hide && mark_uninteresting(commit) < 0)
 		return -1;
 
@@ -575,3 +580,26 @@ void git_revwalk_reset(git_revwalk *walk)
 	git_vector_clear(&walk->twos);
 }
 
+int git_revwalk_add_hide_cb(
+	git_revwalk *walk,
+	git_revwalk_hide_cb hide_cb,
+	void *payload)
+{
+	assert(walk);
+
+	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 revision walker.");
+		return -1;
+	}
+
+	walk->hide_cb = hide_cb;
+	walk->hide_cb_payload = payload;
+
+	return 0;
+}
+
diff --git a/src/revwalk.h b/src/revwalk.h
index a0ce1ae..a0654f3 100644
--- a/src/revwalk.h
+++ b/src/revwalk.h
@@ -39,6 +39,10 @@ struct git_revwalk {
 	/* merge base calculation */
 	git_commit_list_node *one;
 	git_vector twos;
+
+	/* hide callback */
+	git_revwalk_hide_cb hide_cb;
+	void *hide_cb_payload;
 };
 
 git_commit_list_node *git_revwalk__commit_lookup(git_revwalk *walk, const git_oid *oid);