sparse-index: add 'sdir' index extension
[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
8 static void set_index_entry(struct index_state *istate, int nr, struct cache_entry *ce)
9 {
10         ALLOC_GROW(istate->cache, nr + 1, istate->cache_alloc);
11
12         istate->cache[nr] = ce;
13         add_name_hash(istate, ce);
14 }
15
16 static int add_path_to_index(const struct object_id *oid,
17                              struct strbuf *base, const char *path,
18                              unsigned int mode, void *context)
19 {
20         struct index_state *istate = (struct index_state *)context;
21         struct cache_entry *ce;
22         size_t len = base->len;
23
24         if (S_ISDIR(mode))
25                 return READ_TREE_RECURSIVE;
26
27         strbuf_addstr(base, path);
28
29         ce = make_cache_entry(istate, mode, oid, base->buf, 0, 0);
30         ce->ce_flags |= CE_SKIP_WORKTREE;
31         set_index_entry(istate, istate->cache_nr++, ce);
32
33         strbuf_setlen(base, len);
34         return 0;
35 }
36
37 void ensure_full_index(struct index_state *istate)
38 {
39         int i;
40         struct index_state *full;
41         struct strbuf base = STRBUF_INIT;
42
43         if (!istate || !istate->sparse_index)
44                 return;
45
46         if (!istate->repo)
47                 istate->repo = the_repository;
48
49         trace2_region_enter("index", "ensure_full_index", istate->repo);
50
51         /* initialize basics of new index */
52         full = xcalloc(1, sizeof(struct index_state));
53         memcpy(full, istate, sizeof(struct index_state));
54
55         /* then change the necessary things */
56         full->sparse_index = 0;
57         full->cache_alloc = (3 * istate->cache_alloc) / 2;
58         full->cache_nr = 0;
59         ALLOC_ARRAY(full->cache, full->cache_alloc);
60
61         for (i = 0; i < istate->cache_nr; i++) {
62                 struct cache_entry *ce = istate->cache[i];
63                 struct tree *tree;
64                 struct pathspec ps;
65
66                 if (!S_ISSPARSEDIR(ce->ce_mode)) {
67                         set_index_entry(full, full->cache_nr++, ce);
68                         continue;
69                 }
70                 if (!(ce->ce_flags & CE_SKIP_WORKTREE))
71                         warning(_("index entry is a directory, but not sparse (%08x)"),
72                                 ce->ce_flags);
73
74                 /* recursively walk into cd->name */
75                 tree = lookup_tree(istate->repo, &ce->oid);
76
77                 memset(&ps, 0, sizeof(ps));
78                 ps.recursive = 1;
79                 ps.has_wildcard = 1;
80                 ps.max_depth = -1;
81
82                 strbuf_setlen(&base, 0);
83                 strbuf_add(&base, ce->name, strlen(ce->name));
84
85                 read_tree_at(istate->repo, tree, &base, &ps,
86                              add_path_to_index, full);
87
88                 /* free directory entries. full entries are re-used */
89                 discard_cache_entry(ce);
90         }
91
92         /* Copy back into original index. */
93         memcpy(&istate->name_hash, &full->name_hash, sizeof(full->name_hash));
94         istate->sparse_index = 0;
95         free(istate->cache);
96         istate->cache = full->cache;
97         istate->cache_nr = full->cache_nr;
98         istate->cache_alloc = full->cache_alloc;
99
100         strbuf_release(&base);
101         free(full);
102
103         trace2_region_leave("index", "ensure_full_index", istate->repo);
104 }