foreach.c 3.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#include "clar_libgit2.h"
#include "refs.h"

static git_repository *repo;
static git_reference *fake_remote;

void test_refs_branches_foreach__initialize(void)
{
	git_oid id;

	cl_fixture_sandbox("testrepo.git");
	cl_git_pass(git_repository_open(&repo, "testrepo.git"));

	cl_git_pass(git_oid_fromstr(&id, "be3563ae3f795b2b4353bcce3a527ad0a4f7f644"));
15
	cl_git_pass(git_reference_create(&fake_remote, repo, "refs/remotes/nulltoken/master", &id, 0));
16 17 18 19 20
}

void test_refs_branches_foreach__cleanup(void)
{
	git_reference_free(fake_remote);
21 22
	fake_remote = NULL;

23
	git_repository_free(repo);
24
	repo = NULL;
25 26

	cl_fixture_cleanup("testrepo.git");
27 28

	cl_git_sandbox_cleanup();
29 30 31 32
}

static int count_branch_list_cb(const char *branch_name, git_branch_t branch_type, void *payload)
{
nulltoken committed
33
	int *count;
34

35 36 37
	GIT_UNUSED(branch_type);
	GIT_UNUSED(branch_name);

nulltoken committed
38
	count = (int *)payload;
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
	(*count)++;

	return 0;
}

static void assert_retrieval(unsigned int flags, unsigned int expected_count)
{
	int count = 0;

	cl_git_pass(git_branch_foreach(repo, flags, count_branch_list_cb, &count));

	cl_assert_equal_i(expected_count, count);
}

void test_refs_branches_foreach__retrieve_all_branches(void)
{
55
	assert_retrieval(GIT_BRANCH_LOCAL | GIT_BRANCH_REMOTE, 14);
56 57 58 59 60 61 62 63 64
}

void test_refs_branches_foreach__retrieve_remote_branches(void)
{
	assert_retrieval(GIT_BRANCH_REMOTE, 2);
}

void test_refs_branches_foreach__retrieve_local_branches(void)
{
65
	assert_retrieval(GIT_BRANCH_LOCAL, 12);
66 67 68 69 70 71 72 73 74 75 76
}

struct expectations {
	const char *branch_name;
	int encounters;
};

static void assert_branch_has_been_found(struct expectations *findings, const char* expected_branch_name)
{
	int pos = 0;

77
	for (pos = 0; findings[pos].branch_name; ++pos) {
78 79 80 81 82 83 84 85 86 87 88 89
		if (strcmp(expected_branch_name, findings[pos].branch_name) == 0) {
			cl_assert_equal_i(1, findings[pos].encounters);
			return;
		}
	}

	cl_fail("expected branch not found in list.");
}

static int contains_branch_list_cb(const char *branch_name, git_branch_t branch_type, void *payload)
{
	int pos = 0;
nulltoken committed
90
	struct expectations *exp;
91

92 93
	GIT_UNUSED(branch_type);

nulltoken committed
94
	exp = (struct expectations *)payload;
95

96
	for (pos = 0; exp[pos].branch_name; ++pos) {
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
		if (strcmp(branch_name, exp[pos].branch_name) == 0)
			exp[pos].encounters++;
	}

	return 0;
}

/*
 * $ git branch -r
 *  nulltoken/HEAD -> nulltoken/master
 *  nulltoken/master
 */
void test_refs_branches_foreach__retrieve_remote_symbolic_HEAD_when_present(void)
{
	struct expectations exp[] = {
		{ "nulltoken/HEAD", 0 },
		{ "nulltoken/master", 0 },
		{ NULL, 0 }
	};

	git_reference_free(fake_remote);
118
	cl_git_pass(git_reference_symbolic_create(&fake_remote, repo, "refs/remotes/nulltoken/HEAD", "refs/remotes/nulltoken/master", 0));
119 120 121 122 123 124 125 126

	assert_retrieval(GIT_BRANCH_REMOTE, 3);

	cl_git_pass(git_branch_foreach(repo, GIT_BRANCH_REMOTE, contains_branch_list_cb, &exp));

	assert_branch_has_been_found(exp, "nulltoken/HEAD");
	assert_branch_has_been_found(exp, "nulltoken/HEAD");
}
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151

static int branch_list_interrupt_cb(
	const char *branch_name, git_branch_t branch_type, void *payload)
{
	int *count;

	GIT_UNUSED(branch_type);
	GIT_UNUSED(branch_name);

	count = (int *)payload;
	(*count)++;

	return (*count == 5);
}

void test_refs_branches_foreach__can_cancel(void)
{
	int count = 0;

	cl_assert_equal_i(GIT_EUSER,
		git_branch_foreach(repo, GIT_BRANCH_LOCAL | GIT_BRANCH_REMOTE,
			branch_list_interrupt_cb, &count));

	cl_assert_equal_i(5, count);
}
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173

void test_refs_branches_foreach__mix_of_packed_and_loose(void)
{
	struct expectations exp[] = {
		{ "master", 0 },
		{ "origin/HEAD", 0 },
		{ "origin/master", 0 },
		{ "origin/packed", 0 },
		{ NULL, 0 }
	};
	git_repository *r2;

	r2 = cl_git_sandbox_init("testrepo2");

	cl_git_pass(git_branch_foreach(r2, GIT_BRANCH_LOCAL | GIT_BRANCH_REMOTE,
		contains_branch_list_cb, &exp));

	assert_branch_has_been_found(exp, "master");
	assert_branch_has_been_found(exp, "origin/HEAD");
	assert_branch_has_been_found(exp, "origin/master");
	assert_branch_has_been_found(exp, "origin/packed");
}