2 #include "repository.h"
3 #include "sparse-index.h"
8 static void set_index_entry(struct index_state *istate, int nr, struct cache_entry *ce)
10 ALLOC_GROW(istate->cache, nr + 1, istate->cache_alloc);
12 istate->cache[nr] = ce;
13 add_name_hash(istate, ce);
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)
20 struct index_state *istate = (struct index_state *)context;
21 struct cache_entry *ce;
22 size_t len = base->len;
25 return READ_TREE_RECURSIVE;
27 strbuf_addstr(base, path);
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);
33 strbuf_setlen(base, len);
37 void ensure_full_index(struct index_state *istate)
40 struct index_state *full;
41 struct strbuf base = STRBUF_INIT;
43 if (!istate || !istate->sparse_index)
47 istate->repo = the_repository;
49 trace2_region_enter("index", "ensure_full_index", istate->repo);
51 /* initialize basics of new index */
52 full = xcalloc(1, sizeof(struct index_state));
53 memcpy(full, istate, sizeof(struct index_state));
55 /* then change the necessary things */
56 full->sparse_index = 0;
57 full->cache_alloc = (3 * istate->cache_alloc) / 2;
59 ALLOC_ARRAY(full->cache, full->cache_alloc);
61 for (i = 0; i < istate->cache_nr; i++) {
62 struct cache_entry *ce = istate->cache[i];
66 if (!S_ISSPARSEDIR(ce->ce_mode)) {
67 set_index_entry(full, full->cache_nr++, ce);
70 if (!(ce->ce_flags & CE_SKIP_WORKTREE))
71 warning(_("index entry is a directory, but not sparse (%08x)"),
74 /* recursively walk into cd->name */
75 tree = lookup_tree(istate->repo, &ce->oid);
77 memset(&ps, 0, sizeof(ps));
82 strbuf_setlen(&base, 0);
83 strbuf_add(&base, ce->name, strlen(ce->name));
85 read_tree_at(istate->repo, tree, &base, &ps,
86 add_path_to_index, full);
88 /* free directory entries. full entries are re-used */
89 discard_cache_entry(ce);
92 /* Copy back into original index. */
93 memcpy(&istate->name_hash, &full->name_hash, sizeof(full->name_hash));
94 istate->sparse_index = 0;
96 istate->cache = full->cache;
97 istate->cache_nr = full->cache_nr;
98 istate->cache_alloc = full->cache_alloc;
100 strbuf_release(&base);
103 trace2_region_leave("index", "ensure_full_index", istate->repo);