status: use sparse-index throughout
[git/debian.git] / sparse-index.c
blobef53bd2198b479b82e0f3c415e1faf8c9249a34d
1 #include "cache.h"
2 #include "repository.h"
3 #include "sparse-index.h"
4 #include "tree.h"
5 #include "pathspec.h"
6 #include "trace2.h"
7 #include "cache-tree.h"
8 #include "config.h"
9 #include "dir.h"
10 #include "fsmonitor.h"
12 static struct cache_entry *construct_sparse_dir_entry(
13 struct index_state *istate,
14 const char *sparse_dir,
15 struct cache_tree *tree)
17 struct cache_entry *de;
19 de = make_cache_entry(istate, S_IFDIR, &tree->oid, sparse_dir, 0, 0);
21 de->ce_flags |= CE_SKIP_WORKTREE;
22 return de;
26 * Returns the number of entries "inserted" into the index.
28 static int convert_to_sparse_rec(struct index_state *istate,
29 int num_converted,
30 int start, int end,
31 const char *ct_path, size_t ct_pathlen,
32 struct cache_tree *ct)
34 int i, can_convert = 1;
35 int start_converted = num_converted;
36 enum pattern_match_result match;
37 int dtype = DT_UNKNOWN;
38 struct strbuf child_path = STRBUF_INIT;
39 struct pattern_list *pl = istate->sparse_checkout_patterns;
42 * Is the current path outside of the sparse cone?
43 * Then check if the region can be replaced by a sparse
44 * directory entry (everything is sparse and merged).
46 match = path_matches_pattern_list(ct_path, ct_pathlen,
47 NULL, &dtype, pl, istate);
48 if (match != NOT_MATCHED)
49 can_convert = 0;
51 for (i = start; can_convert && i < end; i++) {
52 struct cache_entry *ce = istate->cache[i];
54 if (ce_stage(ce) ||
55 S_ISGITLINK(ce->ce_mode) ||
56 !(ce->ce_flags & CE_SKIP_WORKTREE))
57 can_convert = 0;
60 if (can_convert) {
61 struct cache_entry *se;
62 se = construct_sparse_dir_entry(istate, ct_path, ct);
64 istate->cache[num_converted++] = se;
65 return 1;
68 for (i = start; i < end; ) {
69 int count, span, pos = -1;
70 const char *base, *slash;
71 struct cache_entry *ce = istate->cache[i];
74 * Detect if this is a normal entry outside of any subtree
75 * entry.
77 base = ce->name + ct_pathlen;
78 slash = strchr(base, '/');
80 if (slash)
81 pos = cache_tree_subtree_pos(ct, base, slash - base);
83 if (pos < 0) {
84 istate->cache[num_converted++] = ce;
85 i++;
86 continue;
89 strbuf_setlen(&child_path, 0);
90 strbuf_add(&child_path, ce->name, slash - ce->name + 1);
92 span = ct->down[pos]->cache_tree->entry_count;
93 count = convert_to_sparse_rec(istate,
94 num_converted, i, i + span,
95 child_path.buf, child_path.len,
96 ct->down[pos]->cache_tree);
97 num_converted += count;
98 i += span;
101 strbuf_release(&child_path);
102 return num_converted - start_converted;
105 int set_sparse_index_config(struct repository *repo, int enable)
107 int res;
108 char *config_path = repo_git_path(repo, "config.worktree");
109 res = git_config_set_in_file_gently(config_path,
110 "index.sparse",
111 enable ? "true" : NULL);
112 free(config_path);
114 prepare_repo_settings(repo);
115 repo->settings.sparse_index = enable;
116 return res;
119 static int index_has_unmerged_entries(struct index_state *istate)
121 int i;
122 for (i = 0; i < istate->cache_nr; i++) {
123 if (ce_stage(istate->cache[i]))
124 return 1;
127 return 0;
130 int convert_to_sparse(struct index_state *istate)
132 int test_env;
133 if (istate->split_index || istate->sparse_index ||
134 !core_apply_sparse_checkout || !core_sparse_checkout_cone)
135 return 0;
137 if (!istate->repo)
138 istate->repo = the_repository;
141 * The GIT_TEST_SPARSE_INDEX environment variable triggers the
142 * index.sparse config variable to be on.
144 test_env = git_env_bool("GIT_TEST_SPARSE_INDEX", -1);
145 if (test_env >= 0)
146 set_sparse_index_config(istate->repo, test_env);
149 * Only convert to sparse if index.sparse is set.
151 prepare_repo_settings(istate->repo);
152 if (!istate->repo->settings.sparse_index)
153 return 0;
155 if (!istate->sparse_checkout_patterns) {
156 istate->sparse_checkout_patterns = xcalloc(1, sizeof(struct pattern_list));
157 if (get_sparse_checkout_patterns(istate->sparse_checkout_patterns) < 0)
158 return 0;
161 if (!istate->sparse_checkout_patterns->use_cone_patterns) {
162 warning(_("attempting to use sparse-index without cone mode"));
163 return -1;
167 * NEEDSWORK: If we have unmerged entries, then stay full.
168 * Unmerged entries prevent the cache-tree extension from working.
170 if (index_has_unmerged_entries(istate))
171 return 0;
173 if (cache_tree_update(istate, 0)) {
174 warning(_("unable to update cache-tree, staying full"));
175 return -1;
178 remove_fsmonitor(istate);
180 trace2_region_enter("index", "convert_to_sparse", istate->repo);
181 istate->cache_nr = convert_to_sparse_rec(istate,
182 0, 0, istate->cache_nr,
183 "", 0, istate->cache_tree);
185 /* Clear and recompute the cache-tree */
186 cache_tree_free(&istate->cache_tree);
187 cache_tree_update(istate, 0);
189 istate->sparse_index = 1;
190 trace2_region_leave("index", "convert_to_sparse", istate->repo);
191 return 0;
194 static void set_index_entry(struct index_state *istate, int nr, struct cache_entry *ce)
196 ALLOC_GROW(istate->cache, nr + 1, istate->cache_alloc);
198 istate->cache[nr] = ce;
199 add_name_hash(istate, ce);
202 static int add_path_to_index(const struct object_id *oid,
203 struct strbuf *base, const char *path,
204 unsigned int mode, void *context)
206 struct index_state *istate = (struct index_state *)context;
207 struct cache_entry *ce;
208 size_t len = base->len;
210 if (S_ISDIR(mode))
211 return READ_TREE_RECURSIVE;
213 strbuf_addstr(base, path);
215 ce = make_cache_entry(istate, mode, oid, base->buf, 0, 0);
216 ce->ce_flags |= CE_SKIP_WORKTREE | CE_EXTENDED;
217 set_index_entry(istate, istate->cache_nr++, ce);
219 strbuf_setlen(base, len);
220 return 0;
223 void ensure_full_index(struct index_state *istate)
225 int i;
226 struct index_state *full;
227 struct strbuf base = STRBUF_INIT;
229 if (!istate || !istate->sparse_index)
230 return;
232 if (!istate->repo)
233 istate->repo = the_repository;
235 trace2_region_enter("index", "ensure_full_index", istate->repo);
237 /* initialize basics of new index */
238 full = xcalloc(1, sizeof(struct index_state));
239 memcpy(full, istate, sizeof(struct index_state));
241 /* then change the necessary things */
242 full->sparse_index = 0;
243 full->cache_alloc = (3 * istate->cache_alloc) / 2;
244 full->cache_nr = 0;
245 ALLOC_ARRAY(full->cache, full->cache_alloc);
247 for (i = 0; i < istate->cache_nr; i++) {
248 struct cache_entry *ce = istate->cache[i];
249 struct tree *tree;
250 struct pathspec ps;
252 if (!S_ISSPARSEDIR(ce->ce_mode)) {
253 set_index_entry(full, full->cache_nr++, ce);
254 continue;
256 if (!(ce->ce_flags & CE_SKIP_WORKTREE))
257 warning(_("index entry is a directory, but not sparse (%08x)"),
258 ce->ce_flags);
260 /* recursively walk into cd->name */
261 tree = lookup_tree(istate->repo, &ce->oid);
263 memset(&ps, 0, sizeof(ps));
264 ps.recursive = 1;
265 ps.has_wildcard = 1;
266 ps.max_depth = -1;
268 strbuf_setlen(&base, 0);
269 strbuf_add(&base, ce->name, strlen(ce->name));
271 read_tree_at(istate->repo, tree, &base, &ps,
272 add_path_to_index, full);
274 /* free directory entries. full entries are re-used */
275 discard_cache_entry(ce);
278 /* Copy back into original index. */
279 memcpy(&istate->name_hash, &full->name_hash, sizeof(full->name_hash));
280 istate->sparse_index = 0;
281 free(istate->cache);
282 istate->cache = full->cache;
283 istate->cache_nr = full->cache_nr;
284 istate->cache_alloc = full->cache_alloc;
286 strbuf_release(&base);
287 free(full);
289 /* Clear and recompute the cache-tree */
290 cache_tree_free(&istate->cache_tree);
291 cache_tree_update(istate, 0);
293 trace2_region_leave("index", "ensure_full_index", istate->repo);
297 * This static global helps avoid infinite recursion between
298 * expand_to_path() and index_file_exists().
300 static int in_expand_to_path = 0;
302 void expand_to_path(struct index_state *istate,
303 const char *path, size_t pathlen, int icase)
305 struct strbuf path_mutable = STRBUF_INIT;
306 size_t substr_len;
308 /* prevent extra recursion */
309 if (in_expand_to_path)
310 return;
312 if (!istate || !istate->sparse_index)
313 return;
315 if (!istate->repo)
316 istate->repo = the_repository;
318 in_expand_to_path = 1;
321 * We only need to actually expand a region if the
322 * following are both true:
324 * 1. 'path' is not already in the index.
325 * 2. Some parent directory of 'path' is a sparse directory.
328 if (index_file_exists(istate, path, pathlen, icase))
329 goto cleanup;
331 strbuf_add(&path_mutable, path, pathlen);
332 strbuf_addch(&path_mutable, '/');
334 /* Check the name hash for all parent directories */
335 substr_len = 0;
336 while (substr_len < pathlen) {
337 char temp;
338 char *replace = strchr(path_mutable.buf + substr_len, '/');
340 if (!replace)
341 break;
343 /* replace the character _after_ the slash */
344 replace++;
345 temp = *replace;
346 *replace = '\0';
347 if (index_file_exists(istate, path_mutable.buf,
348 path_mutable.len, icase)) {
350 * We found a parent directory in the name-hash
351 * hashtable, because only sparse directory entries
352 * have a trailing '/' character. Since "path" wasn't
353 * in the index, perhaps it exists within this
354 * sparse-directory. Expand accordingly.
356 ensure_full_index(istate);
357 break;
360 *replace = temp;
361 substr_len = replace - path_mutable.buf;
364 cleanup:
365 strbuf_release(&path_mutable);
366 in_expand_to_path = 0;