sparse-checkout: disable sparse-index
[alt-git.git] / sparse-index.c
blob4c73772c6d6c8f25a5b3c89d47b47b4da4b78a80
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;
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 static int set_index_sparse_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 = 1;
116 return res;
119 int set_sparse_index_config(struct repository *repo, int enable)
121 int res = set_index_sparse_config(repo, enable);
123 prepare_repo_settings(repo);
124 repo->settings.sparse_index = enable;
125 return res;
128 int convert_to_sparse(struct index_state *istate)
130 int test_env;
131 if (istate->split_index || istate->sparse_index ||
132 !core_apply_sparse_checkout || !core_sparse_checkout_cone)
133 return 0;
135 if (!istate->repo)
136 istate->repo = the_repository;
139 * The GIT_TEST_SPARSE_INDEX environment variable triggers the
140 * index.sparse config variable to be on.
142 test_env = git_env_bool("GIT_TEST_SPARSE_INDEX", -1);
143 if (test_env >= 0)
144 set_sparse_index_config(istate->repo, test_env);
147 * Only convert to sparse if index.sparse is set.
149 prepare_repo_settings(istate->repo);
150 if (!istate->repo->settings.sparse_index)
151 return 0;
153 if (!istate->sparse_checkout_patterns) {
154 istate->sparse_checkout_patterns = xcalloc(1, sizeof(struct pattern_list));
155 if (get_sparse_checkout_patterns(istate->sparse_checkout_patterns) < 0)
156 return 0;
159 if (!istate->sparse_checkout_patterns->use_cone_patterns) {
160 warning(_("attempting to use sparse-index without cone mode"));
161 return -1;
164 if (cache_tree_update(istate, 0)) {
165 warning(_("unable to update cache-tree, staying full"));
166 return -1;
169 remove_fsmonitor(istate);
171 trace2_region_enter("index", "convert_to_sparse", istate->repo);
172 istate->cache_nr = convert_to_sparse_rec(istate,
173 0, 0, istate->cache_nr,
174 "", 0, istate->cache_tree);
175 istate->drop_cache_tree = 1;
176 istate->sparse_index = 1;
177 trace2_region_leave("index", "convert_to_sparse", istate->repo);
178 return 0;
181 static void set_index_entry(struct index_state *istate, int nr, struct cache_entry *ce)
183 ALLOC_GROW(istate->cache, nr + 1, istate->cache_alloc);
185 istate->cache[nr] = ce;
186 add_name_hash(istate, ce);
189 static int add_path_to_index(const struct object_id *oid,
190 struct strbuf *base, const char *path,
191 unsigned int mode, void *context)
193 struct index_state *istate = (struct index_state *)context;
194 struct cache_entry *ce;
195 size_t len = base->len;
197 if (S_ISDIR(mode))
198 return READ_TREE_RECURSIVE;
200 strbuf_addstr(base, path);
202 ce = make_cache_entry(istate, mode, oid, base->buf, 0, 0);
203 ce->ce_flags |= CE_SKIP_WORKTREE;
204 set_index_entry(istate, istate->cache_nr++, ce);
206 strbuf_setlen(base, len);
207 return 0;
210 void ensure_full_index(struct index_state *istate)
212 int i;
213 struct index_state *full;
214 struct strbuf base = STRBUF_INIT;
216 if (!istate || !istate->sparse_index)
217 return;
219 if (!istate->repo)
220 istate->repo = the_repository;
222 trace2_region_enter("index", "ensure_full_index", istate->repo);
224 /* initialize basics of new index */
225 full = xcalloc(1, sizeof(struct index_state));
226 memcpy(full, istate, sizeof(struct index_state));
228 /* then change the necessary things */
229 full->sparse_index = 0;
230 full->cache_alloc = (3 * istate->cache_alloc) / 2;
231 full->cache_nr = 0;
232 ALLOC_ARRAY(full->cache, full->cache_alloc);
234 for (i = 0; i < istate->cache_nr; i++) {
235 struct cache_entry *ce = istate->cache[i];
236 struct tree *tree;
237 struct pathspec ps;
239 if (!S_ISSPARSEDIR(ce->ce_mode)) {
240 set_index_entry(full, full->cache_nr++, ce);
241 continue;
243 if (!(ce->ce_flags & CE_SKIP_WORKTREE))
244 warning(_("index entry is a directory, but not sparse (%08x)"),
245 ce->ce_flags);
247 /* recursively walk into cd->name */
248 tree = lookup_tree(istate->repo, &ce->oid);
250 memset(&ps, 0, sizeof(ps));
251 ps.recursive = 1;
252 ps.has_wildcard = 1;
253 ps.max_depth = -1;
255 strbuf_setlen(&base, 0);
256 strbuf_add(&base, ce->name, strlen(ce->name));
258 read_tree_at(istate->repo, tree, &base, &ps,
259 add_path_to_index, full);
261 /* free directory entries. full entries are re-used */
262 discard_cache_entry(ce);
265 /* Copy back into original index. */
266 memcpy(&istate->name_hash, &full->name_hash, sizeof(full->name_hash));
267 istate->sparse_index = 0;
268 free(istate->cache);
269 istate->cache = full->cache;
270 istate->cache_nr = full->cache_nr;
271 istate->cache_alloc = full->cache_alloc;
273 strbuf_release(&base);
274 free(full);
276 trace2_region_leave("index", "ensure_full_index", istate->repo);