]> git.proxmox.com Git - libgit2.git/blob - tests/t0501-walk.c
Redesigned the walking/object lookup interface
[libgit2.git] / tests / t0501-walk.c
1 #include "test_lib.h"
2 #include "test_helpers.h"
3 #include "commit.h"
4
5 #include <git/odb.h>
6 #include <git/commit.h>
7 #include <git/revwalk.h>
8
9 static const char *odb_dir = "../t0501-objects";
10 /*
11 $ git log --oneline --graph --decorate
12 * a4a7dce (HEAD, br2) Merge branch 'master' into br2
13 |\
14 | * 9fd738e (master) a fourth commit
15 | * 4a202b3 a third commit
16 * | c47800c branch commit one
17 |/
18 * 5b5b025 another commit
19 * 8496071 testing
20 */
21 static const char *commit_head = "a4a7dce85cf63874e984719f4fdd239f5145052f";
22
23 static const char *commit_ids[] = {
24 "a4a7dce85cf63874e984719f4fdd239f5145052f", /* 0 */
25 "9fd738e8f7967c078dceed8190330fc8648ee56a", /* 1 */
26 "4a202b346bb0fb0db7eff3cffeb3c70babbd2045", /* 2 */
27 "c47800c7266a2be04c571c04d5a6614691ea99bd", /* 3 */
28 "8496071c1b46c854b31185ea97743be6a8774479", /* 4 */
29 "5b5b025afb0b4c913b4c338a42934a3863bf3644", /* 5 */
30 };
31
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}
35 };
36
37 static const int commit_sorting_time[][6] = {
38 {0, 3, 1, 2, 5, 4}
39 };
40
41 static const int commit_sorting_topo_reverse[][6] = {
42 {4, 5, 3, 2, 1, 0}, {4, 5, 2, 1, 3, 0}
43 };
44
45 static const int commit_sorting_time_reverse[][6] = {
46 {4, 5, 2, 1, 3, 0}
47 };
48
49 static const int commit_count = 6;
50 static const int result_bytes = 24;
51
52
53 static int get_commit_index(git_commit *commit)
54 {
55 int i;
56 char oid[40];
57
58 git_oid_fmt(oid, &commit->object.id);
59
60 for (i = 0; i < commit_count; ++i)
61 if (memcmp(oid, commit_ids[i], 40) == 0)
62 return i;
63
64 return -1;
65 }
66
67 static int test_walk(git_revwalk *walk, git_commit *start_from,
68 int flags, const int possible_results[][6], int results_count)
69 {
70 git_commit *commit = NULL;
71
72 int i;
73 int result_array[commit_count];
74
75 git_revwalk_sorting(walk, flags);
76 git_revwalk_push(walk, start_from);
77
78 for (i = 0; i < commit_count; ++i)
79 result_array[i] = -1;
80
81 i = 0;
82 while ((commit = git_revwalk_next(walk)) != NULL)
83 result_array[i++] = get_commit_index(commit);
84
85 for (i = 0; i < results_count; ++i)
86 if (memcmp(possible_results[i],
87 result_array, result_bytes) == 0)
88 return GIT_SUCCESS;
89
90 return GIT_ERROR;
91 }
92
93 BEGIN_TEST(simple_walk_test)
94 git_odb *db;
95 git_oid id;
96 git_repository *repo;
97 git_revwalk *walk;
98 git_commit *head = NULL;
99
100 must_pass(git_odb_open(&db, odb_dir));
101
102 repo = git_repository_alloc(db);
103 must_be_true(repo != NULL);
104
105 walk = git_revwalk_alloc(repo);
106 must_be_true(walk != NULL);
107
108 git_oid_mkstr(&id, commit_head);
109
110 head = git_commit_lookup(repo, &id);
111 must_be_true(head != NULL);
112
113
114 must_pass(test_walk(walk, head,
115 GIT_SORT_TIME,
116 commit_sorting_time, 1));
117
118 must_pass(test_walk(walk, head,
119 GIT_SORT_TOPOLOGICAL,
120 commit_sorting_topo, 2));
121
122 must_pass(test_walk(walk, head,
123 GIT_SORT_TIME | GIT_SORT_REVERSE,
124 commit_sorting_time_reverse, 1));
125
126 must_pass(test_walk(walk, head,
127 GIT_SORT_TOPOLOGICAL | GIT_SORT_REVERSE,
128 commit_sorting_topo_reverse, 2));
129
130
131 git_revwalk_free(walk);
132 git_repository_free(repo);
133 git_odb_close(db);
134 END_TEST