Skip to content

Commit e7873eb

Browse files
authored
Merge pull request libgit2#4888 from TheBB/add-cb
revwalk: Allow changing hide_cb
2 parents 487233f + 0836f06 commit e7873eb

File tree

3 files changed

+33
-10
lines changed

3 files changed

+33
-10
lines changed

include/git2/revwalk.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ typedef int(*git_revwalk_hide_cb)(
279279
void *payload);
280280

281281
/**
282-
* Adds a callback function to hide a commit and its parents
282+
* Adds, changes or removes a callback function to hide a commit and its parents
283283
*
284284
* @param walk the revision walker
285285
* @param hide_cb callback function to hide a commit and its parents

src/revwalk.c

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -756,15 +756,11 @@ int git_revwalk_add_hide_cb(
756756
if (walk->walking)
757757
git_revwalk_reset(walk);
758758

759-
if (walk->hide_cb) {
760-
/* There is already a callback added */
761-
giterr_set(GITERR_INVALID, "there is already a callback added to hide commits in revwalk");
762-
return -1;
763-
}
764-
765759
walk->hide_cb = hide_cb;
766760
walk->hide_cb_payload = payload;
767-
walk->limited = 1;
761+
762+
if (hide_cb)
763+
walk->limited = 1;
768764

769765
return 0;
770766
}

tests/revwalk/hidecb.c

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,13 +117,40 @@ void test_revwalk_hidecb__hide_none_cb(void)
117117
git_revwalk_free(walk);
118118
}
119119

120-
void test_revwalk_hidecb__add_hide_cb_multiple_times(void)
120+
void test_revwalk_hidecb__unset_cb_before_walk(void)
121121
{
122122
git_revwalk *walk;
123+
git_oid id;
124+
int i, error;
125+
126+
cl_git_pass(git_revwalk_new(&walk, _repo));
127+
cl_git_pass(git_revwalk_add_hide_cb(walk, hide_every_commit_cb, NULL));
128+
cl_git_pass(git_revwalk_add_hide_cb(walk, NULL, NULL));
129+
cl_git_pass(git_revwalk_push(walk, &_head_id));
130+
131+
/* It should return all 6 commits */
132+
i = 0;
133+
while ((error = git_revwalk_next(&id, walk)) == 0)
134+
i++;
135+
136+
cl_assert_equal_i(i, 6);
137+
cl_assert_equal_i(error, GIT_ITEROVER);
138+
139+
git_revwalk_free(walk);
140+
}
141+
142+
void test_revwalk_hidecb__change_cb_before_walk(void)
143+
{
144+
git_revwalk *walk;
145+
git_oid id;
123146

124147
cl_git_pass(git_revwalk_new(&walk, _repo));
148+
cl_git_pass(git_revwalk_add_hide_cb(walk, hide_none_cb, NULL));
125149
cl_git_pass(git_revwalk_add_hide_cb(walk, hide_every_commit_cb, NULL));
126-
cl_git_fail(git_revwalk_add_hide_cb(walk, hide_every_commit_cb, NULL));
150+
cl_git_pass(git_revwalk_push(walk, &_head_id));
151+
152+
/* First call to git_revwalk_next should return GIT_ITEROVER */
153+
cl_assert_equal_i(GIT_ITEROVER, git_revwalk_next(&id, walk));
127154

128155
git_revwalk_free(walk);
129156
}

0 commit comments

Comments
 (0)