lookupbypath.c 2.52 KB
Newer Older
Ben Straub committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include "clar_libgit2.h"

#include "repository.h"

static git_repository *g_repo;
static git_tree *g_root_tree;
static git_commit *g_head_commit;
static git_object *g_expectedobject,
						*g_actualobject;

void test_object_lookupbypath__initialize(void)
{
	git_reference *head;
	git_tree_entry *tree_entry;

	cl_git_pass(git_repository_open(&g_repo, cl_fixture("attr/.gitted")));

	cl_git_pass(git_repository_head(&head, g_repo));
19
	cl_git_pass(git_reference_peel((git_object**)&g_head_commit, head, GIT_OBJECT_COMMIT));
Ben Straub committed
20 21 22
	cl_git_pass(git_commit_tree(&g_root_tree, g_head_commit));
	cl_git_pass(git_tree_entry_bypath(&tree_entry, g_root_tree, "subdir/subdir_test2.txt"));
	cl_git_pass(git_object_lookup(&g_expectedobject, g_repo, git_tree_entry_id(tree_entry),
23
				GIT_OBJECT_ANY));
Ben Straub committed
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44

	git_tree_entry_free(tree_entry);
	git_reference_free(head);

	g_actualobject = NULL;
}
void test_object_lookupbypath__cleanup(void)
{
	git_object_free(g_actualobject);
	git_object_free(g_expectedobject);
	git_tree_free(g_root_tree);
	git_commit_free(g_head_commit);
	g_expectedobject = NULL;
	git_repository_free(g_repo);
	g_repo = NULL;
}

void test_object_lookupbypath__errors(void)
{
	cl_assert_equal_i(GIT_EINVALIDSPEC,
			git_object_lookup_bypath(&g_actualobject, (git_object*)g_root_tree,
45
				"subdir/subdir_test2.txt", GIT_OBJECT_TREE)); /* It's not a tree */
Ben Straub committed
46 47
	cl_assert_equal_i(GIT_ENOTFOUND,
			git_object_lookup_bypath(&g_actualobject, (git_object*)g_root_tree,
48
				"file/doesnt/exist", GIT_OBJECT_ANY));
Ben Straub committed
49 50 51 52 53
}

void test_object_lookupbypath__from_root_tree(void)
{
	cl_git_pass(git_object_lookup_bypath(&g_actualobject, (git_object*)g_root_tree,
54
				"subdir/subdir_test2.txt", GIT_OBJECT_BLOB));
55 56
	cl_assert_equal_oid(git_object_id(g_expectedobject),
		git_object_id(g_actualobject));
Ben Straub committed
57 58 59 60 61
}

void test_object_lookupbypath__from_head_commit(void)
{
	cl_git_pass(git_object_lookup_bypath(&g_actualobject, (git_object*)g_head_commit,
62
				"subdir/subdir_test2.txt", GIT_OBJECT_BLOB));
63 64
	cl_assert_equal_oid(git_object_id(g_expectedobject),
				git_object_id(g_actualobject));
Ben Straub committed
65 66 67 68 69 70 71 72 73 74 75
}

void test_object_lookupbypath__from_subdir_tree(void)
{
	git_tree_entry *entry = NULL;
	git_tree *tree = NULL;

	cl_git_pass(git_tree_entry_bypath(&entry, g_root_tree, "subdir"));
	cl_git_pass(git_tree_lookup(&tree, g_repo, git_tree_entry_id(entry)));

	cl_git_pass(git_object_lookup_bypath(&g_actualobject, (git_object*)tree,
76
				"subdir_test2.txt", GIT_OBJECT_BLOB));
77 78
	cl_assert_equal_oid(git_object_id(g_expectedobject),
				git_object_id(g_actualobject));
Ben Straub committed
79 80 81 82 83

	git_tree_entry_free(entry);
	git_tree_free(tree);
}