]>
git.proxmox.com Git - libgit2.git/blob - tests/repo/hashfile.c
1 #include "clar_libgit2.h"
3 static git_repository
*_repo
;
5 void test_repo_hashfile__initialize(void)
7 _repo
= cl_git_sandbox_init("status");
10 void test_repo_hashfile__cleanup(void)
12 cl_fixture_cleanup("absolute");
13 cl_git_sandbox_cleanup();
17 void test_repo_hashfile__simple(void)
20 git_str full
= GIT_STR_INIT
;
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
);
27 cl_git_pass(git_str_joinpath(&full
, git_repository_workdir(_repo
), "current_file"));
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
);
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
));
38 git_str_dispose(&full
);
41 void test_repo_hashfile__filtered_in_workdir(void)
43 git_str root
= GIT_STR_INIT
, txt
= GIT_STR_INIT
, bin
= GIT_STR_INIT
;
44 char cwd
[GIT_PATH_MAX
];
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"));
53 cl_repo_set_bool(_repo
, "core.autocrlf", true);
55 cl_git_append2file("status/.gitattributes", "*.txt text\n*.bin binary\n\n");
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");
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
));
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
));
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
);
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
);
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
);
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
);
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
));
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
));
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
);
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
);
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
);
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
);
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
));
122 git_str_dispose(&txt
);
123 git_str_dispose(&bin
);
124 git_str_dispose(&root
);
127 void test_repo_hashfile__filtered_outside_workdir(void)
129 git_str root
= GIT_STR_INIT
, txt
= GIT_STR_INIT
, bin
= GIT_STR_INIT
;
130 char cwd
[GIT_PATH_MAX
];
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"));
139 cl_repo_set_bool(_repo
, "core.autocrlf", true);
140 cl_git_append2file("status/.gitattributes", "*.txt text\n*.bin binary\n\n");
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");
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
));
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
);
157 * equal hashes because no filtering occurs for absolute paths outside the working
158 * directory unless as_path is specified
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
);
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
);
168 git_str_dispose(&txt
);
169 git_str_dispose(&bin
);
170 git_str_dispose(&root
);