1 #include "clar_libgit2.h"
2
3 #include "repository.h"
4
5 static git_repository *g_repo;
6 static git_tree *g_root_tree;
7 static git_commit *g_head_commit;
8 static git_object *g_expectedobject,
9 *g_actualobject;
10
test_object_lookupbypath__initialize(void)11 void test_object_lookupbypath__initialize(void)
12 {
13 git_reference *head;
14 git_tree_entry *tree_entry;
15
16 cl_git_pass(git_repository_open(&g_repo, cl_fixture("attr/.gitted")));
17
18 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));
20 cl_git_pass(git_commit_tree(&g_root_tree, g_head_commit));
21 cl_git_pass(git_tree_entry_bypath(&tree_entry, g_root_tree, "subdir/subdir_test2.txt"));
22 cl_git_pass(git_object_lookup(&g_expectedobject, g_repo, git_tree_entry_id(tree_entry),
23 GIT_OBJECT_ANY));
24
25 git_tree_entry_free(tree_entry);
26 git_reference_free(head);
27
28 g_actualobject = NULL;
29 }
test_object_lookupbypath__cleanup(void)30 void test_object_lookupbypath__cleanup(void)
31 {
32 git_object_free(g_actualobject);
33 git_object_free(g_expectedobject);
34 git_tree_free(g_root_tree);
35 git_commit_free(g_head_commit);
36 g_expectedobject = NULL;
37 git_repository_free(g_repo);
38 g_repo = NULL;
39 }
40
test_object_lookupbypath__errors(void)41 void test_object_lookupbypath__errors(void)
42 {
43 cl_assert_equal_i(GIT_EINVALIDSPEC,
44 git_object_lookup_bypath(&g_actualobject, (git_object*)g_root_tree,
45 "subdir/subdir_test2.txt", GIT_OBJECT_TREE)); /* It's not a tree */
46 cl_assert_equal_i(GIT_ENOTFOUND,
47 git_object_lookup_bypath(&g_actualobject, (git_object*)g_root_tree,
48 "file/doesnt/exist", GIT_OBJECT_ANY));
49 }
50
test_object_lookupbypath__from_root_tree(void)51 void test_object_lookupbypath__from_root_tree(void)
52 {
53 cl_git_pass(git_object_lookup_bypath(&g_actualobject, (git_object*)g_root_tree,
54 "subdir/subdir_test2.txt", GIT_OBJECT_BLOB));
55 cl_assert_equal_oid(git_object_id(g_expectedobject),
56 git_object_id(g_actualobject));
57 }
58
test_object_lookupbypath__from_head_commit(void)59 void test_object_lookupbypath__from_head_commit(void)
60 {
61 cl_git_pass(git_object_lookup_bypath(&g_actualobject, (git_object*)g_head_commit,
62 "subdir/subdir_test2.txt", GIT_OBJECT_BLOB));
63 cl_assert_equal_oid(git_object_id(g_expectedobject),
64 git_object_id(g_actualobject));
65 }
66
test_object_lookupbypath__from_subdir_tree(void)67 void test_object_lookupbypath__from_subdir_tree(void)
68 {
69 git_tree_entry *entry = NULL;
70 git_tree *tree = NULL;
71
72 cl_git_pass(git_tree_entry_bypath(&entry, g_root_tree, "subdir"));
73 cl_git_pass(git_tree_lookup(&tree, g_repo, git_tree_entry_id(entry)));
74
75 cl_git_pass(git_object_lookup_bypath(&g_actualobject, (git_object*)tree,
76 "subdir_test2.txt", GIT_OBJECT_BLOB));
77 cl_assert_equal_oid(git_object_id(g_expectedobject),
78 git_object_id(g_actualobject));
79
80 git_tree_entry_free(entry);
81 git_tree_free(tree);
82 }
83
84