]> git.proxmox.com Git - libgit2.git/blob - tests/t05-revwalk.c
Merge pull request #349 from MasterGrumpy/development
[libgit2.git] / tests / t05-revwalk.c
1 /*
2 * This file is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License, version 2,
4 * as published by the Free Software Foundation.
5 *
6 * In addition to the permissions in the GNU General Public License,
7 * the authors give you unlimited permission to link the compiled
8 * version of this file into combinations with other programs,
9 * and to distribute those combinations without any restriction
10 * coming from the use of this file. (The General Public License
11 * restrictions do apply in other respects; for example, they cover
12 * modification of the file, and distribution when not linked into
13 * a combined executable.)
14 *
15 * This file is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; see the file COPYING. If not, write to
22 * the Free Software Foundation, 51 Franklin Street, Fifth Floor,
23 * Boston, MA 02110-1301, USA.
24 */
25 #include "test_lib.h"
26 #include "test_helpers.h"
27
28 /*
29 $ git log --oneline --graph --decorate
30 * a4a7dce (HEAD, br2) Merge branch 'master' into br2
31 |\
32 | * 9fd738e (master) a fourth commit
33 | * 4a202b3 a third commit
34 * | c47800c branch commit one
35 |/
36 * 5b5b025 another commit
37 * 8496071 testing
38 */
39 static const char *commit_head = "a4a7dce85cf63874e984719f4fdd239f5145052f";
40
41 static const char *commit_ids[] = {
42 "a4a7dce85cf63874e984719f4fdd239f5145052f", /* 0 */
43 "9fd738e8f7967c078dceed8190330fc8648ee56a", /* 1 */
44 "4a202b346bb0fb0db7eff3cffeb3c70babbd2045", /* 2 */
45 "c47800c7266a2be04c571c04d5a6614691ea99bd", /* 3 */
46 "8496071c1b46c854b31185ea97743be6a8774479", /* 4 */
47 "5b5b025afb0b4c913b4c338a42934a3863bf3644", /* 5 */
48 };
49
50 /* Careful: there are two possible topological sorts */
51 static const int commit_sorting_topo[][6] = {
52 {0, 1, 2, 3, 5, 4}, {0, 3, 1, 2, 5, 4}
53 };
54
55 static const int commit_sorting_time[][6] = {
56 {0, 3, 1, 2, 5, 4}
57 };
58
59 static const int commit_sorting_topo_reverse[][6] = {
60 {4, 5, 3, 2, 1, 0}, {4, 5, 2, 1, 3, 0}
61 };
62
63 static const int commit_sorting_time_reverse[][6] = {
64 {4, 5, 2, 1, 3, 0}
65 };
66
67 #define commit_count 6
68 static const int result_bytes = 24;
69
70
71 static int get_commit_index(git_oid *raw_oid)
72 {
73 int i;
74 char oid[40];
75
76 git_oid_fmt(oid, raw_oid);
77
78 for (i = 0; i < commit_count; ++i)
79 if (memcmp(oid, commit_ids[i], 40) == 0)
80 return i;
81
82 return -1;
83 }
84
85 static int test_walk(git_revwalk *walk, const git_oid *root,
86 int flags, const int possible_results[][6], int results_count)
87 {
88 git_oid oid;
89
90 int i;
91 int result_array[commit_count];
92
93 git_revwalk_sorting(walk, flags);
94 git_revwalk_push(walk, root);
95
96 for (i = 0; i < commit_count; ++i)
97 result_array[i] = -1;
98
99 i = 0;
100
101 while (git_revwalk_next(&oid, walk) == GIT_SUCCESS) {
102 result_array[i++] = get_commit_index(&oid);
103 /*{
104 char str[41];
105 git_oid_fmt(str, &oid);
106 str[40] = 0;
107 printf(" %d) %s\n", i, str);
108 }*/
109 }
110
111 for (i = 0; i < results_count; ++i)
112 if (memcmp(possible_results[i],
113 result_array, result_bytes) == 0)
114 return GIT_SUCCESS;
115
116 return GIT_ERROR;
117 }
118
119 BEGIN_TEST(walk0, "do a simple walk on a repo with different sorting modes")
120 git_oid id;
121 git_repository *repo;
122 git_revwalk *walk;
123
124 must_pass(git_repository_open(&repo, REPOSITORY_FOLDER));
125 must_pass(git_revwalk_new(&walk, repo));
126
127 git_oid_fromstr(&id, commit_head);
128
129 must_pass(test_walk(walk, &id, GIT_SORT_TIME, commit_sorting_time, 1));
130 must_pass(test_walk(walk, &id, GIT_SORT_TOPOLOGICAL, commit_sorting_topo, 2));
131 must_pass(test_walk(walk, &id, GIT_SORT_TIME | GIT_SORT_REVERSE, commit_sorting_time_reverse, 1));
132 must_pass(test_walk(walk, &id, GIT_SORT_TOPOLOGICAL | GIT_SORT_REVERSE, commit_sorting_topo_reverse, 2));
133
134 git_revwalk_free(walk);
135 git_repository_free(repo);
136 END_TEST
137
138 BEGIN_SUITE(revwalk)
139 ADD_TEST(walk0);
140 END_SUITE