2 #include "repository.h"
3 #include "sparse-index.h"
7 #include "cache-tree.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
;
26 * Returns the number of entries "inserted" into the index.
28 static int convert_to_sparse_rec(struct index_state
*istate
,
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 struct strbuf child_path
= STRBUF_INIT
;
39 * Is the current path outside of the sparse cone?
40 * Then check if the region can be replaced by a sparse
41 * directory entry (everything is sparse and merged).
43 if (path_in_sparse_checkout(ct_path
, istate
))
46 for (i
= start
; can_convert
&& i
< end
; i
++) {
47 struct cache_entry
*ce
= istate
->cache
[i
];
50 S_ISGITLINK(ce
->ce_mode
) ||
51 !(ce
->ce_flags
& CE_SKIP_WORKTREE
))
56 struct cache_entry
*se
;
57 se
= construct_sparse_dir_entry(istate
, ct_path
, ct
);
59 istate
->cache
[num_converted
++] = se
;
63 for (i
= start
; i
< end
; ) {
64 int count
, span
, pos
= -1;
65 const char *base
, *slash
;
66 struct cache_entry
*ce
= istate
->cache
[i
];
69 * Detect if this is a normal entry outside of any subtree
72 base
= ce
->name
+ ct_pathlen
;
73 slash
= strchr(base
, '/');
76 pos
= cache_tree_subtree_pos(ct
, base
, slash
- base
);
79 istate
->cache
[num_converted
++] = ce
;
84 strbuf_setlen(&child_path
, 0);
85 strbuf_add(&child_path
, ce
->name
, slash
- ce
->name
+ 1);
87 span
= ct
->down
[pos
]->cache_tree
->entry_count
;
88 count
= convert_to_sparse_rec(istate
,
89 num_converted
, i
, i
+ span
,
90 child_path
.buf
, child_path
.len
,
91 ct
->down
[pos
]->cache_tree
);
92 num_converted
+= count
;
96 strbuf_release(&child_path
);
97 return num_converted
- start_converted
;
100 int set_sparse_index_config(struct repository
*repo
, int enable
)
103 char *config_path
= repo_git_path(repo
, "config.worktree");
104 res
= git_config_set_in_file_gently(config_path
,
106 enable
? "true" : NULL
);
109 prepare_repo_settings(repo
);
110 repo
->settings
.sparse_index
= enable
;
114 static int index_has_unmerged_entries(struct index_state
*istate
)
117 for (i
= 0; i
< istate
->cache_nr
; i
++) {
118 if (ce_stage(istate
->cache
[i
]))
125 int convert_to_sparse(struct index_state
*istate
, int flags
)
128 if (istate
->sparse_index
|| !istate
->cache_nr
||
129 !core_apply_sparse_checkout
|| !core_sparse_checkout_cone
)
133 istate
->repo
= the_repository
;
135 if (!(flags
& SPARSE_INDEX_MEMORY_ONLY
)) {
137 * The sparse index is not (yet) integrated with a split index.
139 if (istate
->split_index
)
142 * The GIT_TEST_SPARSE_INDEX environment variable triggers the
143 * index.sparse config variable to be on.
145 test_env
= git_env_bool("GIT_TEST_SPARSE_INDEX", -1);
147 set_sparse_index_config(istate
->repo
, test_env
);
150 * Only convert to sparse if index.sparse is set.
152 prepare_repo_settings(istate
->repo
);
153 if (!istate
->repo
->settings
.sparse_index
)
157 if (init_sparse_checkout_patterns(istate
))
161 * We need cone-mode patterns to use sparse-index. If a user edits
162 * their sparse-checkout file manually, then we can detect during
163 * parsing that they are not actually using cone-mode patterns and
164 * hence we need to abort this conversion _without error_. Warnings
165 * already exist in the pattern parsing to inform the user of their
168 if (!istate
->sparse_checkout_patterns
->use_cone_patterns
)
172 * NEEDSWORK: If we have unmerged entries, then stay full.
173 * Unmerged entries prevent the cache-tree extension from working.
175 if (index_has_unmerged_entries(istate
))
178 /* Clear and recompute the cache-tree */
179 cache_tree_free(&istate
->cache_tree
);
181 * Silently return if there is a problem with the cache tree update,
182 * which might just be due to a conflict state in some entry.
184 * This might create new tree objects, so be sure to use
185 * WRITE_TREE_MISSING_OK.
187 if (cache_tree_update(istate
, WRITE_TREE_MISSING_OK
))
190 remove_fsmonitor(istate
);
192 trace2_region_enter("index", "convert_to_sparse", istate
->repo
);
193 istate
->cache_nr
= convert_to_sparse_rec(istate
,
194 0, 0, istate
->cache_nr
,
195 "", 0, istate
->cache_tree
);
197 /* Clear and recompute the cache-tree */
198 cache_tree_free(&istate
->cache_tree
);
199 cache_tree_update(istate
, 0);
201 istate
->fsmonitor_has_run_once
= 0;
202 FREE_AND_NULL(istate
->fsmonitor_dirty
);
203 FREE_AND_NULL(istate
->fsmonitor_last_update
);
205 istate
->sparse_index
= 1;
206 trace2_region_leave("index", "convert_to_sparse", istate
->repo
);
210 static void set_index_entry(struct index_state
*istate
, int nr
, struct cache_entry
*ce
)
212 ALLOC_GROW(istate
->cache
, nr
+ 1, istate
->cache_alloc
);
214 istate
->cache
[nr
] = ce
;
215 add_name_hash(istate
, ce
);
218 static int add_path_to_index(const struct object_id
*oid
,
219 struct strbuf
*base
, const char *path
,
220 unsigned int mode
, void *context
)
222 struct index_state
*istate
= (struct index_state
*)context
;
223 struct cache_entry
*ce
;
224 size_t len
= base
->len
;
227 return READ_TREE_RECURSIVE
;
229 strbuf_addstr(base
, path
);
231 ce
= make_cache_entry(istate
, mode
, oid
, base
->buf
, 0, 0);
232 ce
->ce_flags
|= CE_SKIP_WORKTREE
| CE_EXTENDED
;
233 set_index_entry(istate
, istate
->cache_nr
++, ce
);
235 strbuf_setlen(base
, len
);
239 void ensure_full_index(struct index_state
*istate
)
242 struct index_state
*full
;
243 struct strbuf base
= STRBUF_INIT
;
245 if (!istate
|| !istate
->sparse_index
)
249 istate
->repo
= the_repository
;
251 trace2_region_enter("index", "ensure_full_index", istate
->repo
);
253 /* initialize basics of new index */
254 full
= xcalloc(1, sizeof(struct index_state
));
255 memcpy(full
, istate
, sizeof(struct index_state
));
257 /* then change the necessary things */
258 full
->sparse_index
= 0;
259 full
->cache_alloc
= (3 * istate
->cache_alloc
) / 2;
261 ALLOC_ARRAY(full
->cache
, full
->cache_alloc
);
263 for (i
= 0; i
< istate
->cache_nr
; i
++) {
264 struct cache_entry
*ce
= istate
->cache
[i
];
268 if (!S_ISSPARSEDIR(ce
->ce_mode
)) {
269 set_index_entry(full
, full
->cache_nr
++, ce
);
272 if (!(ce
->ce_flags
& CE_SKIP_WORKTREE
))
273 warning(_("index entry is a directory, but not sparse (%08x)"),
276 /* recursively walk into cd->name */
277 tree
= lookup_tree(istate
->repo
, &ce
->oid
);
279 memset(&ps
, 0, sizeof(ps
));
284 strbuf_setlen(&base
, 0);
285 strbuf_add(&base
, ce
->name
, strlen(ce
->name
));
287 read_tree_at(istate
->repo
, tree
, &base
, &ps
,
288 add_path_to_index
, full
);
290 /* free directory entries. full entries are re-used */
291 discard_cache_entry(ce
);
294 /* Copy back into original index. */
295 memcpy(&istate
->name_hash
, &full
->name_hash
, sizeof(full
->name_hash
));
296 istate
->sparse_index
= 0;
298 istate
->cache
= full
->cache
;
299 istate
->cache_nr
= full
->cache_nr
;
300 istate
->cache_alloc
= full
->cache_alloc
;
301 istate
->fsmonitor_has_run_once
= 0;
302 FREE_AND_NULL(istate
->fsmonitor_dirty
);
303 FREE_AND_NULL(istate
->fsmonitor_last_update
);
305 strbuf_release(&base
);
308 /* Clear and recompute the cache-tree */
309 cache_tree_free(&istate
->cache_tree
);
310 cache_tree_update(istate
, 0);
312 trace2_region_leave("index", "ensure_full_index", istate
->repo
);
316 * This static global helps avoid infinite recursion between
317 * expand_to_path() and index_file_exists().
319 static int in_expand_to_path
= 0;
321 void expand_to_path(struct index_state
*istate
,
322 const char *path
, size_t pathlen
, int icase
)
324 struct strbuf path_mutable
= STRBUF_INIT
;
327 /* prevent extra recursion */
328 if (in_expand_to_path
)
331 if (!istate
|| !istate
->sparse_index
)
335 istate
->repo
= the_repository
;
337 in_expand_to_path
= 1;
340 * We only need to actually expand a region if the
341 * following are both true:
343 * 1. 'path' is not already in the index.
344 * 2. Some parent directory of 'path' is a sparse directory.
347 if (index_file_exists(istate
, path
, pathlen
, icase
))
350 strbuf_add(&path_mutable
, path
, pathlen
);
351 strbuf_addch(&path_mutable
, '/');
353 /* Check the name hash for all parent directories */
355 while (substr_len
< pathlen
) {
357 char *replace
= strchr(path_mutable
.buf
+ substr_len
, '/');
362 /* replace the character _after_ the slash */
366 if (index_file_exists(istate
, path_mutable
.buf
,
367 path_mutable
.len
, icase
)) {
369 * We found a parent directory in the name-hash
370 * hashtable, because only sparse directory entries
371 * have a trailing '/' character. Since "path" wasn't
372 * in the index, perhaps it exists within this
373 * sparse-directory. Expand accordingly.
375 ensure_full_index(istate
);
380 substr_len
= replace
- path_mutable
.buf
;
384 strbuf_release(&path_mutable
);
385 in_expand_to_path
= 0;