fsmonitor: integrate with sparse index
[git] / sparse-index.c
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"
11
12 static struct cache_entry *construct_sparse_dir_entry(
13                                 struct index_state *istate,
14                                 const char *sparse_dir,
15                                 struct cache_tree *tree)
16 {
17         struct cache_entry *de;
18
19         de = make_cache_entry(istate, S_IFDIR, &tree->oid, sparse_dir, 0, 0);
20
21         de->ce_flags |= CE_SKIP_WORKTREE;
22         return de;
23 }
24
25 /*
26  * Returns the number of entries "inserted" into the index.
27  */
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)
33 {
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;
40
41         /*
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).
45          */
46         match = path_matches_pattern_list(ct_path, ct_pathlen,
47                                           NULL, &dtype, pl, istate);
48         if (match != NOT_MATCHED)
49                 can_convert = 0;
50
51         for (i = start; can_convert && i < end; i++) {
52                 struct cache_entry *ce = istate->cache[i];
53
54                 if (ce_stage(ce) ||
55                     S_ISGITLINK(ce->ce_mode) ||
56                     !(ce->ce_flags & CE_SKIP_WORKTREE))
57                         can_convert = 0;
58         }
59
60         if (can_convert) {
61                 struct cache_entry *se;
62                 se = construct_sparse_dir_entry(istate, ct_path, ct);
63
64                 istate->cache[num_converted++] = se;
65                 return 1;
66         }
67
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];
72
73                 /*
74                  * Detect if this is a normal entry outside of any subtree
75                  * entry.
76                  */
77                 base = ce->name + ct_pathlen;
78                 slash = strchr(base, '/');
79
80                 if (slash)
81                         pos = cache_tree_subtree_pos(ct, base, slash - base);
82
83                 if (pos < 0) {
84                         istate->cache[num_converted++] = ce;
85                         i++;
86                         continue;
87                 }
88
89                 strbuf_setlen(&child_path, 0);
90                 strbuf_add(&child_path, ce->name, slash - ce->name + 1);
91
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;
99         }
100
101         strbuf_release(&child_path);
102         return num_converted - start_converted;
103 }
104
105 static int set_index_sparse_config(struct repository *repo, int enable)
106 {
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);
113
114         prepare_repo_settings(repo);
115         repo->settings.sparse_index = 1;
116         return res;
117 }
118
119 int set_sparse_index_config(struct repository *repo, int enable)
120 {
121         int res = set_index_sparse_config(repo, enable);
122
123         prepare_repo_settings(repo);
124         repo->settings.sparse_index = enable;
125         return res;
126 }
127
128 static int index_has_unmerged_entries(struct index_state *istate)
129 {
130         int i;
131         for (i = 0; i < istate->cache_nr; i++) {
132                 if (ce_stage(istate->cache[i]))
133                         return 1;
134         }
135
136         return 0;
137 }
138
139 int convert_to_sparse(struct index_state *istate)
140 {
141         int test_env;
142         if (istate->split_index || istate->sparse_index ||
143             !core_apply_sparse_checkout || !core_sparse_checkout_cone)
144                 return 0;
145
146         if (!istate->repo)
147                 istate->repo = the_repository;
148
149         /*
150          * The GIT_TEST_SPARSE_INDEX environment variable triggers the
151          * index.sparse config variable to be on.
152          */
153         test_env = git_env_bool("GIT_TEST_SPARSE_INDEX", -1);
154         if (test_env >= 0)
155                 set_sparse_index_config(istate->repo, test_env);
156
157         /*
158          * Only convert to sparse if index.sparse is set.
159          */
160         prepare_repo_settings(istate->repo);
161         if (!istate->repo->settings.sparse_index)
162                 return 0;
163
164         if (!istate->sparse_checkout_patterns) {
165                 istate->sparse_checkout_patterns = xcalloc(1, sizeof(struct pattern_list));
166                 if (get_sparse_checkout_patterns(istate->sparse_checkout_patterns) < 0)
167                         return 0;
168         }
169
170         if (!istate->sparse_checkout_patterns->use_cone_patterns) {
171                 warning(_("attempting to use sparse-index without cone mode"));
172                 return -1;
173         }
174
175         /*
176          * NEEDSWORK: If we have unmerged entries, then stay full.
177          * Unmerged entries prevent the cache-tree extension from working.
178          */
179         if (index_has_unmerged_entries(istate))
180                 return 0;
181
182         if (cache_tree_update(istate, 0)) {
183                 warning(_("unable to update cache-tree, staying full"));
184                 return -1;
185         }
186
187         remove_fsmonitor(istate);
188
189         trace2_region_enter("index", "convert_to_sparse", istate->repo);
190         istate->cache_nr = convert_to_sparse_rec(istate,
191                                                  0, 0, istate->cache_nr,
192                                                  "", 0, istate->cache_tree);
193
194         /* Clear and recompute the cache-tree */
195         cache_tree_free(&istate->cache_tree);
196         cache_tree_update(istate, 0);
197
198         istate->fsmonitor_has_run_once = 0;
199         FREE_AND_NULL(istate->fsmonitor_dirty);
200         FREE_AND_NULL(istate->fsmonitor_last_update);
201
202         istate->sparse_index = 1;
203         trace2_region_leave("index", "convert_to_sparse", istate->repo);
204         return 0;
205 }
206
207 static void set_index_entry(struct index_state *istate, int nr, struct cache_entry *ce)
208 {
209         ALLOC_GROW(istate->cache, nr + 1, istate->cache_alloc);
210
211         istate->cache[nr] = ce;
212         add_name_hash(istate, ce);
213 }
214
215 static int add_path_to_index(const struct object_id *oid,
216                              struct strbuf *base, const char *path,
217                              unsigned int mode, void *context)
218 {
219         struct index_state *istate = (struct index_state *)context;
220         struct cache_entry *ce;
221         size_t len = base->len;
222
223         if (S_ISDIR(mode))
224                 return READ_TREE_RECURSIVE;
225
226         strbuf_addstr(base, path);
227
228         ce = make_cache_entry(istate, mode, oid, base->buf, 0, 0);
229         ce->ce_flags |= CE_SKIP_WORKTREE | CE_EXTENDED;
230         set_index_entry(istate, istate->cache_nr++, ce);
231
232         strbuf_setlen(base, len);
233         return 0;
234 }
235
236 void ensure_full_index(struct index_state *istate)
237 {
238         int i;
239         struct index_state *full;
240         struct strbuf base = STRBUF_INIT;
241
242         if (!istate || !istate->sparse_index)
243                 return;
244
245         if (!istate->repo)
246                 istate->repo = the_repository;
247
248         trace2_region_enter("index", "ensure_full_index", istate->repo);
249
250         /* initialize basics of new index */
251         full = xcalloc(1, sizeof(struct index_state));
252         memcpy(full, istate, sizeof(struct index_state));
253
254         /* then change the necessary things */
255         full->sparse_index = 0;
256         full->cache_alloc = (3 * istate->cache_alloc) / 2;
257         full->cache_nr = 0;
258         ALLOC_ARRAY(full->cache, full->cache_alloc);
259
260         for (i = 0; i < istate->cache_nr; i++) {
261                 struct cache_entry *ce = istate->cache[i];
262                 struct tree *tree;
263                 struct pathspec ps;
264
265                 if (!S_ISSPARSEDIR(ce->ce_mode)) {
266                         set_index_entry(full, full->cache_nr++, ce);
267                         continue;
268                 }
269                 if (!(ce->ce_flags & CE_SKIP_WORKTREE))
270                         warning(_("index entry is a directory, but not sparse (%08x)"),
271                                 ce->ce_flags);
272
273                 /* recursively walk into cd->name */
274                 tree = lookup_tree(istate->repo, &ce->oid);
275
276                 memset(&ps, 0, sizeof(ps));
277                 ps.recursive = 1;
278                 ps.has_wildcard = 1;
279                 ps.max_depth = -1;
280
281                 strbuf_setlen(&base, 0);
282                 strbuf_add(&base, ce->name, strlen(ce->name));
283
284                 read_tree_at(istate->repo, tree, &base, &ps,
285                              add_path_to_index, full);
286
287                 /* free directory entries. full entries are re-used */
288                 discard_cache_entry(ce);
289         }
290
291         /* Copy back into original index. */
292         memcpy(&istate->name_hash, &full->name_hash, sizeof(full->name_hash));
293         istate->sparse_index = 0;
294         free(istate->cache);
295         istate->cache = full->cache;
296         istate->cache_nr = full->cache_nr;
297         istate->cache_alloc = full->cache_alloc;
298         istate->fsmonitor_has_run_once = 0;
299         FREE_AND_NULL(istate->fsmonitor_dirty);
300         FREE_AND_NULL(istate->fsmonitor_last_update);
301
302         strbuf_release(&base);
303         free(full);
304
305         /* Clear and recompute the cache-tree */
306         cache_tree_free(&istate->cache_tree);
307         cache_tree_update(istate, 0);
308
309         trace2_region_leave("index", "ensure_full_index", istate->repo);
310 }
311
312 /*
313  * This static global helps avoid infinite recursion between
314  * expand_to_path() and index_file_exists().
315  */
316 static int in_expand_to_path = 0;
317
318 void expand_to_path(struct index_state *istate,
319                     const char *path, size_t pathlen, int icase)
320 {
321         struct strbuf path_mutable = STRBUF_INIT;
322         size_t substr_len;
323
324         /* prevent extra recursion */
325         if (in_expand_to_path)
326                 return;
327
328         if (!istate || !istate->sparse_index)
329                 return;
330
331         if (!istate->repo)
332                 istate->repo = the_repository;
333
334         in_expand_to_path = 1;
335
336         /*
337          * We only need to actually expand a region if the
338          * following are both true:
339          *
340          * 1. 'path' is not already in the index.
341          * 2. Some parent directory of 'path' is a sparse directory.
342          */
343
344         if (index_file_exists(istate, path, pathlen, icase))
345                 goto cleanup;
346
347         strbuf_add(&path_mutable, path, pathlen);
348         strbuf_addch(&path_mutable, '/');
349
350         /* Check the name hash for all parent directories */
351         substr_len = 0;
352         while (substr_len < pathlen) {
353                 char temp;
354                 char *replace = strchr(path_mutable.buf + substr_len, '/');
355
356                 if (!replace)
357                         break;
358
359                 /* replace the character _after_ the slash */
360                 replace++;
361                 temp = *replace;
362                 *replace = '\0';
363                 if (index_file_exists(istate, path_mutable.buf,
364                                       path_mutable.len, icase)) {
365                         /*
366                          * We found a parent directory in the name-hash
367                          * hashtable, because only sparse directory entries
368                          * have a trailing '/' character.  Since "path" wasn't
369                          * in the index, perhaps it exists within this
370                          * sparse-directory.  Expand accordingly.
371                          */
372                         ensure_full_index(istate);
373                         break;
374                 }
375
376                 *replace = temp;
377                 substr_len = replace - path_mutable.buf;
378         }
379
380 cleanup:
381         strbuf_release(&path_mutable);
382         in_expand_to_path = 0;
383 }