1 #include "clar_libgit2.h"
4 * a4a7dce [0] Merge branch 'master' into br2
6 | * 9fd738e [1] a fourth commit
7 | * 4a202b3 [2] a third commit
8 * | c47800c [3] branch commit one
10 * 5b5b025 [5] another commit
13 static const char *commit_head
= "a4a7dce85cf63874e984719f4fdd239f5145052f";
15 static const char *commit_ids
[] = {
16 "a4a7dce85cf63874e984719f4fdd239f5145052f", /* 0 */
17 "9fd738e8f7967c078dceed8190330fc8648ee56a", /* 1 */
18 "4a202b346bb0fb0db7eff3cffeb3c70babbd2045", /* 2 */
19 "c47800c7266a2be04c571c04d5a6614691ea99bd", /* 3 */
20 "8496071c1b46c854b31185ea97743be6a8774479", /* 4 */
21 "5b5b025afb0b4c913b4c338a42934a3863bf3644", /* 5 */
24 /* Careful: there are two possible topological sorts */
25 static const int commit_sorting_topo
[][6] = {
26 {0, 1, 2, 3, 5, 4}, {0, 3, 1, 2, 5, 4}
29 static const int commit_sorting_time
[][6] = {
33 static const int commit_sorting_topo_reverse
[][6] = {
34 {4, 5, 3, 2, 1, 0}, {4, 5, 2, 1, 3, 0}
37 static const int commit_sorting_time_reverse
[][6] = {
41 /* This is specified unsorted, so both combinations are possible */
42 static const int commit_sorting_segment
[][6] = {
43 {1, 2, -1, -1, -1, -1}, {2, 1, -1, -1, -1, -1}
46 #define commit_count 6
47 static const int result_bytes
= 24;
50 static int get_commit_index(git_oid
*raw_oid
)
53 char oid
[GIT_OID_HEXSZ
];
55 git_oid_fmt(oid
, raw_oid
);
57 for (i
= 0; i
< commit_count
; ++i
)
58 if (memcmp(oid
, commit_ids
[i
], GIT_OID_HEXSZ
) == 0)
64 static int test_walk_only(git_revwalk
*walk
,
65 const int possible_results
[][commit_count
], int results_count
)
69 int result_array
[commit_count
];
71 for (i
= 0; i
< commit_count
; ++i
)
75 while (git_revwalk_next(&oid
, walk
) == 0) {
76 result_array
[i
++] = get_commit_index(&oid
);
78 char str[GIT_OID_HEXSZ+1];
79 git_oid_fmt(str, &oid);
80 str[GIT_OID_HEXSZ] = 0;
81 printf(" %d) %s\n", i, str);
85 for (i
= 0; i
< results_count
; ++i
)
86 if (memcmp(possible_results
[i
],
87 result_array
, result_bytes
) == 0)
93 static int test_walk(git_revwalk
*walk
, const git_oid
*root
,
94 int flags
, const int possible_results
[][6], int results_count
)
96 git_revwalk_sorting(walk
, flags
);
97 git_revwalk_push(walk
, root
);
99 return test_walk_only(walk
, possible_results
, results_count
);
102 static git_repository
*_repo
= NULL
;
103 static git_revwalk
*_walk
= NULL
;
104 static const char *_fixture
= NULL
;
106 void test_revwalk_basic__initialize(void)
110 void test_revwalk_basic__cleanup(void)
112 git_revwalk_free(_walk
);
115 cl_git_sandbox_cleanup();
117 git_repository_free(_repo
);
124 static void revwalk_basic_setup_walk(const char *fixture
)
128 _repo
= cl_git_sandbox_init(fixture
);
130 cl_git_pass(git_repository_open(&_repo
, cl_fixture("testrepo.git")));
133 cl_git_pass(git_revwalk_new(&_walk
, _repo
));
136 void test_revwalk_basic__sorting_modes(void)
140 revwalk_basic_setup_walk(NULL
);
142 git_oid_fromstr(&id
, commit_head
);
144 cl_git_pass(test_walk(_walk
, &id
, GIT_SORT_TIME
, commit_sorting_time
, 1));
145 cl_git_pass(test_walk(_walk
, &id
, GIT_SORT_TOPOLOGICAL
, commit_sorting_topo
, 2));
146 cl_git_pass(test_walk(_walk
, &id
, GIT_SORT_TIME
| GIT_SORT_REVERSE
, commit_sorting_time_reverse
, 1));
147 cl_git_pass(test_walk(_walk
, &id
, GIT_SORT_TOPOLOGICAL
| GIT_SORT_REVERSE
, commit_sorting_topo_reverse
, 2));
150 void test_revwalk_basic__glob_heads(void)
155 revwalk_basic_setup_walk(NULL
);
157 cl_git_pass(git_revwalk_push_glob(_walk
, "heads"));
159 while (git_revwalk_next(&oid
, _walk
) == 0)
162 /* git log --branches --oneline | wc -l => 14 */
163 cl_assert_equal_i(i
, 14);
166 void test_revwalk_basic__glob_heads_with_invalid(void)
171 revwalk_basic_setup_walk("testrepo");
173 cl_git_mkfile("testrepo/.git/refs/heads/garbage", "not-a-ref");
174 cl_git_pass(git_revwalk_push_glob(_walk
, "heads"));
176 for (i
= 0; !git_revwalk_next(&oid
, _walk
); ++i
)
179 /* git log --branches --oneline | wc -l => 16 */
180 cl_assert_equal_i(20, i
);
183 void test_revwalk_basic__push_head(void)
188 revwalk_basic_setup_walk(NULL
);
190 cl_git_pass(git_revwalk_push_head(_walk
));
192 while (git_revwalk_next(&oid
, _walk
) == 0) {
196 /* git log HEAD --oneline | wc -l => 7 */
197 cl_assert_equal_i(i
, 7);
200 void test_revwalk_basic__sorted_after_reset(void)
205 revwalk_basic_setup_walk(NULL
);
207 git_oid_fromstr(&oid
, commit_head
);
209 /* push, sort, and test the walk */
210 cl_git_pass(git_revwalk_push(_walk
, &oid
));
211 git_revwalk_sorting(_walk
, GIT_SORT_TIME
);
213 cl_git_pass(test_walk_only(_walk
, commit_sorting_time
, 2));
215 /* reset, push, and test again - we should see all entries */
216 git_revwalk_reset(_walk
);
217 cl_git_pass(git_revwalk_push(_walk
, &oid
));
219 while (git_revwalk_next(&oid
, _walk
) == 0)
222 cl_assert_equal_i(i
, commit_count
);
225 void test_revwalk_basic__push_head_hide_ref(void)
230 revwalk_basic_setup_walk(NULL
);
232 cl_git_pass(git_revwalk_push_head(_walk
));
233 cl_git_pass(git_revwalk_hide_ref(_walk
, "refs/heads/packed-test"));
235 while (git_revwalk_next(&oid
, _walk
) == 0) {
239 /* git log HEAD --oneline --not refs/heads/packed-test | wc -l => 4 */
240 cl_assert_equal_i(i
, 4);
243 void test_revwalk_basic__push_head_hide_ref_nobase(void)
248 revwalk_basic_setup_walk(NULL
);
250 cl_git_pass(git_revwalk_push_head(_walk
));
251 cl_git_pass(git_revwalk_hide_ref(_walk
, "refs/heads/packed"));
253 while (git_revwalk_next(&oid
, _walk
) == 0) {
257 /* git log HEAD --oneline --not refs/heads/packed | wc -l => 7 */
258 cl_assert_equal_i(i
, 7);
262 * $ git rev-list HEAD 5b5b02 ^refs/heads/packed-test
263 * a65fedf39aefe402d3bb6e24df4d4f5fe4547750
264 * be3563ae3f795b2b4353bcce3a527ad0a4f7f644
265 * c47800c7266a2be04c571c04d5a6614691ea99bd
266 * 9fd738e8f7967c078dceed8190330fc8648ee56a
268 * $ git log HEAD 5b5b02 --oneline --not refs/heads/packed-test | wc -l => 4
270 * be3563a Merge branch 'br2'
271 * c47800c branch commit one
272 * 9fd738e a fourth commit
274 void test_revwalk_basic__multiple_push_1(void)
279 revwalk_basic_setup_walk(NULL
);
281 cl_git_pass(git_revwalk_push_head(_walk
));
283 cl_git_pass(git_revwalk_hide_ref(_walk
, "refs/heads/packed-test"));
285 cl_git_pass(git_oid_fromstr(&oid
, "5b5b025afb0b4c913b4c338a42934a3863bf3644"));
286 cl_git_pass(git_revwalk_push(_walk
, &oid
));
288 while (git_revwalk_next(&oid
, _walk
) == 0)
291 cl_assert_equal_i(i
, 4);
295 * Difference between test_revwalk_basic__multiple_push_1 and
296 * test_revwalk_basic__multiple_push_2 is in the order reference
297 * refs/heads/packed-test and commit 5b5b02 are pushed.
298 * revwalk should return same commits in both the tests.
300 * $ git rev-list 5b5b02 HEAD ^refs/heads/packed-test
301 * a65fedf39aefe402d3bb6e24df4d4f5fe4547750
302 * be3563ae3f795b2b4353bcce3a527ad0a4f7f644
303 * c47800c7266a2be04c571c04d5a6614691ea99bd
304 * 9fd738e8f7967c078dceed8190330fc8648ee56a
306 * $ git log 5b5b02 HEAD --oneline --not refs/heads/packed-test | wc -l => 4
308 * be3563a Merge branch 'br2'
309 * c47800c branch commit one
310 * 9fd738e a fourth commit
312 void test_revwalk_basic__multiple_push_2(void)
317 revwalk_basic_setup_walk(NULL
);
319 cl_git_pass(git_oid_fromstr(&oid
, "5b5b025afb0b4c913b4c338a42934a3863bf3644"));
320 cl_git_pass(git_revwalk_push(_walk
, &oid
));
322 cl_git_pass(git_revwalk_hide_ref(_walk
, "refs/heads/packed-test"));
324 cl_git_pass(git_revwalk_push_head(_walk
));
326 while (git_revwalk_next(&oid
, _walk
) == 0)
329 cl_assert_equal_i(i
, 4);
332 void test_revwalk_basic__disallow_non_commit(void)
336 revwalk_basic_setup_walk(NULL
);
338 cl_git_pass(git_oid_fromstr(&oid
, "521d87c1ec3aef9824daf6d96cc0ae3710766d91"));
339 cl_git_fail(git_revwalk_push(_walk
, &oid
));
342 void test_revwalk_basic__hide_then_push(void)
347 revwalk_basic_setup_walk(NULL
);
348 cl_git_pass(git_oid_fromstr(&oid
, "5b5b025afb0b4c913b4c338a42934a3863bf3644"));
350 cl_git_pass(git_revwalk_hide(_walk
, &oid
));
351 cl_git_pass(git_revwalk_push(_walk
, &oid
));
353 while (git_revwalk_next(&oid
, _walk
) == 0)
356 cl_assert_equal_i(i
, 0);
359 void test_revwalk_basic__topo_crash(void)
362 git_oid_fromstr(&oid
, "5b5b025afb0b4c913b4c338a42934a3863bf3644");
364 revwalk_basic_setup_walk(NULL
);
365 git_revwalk_sorting(_walk
, GIT_SORT_TOPOLOGICAL
);
367 cl_git_pass(git_revwalk_push(_walk
, &oid
));
368 cl_git_pass(git_revwalk_hide(_walk
, &oid
));
370 git_revwalk_next(&oid
, _walk
);
373 void test_revwalk_basic__from_new_to_old(void)
375 git_oid from_oid
, to_oid
, oid
;
378 revwalk_basic_setup_walk(NULL
);
379 git_revwalk_sorting(_walk
, GIT_SORT_TIME
);
381 cl_git_pass(git_oid_fromstr(&to_oid
, "5b5b025afb0b4c913b4c338a42934a3863bf3644"));
382 cl_git_pass(git_oid_fromstr(&from_oid
, "a4a7dce85cf63874e984719f4fdd239f5145052f"));
384 cl_git_pass(git_revwalk_push(_walk
, &to_oid
));
385 cl_git_pass(git_revwalk_hide(_walk
, &from_oid
));
387 while (git_revwalk_next(&oid
, _walk
) == 0)
390 cl_assert_equal_i(i
, 0);
393 void test_revwalk_basic__push_range(void)
395 revwalk_basic_setup_walk(NULL
);
397 git_revwalk_reset(_walk
);
398 git_revwalk_sorting(_walk
, 0);
399 cl_git_pass(git_revwalk_push_range(_walk
, "9fd738e~2..9fd738e"));
400 cl_git_pass(test_walk_only(_walk
, commit_sorting_segment
, 2));
403 void test_revwalk_basic__push_range_merge_base(void)
405 revwalk_basic_setup_walk(NULL
);
407 git_revwalk_reset(_walk
);
408 git_revwalk_sorting(_walk
, 0);
409 cl_git_fail_with(GIT_EINVALIDSPEC
, git_revwalk_push_range(_walk
, "HEAD...HEAD~2"));
412 void test_revwalk_basic__push_range_no_range(void)
414 revwalk_basic_setup_walk(NULL
);
416 git_revwalk_reset(_walk
);
417 git_revwalk_sorting(_walk
, 0);
418 cl_git_fail_with(GIT_EINVALIDSPEC
, git_revwalk_push_range(_walk
, "HEAD"));
421 void test_revwalk_basic__push_mixed(void)
426 revwalk_basic_setup_walk(NULL
);
428 git_revwalk_reset(_walk
);
429 git_revwalk_sorting(_walk
, 0);
430 cl_git_pass(git_revwalk_push_glob(_walk
, "tags"));
432 while (git_revwalk_next(&oid
, _walk
) == 0) {
436 /* git rev-list --count --glob=tags #=> 9 */
437 cl_assert_equal_i(9, i
);
440 void test_revwalk_basic__push_all(void)
445 revwalk_basic_setup_walk(NULL
);
447 git_revwalk_reset(_walk
);
448 git_revwalk_sorting(_walk
, 0);
449 cl_git_pass(git_revwalk_push_glob(_walk
, "*"));
451 while (git_revwalk_next(&oid
, _walk
) == 0) {
455 /* git rev-list --count --all #=> 15 */
456 cl_assert_equal_i(15, i
);
460 * $ git rev-list br2 master e908
461 * a65fedf39aefe402d3bb6e24df4d4f5fe4547750
462 * e90810b8df3e80c413d903f631643c716887138d
463 * 6dcf9bf7541ee10456529833502442f385010c3d
464 * a4a7dce85cf63874e984719f4fdd239f5145052f
465 * be3563ae3f795b2b4353bcce3a527ad0a4f7f644
466 * c47800c7266a2be04c571c04d5a6614691ea99bd
467 * 9fd738e8f7967c078dceed8190330fc8648ee56a
468 * 4a202b346bb0fb0db7eff3cffeb3c70babbd2045
469 * 5b5b025afb0b4c913b4c338a42934a3863bf3644
470 * 8496071c1b46c854b31185ea97743be6a8774479
473 void test_revwalk_basic__mimic_git_rev_list(void)
477 revwalk_basic_setup_walk(NULL
);
478 git_revwalk_sorting(_walk
, GIT_SORT_TIME
);
480 cl_git_pass(git_revwalk_push_ref(_walk
, "refs/heads/br2"));
481 cl_git_pass(git_revwalk_push_ref(_walk
, "refs/heads/master"));
482 cl_git_pass(git_oid_fromstr(&oid
, "e90810b8df3e80c413d903f631643c716887138d"));
483 cl_git_pass(git_revwalk_push(_walk
, &oid
));
485 cl_git_pass(git_revwalk_next(&oid
, _walk
));
486 cl_assert(!git_oid_streq(&oid
, "a65fedf39aefe402d3bb6e24df4d4f5fe4547750"));
488 cl_git_pass(git_revwalk_next(&oid
, _walk
));
489 cl_assert(!git_oid_streq(&oid
, "e90810b8df3e80c413d903f631643c716887138d"));
491 cl_git_pass(git_revwalk_next(&oid
, _walk
));
492 cl_assert(!git_oid_streq(&oid
, "6dcf9bf7541ee10456529833502442f385010c3d"));
494 cl_git_pass(git_revwalk_next(&oid
, _walk
));
495 cl_assert(!git_oid_streq(&oid
, "a4a7dce85cf63874e984719f4fdd239f5145052f"));
497 cl_git_pass(git_revwalk_next(&oid
, _walk
));
498 cl_assert(!git_oid_streq(&oid
, "be3563ae3f795b2b4353bcce3a527ad0a4f7f644"));
500 cl_git_pass(git_revwalk_next(&oid
, _walk
));
501 cl_assert(!git_oid_streq(&oid
, "c47800c7266a2be04c571c04d5a6614691ea99bd"));
503 cl_git_pass(git_revwalk_next(&oid
, _walk
));
504 cl_assert(!git_oid_streq(&oid
, "9fd738e8f7967c078dceed8190330fc8648ee56a"));
506 cl_git_pass(git_revwalk_next(&oid
, _walk
));
507 cl_assert(!git_oid_streq(&oid
, "4a202b346bb0fb0db7eff3cffeb3c70babbd2045"));
509 cl_git_pass(git_revwalk_next(&oid
, _walk
));
510 cl_assert(!git_oid_streq(&oid
, "5b5b025afb0b4c913b4c338a42934a3863bf3644"));
512 cl_git_pass(git_revwalk_next(&oid
, _walk
));
513 cl_assert(!git_oid_streq(&oid
, "8496071c1b46c854b31185ea97743be6a8774479"));
515 cl_git_fail_with(git_revwalk_next(&oid
, _walk
), GIT_ITEROVER
);
518 void test_revwalk_basic__big_timestamp(void)
527 revwalk_basic_setup_walk("testrepo.git");
529 cl_git_pass(git_repository_head(&head
, _repo
));
530 cl_git_pass(git_reference_peel((git_object
**) &tip
, head
, GIT_OBJECT_COMMIT
));
532 /* Commit with a far-ahead timestamp, we should be able to parse it in the revwalk */
533 cl_git_pass(git_signature_new(&sig
, "Joe", "joe@example.com", INT64_C(2399662595), 0));
534 cl_git_pass(git_commit_tree(&tree
, tip
));
536 cl_git_pass(git_commit_create(&id
, _repo
, "HEAD", sig
, sig
, NULL
, "some message", tree
, 1,
537 (const git_commit
**)&tip
));
539 cl_git_pass(git_revwalk_push_head(_walk
));
541 while ((error
= git_revwalk_next(&id
, _walk
)) == 0) {
545 cl_assert_equal_i(GIT_ITEROVER
, error
);
548 git_commit_free(tip
);
549 git_reference_free(head
);
550 git_signature_free(sig
);
554 /* Ensure that we correctly hide a commit that is (timewise) older
555 * than the commits that we are showing.
557 * % git rev-list 8e73b76..bd75801
558 * bd758010071961f28336333bc41e9c64c9a64866
560 void test_revwalk_basic__old_hidden_commit_one(void)
562 git_oid new_id
, old_id
, oid
;
564 revwalk_basic_setup_walk("testrepo.git");
566 cl_git_pass(git_oid_fromstr(&new_id
, "bd758010071961f28336333bc41e9c64c9a64866"));
567 cl_git_pass(git_revwalk_push(_walk
, &new_id
));
569 cl_git_pass(git_oid_fromstr(&old_id
, "8e73b769e97678d684b809b163bebdae2911720f"));
570 cl_git_pass(git_revwalk_hide(_walk
, &old_id
));
572 cl_git_pass(git_revwalk_next(&oid
, _walk
));
573 cl_assert(!git_oid_streq(&oid
, "bd758010071961f28336333bc41e9c64c9a64866"));
575 cl_git_fail_with(GIT_ITEROVER
, git_revwalk_next(&oid
, _walk
));
578 /* Ensure that we correctly hide a commit that is (timewise) older
579 * than the commits that we are showing.
581 * % git rev-list bd75801 ^b91e763
582 * bd758010071961f28336333bc41e9c64c9a64866
584 void test_revwalk_basic__old_hidden_commit_two(void)
586 git_oid new_id
, old_id
, oid
;
588 revwalk_basic_setup_walk("testrepo.git");
590 cl_git_pass(git_oid_fromstr(&new_id
, "bd758010071961f28336333bc41e9c64c9a64866"));
591 cl_git_pass(git_revwalk_push(_walk
, &new_id
));
593 cl_git_pass(git_oid_fromstr(&old_id
, "b91e763008b10db366442469339f90a2b8400d0a"));
594 cl_git_pass(git_revwalk_hide(_walk
, &old_id
));
596 cl_git_pass(git_revwalk_next(&oid
, _walk
));
597 cl_assert(!git_oid_streq(&oid
, "bd758010071961f28336333bc41e9c64c9a64866"));
599 cl_git_fail_with(GIT_ITEROVER
, git_revwalk_next(&oid
, _walk
));
603 * Ensure that we correctly hide all parent commits of a newer
604 * commit when first hiding older commits.
606 * % git rev-list D ^B ^A ^E
607 * 790ba0facf6fd103699a5c40cd19dad277ff49cd
608 * b82cee5004151ae0c4f82b69fb71b87477664b6f
610 void test_revwalk_basic__newer_hidden_commit_hides_old_commits(void)
614 revwalk_basic_setup_walk("revwalk.git");
616 cl_git_pass(git_revwalk_push_ref(_walk
, "refs/heads/D"));
617 cl_git_pass(git_revwalk_hide_ref(_walk
, "refs/heads/B"));
618 cl_git_pass(git_revwalk_hide_ref(_walk
, "refs/heads/A"));
619 cl_git_pass(git_revwalk_hide_ref(_walk
, "refs/heads/E"));
621 cl_git_pass(git_revwalk_next(&oid
, _walk
));
622 cl_assert(git_oid_streq(&oid
, "b82cee5004151ae0c4f82b69fb71b87477664b6f"));
623 cl_git_pass(git_revwalk_next(&oid
, _walk
));
624 cl_assert(git_oid_streq(&oid
, "790ba0facf6fd103699a5c40cd19dad277ff49cd"));
626 cl_git_fail_with(GIT_ITEROVER
, git_revwalk_next(&oid
, _walk
));