]> git.proxmox.com Git - libgit2.git/blob - tests/repo/hashfile.c
New upstream version 1.4.3+dfsg.1
[libgit2.git] / tests / repo / hashfile.c
1 #include "clar_libgit2.h"
2
3 static git_repository *_repo;
4
5 void test_repo_hashfile__initialize(void)
6 {
7 _repo = cl_git_sandbox_init("status");
8 }
9
10 void test_repo_hashfile__cleanup(void)
11 {
12 cl_fixture_cleanup("absolute");
13 cl_git_sandbox_cleanup();
14 _repo = NULL;
15 }
16
17 void test_repo_hashfile__simple(void)
18 {
19 git_oid a, b;
20 git_str full = GIT_STR_INIT;
21
22 /* hash with repo relative path */
23 cl_git_pass(git_odb_hashfile(&a, "status/current_file", GIT_OBJECT_BLOB));
24 cl_git_pass(git_repository_hashfile(&b, _repo, "current_file", GIT_OBJECT_BLOB, NULL));
25 cl_assert_equal_oid(&a, &b);
26
27 cl_git_pass(git_str_joinpath(&full, git_repository_workdir(_repo), "current_file"));
28
29 /* hash with full path */
30 cl_git_pass(git_odb_hashfile(&a, full.ptr, GIT_OBJECT_BLOB));
31 cl_git_pass(git_repository_hashfile(&b, _repo, full.ptr, GIT_OBJECT_BLOB, NULL));
32 cl_assert_equal_oid(&a, &b);
33
34 /* hash with invalid type */
35 cl_git_fail(git_odb_hashfile(&a, full.ptr, GIT_OBJECT_ANY));
36 cl_git_fail(git_repository_hashfile(&b, _repo, full.ptr, GIT_OBJECT_OFS_DELTA, NULL));
37
38 git_str_dispose(&full);
39 }
40
41 void test_repo_hashfile__filtered_in_workdir(void)
42 {
43 git_str root = GIT_STR_INIT, txt = GIT_STR_INIT, bin = GIT_STR_INIT;
44 char cwd[GIT_PATH_MAX];
45 git_oid a, b;
46
47 cl_must_pass(p_getcwd(cwd, GIT_PATH_MAX));
48 cl_must_pass(p_mkdir("absolute", 0777));
49 cl_git_pass(git_str_joinpath(&root, cwd, "status"));
50 cl_git_pass(git_str_joinpath(&txt, root.ptr, "testfile.txt"));
51 cl_git_pass(git_str_joinpath(&bin, root.ptr, "testfile.bin"));
52
53 cl_repo_set_bool(_repo, "core.autocrlf", true);
54
55 cl_git_append2file("status/.gitattributes", "*.txt text\n*.bin binary\n\n");
56
57 /* create some sample content with CRLF in it */
58 cl_git_mkfile("status/testfile.txt", "content\r\n");
59 cl_git_mkfile("status/testfile.bin", "other\r\nstuff\r\n");
60
61 /* not equal hashes because of filtering */
62 cl_git_pass(git_odb_hashfile(&a, "status/testfile.txt", GIT_OBJECT_BLOB));
63 cl_git_pass(git_repository_hashfile(&b, _repo, "testfile.txt", GIT_OBJECT_BLOB, NULL));
64 cl_assert(git_oid_cmp(&a, &b));
65
66 /* not equal hashes because of filtering when specified by absolute path */
67 cl_git_pass(git_odb_hashfile(&a, "status/testfile.txt", GIT_OBJECT_BLOB));
68 cl_git_pass(git_repository_hashfile(&b, _repo, txt.ptr, GIT_OBJECT_BLOB, NULL));
69 cl_assert(git_oid_cmp(&a, &b));
70
71 /* equal hashes because filter is binary */
72 cl_git_pass(git_odb_hashfile(&a, "status/testfile.bin", GIT_OBJECT_BLOB));
73 cl_git_pass(git_repository_hashfile(&b, _repo, "testfile.bin", GIT_OBJECT_BLOB, NULL));
74 cl_assert_equal_oid(&a, &b);
75
76 /* equal hashes because filter is binary when specified by absolute path */
77 cl_git_pass(git_odb_hashfile(&a, "status/testfile.bin", GIT_OBJECT_BLOB));
78 cl_git_pass(git_repository_hashfile(&b, _repo, bin.ptr, GIT_OBJECT_BLOB, NULL));
79 cl_assert_equal_oid(&a, &b);
80
81 /* equal hashes when 'as_file' points to binary filtering */
82 cl_git_pass(git_odb_hashfile(&a, "status/testfile.txt", GIT_OBJECT_BLOB));
83 cl_git_pass(git_repository_hashfile(&b, _repo, "testfile.txt", GIT_OBJECT_BLOB, "foo.bin"));
84 cl_assert_equal_oid(&a, &b);
85
86 /* equal hashes when 'as_file' points to binary filtering (absolute path) */
87 cl_git_pass(git_odb_hashfile(&a, "status/testfile.txt", GIT_OBJECT_BLOB));
88 cl_git_pass(git_repository_hashfile(&b, _repo, txt.ptr, GIT_OBJECT_BLOB, "foo.bin"));
89 cl_assert_equal_oid(&a, &b);
90
91 /* not equal hashes when 'as_file' points to text filtering */
92 cl_git_pass(git_odb_hashfile(&a, "status/testfile.bin", GIT_OBJECT_BLOB));
93 cl_git_pass(git_repository_hashfile(&b, _repo, "testfile.bin", GIT_OBJECT_BLOB, "foo.txt"));
94 cl_assert(git_oid_cmp(&a, &b));
95
96 /* not equal hashes when 'as_file' points to text filtering */
97 cl_git_pass(git_odb_hashfile(&a, "status/testfile.bin", GIT_OBJECT_BLOB));
98 cl_git_pass(git_repository_hashfile(&b, _repo, bin.ptr, GIT_OBJECT_BLOB, "foo.txt"));
99 cl_assert(git_oid_cmp(&a, &b));
100
101 /* equal hashes when 'as_file' is empty and turns off filtering */
102 cl_git_pass(git_odb_hashfile(&a, "status/testfile.txt", GIT_OBJECT_BLOB));
103 cl_git_pass(git_repository_hashfile(&b, _repo, "testfile.txt", GIT_OBJECT_BLOB, ""));
104 cl_assert_equal_oid(&a, &b);
105
106 cl_git_pass(git_odb_hashfile(&a, "status/testfile.bin", GIT_OBJECT_BLOB));
107 cl_git_pass(git_repository_hashfile(&b, _repo, "testfile.bin", GIT_OBJECT_BLOB, ""));
108 cl_assert_equal_oid(&a, &b);
109
110 cl_git_pass(git_odb_hashfile(&a, "status/testfile.txt", GIT_OBJECT_BLOB));
111 cl_git_pass(git_repository_hashfile(&b, _repo, txt.ptr, GIT_OBJECT_BLOB, ""));
112 cl_assert_equal_oid(&a, &b);
113
114 cl_git_pass(git_odb_hashfile(&a, "status/testfile.bin", GIT_OBJECT_BLOB));
115 cl_git_pass(git_repository_hashfile(&b, _repo, bin.ptr, GIT_OBJECT_BLOB, ""));
116 cl_assert_equal_oid(&a, &b);
117
118 /* some hash type failures */
119 cl_git_fail(git_odb_hashfile(&a, "status/testfile.txt", 0));
120 cl_git_fail(git_repository_hashfile(&b, _repo, "testfile.txt", GIT_OBJECT_ANY, NULL));
121
122 git_str_dispose(&txt);
123 git_str_dispose(&bin);
124 git_str_dispose(&root);
125 }
126
127 void test_repo_hashfile__filtered_outside_workdir(void)
128 {
129 git_str root = GIT_STR_INIT, txt = GIT_STR_INIT, bin = GIT_STR_INIT;
130 char cwd[GIT_PATH_MAX];
131 git_oid a, b;
132
133 cl_must_pass(p_getcwd(cwd, GIT_PATH_MAX));
134 cl_must_pass(p_mkdir("absolute", 0777));
135 cl_git_pass(git_str_joinpath(&root, cwd, "absolute"));
136 cl_git_pass(git_str_joinpath(&txt, root.ptr, "testfile.txt"));
137 cl_git_pass(git_str_joinpath(&bin, root.ptr, "testfile.bin"));
138
139 cl_repo_set_bool(_repo, "core.autocrlf", true);
140 cl_git_append2file("status/.gitattributes", "*.txt text\n*.bin binary\n\n");
141
142 /* create some sample content with CRLF in it */
143 cl_git_mkfile("absolute/testfile.txt", "content\r\n");
144 cl_git_mkfile("absolute/testfile.bin", "other\r\nstuff\r\n");
145
146 /* not equal hashes because of filtering */
147 cl_git_pass(git_odb_hashfile(&a, "absolute/testfile.txt", GIT_OBJECT_BLOB));
148 cl_git_pass(git_repository_hashfile(&b, _repo, txt.ptr, GIT_OBJECT_BLOB, "testfile.txt"));
149 cl_assert(git_oid_cmp(&a, &b));
150
151 /* equal hashes because filter is binary */
152 cl_git_pass(git_odb_hashfile(&a, "absolute/testfile.bin", GIT_OBJECT_BLOB));
153 cl_git_pass(git_repository_hashfile(&b, _repo, bin.ptr, GIT_OBJECT_BLOB, "testfile.bin"));
154 cl_assert_equal_oid(&a, &b);
155
156 /*
157 * equal hashes because no filtering occurs for absolute paths outside the working
158 * directory unless as_path is specified
159 */
160 cl_git_pass(git_odb_hashfile(&a, "absolute/testfile.txt", GIT_OBJECT_BLOB));
161 cl_git_pass(git_repository_hashfile(&b, _repo, txt.ptr, GIT_OBJECT_BLOB, NULL));
162 cl_assert_equal_oid(&a, &b);
163
164 cl_git_pass(git_odb_hashfile(&a, "absolute/testfile.bin", GIT_OBJECT_BLOB));
165 cl_git_pass(git_repository_hashfile(&b, _repo, bin.ptr, GIT_OBJECT_BLOB, NULL));
166 cl_assert_equal_oid(&a, &b);
167
168 git_str_dispose(&txt);
169 git_str_dispose(&bin);
170 git_str_dispose(&root);
171 }