basic.c 4.59 KB
Newer Older
1 2 3
#include "clar_libgit2.h"

/*
4
	*   a4a7dce [0] Merge branch 'master' into br2
5
	|\
6 7 8
	| * 9fd738e [1] a fourth commit
	| * 4a202b3 [2] a third commit
	* | c47800c [3] branch commit one
9
	|/
10 11
	* 5b5b025 [5] another commit
	* 8496071 [4] testing
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
*/
static const char *commit_head = "a4a7dce85cf63874e984719f4fdd239f5145052f";

static const char *commit_ids[] = {
	"a4a7dce85cf63874e984719f4fdd239f5145052f", /* 0 */
	"9fd738e8f7967c078dceed8190330fc8648ee56a", /* 1 */
	"4a202b346bb0fb0db7eff3cffeb3c70babbd2045", /* 2 */
	"c47800c7266a2be04c571c04d5a6614691ea99bd", /* 3 */
	"8496071c1b46c854b31185ea97743be6a8774479", /* 4 */
	"5b5b025afb0b4c913b4c338a42934a3863bf3644", /* 5 */
};

/* Careful: there are two possible topological sorts */
static const int commit_sorting_topo[][6] = {
	{0, 1, 2, 3, 5, 4}, {0, 3, 1, 2, 5, 4}
};

static const int commit_sorting_time[][6] = {
	{0, 3, 1, 2, 5, 4}
};

static const int commit_sorting_topo_reverse[][6] = {
	{4, 5, 3, 2, 1, 0}, {4, 5, 2, 1, 3, 0}
};

static const int commit_sorting_time_reverse[][6] = {
	{4, 5, 2, 1, 3, 0}
};

41 42 43 44
static const int commit_sorting_segment[][6] = {
	{1, 2, -1, -1, -1, -1}
};

45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
#define commit_count 6
static const int result_bytes = 24;


static int get_commit_index(git_oid *raw_oid)
{
	int i;
	char oid[40];

	git_oid_fmt(oid, raw_oid);

	for (i = 0; i < commit_count; ++i)
		if (memcmp(oid, commit_ids[i], 40) == 0)
			return i;

	return -1;
}

63 64
static int test_walk_only(git_revwalk *walk,
		const int possible_results[][commit_count], int results_count)
65 66 67 68 69 70 71 72 73
{
	git_oid oid;
	int i;
	int result_array[commit_count];

	for (i = 0; i < commit_count; ++i)
		result_array[i] = -1;

	i = 0;
74
	while (git_revwalk_next(&oid, walk) == 0) {
75 76 77 78 79 80 81 82 83 84 85 86
		result_array[i++] = get_commit_index(&oid);
		/*{
			char str[41];
			git_oid_fmt(str, &oid);
			str[40] = 0;
			printf("  %d) %s\n", i, str);
		}*/
	}

	for (i = 0; i < results_count; ++i)
		if (memcmp(possible_results[i],
				result_array, result_bytes) == 0)
87
			return 0;
88 89 90 91

	return GIT_ERROR;
}

92 93 94 95 96 97 98 99 100
static int test_walk(git_revwalk *walk, const git_oid *root,
		int flags, const int possible_results[][6], int results_count)
{
	git_revwalk_sorting(walk, flags);
	git_revwalk_push(walk, root);

	return test_walk_only(walk, possible_results, results_count);
}

101 102 103 104 105 106 107 108 109 110 111 112
static git_repository *_repo;
static git_revwalk *_walk;

void test_revwalk_basic__initialize(void)
{
	cl_git_pass(git_repository_open(&_repo, cl_fixture("testrepo.git")));
	cl_git_pass(git_revwalk_new(&_walk, _repo));
}

void test_revwalk_basic__cleanup(void)
{
	git_revwalk_free(_walk);
113
	_walk = NULL;
114
	git_repository_free(_repo);
115
	_repo = NULL;
116 117 118 119 120 121 122 123 124 125 126 127 128
}

void test_revwalk_basic__sorting_modes(void)
{
	git_oid id;

	git_oid_fromstr(&id, commit_head);

	cl_git_pass(test_walk(_walk, &id, GIT_SORT_TIME, commit_sorting_time, 1));
	cl_git_pass(test_walk(_walk, &id, GIT_SORT_TOPOLOGICAL, commit_sorting_topo, 2));
	cl_git_pass(test_walk(_walk, &id, GIT_SORT_TIME | GIT_SORT_REVERSE, commit_sorting_time_reverse, 1));
	cl_git_pass(test_walk(_walk, &id, GIT_SORT_TOPOLOGICAL | GIT_SORT_REVERSE, commit_sorting_topo_reverse, 2));
}
129 130 131 132 133 134 135 136

void test_revwalk_basic__glob_heads(void)
{
	int i = 0;
	git_oid oid;

	cl_git_pass(git_revwalk_push_glob(_walk, "heads"));

137
	while (git_revwalk_next(&oid, _walk) == 0) {
138 139 140
		i++;
	}

141 142
	/* git log --branches --oneline | wc -l => 14 */
	cl_assert(i == 14);
143
}
144 145 146 147 148 149 150 151

void test_revwalk_basic__push_head(void)
{
	int i = 0;
	git_oid oid;

	cl_git_pass(git_revwalk_push_head(_walk));

152
	while (git_revwalk_next(&oid, _walk) == 0) {
153 154 155 156 157 158
		i++;
	}

	/* git log HEAD --oneline | wc -l => 7 */
	cl_assert(i == 7);
}
159

160
void test_revwalk_basic__push_head_hide_ref(void)
161 162 163 164 165
{
	int i = 0;
	git_oid oid;

	cl_git_pass(git_revwalk_push_head(_walk));
166
	cl_git_pass(git_revwalk_hide_ref(_walk, "refs/heads/packed-test"));
167

168
	while (git_revwalk_next(&oid, _walk) == 0) {
169 170 171 172 173 174
		i++;
	}

	/* git log HEAD --oneline --not refs/heads/packed-test | wc -l => 4 */
	cl_assert(i == 4);
}
175 176 177 178 179 180 181 182 183

void test_revwalk_basic__push_head_hide_ref_nobase(void)
{
	int i = 0;
	git_oid oid;

	cl_git_pass(git_revwalk_push_head(_walk));
	cl_git_pass(git_revwalk_hide_ref(_walk, "refs/heads/packed"));

184
	while (git_revwalk_next(&oid, _walk) == 0) {
185 186 187 188 189 190
		i++;
	}

	/* git log HEAD --oneline --not refs/heads/packed | wc -l => 7 */
	cl_assert(i == 7);
}
191 192 193 194 195 196 197 198

void test_revwalk_basic__disallow_non_commit(void)
{
	git_oid oid;

	cl_git_pass(git_oid_fromstr(&oid, "521d87c1ec3aef9824daf6d96cc0ae3710766d91"));
	cl_git_fail(git_revwalk_push(_walk, &oid));
}
199 200 201 202 203 204 205 206

void test_revwalk_basic__push_range(void)
{
	git_revwalk_reset(_walk);
	git_revwalk_sorting(_walk, 0);
	cl_git_pass(git_revwalk_push_range(_walk, "9fd738e~2..9fd738e"));
	cl_git_pass(test_walk_only(_walk, commit_sorting_segment, 1));
}