|
Packit Service |
20376f |
#include "clar_libgit2.h"
|
|
Packit Service |
20376f |
#include "refs.h"
|
|
Packit Service |
20376f |
|
|
Packit Service |
20376f |
static git_repository *repo;
|
|
Packit Service |
20376f |
static git_reference *fake_remote;
|
|
Packit Service |
20376f |
|
|
Packit Service |
20376f |
void test_refs_branches_iterator__initialize(void)
|
|
Packit Service |
20376f |
{
|
|
Packit Service |
20376f |
git_oid id;
|
|
Packit Service |
20376f |
|
|
Packit Service |
20376f |
cl_fixture_sandbox("testrepo.git");
|
|
Packit Service |
20376f |
cl_git_pass(git_repository_open(&repo, "testrepo.git"));
|
|
Packit Service |
20376f |
|
|
Packit Service |
20376f |
cl_git_pass(git_oid_fromstr(&id, "be3563ae3f795b2b4353bcce3a527ad0a4f7f644"));
|
|
Packit Service |
20376f |
cl_git_pass(git_reference_create(&fake_remote, repo, "refs/remotes/nulltoken/master", &id, 0, NULL));
|
|
Packit Service |
20376f |
}
|
|
Packit Service |
20376f |
|
|
Packit Service |
20376f |
void test_refs_branches_iterator__cleanup(void)
|
|
Packit Service |
20376f |
{
|
|
Packit Service |
20376f |
git_reference_free(fake_remote);
|
|
Packit Service |
20376f |
fake_remote = NULL;
|
|
Packit Service |
20376f |
|
|
Packit Service |
20376f |
git_repository_free(repo);
|
|
Packit Service |
20376f |
repo = NULL;
|
|
Packit Service |
20376f |
|
|
Packit Service |
20376f |
cl_fixture_cleanup("testrepo.git");
|
|
Packit Service |
20376f |
|
|
Packit Service |
20376f |
cl_git_sandbox_cleanup();
|
|
Packit Service |
20376f |
}
|
|
Packit Service |
20376f |
|
|
Packit Service |
20376f |
static void assert_retrieval(unsigned int flags, unsigned int expected_count)
|
|
Packit Service |
20376f |
{
|
|
Packit Service |
20376f |
git_branch_iterator *iter;
|
|
Packit Service |
20376f |
git_reference *ref;
|
|
Packit Service |
20376f |
int count = 0, error;
|
|
Packit Service |
20376f |
git_branch_t type;
|
|
Packit Service |
20376f |
|
|
Packit Service |
20376f |
cl_git_pass(git_branch_iterator_new(&iter, repo, flags));
|
|
Packit Service |
20376f |
while ((error = git_branch_next(&ref, &type, iter)) == 0) {
|
|
Packit Service |
20376f |
count++;
|
|
Packit Service |
20376f |
git_reference_free(ref);
|
|
Packit Service |
20376f |
}
|
|
Packit Service |
20376f |
|
|
Packit Service |
20376f |
git_branch_iterator_free(iter);
|
|
Packit Service |
20376f |
cl_assert_equal_i(error, GIT_ITEROVER);
|
|
Packit Service |
20376f |
cl_assert_equal_i(expected_count, count);
|
|
Packit Service |
20376f |
}
|
|
Packit Service |
20376f |
|
|
Packit Service |
20376f |
void test_refs_branches_iterator__retrieve_all_branches(void)
|
|
Packit Service |
20376f |
{
|
|
Packit Service |
20376f |
assert_retrieval(GIT_BRANCH_ALL, 14);
|
|
Packit Service |
20376f |
}
|
|
Packit Service |
20376f |
|
|
Packit Service |
20376f |
void test_refs_branches_iterator__retrieve_remote_branches(void)
|
|
Packit Service |
20376f |
{
|
|
Packit Service |
20376f |
assert_retrieval(GIT_BRANCH_REMOTE, 2);
|
|
Packit Service |
20376f |
}
|
|
Packit Service |
20376f |
|
|
Packit Service |
20376f |
void test_refs_branches_iterator__retrieve_local_branches(void)
|
|
Packit Service |
20376f |
{
|
|
Packit Service |
20376f |
assert_retrieval(GIT_BRANCH_LOCAL, 12);
|
|
Packit Service |
20376f |
}
|
|
Packit Service |
20376f |
|
|
Packit Service |
20376f |
struct expectations {
|
|
Packit Service |
20376f |
const char *branch_name;
|
|
Packit Service |
20376f |
int encounters;
|
|
Packit Service |
20376f |
};
|
|
Packit Service |
20376f |
|
|
Packit Service |
20376f |
static void assert_branch_has_been_found(struct expectations *findings, const char* expected_branch_name)
|
|
Packit Service |
20376f |
{
|
|
Packit Service |
20376f |
int pos = 0;
|
|
Packit Service |
20376f |
|
|
Packit Service |
20376f |
for (pos = 0; findings[pos].branch_name; ++pos) {
|
|
Packit Service |
20376f |
if (strcmp(expected_branch_name, findings[pos].branch_name) == 0) {
|
|
Packit Service |
20376f |
cl_assert_equal_i(1, findings[pos].encounters);
|
|
Packit Service |
20376f |
return;
|
|
Packit Service |
20376f |
}
|
|
Packit Service |
20376f |
}
|
|
Packit Service |
20376f |
|
|
Packit Service |
20376f |
cl_fail("expected branch not found in list.");
|
|
Packit Service |
20376f |
}
|
|
Packit Service |
20376f |
|
|
Packit Service |
20376f |
static void contains_branches(struct expectations exp[], git_branch_iterator *iter)
|
|
Packit Service |
20376f |
{
|
|
Packit Service |
20376f |
git_reference *ref;
|
|
Packit Service |
20376f |
git_branch_t type;
|
|
Packit Service |
20376f |
int error, pos = 0;
|
|
Packit Service |
20376f |
|
|
Packit Service |
20376f |
while ((error = git_branch_next(&ref, &type, iter)) == 0) {
|
|
Packit Service |
20376f |
for (pos = 0; exp[pos].branch_name; ++pos) {
|
|
Packit Service |
20376f |
if (strcmp(git_reference_shorthand(ref), exp[pos].branch_name) == 0)
|
|
Packit Service |
20376f |
exp[pos].encounters++;
|
|
Packit Service |
20376f |
}
|
|
Packit Service |
20376f |
|
|
Packit Service |
20376f |
git_reference_free(ref);
|
|
Packit Service |
20376f |
}
|
|
Packit Service |
20376f |
|
|
Packit Service |
20376f |
cl_assert_equal_i(error, GIT_ITEROVER);
|
|
Packit Service |
20376f |
}
|
|
Packit Service |
20376f |
|
|
Packit Service |
20376f |
/*
|
|
Packit Service |
20376f |
* $ git branch -r
|
|
Packit Service |
20376f |
* nulltoken/HEAD -> nulltoken/master
|
|
Packit Service |
20376f |
* nulltoken/master
|
|
Packit Service |
20376f |
*/
|
|
Packit Service |
20376f |
void test_refs_branches_iterator__retrieve_remote_symbolic_HEAD_when_present(void)
|
|
Packit Service |
20376f |
{
|
|
Packit Service |
20376f |
git_branch_iterator *iter;
|
|
Packit Service |
20376f |
struct expectations exp[] = {
|
|
Packit Service |
20376f |
{ "nulltoken/HEAD", 0 },
|
|
Packit Service |
20376f |
{ "nulltoken/master", 0 },
|
|
Packit Service |
20376f |
{ NULL, 0 }
|
|
Packit Service |
20376f |
};
|
|
Packit Service |
20376f |
|
|
Packit Service |
20376f |
git_reference_free(fake_remote);
|
|
Packit Service |
20376f |
cl_git_pass(git_reference_symbolic_create(&fake_remote, repo, "refs/remotes/nulltoken/HEAD", "refs/remotes/nulltoken/master", 0, NULL));
|
|
Packit Service |
20376f |
|
|
Packit Service |
20376f |
assert_retrieval(GIT_BRANCH_REMOTE, 3);
|
|
Packit Service |
20376f |
|
|
Packit Service |
20376f |
cl_git_pass(git_branch_iterator_new(&iter, repo, GIT_BRANCH_REMOTE));
|
|
Packit Service |
20376f |
contains_branches(exp, iter);
|
|
Packit Service |
20376f |
git_branch_iterator_free(iter);
|
|
Packit Service |
20376f |
|
|
Packit Service |
20376f |
assert_branch_has_been_found(exp, "nulltoken/HEAD");
|
|
Packit Service |
20376f |
assert_branch_has_been_found(exp, "nulltoken/master");
|
|
Packit Service |
20376f |
}
|
|
Packit Service |
20376f |
|
|
Packit Service |
20376f |
void test_refs_branches_iterator__mix_of_packed_and_loose(void)
|
|
Packit Service |
20376f |
{
|
|
Packit Service |
20376f |
git_branch_iterator *iter;
|
|
Packit Service |
20376f |
struct expectations exp[] = {
|
|
Packit Service |
20376f |
{ "master", 0 },
|
|
Packit Service |
20376f |
{ "origin/HEAD", 0 },
|
|
Packit Service |
20376f |
{ "origin/master", 0 },
|
|
Packit Service |
20376f |
{ "origin/packed", 0 },
|
|
Packit Service |
20376f |
{ NULL, 0 }
|
|
Packit Service |
20376f |
};
|
|
Packit Service |
20376f |
git_repository *r2;
|
|
Packit Service |
20376f |
|
|
Packit Service |
20376f |
r2 = cl_git_sandbox_init("testrepo2");
|
|
Packit Service |
20376f |
|
|
Packit Service |
20376f |
cl_git_pass(git_branch_iterator_new(&iter, r2, GIT_BRANCH_ALL));
|
|
Packit Service |
20376f |
contains_branches(exp, iter);
|
|
Packit Service |
20376f |
|
|
Packit Service |
20376f |
git_branch_iterator_free(iter);
|
|
Packit Service |
20376f |
|
|
Packit Service |
20376f |
assert_branch_has_been_found(exp, "master");
|
|
Packit Service |
20376f |
assert_branch_has_been_found(exp, "origin/HEAD");
|
|
Packit Service |
20376f |
assert_branch_has_been_found(exp, "origin/master");
|
|
Packit Service |
20376f |
assert_branch_has_been_found(exp, "origin/packed");
|
|
Packit Service |
20376f |
}
|