]>
git.proxmox.com Git - libgit2.git/blob - tests/index/version.c
7ada302b5ffc15764041062c395d34793382b439
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
[] = {
46 git_index_entry entry
;
50 g_repo
= cl_git_sandbox_init("empty_standard_repo");
51 cl_git_pass(git_repository_index(&index
, g_repo
));
52 cl_git_pass(git_index_set_version(index
, 4));
54 for (i
= 0; i
< ARRAY_SIZE(paths
); i
++) {
55 memset(&entry
, 0, sizeof(entry
));
56 entry
.path
= paths
[i
];
57 entry
.mode
= GIT_FILEMODE_BLOB
;
58 cl_git_pass(git_index_add_frombuffer(index
, &entry
, paths
[i
],
59 strlen(paths
[i
]) + 1));
61 cl_assert_equal_sz(git_index_entrycount(index
), ARRAY_SIZE(paths
));
63 cl_git_pass(git_index_write(index
));
64 git_index_free(index
);
66 cl_git_pass(git_repository_index(&index
, g_repo
));
67 cl_assert(git_index_version(index
) == 4);
69 for (i
= 0; i
< ARRAY_SIZE(paths
); i
++) {
70 const git_index_entry
*e
;
72 cl_assert(e
= git_index_get_bypath(index
, paths
[i
], 0));
73 cl_assert_equal_s(paths
[i
], e
->path
);
76 git_index_free(index
);
79 void test_index_version__v4_uses_path_compression(void)
81 git_index_entry entry
;
83 char path
[250], buf
[1];
87 memset(path
, 'a', sizeof(path
));
88 memset(buf
, 'a', sizeof(buf
));
90 memset(&entry
, 0, sizeof(entry
));
92 entry
.mode
= GIT_FILEMODE_BLOB
;
94 g_repo
= cl_git_sandbox_init("indexv4");
95 cl_git_pass(git_repository_index(&index
, g_repo
));
97 /* write 676 paths of 250 bytes length */
98 for (i
= 'a'; i
<= 'z'; i
++) {
99 for (j
= 'a'; j
< 'z'; j
++) {
100 path
[ARRAY_SIZE(path
) - 3] = i
;
101 path
[ARRAY_SIZE(path
) - 2] = j
;
102 path
[ARRAY_SIZE(path
) - 1] = '\0';
103 cl_git_pass(git_index_add_frombuffer(index
, &entry
, buf
, sizeof(buf
)));
107 cl_git_pass(git_index_write(index
));
108 cl_git_pass(p_stat(git_index_path(index
), &st
));
111 * Without path compression, the written paths would at
114 * (entries * pathlen) = len
115 * (676 * 250) = 169000
117 * bytes. As index v4 uses suffix-compression and our
118 * written paths only differ in the last two entries,
119 * this number will be much smaller, e.g.
121 * (1 * pathlen) + (675 * 2) = len
126 * Note that the above calculations do not include
127 * additional metadata of the index, e.g. OIDs or
128 * index extensions. Including those we get an index
129 * of approx. 200kB without compression and 40kB with
130 * compression. As this is a lot smaller than without
131 * compression, we can verify that path compression is
134 cl_assert_(st
.st_size
< 75000, "path compression not enabled");
136 git_index_free(index
);