]>
git.proxmox.com Git - libgit2.git/blob - tests/index/version.c
1 #include "clar_libgit2.h"
4 static git_repository
*g_repo
= NULL
;
6 void test_index_version__cleanup(void)
8 cl_git_sandbox_cleanup();
12 void test_index_version__can_read_v4(void)
14 const char *paths
[] = {
15 "file.tx", "file.txt", "file.txz", "foo", "zzz",
20 g_repo
= cl_git_sandbox_init("indexv4");
22 cl_git_pass(git_repository_index(&index
, g_repo
));
23 cl_assert_equal_sz(git_index_entrycount(index
), 5);
25 for (i
= 0; i
< ARRAY_SIZE(paths
); i
++) {
26 const git_index_entry
*entry
=
27 git_index_get_bypath(index
, paths
[i
], GIT_INDEX_STAGE_NORMAL
);
29 cl_assert(entry
!= NULL
);
32 git_index_free(index
);
35 void test_index_version__can_write_v4(void)
37 const char *paths
[] = {
47 git_index_entry entry
;
51 g_repo
= cl_git_sandbox_init("empty_standard_repo");
52 cl_git_pass(git_repository_index(&index
, g_repo
));
53 cl_git_pass(git_index_set_version(index
, 4));
55 for (i
= 0; i
< ARRAY_SIZE(paths
); i
++) {
56 memset(&entry
, 0, sizeof(entry
));
57 entry
.path
= paths
[i
];
58 entry
.mode
= GIT_FILEMODE_BLOB
;
59 cl_git_pass(git_index_add_from_buffer(index
, &entry
, paths
[i
],
60 strlen(paths
[i
]) + 1));
62 cl_assert_equal_sz(git_index_entrycount(index
), ARRAY_SIZE(paths
));
64 cl_git_pass(git_index_write(index
));
65 git_index_free(index
);
67 cl_git_pass(git_repository_open(&repo
, git_repository_path(g_repo
)));
68 cl_git_pass(git_repository_index(&index
, repo
));
69 cl_assert(git_index_version(index
) == 4);
71 for (i
= 0; i
< ARRAY_SIZE(paths
); i
++) {
72 const git_index_entry
*e
;
74 cl_assert(e
= git_index_get_bypath(index
, paths
[i
], 0));
75 cl_assert_equal_s(paths
[i
], e
->path
);
78 git_index_free(index
);
79 git_repository_free(repo
);
82 void test_index_version__v4_uses_path_compression(void)
84 git_index_entry entry
;
86 char path
[250], buf
[1];
90 memset(path
, 'a', sizeof(path
));
91 memset(buf
, 'a', sizeof(buf
));
93 memset(&entry
, 0, sizeof(entry
));
95 entry
.mode
= GIT_FILEMODE_BLOB
;
97 g_repo
= cl_git_sandbox_init("indexv4");
98 cl_git_pass(git_repository_index(&index
, g_repo
));
100 /* write 676 paths of 250 bytes length */
101 for (i
= 'a'; i
<= 'z'; i
++) {
102 for (j
= 'a'; j
< 'z'; j
++) {
103 path
[ARRAY_SIZE(path
) - 3] = i
;
104 path
[ARRAY_SIZE(path
) - 2] = j
;
105 path
[ARRAY_SIZE(path
) - 1] = '\0';
106 cl_git_pass(git_index_add_from_buffer(index
, &entry
, buf
, sizeof(buf
)));
110 cl_git_pass(git_index_write(index
));
111 cl_git_pass(p_stat(git_index_path(index
), &st
));
114 * Without path compression, the written paths would at
117 * (entries * pathlen) = len
118 * (676 * 250) = 169000
120 * bytes. As index v4 uses suffix-compression and our
121 * written paths only differ in the last two entries,
122 * this number will be much smaller, e.g.
124 * (1 * pathlen) + (675 * 2) = len
129 * Note that the above calculations do not include
130 * additional metadata of the index, e.g. OIDs or
131 * index extensions. Including those we get an index
132 * of approx. 200kB without compression and 40kB with
133 * compression. As this is a lot smaller than without
134 * compression, we can verify that path compression is
137 cl_assert_(st
.st_size
< 75000, "path compression not enabled");
139 git_index_free(index
);