Skip to content

Commit 3a66607

Browse files
committed
Unit Tests for hide_cb in revwalk
1 parent 892aa80 commit 3a66607

File tree

2 files changed

+201
-2
lines changed

2 files changed

+201
-2
lines changed

include/git2/revwalk.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ GIT_EXTERN(git_repository *) git_revwalk_repository(git_revwalk *walk);
263263

264264
/**
265265
* This is a callback function that user can provide to hide a
266-
* commit and its parents. If the callback function returns true,
266+
* commit and its parents. If the callback function returns non-zero value,
267267
* then this commit and its parents will be hidden.
268268
*
269269
* @param commit_id oid of Commit
@@ -274,7 +274,7 @@ typedef int(*git_revwalk_hide_cb)(
274274
void *payload);
275275

276276
/**
277-
* Adds a callback function to hide a commit
277+
* Adds a callback function to hide a commit and its parents
278278
*
279279
* @param walk the revision walker
280280
* @param hide_cb callback function to hide a commit and its parents

tests/revwalk/hidecb.c

Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
1+
#include "clar_libgit2.h"
2+
/*
3+
* a4a7dce [0] Merge branch 'master' into br2
4+
|\
5+
| * 9fd738e [1] a fourth commit
6+
| * 4a202b3 [2] a third commit
7+
* | c47800c [3] branch commit one
8+
|/
9+
* 5b5b025 [5] another commit
10+
* 8496071 [4] testing
11+
*/
12+
static const char *commit_head = "a4a7dce85cf63874e984719f4fdd239f5145052f";
13+
14+
static const char *commit_strs[] = {
15+
"a4a7dce85cf63874e984719f4fdd239f5145052f", /* 0 */
16+
"9fd738e8f7967c078dceed8190330fc8648ee56a", /* 1 */
17+
"4a202b346bb0fb0db7eff3cffeb3c70babbd2045", /* 2 */
18+
"c47800c7266a2be04c571c04d5a6614691ea99bd", /* 3 */
19+
"8496071c1b46c854b31185ea97743be6a8774479", /* 4 */
20+
"5b5b025afb0b4c913b4c338a42934a3863bf3644", /* 5 */
21+
};
22+
23+
#define commit_count 6
24+
25+
static git_oid commit_ids[commit_count];
26+
static git_oid _head_id;
27+
static git_repository *_repo;
28+
29+
30+
void test_revwalk_hidecb__initialize(void)
31+
{
32+
int i;
33+
34+
cl_git_pass(git_repository_open(&_repo, cl_fixture("testrepo.git")));
35+
cl_git_pass(git_oid_fromstr(&_head_id, commit_head));
36+
37+
for (i = 0; i < commit_count; i++)
38+
{
39+
cl_git_pass(git_oid_fromstr(&commit_ids[i], commit_strs[i]));
40+
}
41+
42+
}
43+
44+
void test_revwalk_hidecb__cleanup(void)
45+
{
46+
git_repository_free(_repo);
47+
_repo = NULL;
48+
}
49+
50+
/* Hide all commits */
51+
static int hide_every_commit_cb(const git_oid *commit_id, void *data)
52+
{
53+
return 1;
54+
}
55+
56+
/* Do not hide anything */
57+
static int hide_none_cb(const git_oid *commit_id, void *data)
58+
{
59+
return 0;
60+
}
61+
62+
/* Hide some commits */
63+
static int hide_commit_cb(const git_oid *commit_id, void *data)
64+
{
65+
if (0 == git_oid_cmp(commit_id, &commit_ids[3]))
66+
return 1;
67+
else
68+
return 0;
69+
70+
}
71+
72+
/* In payload data, pointer to a commit id is passed */
73+
static int hide_commit_use_payload_cb(const git_oid *commit_id, void *data)
74+
{
75+
git_oid *hide_commit_id = data;
76+
if (0 == git_oid_cmp(commit_id, hide_commit_id))
77+
return 1;
78+
else
79+
return 0;
80+
}
81+
82+
void test_revwalk_hidecb__hide_all_cb(void)
83+
{
84+
git_revwalk *walk;
85+
git_oid id;
86+
87+
cl_git_pass(git_revwalk_new(&walk, _repo));
88+
cl_git_pass(git_revwalk_add_hide_cb(walk, hide_every_commit_cb, NULL));
89+
cl_git_pass(git_revwalk_push(walk, &_head_id));
90+
91+
/* First call to git_revwalk_next should return GIT_ITEROVER */
92+
cl_assert_equal_i(GIT_ITEROVER, git_revwalk_next(&id, walk));
93+
94+
git_revwalk_free(walk);
95+
}
96+
97+
98+
void test_revwalk_hidecb__hide_none_cb(void)
99+
{
100+
git_revwalk *walk;
101+
int i, error;
102+
git_oid id;
103+
104+
cl_git_pass(git_revwalk_new(&walk, _repo));
105+
cl_git_pass(git_revwalk_add_hide_cb(walk, hide_none_cb, NULL));
106+
cl_git_pass(git_revwalk_push(walk, &_head_id));
107+
108+
/* It should return all 6 commits */
109+
i = 0;
110+
while ((error = git_revwalk_next(&id, walk)) == 0)
111+
i++;
112+
113+
cl_assert_equal_i(i, 6);
114+
cl_assert_equal_i(error, GIT_ITEROVER);
115+
116+
git_revwalk_free(walk);
117+
}
118+
119+
void test_revwalk_hidecb__add_hide_cb_multiple_times(void)
120+
{
121+
git_revwalk *walk;
122+
123+
cl_git_pass(git_revwalk_new(&walk, _repo));
124+
cl_git_pass(git_revwalk_add_hide_cb(walk, hide_every_commit_cb, NULL));
125+
cl_git_fail(git_revwalk_add_hide_cb(walk, hide_every_commit_cb, NULL));
126+
127+
git_revwalk_free(walk);
128+
}
129+
130+
void test_revwalk_hidecb__add_hide_cb_during_walking(void)
131+
{
132+
git_revwalk *walk;
133+
git_oid id;
134+
int error;
135+
136+
cl_git_pass(git_revwalk_new(&walk, _repo));
137+
cl_git_pass(git_revwalk_push(walk, &_head_id));
138+
139+
/* Start walking without adding hide callback */
140+
cl_git_pass(git_revwalk_next(&id, walk));
141+
142+
/* Now add hide callback */
143+
cl_git_pass(git_revwalk_add_hide_cb(walk, hide_none_cb, NULL));
144+
145+
/* walk should be reset */
146+
error = git_revwalk_next(&id, walk);
147+
cl_assert_equal_i(error, GIT_ITEROVER);
148+
149+
git_revwalk_free(walk);
150+
}
151+
152+
void test_revwalk_hidecb__hide_some_commits(void)
153+
{
154+
git_revwalk *walk;
155+
git_oid id;
156+
int i, error;
157+
158+
cl_git_pass(git_revwalk_new(&walk, _repo));
159+
cl_git_pass(git_revwalk_push(walk, &_head_id));
160+
161+
/* Add hide callback */
162+
cl_git_pass(git_revwalk_add_hide_cb(walk, hide_commit_cb, NULL));
163+
164+
i = 0;
165+
while ((error = git_revwalk_next(&id, walk)) == 0) {
166+
cl_assert_equal_i(git_oid_cmp(&id, &commit_ids[i]), 0);
167+
i++;
168+
}
169+
170+
cl_assert_equal_i(i, 3);
171+
cl_assert_equal_i(error, GIT_ITEROVER);
172+
173+
git_revwalk_free(walk);
174+
}
175+
176+
void test_revwalk_hidecb__test_payload(void)
177+
{
178+
git_revwalk *walk;
179+
git_oid id;
180+
int i, error;
181+
182+
cl_git_pass(git_revwalk_new(&walk, _repo));
183+
cl_git_pass(git_revwalk_push(walk, &_head_id));
184+
185+
/* Add hide callback, pass id of parent of initial commit as payload data */
186+
cl_git_pass(git_revwalk_add_hide_cb(walk, hide_commit_use_payload_cb, &commit_ids[5]));
187+
188+
i = 0;
189+
while ((error = git_revwalk_next(&id, walk)) == 0) {
190+
cl_assert_equal_i(git_oid_cmp(&id, &commit_ids[i]), 0);
191+
i++;
192+
}
193+
194+
/* walker should return four commits */
195+
cl_assert_equal_i(i, 4);
196+
cl_assert_equal_i(error, GIT_ITEROVER);
197+
198+
git_revwalk_free(walk);
199+
}

0 commit comments

Comments
 (0)