]>
git.proxmox.com Git - libgit2.git/blob - tests/repo/hashfile.c
1 #include "clar_libgit2.h"
4 static git_repository
*_repo
;
6 void test_repo_hashfile__initialize(void)
8 _repo
= cl_git_sandbox_init("status");
11 void test_repo_hashfile__cleanup(void)
13 cl_fixture_cleanup("absolute");
14 cl_git_sandbox_cleanup();
18 void test_repo_hashfile__simple(void)
21 git_buf full
= GIT_BUF_INIT
;
23 /* hash with repo relative path */
24 cl_git_pass(git_odb_hashfile(&a
, "status/current_file", GIT_OBJECT_BLOB
));
25 cl_git_pass(git_repository_hashfile(&b
, _repo
, "current_file", GIT_OBJECT_BLOB
, NULL
));
26 cl_assert_equal_oid(&a
, &b
);
28 cl_git_pass(git_buf_joinpath(&full
, git_repository_workdir(_repo
), "current_file"));
30 /* hash with full path */
31 cl_git_pass(git_odb_hashfile(&a
, full
.ptr
, GIT_OBJECT_BLOB
));
32 cl_git_pass(git_repository_hashfile(&b
, _repo
, full
.ptr
, GIT_OBJECT_BLOB
, NULL
));
33 cl_assert_equal_oid(&a
, &b
);
35 /* hash with invalid type */
36 cl_git_fail(git_odb_hashfile(&a
, full
.ptr
, GIT_OBJECT_ANY
));
37 cl_git_fail(git_repository_hashfile(&b
, _repo
, full
.ptr
, GIT_OBJECT_OFS_DELTA
, NULL
));
39 git_buf_dispose(&full
);
42 void test_repo_hashfile__filtered_in_workdir(void)
44 git_buf root
= GIT_BUF_INIT
, txt
= GIT_BUF_INIT
, bin
= GIT_BUF_INIT
;
45 char cwd
[GIT_PATH_MAX
];
48 cl_must_pass(p_getcwd(cwd
, GIT_PATH_MAX
));
49 cl_must_pass(p_mkdir("absolute", 0777));
50 cl_git_pass(git_buf_joinpath(&root
, cwd
, "status"));
51 cl_git_pass(git_buf_joinpath(&txt
, root
.ptr
, "testfile.txt"));
52 cl_git_pass(git_buf_joinpath(&bin
, root
.ptr
, "testfile.bin"));
54 cl_repo_set_bool(_repo
, "core.autocrlf", true);
56 cl_git_append2file("status/.gitattributes", "*.txt text\n*.bin binary\n\n");
58 /* create some sample content with CRLF in it */
59 cl_git_mkfile("status/testfile.txt", "content\r\n");
60 cl_git_mkfile("status/testfile.bin", "other\r\nstuff\r\n");
62 /* not equal hashes because of filtering */
63 cl_git_pass(git_odb_hashfile(&a
, "status/testfile.txt", GIT_OBJECT_BLOB
));
64 cl_git_pass(git_repository_hashfile(&b
, _repo
, "testfile.txt", GIT_OBJECT_BLOB
, NULL
));
65 cl_assert(git_oid_cmp(&a
, &b
));
67 /* not equal hashes because of filtering when specified by absolute path */
68 cl_git_pass(git_odb_hashfile(&a
, "status/testfile.txt", GIT_OBJECT_BLOB
));
69 cl_git_pass(git_repository_hashfile(&b
, _repo
, txt
.ptr
, GIT_OBJECT_BLOB
, NULL
));
70 cl_assert(git_oid_cmp(&a
, &b
));
72 /* equal hashes because filter is binary */
73 cl_git_pass(git_odb_hashfile(&a
, "status/testfile.bin", GIT_OBJECT_BLOB
));
74 cl_git_pass(git_repository_hashfile(&b
, _repo
, "testfile.bin", GIT_OBJECT_BLOB
, NULL
));
75 cl_assert_equal_oid(&a
, &b
);
77 /* equal hashes because filter is binary when specified by absolute path */
78 cl_git_pass(git_odb_hashfile(&a
, "status/testfile.bin", GIT_OBJECT_BLOB
));
79 cl_git_pass(git_repository_hashfile(&b
, _repo
, bin
.ptr
, GIT_OBJECT_BLOB
, NULL
));
80 cl_assert_equal_oid(&a
, &b
);
82 /* equal hashes when 'as_file' points to binary filtering */
83 cl_git_pass(git_odb_hashfile(&a
, "status/testfile.txt", GIT_OBJECT_BLOB
));
84 cl_git_pass(git_repository_hashfile(&b
, _repo
, "testfile.txt", GIT_OBJECT_BLOB
, "foo.bin"));
85 cl_assert_equal_oid(&a
, &b
);
87 /* equal hashes when 'as_file' points to binary filtering (absolute path) */
88 cl_git_pass(git_odb_hashfile(&a
, "status/testfile.txt", GIT_OBJECT_BLOB
));
89 cl_git_pass(git_repository_hashfile(&b
, _repo
, txt
.ptr
, GIT_OBJECT_BLOB
, "foo.bin"));
90 cl_assert_equal_oid(&a
, &b
);
92 /* not equal hashes when 'as_file' points to text filtering */
93 cl_git_pass(git_odb_hashfile(&a
, "status/testfile.bin", GIT_OBJECT_BLOB
));
94 cl_git_pass(git_repository_hashfile(&b
, _repo
, "testfile.bin", GIT_OBJECT_BLOB
, "foo.txt"));
95 cl_assert(git_oid_cmp(&a
, &b
));
97 /* not equal hashes when 'as_file' points to text filtering */
98 cl_git_pass(git_odb_hashfile(&a
, "status/testfile.bin", GIT_OBJECT_BLOB
));
99 cl_git_pass(git_repository_hashfile(&b
, _repo
, bin
.ptr
, GIT_OBJECT_BLOB
, "foo.txt"));
100 cl_assert(git_oid_cmp(&a
, &b
));
102 /* equal hashes when 'as_file' is empty and turns off filtering */
103 cl_git_pass(git_odb_hashfile(&a
, "status/testfile.txt", GIT_OBJECT_BLOB
));
104 cl_git_pass(git_repository_hashfile(&b
, _repo
, "testfile.txt", GIT_OBJECT_BLOB
, ""));
105 cl_assert_equal_oid(&a
, &b
);
107 cl_git_pass(git_odb_hashfile(&a
, "status/testfile.bin", GIT_OBJECT_BLOB
));
108 cl_git_pass(git_repository_hashfile(&b
, _repo
, "testfile.bin", GIT_OBJECT_BLOB
, ""));
109 cl_assert_equal_oid(&a
, &b
);
111 cl_git_pass(git_odb_hashfile(&a
, "status/testfile.txt", GIT_OBJECT_BLOB
));
112 cl_git_pass(git_repository_hashfile(&b
, _repo
, txt
.ptr
, GIT_OBJECT_BLOB
, ""));
113 cl_assert_equal_oid(&a
, &b
);
115 cl_git_pass(git_odb_hashfile(&a
, "status/testfile.bin", GIT_OBJECT_BLOB
));
116 cl_git_pass(git_repository_hashfile(&b
, _repo
, bin
.ptr
, GIT_OBJECT_BLOB
, ""));
117 cl_assert_equal_oid(&a
, &b
);
119 /* some hash type failures */
120 cl_git_fail(git_odb_hashfile(&a
, "status/testfile.txt", 0));
121 cl_git_fail(git_repository_hashfile(&b
, _repo
, "testfile.txt", GIT_OBJECT_ANY
, NULL
));
123 git_buf_dispose(&txt
);
124 git_buf_dispose(&bin
);
125 git_buf_dispose(&root
);
128 void test_repo_hashfile__filtered_outside_workdir(void)
130 git_buf root
= GIT_BUF_INIT
, txt
= GIT_BUF_INIT
, bin
= GIT_BUF_INIT
;
131 char cwd
[GIT_PATH_MAX
];
134 cl_must_pass(p_getcwd(cwd
, GIT_PATH_MAX
));
135 cl_must_pass(p_mkdir("absolute", 0777));
136 cl_git_pass(git_buf_joinpath(&root
, cwd
, "absolute"));
137 cl_git_pass(git_buf_joinpath(&txt
, root
.ptr
, "testfile.txt"));
138 cl_git_pass(git_buf_joinpath(&bin
, root
.ptr
, "testfile.bin"));
140 cl_repo_set_bool(_repo
, "core.autocrlf", true);
141 cl_git_append2file("status/.gitattributes", "*.txt text\n*.bin binary\n\n");
143 /* create some sample content with CRLF in it */
144 cl_git_mkfile("absolute/testfile.txt", "content\r\n");
145 cl_git_mkfile("absolute/testfile.bin", "other\r\nstuff\r\n");
147 /* not equal hashes because of filtering */
148 cl_git_pass(git_odb_hashfile(&a
, "absolute/testfile.txt", GIT_OBJECT_BLOB
));
149 cl_git_pass(git_repository_hashfile(&b
, _repo
, txt
.ptr
, GIT_OBJECT_BLOB
, "testfile.txt"));
150 cl_assert(git_oid_cmp(&a
, &b
));
152 /* equal hashes because filter is binary */
153 cl_git_pass(git_odb_hashfile(&a
, "absolute/testfile.bin", GIT_OBJECT_BLOB
));
154 cl_git_pass(git_repository_hashfile(&b
, _repo
, bin
.ptr
, GIT_OBJECT_BLOB
, "testfile.bin"));
155 cl_assert_equal_oid(&a
, &b
);
158 * equal hashes because no filtering occurs for absolute paths outside the working
159 * directory unless as_path is specified
161 cl_git_pass(git_odb_hashfile(&a
, "absolute/testfile.txt", GIT_OBJECT_BLOB
));
162 cl_git_pass(git_repository_hashfile(&b
, _repo
, txt
.ptr
, GIT_OBJECT_BLOB
, NULL
));
163 cl_assert_equal_oid(&a
, &b
);
165 cl_git_pass(git_odb_hashfile(&a
, "absolute/testfile.bin", GIT_OBJECT_BLOB
));
166 cl_git_pass(git_repository_hashfile(&b
, _repo
, bin
.ptr
, GIT_OBJECT_BLOB
, NULL
));
167 cl_assert_equal_oid(&a
, &b
);
169 git_buf_dispose(&txt
);
170 git_buf_dispose(&bin
);
171 git_buf_dispose(&root
);