]> git.proxmox.com Git - libgit2.git/blob - tests/index/version.c
7ada302b5ffc15764041062c395d34793382b439
[libgit2.git] / tests / index / version.c
1 #include "clar_libgit2.h"
2 #include "index.h"
3
4 static git_repository *g_repo = NULL;
5
6 void test_index_version__cleanup(void)
7 {
8 cl_git_sandbox_cleanup();
9 g_repo = NULL;
10 }
11
12 void test_index_version__can_read_v4(void)
13 {
14 const char *paths[] = {
15 "file.tx", "file.txt", "file.txz", "foo", "zzz",
16 };
17 git_index *index;
18 size_t i;
19
20 g_repo = cl_git_sandbox_init("indexv4");
21
22 cl_git_pass(git_repository_index(&index, g_repo));
23 cl_assert_equal_sz(git_index_entrycount(index), 5);
24
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);
28
29 cl_assert(entry != NULL);
30 }
31
32 git_index_free(index);
33 }
34
35 void test_index_version__can_write_v4(void)
36 {
37 const char *paths[] = {
38 "foo",
39 "foox",
40 "foobar",
41 "foobal",
42 "x",
43 "xz",
44 "xyzzyx"
45 };
46 git_index_entry entry;
47 git_index *index;
48 size_t i;
49
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));
53
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));
60 }
61 cl_assert_equal_sz(git_index_entrycount(index), ARRAY_SIZE(paths));
62
63 cl_git_pass(git_index_write(index));
64 git_index_free(index);
65
66 cl_git_pass(git_repository_index(&index, g_repo));
67 cl_assert(git_index_version(index) == 4);
68
69 for (i = 0; i < ARRAY_SIZE(paths); i++) {
70 const git_index_entry *e;
71
72 cl_assert(e = git_index_get_bypath(index, paths[i], 0));
73 cl_assert_equal_s(paths[i], e->path);
74 }
75
76 git_index_free(index);
77 }
78
79 void test_index_version__v4_uses_path_compression(void)
80 {
81 git_index_entry entry;
82 git_index *index;
83 char path[250], buf[1];
84 struct stat st;
85 char i, j;
86
87 memset(path, 'a', sizeof(path));
88 memset(buf, 'a', sizeof(buf));
89
90 memset(&entry, 0, sizeof(entry));
91 entry.path = path;
92 entry.mode = GIT_FILEMODE_BLOB;
93
94 g_repo = cl_git_sandbox_init("indexv4");
95 cl_git_pass(git_repository_index(&index, g_repo));
96
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)));
104 }
105 }
106
107 cl_git_pass(git_index_write(index));
108 cl_git_pass(p_stat(git_index_path(index), &st));
109
110 /*
111 * Without path compression, the written paths would at
112 * least take
113 *
114 * (entries * pathlen) = len
115 * (676 * 250) = 169000
116 *
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.
120 *
121 * (1 * pathlen) + (675 * 2) = len
122 * 676 + 1350 = 2026
123 *
124 * bytes.
125 *
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
132 * used.
133 */
134 cl_assert_(st.st_size < 75000, "path compression not enabled");
135
136 git_index_free(index);
137 }