|
| 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 | + cl_git_pass(git_oid_fromstr(&commit_ids[i], commit_strs[i])); |
| 39 | + |
| 40 | +} |
| 41 | + |
| 42 | +void test_revwalk_hidecb__cleanup(void) |
| 43 | +{ |
| 44 | + git_repository_free(_repo); |
| 45 | + _repo = NULL; |
| 46 | +} |
| 47 | + |
| 48 | +/* Hide all commits */ |
| 49 | +static int hide_every_commit_cb(const git_oid *commit_id, void *data) |
| 50 | +{ |
| 51 | + return 1; |
| 52 | +} |
| 53 | + |
| 54 | +/* Do not hide anything */ |
| 55 | +static int hide_none_cb(const git_oid *commit_id, void *data) |
| 56 | +{ |
| 57 | + return 0; |
| 58 | +} |
| 59 | + |
| 60 | +/* Hide some commits */ |
| 61 | +static int hide_commit_cb(const git_oid *commit_id, void *data) |
| 62 | +{ |
| 63 | + if (0 == git_oid_cmp(commit_id, &commit_ids[5])) |
| 64 | + return 1; |
| 65 | + else |
| 66 | + return 0; |
| 67 | + |
| 68 | +} |
| 69 | + |
| 70 | +/* In payload data, pointer to a commit id is passed */ |
| 71 | +static int hide_commit_use_payload_cb(const git_oid *commit_id, void *data) |
| 72 | +{ |
| 73 | + git_oid *hide_commit_id = data; |
| 74 | + if (git_oid_cmp(commit_id, hide_commit_id) == 0) |
| 75 | + return 1; |
| 76 | + else |
| 77 | + return 0; |
| 78 | +} |
| 79 | + |
| 80 | +void test_revwalk_hidecb__hide_all_cb(void) |
| 81 | +{ |
| 82 | + git_revwalk *walk; |
| 83 | + git_oid id; |
| 84 | + |
| 85 | + cl_git_pass(git_revwalk_new(&walk, _repo)); |
| 86 | + cl_git_pass(git_revwalk_add_hide_cb(walk, hide_every_commit_cb, NULL)); |
| 87 | + cl_git_pass(git_revwalk_push(walk, &_head_id)); |
| 88 | + |
| 89 | + /* First call to git_revwalk_next should return GIT_ITEROVER */ |
| 90 | + cl_assert_equal_i(GIT_ITEROVER, git_revwalk_next(&id, walk)); |
| 91 | + |
| 92 | + git_revwalk_free(walk); |
| 93 | +} |
| 94 | + |
| 95 | + |
| 96 | +void test_revwalk_hidecb__hide_none_cb(void) |
| 97 | +{ |
| 98 | + git_revwalk *walk; |
| 99 | + int i, error; |
| 100 | + git_oid id; |
| 101 | + |
| 102 | + cl_git_pass(git_revwalk_new(&walk, _repo)); |
| 103 | + cl_git_pass(git_revwalk_add_hide_cb(walk, hide_none_cb, NULL)); |
| 104 | + cl_git_pass(git_revwalk_push(walk, &_head_id)); |
| 105 | + |
| 106 | + /* It should return all 6 commits */ |
| 107 | + i = 0; |
| 108 | + while ((error = git_revwalk_next(&id, walk)) == 0) |
| 109 | + i++; |
| 110 | + |
| 111 | + cl_assert_equal_i(i, 6); |
| 112 | + cl_assert_equal_i(error, GIT_ITEROVER); |
| 113 | + |
| 114 | + git_revwalk_free(walk); |
| 115 | +} |
| 116 | + |
| 117 | +void test_revwalk_hidecb__add_hide_cb_multiple_times(void) |
| 118 | +{ |
| 119 | + git_revwalk *walk; |
| 120 | + |
| 121 | + cl_git_pass(git_revwalk_new(&walk, _repo)); |
| 122 | + cl_git_pass(git_revwalk_add_hide_cb(walk, hide_every_commit_cb, NULL)); |
| 123 | + cl_git_fail(git_revwalk_add_hide_cb(walk, hide_every_commit_cb, NULL)); |
| 124 | + |
| 125 | + git_revwalk_free(walk); |
| 126 | +} |
| 127 | + |
| 128 | +void test_revwalk_hidecb__add_hide_cb_during_walking(void) |
| 129 | +{ |
| 130 | + git_revwalk *walk; |
| 131 | + git_oid id; |
| 132 | + int error; |
| 133 | + |
| 134 | + cl_git_pass(git_revwalk_new(&walk, _repo)); |
| 135 | + cl_git_pass(git_revwalk_push(walk, &_head_id)); |
| 136 | + |
| 137 | + /* Start walking without adding hide callback */ |
| 138 | + cl_git_pass(git_revwalk_next(&id, walk)); |
| 139 | + |
| 140 | + /* Now add hide callback */ |
| 141 | + cl_git_pass(git_revwalk_add_hide_cb(walk, hide_none_cb, NULL)); |
| 142 | + |
| 143 | + /* walk should be reset */ |
| 144 | + error = git_revwalk_next(&id, walk); |
| 145 | + cl_assert_equal_i(error, GIT_ITEROVER); |
| 146 | + |
| 147 | + git_revwalk_free(walk); |
| 148 | +} |
| 149 | + |
| 150 | +void test_revwalk_hidecb__hide_some_commits(void) |
| 151 | +{ |
| 152 | + git_revwalk *walk; |
| 153 | + git_oid id; |
| 154 | + int i, error; |
| 155 | + |
| 156 | + cl_git_pass(git_revwalk_new(&walk, _repo)); |
| 157 | + cl_git_pass(git_revwalk_push(walk, &_head_id)); |
| 158 | + |
| 159 | + /* Add hide callback */ |
| 160 | + cl_git_pass(git_revwalk_add_hide_cb(walk, hide_commit_cb, NULL)); |
| 161 | + |
| 162 | + i = 0; |
| 163 | + while ((error = git_revwalk_next(&id, walk)) == 0) { |
| 164 | + cl_assert_equal_i(git_oid_cmp(&id, &commit_ids[i]), 0); |
| 165 | + i++; |
| 166 | + } |
| 167 | + |
| 168 | + cl_assert_equal_i(i, 4); |
| 169 | + cl_assert_equal_i(error, GIT_ITEROVER); |
| 170 | + |
| 171 | + git_revwalk_free(walk); |
| 172 | +} |
| 173 | + |
| 174 | +void test_revwalk_hidecb__test_payload(void) |
| 175 | +{ |
| 176 | + git_revwalk *walk; |
| 177 | + git_oid id; |
| 178 | + int i, error; |
| 179 | + |
| 180 | + cl_git_pass(git_revwalk_new(&walk, _repo)); |
| 181 | + cl_git_pass(git_revwalk_push(walk, &_head_id)); |
| 182 | + |
| 183 | + /* Add hide callback, pass id of parent of initial commit as payload data */ |
| 184 | + cl_git_pass(git_revwalk_add_hide_cb(walk, hide_commit_use_payload_cb, &commit_ids[5])); |
| 185 | + |
| 186 | + i = 0; |
| 187 | + while ((error = git_revwalk_next(&id, walk)) == 0) { |
| 188 | + cl_assert_equal_i(git_oid_cmp(&id, &commit_ids[i]), 0); |
| 189 | + i++; |
| 190 | + } |
| 191 | + |
| 192 | + /* walker should return four commits */ |
| 193 | + cl_assert_equal_i(i, 4); |
| 194 | + cl_assert_equal_i(error, GIT_ITEROVER); |
| 195 | + |
| 196 | + git_revwalk_free(walk); |
| 197 | +} |
| 198 | + |
0 commit comments