]> git.proxmox.com Git - libgit2.git/blob - tests/repo/hashfile.c
0fb4e6772a1e10cb9b0bd83fe47134a56dcd72d8
[libgit2.git] / tests / repo / hashfile.c
1 #include "clar_libgit2.h"
2 #include "buffer.h"
3
4 static git_repository *_repo;
5
6 void test_repo_hashfile__initialize(void)
7 {
8 _repo = cl_git_sandbox_init("status");
9 }
10
11 void test_repo_hashfile__cleanup(void)
12 {
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_buf full = GIT_BUF_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_buf_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_buf_dispose(&full);
39 }
40
41 void test_repo_hashfile__filtered(void)
42 {
43 git_oid a, b;
44
45 cl_repo_set_bool(_repo, "core.autocrlf", true);
46
47 cl_git_append2file("status/.gitattributes", "*.txt text\n*.bin binary\n\n");
48
49 /* create some sample content with CRLF in it */
50 cl_git_mkfile("status/testfile.txt", "content\r\n");
51 cl_git_mkfile("status/testfile.bin", "other\r\nstuff\r\n");
52
53 /* not equal hashes because of filtering */
54 cl_git_pass(git_odb_hashfile(&a, "status/testfile.txt", GIT_OBJECT_BLOB));
55 cl_git_pass(git_repository_hashfile(&b, _repo, "testfile.txt", GIT_OBJECT_BLOB, NULL));
56 cl_assert(git_oid_cmp(&a, &b));
57
58 /* equal hashes because filter is binary */
59 cl_git_pass(git_odb_hashfile(&a, "status/testfile.bin", GIT_OBJECT_BLOB));
60 cl_git_pass(git_repository_hashfile(&b, _repo, "testfile.bin", GIT_OBJECT_BLOB, NULL));
61 cl_assert_equal_oid(&a, &b);
62
63 /* equal hashes when 'as_file' points to binary filtering */
64 cl_git_pass(git_odb_hashfile(&a, "status/testfile.txt", GIT_OBJECT_BLOB));
65 cl_git_pass(git_repository_hashfile(&b, _repo, "testfile.txt", GIT_OBJECT_BLOB, "foo.bin"));
66 cl_assert_equal_oid(&a, &b);
67
68 /* not equal hashes when 'as_file' points to text filtering */
69 cl_git_pass(git_odb_hashfile(&a, "status/testfile.bin", GIT_OBJECT_BLOB));
70 cl_git_pass(git_repository_hashfile(&b, _repo, "testfile.bin", GIT_OBJECT_BLOB, "foo.txt"));
71 cl_assert(git_oid_cmp(&a, &b));
72
73 /* equal hashes when 'as_file' is empty and turns off filtering */
74 cl_git_pass(git_odb_hashfile(&a, "status/testfile.txt", GIT_OBJECT_BLOB));
75 cl_git_pass(git_repository_hashfile(&b, _repo, "testfile.txt", GIT_OBJECT_BLOB, ""));
76 cl_assert_equal_oid(&a, &b);
77
78 cl_git_pass(git_odb_hashfile(&a, "status/testfile.bin", GIT_OBJECT_BLOB));
79 cl_git_pass(git_repository_hashfile(&b, _repo, "testfile.bin", GIT_OBJECT_BLOB, ""));
80 cl_assert_equal_oid(&a, &b);
81
82 /* some hash type failures */
83 cl_git_fail(git_odb_hashfile(&a, "status/testfile.txt", 0));
84 cl_git_fail(git_repository_hashfile(&b, _repo, "testfile.txt", GIT_OBJECT_ANY, NULL));
85 }