Merge pull request #2181 from anuraggup/hide_cb Callback function to hide commit and its parents in revision walker
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 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
diff --git a/include/git2/revwalk.h b/include/git2/revwalk.h
index aef0b5f..1bd6186 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 non-zero value,
+ * 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 and its parents
+ *
+ * @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..f010936 100644
--- a/src/revwalk.c
+++ b/src/revwalk.c
@@ -81,6 +81,9 @@ 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;
@@ -178,11 +181,6 @@ static int push_ref(git_revwalk *walk, const char *refname, int hide, int from_g
return push_commit(walk, &oid, hide, from_glob);
}
-struct push_cb_data {
- git_revwalk *walk;
- int hide;
-};
-
static int push_glob(git_revwalk *walk, const char *glob, int hide)
{
int error = 0;
@@ -575,3 +573,25 @@ 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);
diff --git a/tests/revwalk/hidecb.c b/tests/revwalk/hidecb.c
new file mode 100644
index 0000000..c13a177
--- /dev/null
+++ b/tests/revwalk/hidecb.c
@@ -0,0 +1,198 @@
+#include "clar_libgit2.h"
+/*
+* a4a7dce [0] Merge branch 'master' into br2
+|\
+| * 9fd738e [1] a fourth commit
+| * 4a202b3 [2] a third commit
+* | c47800c [3] branch commit one
+|/
+* 5b5b025 [5] another commit
+* 8496071 [4] testing
+*/
+static const char *commit_head = "a4a7dce85cf63874e984719f4fdd239f5145052f";
+
+static const char *commit_strs[] = {
+ "a4a7dce85cf63874e984719f4fdd239f5145052f", /* 0 */
+ "9fd738e8f7967c078dceed8190330fc8648ee56a", /* 1 */
+ "4a202b346bb0fb0db7eff3cffeb3c70babbd2045", /* 2 */
+ "c47800c7266a2be04c571c04d5a6614691ea99bd", /* 3 */
+ "8496071c1b46c854b31185ea97743be6a8774479", /* 4 */
+ "5b5b025afb0b4c913b4c338a42934a3863bf3644", /* 5 */
+};
+
+#define commit_count 6
+
+static git_oid commit_ids[commit_count];
+static git_oid _head_id;
+static git_repository *_repo;
+
+
+void test_revwalk_hidecb__initialize(void)
+{
+ int i;
+
+ cl_git_pass(git_repository_open(&_repo, cl_fixture("testrepo.git")));
+ cl_git_pass(git_oid_fromstr(&_head_id, commit_head));
+
+ for (i = 0; i < commit_count; i++)
+ cl_git_pass(git_oid_fromstr(&commit_ids[i], commit_strs[i]));
+
+}
+
+void test_revwalk_hidecb__cleanup(void)
+{
+ git_repository_free(_repo);
+ _repo = NULL;
+}
+
+/* Hide all commits */
+static int hide_every_commit_cb(const git_oid *commit_id, void *data)
+{
+ return 1;
+}
+
+/* Do not hide anything */
+static int hide_none_cb(const git_oid *commit_id, void *data)
+{
+ return 0;
+}
+
+/* Hide some commits */
+static int hide_commit_cb(const git_oid *commit_id, void *data)
+{
+ if (0 == git_oid_cmp(commit_id, &commit_ids[5]))
+ return 1;
+ else
+ return 0;
+
+}
+
+/* In payload data, pointer to a commit id is passed */
+static int hide_commit_use_payload_cb(const git_oid *commit_id, void *data)
+{
+ git_oid *hide_commit_id = data;
+ if (git_oid_cmp(commit_id, hide_commit_id) == 0)
+ return 1;
+ else
+ return 0;
+}
+
+void test_revwalk_hidecb__hide_all_cb(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_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);
+}
+
+
+void test_revwalk_hidecb__hide_none_cb(void)
+{
+ git_revwalk *walk;
+ int i, error;
+ 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_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__add_hide_cb_multiple_times(void)
+{
+ git_revwalk *walk;
+
+ cl_git_pass(git_revwalk_new(&walk, _repo));
+ 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));
+
+ git_revwalk_free(walk);
+}
+
+void test_revwalk_hidecb__add_hide_cb_during_walking(void)
+{
+ git_revwalk *walk;
+ git_oid id;
+ int error;
+
+ cl_git_pass(git_revwalk_new(&walk, _repo));
+ cl_git_pass(git_revwalk_push(walk, &_head_id));
+
+ /* Start walking without adding hide callback */
+ cl_git_pass(git_revwalk_next(&id, walk));
+
+ /* Now add hide callback */
+ cl_git_pass(git_revwalk_add_hide_cb(walk, hide_none_cb, NULL));
+
+ /* walk should be reset */
+ error = git_revwalk_next(&id, walk);
+ cl_assert_equal_i(error, GIT_ITEROVER);
+
+ git_revwalk_free(walk);
+}
+
+void test_revwalk_hidecb__hide_some_commits(void)
+{
+ git_revwalk *walk;
+ git_oid id;
+ int i, error;
+
+ cl_git_pass(git_revwalk_new(&walk, _repo));
+ cl_git_pass(git_revwalk_push(walk, &_head_id));
+
+ /* Add hide callback */
+ cl_git_pass(git_revwalk_add_hide_cb(walk, hide_commit_cb, NULL));
+
+ i = 0;
+ while ((error = git_revwalk_next(&id, walk)) == 0) {
+ cl_assert_equal_i(git_oid_cmp(&id, &commit_ids[i]), 0);
+ i++;
+ }
+
+ cl_assert_equal_i(i, 4);
+ cl_assert_equal_i(error, GIT_ITEROVER);
+
+ git_revwalk_free(walk);
+}
+
+void test_revwalk_hidecb__test_payload(void)
+{
+ git_revwalk *walk;
+ git_oid id;
+ int i, error;
+
+ cl_git_pass(git_revwalk_new(&walk, _repo));
+ cl_git_pass(git_revwalk_push(walk, &_head_id));
+
+ /* Add hide callback, pass id of parent of initial commit as payload data */
+ cl_git_pass(git_revwalk_add_hide_cb(walk, hide_commit_use_payload_cb, &commit_ids[5]));
+
+ i = 0;
+ while ((error = git_revwalk_next(&id, walk)) == 0) {
+ cl_assert_equal_i(git_oid_cmp(&id, &commit_ids[i]), 0);
+ i++;
+ }
+
+ /* walker should return four commits */
+ cl_assert_equal_i(i, 4);
+ cl_assert_equal_i(error, GIT_ITEROVER);
+
+ git_revwalk_free(walk);
+}
+