walk.c 1.96 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 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
#include "clar_libgit2.h"
#include "tree.h"

static const char *tree_oid = "1810dff58d8a660512d4832e740f692884338ccd";
static git_repository *g_repo;

void test_object_tree_walk__initialize(void)
{
   g_repo = cl_git_sandbox_init("testrepo");
}

void test_object_tree_walk__cleanup(void)
{
   cl_git_sandbox_cleanup();
}

static int treewalk_count_cb(
	const char *root, const git_tree_entry *entry, void *payload)
{
	int *count = payload;

	GIT_UNUSED(root);
	GIT_UNUSED(entry);

	(*count) += 1;

	return 0;
}

void test_object_tree_walk__0(void)
{
	git_oid id;
	git_tree *tree;
	int ct;

	git_oid_fromstr(&id, tree_oid);

	cl_git_pass(git_tree_lookup(&tree, g_repo, &id));

	ct = 0;
41
	cl_git_pass(git_tree_walk(tree, GIT_TREEWALK_PRE, treewalk_count_cb, &ct));
42 43 44
	cl_assert_equal_i(3, ct);

	ct = 0;
45
	cl_git_pass(git_tree_walk(tree, GIT_TREEWALK_POST, treewalk_count_cb, &ct));
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
	cl_assert_equal_i(3, ct);

	git_tree_free(tree);
}


static int treewalk_stop_cb(
	const char *root, const git_tree_entry *entry, void *payload)
{
	int *count = payload;

	GIT_UNUSED(root);
	GIT_UNUSED(entry);

	(*count) += 1;

62
	return (*count == 2) ? -1 : 0;
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
}

static int treewalk_stop_immediately_cb(
	const char *root, const git_tree_entry *entry, void *payload)
{
	GIT_UNUSED(root);
	GIT_UNUSED(entry);
	GIT_UNUSED(payload);
	return -100;
}

void test_object_tree_walk__1(void)
{
	git_oid id;
	git_tree *tree;
	int ct;

	git_oid_fromstr(&id, tree_oid);

	cl_git_pass(git_tree_lookup(&tree, g_repo, &id));

	ct = 0;
	cl_assert_equal_i(
86
		GIT_EUSER, git_tree_walk(tree, GIT_TREEWALK_PRE, treewalk_stop_cb, &ct));
87 88 89 90
	cl_assert_equal_i(2, ct);

	ct = 0;
	cl_assert_equal_i(
91
		GIT_EUSER, git_tree_walk(tree, GIT_TREEWALK_POST, treewalk_stop_cb, &ct));
92 93 94 95
	cl_assert_equal_i(2, ct);

	cl_assert_equal_i(
		GIT_EUSER, git_tree_walk(
96
			tree, GIT_TREEWALK_PRE, treewalk_stop_immediately_cb, NULL));
97 98 99

	cl_assert_equal_i(
		GIT_EUSER, git_tree_walk(
100
			tree, GIT_TREEWALK_POST, treewalk_stop_immediately_cb, NULL));
101 102 103

	git_tree_free(tree);
}