Re: [PATCH v3 05/20] sparse-index: implement ensure_full_index()
From: Ævar Arnfjörð Bjarmason <hidden>
Date: 2021-03-17 13:04:43
On Tue, Mar 16 2021, Derrick Stolee via GitGitGadget wrote:
From: Derrick Stolee <redacted>
[...]
+static int add_path_to_index(const struct object_id *oid,
+ struct strbuf *base, const char *path,
+ unsigned int mode, void *context)
+{
+ struct index_state *istate = (struct index_state *)context;
+ struct cache_entry *ce;
+ size_t len = base->len;
+
+ if (S_ISDIR(mode))
+ return READ_TREE_RECURSIVE;
+
+ strbuf_addstr(base, path);
+
+ ce = make_cache_entry(istate, mode, oid, base->buf, 0, 0);
+ ce->ce_flags |= CE_SKIP_WORKTREE;
+ set_index_entry(istate, istate->cache_nr++, ce);
+
+ strbuf_setlen(base, len);
+ return 0;
+}
void ensure_full_index(struct index_state *istate)
{
- /* intentionally left blank */
+ int i;
+ struct index_state *full;
+ struct strbuf base = STRBUF_INIT;
+
+ if (!istate || !istate->sparse_index)
+ return;
+
+ if (!istate->repo)
+ istate->repo = the_repository;
+
+ trace2_region_enter("index", "ensure_full_index", istate->repo);
+
+ /* initialize basics of new index */
+ full = xcalloc(1, sizeof(struct index_state));
+ memcpy(full, istate, sizeof(struct index_state));
+
+ /* then change the necessary things */
+ full->sparse_index = 0;
+ full->cache_alloc = (3 * istate->cache_alloc) / 2;
+ full->cache_nr = 0;
+ ALLOC_ARRAY(full->cache, full->cache_alloc);
+
+ for (i = 0; i < istate->cache_nr; i++) {
+ struct cache_entry *ce = istate->cache[i];
+ struct tree *tree;
+ struct pathspec ps;
+
+ if (!S_ISSPARSEDIR(ce->ce_mode)) {
+ set_index_entry(full, full->cache_nr++, ce);
+ continue;
+ }
+ if (!(ce->ce_flags & CE_SKIP_WORKTREE))
+ warning(_("index entry is a directory, but not sparse (%08x)"),
+ ce->ce_flags);
+
+ /* recursively walk into cd->name */
+ tree = lookup_tree(istate->repo, &ce->oid);
+
+ memset(&ps, 0, sizeof(ps));
+ ps.recursive = 1;
+ ps.has_wildcard = 1;
+ ps.max_depth = -1;
+
+ strbuf_setlen(&base, 0);
+ strbuf_add(&base, ce->name, strlen(ce->name));
+
+ read_tree_at(istate->repo, tree, &base, &ps,
+ add_path_to_index, full);
+
+ /* free directory entries. full entries are re-used */
+ discard_cache_entry(ce);
+ }
+
+ /* Copy back into original index. */
+ memcpy(&istate->name_hash, &full->name_hash, sizeof(full->name_hash));
+ istate->sparse_index = 0;
+ free(istate->cache);
+ istate->cache = full->cache;
+ istate->cache_nr = full->cache_nr;
+ istate->cache_alloc = full->cache_alloc;
+
+ strbuf_release(&base);
+ free(full);
+
+ trace2_region_leave("index", "ensure_full_index", istate->repo);
}Not that I mind having added the read_tree_at() again, but just thinking aloud here. So we need this loop here because there's nothing like a read_tree_at() that knows how to start at the non-tree root of the index, and then for each directory there we're going to perform the equivalent of a read_tree() there, but we need to set the base for add_path_to_index() since we started at subdirs, not the root. That's fine, but grepping around a bit I wonder if we shouldn't eventually have some slightly fancier API that just works like read_tree() but takes an optional "start at the index's root" instead. Well, things that want that usually care about the index-specific bits, whereas this "I just care about the tree for these" is more of a special case I guess.