2 #include "test_helpers.h"
6 #include <git/commit.h>
7 #include <git/revwalk.h>
9 static const char *odb_dir
= "../t0501-objects";
11 $ git log --oneline --graph --decorate
12 * a4a7dce (HEAD, br2) Merge branch 'master' into br2
14 | * 9fd738e (master) a fourth commit
15 | * 4a202b3 a third commit
16 * | c47800c branch commit one
18 * 5b5b025 another commit
21 static const char *commit_head
= "a4a7dce85cf63874e984719f4fdd239f5145052f";
23 static const char *commit_ids
[] = {
24 "a4a7dce85cf63874e984719f4fdd239f5145052f", /* 0 */
25 "9fd738e8f7967c078dceed8190330fc8648ee56a", /* 1 */
26 "4a202b346bb0fb0db7eff3cffeb3c70babbd2045", /* 2 */
27 "c47800c7266a2be04c571c04d5a6614691ea99bd", /* 3 */
28 "8496071c1b46c854b31185ea97743be6a8774479", /* 4 */
29 "5b5b025afb0b4c913b4c338a42934a3863bf3644", /* 5 */
32 /* Careful: there are two possible topological sorts */
33 static const int commit_sorting_topo
[][6] = {
34 {0, 1, 2, 3, 5, 4}, {0, 3, 1, 2, 5, 4}
37 static const int commit_sorting_time
[][6] = {
41 static const int commit_sorting_topo_reverse
[][6] = {
42 {4, 5, 3, 2, 1, 0}, {4, 5, 2, 1, 3, 0}
45 static const int commit_sorting_time_reverse
[][6] = {
49 static const int commit_count
= 6;
50 static const int result_bytes
= 24;
53 static int get_commit_index(git_commit
*commit
)
58 git_oid_fmt(oid
, &commit
->object
.id
);
60 for (i
= 0; i
< commit_count
; ++i
)
61 if (memcmp(oid
, commit_ids
[i
], 40) == 0)
67 static int test_walk(git_revwalk
*walk
, git_commit
*start_from
,
68 int flags
, const int possible_results
[][6], int results_count
)
70 git_commit
*commit
= NULL
;
73 int result_array
[commit_count
];
75 git_revwalk_sorting(walk
, flags
);
76 git_revwalk_push(walk
, start_from
);
78 for (i
= 0; i
< commit_count
; ++i
)
82 while ((commit
= git_revwalk_next(walk
)) != NULL
)
83 result_array
[i
++] = get_commit_index(commit
);
85 for (i
= 0; i
< results_count
; ++i
)
86 if (memcmp(possible_results
[i
],
87 result_array
, result_bytes
) == 0)
93 BEGIN_TEST(simple_walk_test
)
98 git_commit
*head
= NULL
;
100 must_pass(git_odb_open(&db
, odb_dir
));
102 repo
= git_repository_alloc(db
);
103 must_be_true(repo
!= NULL
);
105 walk
= git_revwalk_alloc(repo
);
106 must_be_true(walk
!= NULL
);
108 git_oid_mkstr(&id
, commit_head
);
110 head
= git_commit_lookup(repo
, &id
);
111 must_be_true(head
!= NULL
);
114 must_pass(test_walk(walk
, head
,
116 commit_sorting_time
, 1));
118 must_pass(test_walk(walk
, head
,
119 GIT_SORT_TOPOLOGICAL
,
120 commit_sorting_topo
, 2));
122 must_pass(test_walk(walk
, head
,
123 GIT_SORT_TIME
| GIT_SORT_REVERSE
,
124 commit_sorting_time_reverse
, 1));
126 must_pass(test_walk(walk
, head
,
127 GIT_SORT_TOPOLOGICAL
| GIT_SORT_REVERSE
,
128 commit_sorting_topo_reverse
, 2));
131 git_revwalk_free(walk
);
132 git_repository_free(repo
);