]> git.proxmox.com Git - libgit2.git/blame - tests/libgit2/index/cache.c
Merge https://salsa.debian.org/debian/libgit2 into proxmox/bullseye
[libgit2.git] / tests / libgit2 / index / cache.c
CommitLineData
ee4db1c1
CMN
1#include "clar_libgit2.h"
2#include "git2.h"
3#include "index.h"
4#include "tree-cache.h"
5
6static git_repository *g_repo;
7
8void test_index_cache__initialize(void)
9{
10 g_repo = cl_git_sandbox_init("testrepo");
11}
12
13void test_index_cache__cleanup(void)
14{
15 cl_git_sandbox_cleanup();
16 g_repo = NULL;
17}
18
c2f8b215
CMN
19void test_index_cache__write_extension_at_root(void)
20{
21 git_index *index;
22 git_tree *tree;
23 git_oid id;
24 const char *tree_id_str = "45dd856fdd4d89b884c340ba0e047752d9b085d6";
25 const char *index_file = "index-tree";
26
27 cl_git_pass(git_index_open(&index, index_file));
28 cl_assert(index->tree == NULL);
29 cl_git_pass(git_oid_fromstr(&id, tree_id_str));
30 cl_git_pass(git_tree_lookup(&tree, g_repo, &id));
31 cl_git_pass(git_index_read_tree(index, tree));
32 git_tree_free(tree);
33
34 cl_assert(index->tree);
35 cl_git_pass(git_index_write(index));
36 git_index_free(index);
37
38 cl_git_pass(git_index_open(&index, index_file));
39 cl_assert(index->tree);
40
bb0757d5 41 cl_assert_equal_i(git_index_entrycount(index), index->tree->entry_count);
c2f8b215
CMN
42 cl_assert_equal_i(0, index->tree->children_count);
43
44 cl_assert(git_oid_equal(&id, &index->tree->oid));
45
46 cl_git_pass(p_unlink(index_file));
47 git_index_free(index);
48}
49
50void test_index_cache__write_extension_invalidated_root(void)
51{
52 git_index *index;
53 git_tree *tree;
54 git_oid id;
55 const char *tree_id_str = "45dd856fdd4d89b884c340ba0e047752d9b085d6";
56 const char *index_file = "index-tree-invalidated";
57 git_index_entry entry;
58
59 cl_git_pass(git_index_open(&index, index_file));
60 cl_assert(index->tree == NULL);
61 cl_git_pass(git_oid_fromstr(&id, tree_id_str));
62 cl_git_pass(git_tree_lookup(&tree, g_repo, &id));
63 cl_git_pass(git_index_read_tree(index, tree));
64 git_tree_free(tree);
65
66 cl_assert(index->tree);
67
68 memset(&entry, 0x0, sizeof(git_index_entry));
69 git_oid_cpy(&entry.id, &git_index_get_byindex(index, 0)->id);
70 entry.mode = GIT_FILEMODE_BLOB;
71 entry.path = "some-new-file.txt";
72
73 cl_git_pass(git_index_add(index, &entry));
74
75 cl_assert_equal_i(-1, index->tree->entry_count);
76
77 cl_git_pass(git_index_write(index));
78 git_index_free(index);
79
80 cl_git_pass(git_index_open(&index, index_file));
81 cl_assert(index->tree);
82
83 cl_assert_equal_i(-1, index->tree->entry_count);
84 cl_assert_equal_i(0, index->tree->children_count);
85
86 cl_assert(git_oid_cmp(&id, &index->tree->oid));
87
88 cl_git_pass(p_unlink(index_file));
89 git_index_free(index);
90}
91
ee4db1c1
CMN
92void test_index_cache__read_tree_no_children(void)
93{
94 git_index *index;
95 git_index_entry entry;
96 git_tree *tree;
97 git_oid id;
98
99 cl_git_pass(git_index_new(&index));
100 cl_assert(index->tree == NULL);
101 cl_git_pass(git_oid_fromstr(&id, "45dd856fdd4d89b884c340ba0e047752d9b085d6"));
102 cl_git_pass(git_tree_lookup(&tree, g_repo, &id));
103 cl_git_pass(git_index_read_tree(index, tree));
104 git_tree_free(tree);
105
106 cl_assert(index->tree);
107 cl_assert(git_oid_equal(&id, &index->tree->oid));
108 cl_assert_equal_i(0, index->tree->children_count);
bb0757d5 109 cl_assert_equal_i(git_index_entrycount(index), index->tree->entry_count);
ee4db1c1
CMN
110
111 memset(&entry, 0x0, sizeof(git_index_entry));
112 entry.path = "new.txt";
113 entry.mode = GIT_FILEMODE_BLOB;
4afe536b 114 git_oid_fromstr(&entry.id, "d4bcc68acd4410bf836a39f20afb2c2ece09584e");
ee4db1c1
CMN
115
116 cl_git_pass(git_index_add(index, &entry));
c2f8b215 117 cl_assert_equal_i(-1, index->tree->entry_count);
ee4db1c1
CMN
118
119 git_index_free(index);
120}
121
795d8e93
CMN
122void test_index_cache__two_levels(void)
123{
124 git_tree *tree;
125 git_oid tree_id;
126 git_index *index;
127 git_index_entry entry;
128 const git_tree_cache *tree_cache;
129
130 cl_git_pass(git_repository_index(&index, g_repo));
131 cl_git_pass(git_index_clear(index));
132
133 memset(&entry, 0x0, sizeof(entry));
134 entry.mode = GIT_FILEMODE_BLOB;
135 cl_git_pass(git_oid_fromstr(&entry.id, "a8233120f6ad708f843d861ce2b7228ec4e3dec6"));
136 entry.path = "top-level.txt";
137 cl_git_pass(git_index_add(index, &entry));
138
139 entry.path = "subdir/file.txt";
140 cl_git_pass(git_index_add(index, &entry));
141
142 /* the read-tree fills the tree cache */
143 cl_git_pass(git_index_write_tree(&tree_id, index));
144 cl_git_pass(git_tree_lookup(&tree, g_repo, &tree_id));
145 cl_git_pass(git_index_read_tree(index, tree));
146 git_tree_free(tree);
147 cl_git_pass(git_index_write(index));
148
149 /* we now must have cache entries for "" and "subdir" */
150 cl_assert(index->tree);
151 cl_assert(git_tree_cache_get(index->tree, "subdir"));
152
153 cl_git_pass(git_index_read(index, true));
154 /* we must still have cache entries for "" and "subdir", since we wrote it out */
155 cl_assert(index->tree);
156 cl_assert(git_tree_cache_get(index->tree, "subdir"));
157
158 entry.path = "top-level.txt";
159 cl_git_pass(git_oid_fromstr(&entry.id, "3697d64be941a53d4ae8f6a271e4e3fa56b022cc"));
160 cl_git_pass(git_index_add(index, &entry));
161
162 /* writ out the index after we invalidate the root */
163 cl_git_pass(git_index_write(index));
164 cl_git_pass(git_index_read(index, true));
165
166 /* the cache for the subtree must still be valid, even if the root isn't */
167 cl_assert(index->tree);
168 cl_assert_equal_i(-1, index->tree->entry_count);
169 cl_assert_equal_i(1, index->tree->children_count);
170 tree_cache = git_tree_cache_get(index->tree, "subdir");
171 cl_assert(tree_cache);
bb0757d5 172 cl_assert_equal_i(1, tree_cache->entry_count);
cdd71711
ET
173
174 git_index_free(index);
795d8e93
CMN
175}
176
ee4db1c1
CMN
177void test_index_cache__read_tree_children(void)
178{
179 git_index *index;
180 git_index_entry entry;
181 git_tree *tree;
182 const git_tree_cache *cache;
183 git_oid tree_id;
184
185 cl_git_pass(git_repository_index(&index, g_repo));
186 cl_git_pass(git_index_clear(index));
187 cl_assert(index->tree == NULL);
188
189
190 /* add a bunch of entries at different levels */
191 memset(&entry, 0x0, sizeof(git_index_entry));
192 entry.path = "top-level";
193 entry.mode = GIT_FILEMODE_BLOB;
4afe536b 194 git_oid_fromstr(&entry.id, "ee3fa1b8c00aff7fe02065fdb50864bb0d932ccf");
ee4db1c1
CMN
195 cl_git_pass(git_index_add(index, &entry));
196
197
198 entry.path = "subdir/some-file";
199 cl_git_pass(git_index_add(index, &entry));
200
201 entry.path = "subdir/even-deeper/some-file";
202 cl_git_pass(git_index_add(index, &entry));
203
204 entry.path = "subdir2/some-file";
205 cl_git_pass(git_index_add(index, &entry));
206
207 cl_git_pass(git_index_write_tree(&tree_id, index));
208 cl_git_pass(git_index_clear(index));
209 cl_assert(index->tree == NULL);
210
211 cl_git_pass(git_tree_lookup(&tree, g_repo, &tree_id));
212 cl_git_pass(git_index_read_tree(index, tree));
213 git_tree_free(tree);
214
215 cl_assert(index->tree);
216 cl_assert_equal_i(2, index->tree->children_count);
217
218 /* override with a slightly different id, also dummy */
219 entry.path = "subdir/some-file";
4afe536b 220 git_oid_fromstr(&entry.id, "ee3fa1b8c00aff7fe02065fdb50864bb0d932ccf");
ee4db1c1
CMN
221 cl_git_pass(git_index_add(index, &entry));
222
c2f8b215 223 cl_assert_equal_i(-1, index->tree->entry_count);
ee4db1c1
CMN
224
225 cache = git_tree_cache_get(index->tree, "subdir");
226 cl_assert(cache);
c2f8b215 227 cl_assert_equal_i(-1, cache->entry_count);
ee4db1c1
CMN
228
229 cache = git_tree_cache_get(index->tree, "subdir/even-deeper");
230 cl_assert(cache);
bb0757d5 231 cl_assert_equal_i(1, cache->entry_count);
ee4db1c1
CMN
232
233 cache = git_tree_cache_get(index->tree, "subdir2");
234 cl_assert(cache);
bb0757d5 235 cl_assert_equal_i(1, cache->entry_count);
ee4db1c1
CMN
236
237 git_index_free(index);
238}