reset: make sparse-aware (except --mixed)

Remove `ensure_full_index` guard on `prime_cache_tree` and update `prime_cache_tree_rec` to correctly reconstruct sparse directory entries in the cache tree. While processing a tree's entries, `prime_cache_tree_rec` must determine whether a directory entry is sparse or not by searching for it in the index (*without* expanding the index). If a matching sparse directory index entry is found, no subtrees are added to the cache tree entry and the entry count is set to 1 (representing the sparse directory itself). Otherwise, the tree is assumed to not be sparse and its subtrees are recursively added to the cache tree. Helped-by: Elijah Newren <newren@gmail.com> Signed-off-by: Victoria Dye <vdye@github.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Victoria Dye committed Nov 29, 2021 at 15:52 UTC 20ec2d034cda1afef15a4dcc6e275d7a69413510
4 files changed +86 -13
cache-tree.c
+43 -4
@@ -740,15 +740,26 @@ out:
740 return ret;
741 }
742
743 +static void prime_cache_tree_sparse_dir(struct cache_tree *it,
744 + struct tree *tree)
745 +{
746 +
747 + oidcpy(&it->oid, &tree->object.oid);
748 + it->entry_count = 1;
749 +}
750 +
751 static void prime_cache_tree_rec(struct repository *r,
752 struct cache_tree *it,
745 - struct tree *tree)
753 + struct tree *tree,
754 + struct strbuf *tree_path)
755 {
756 struct tree_desc desc;
757 struct name_entry entry;
758 int cnt;
759 + int base_path_len = tree_path->len;
760
761 oidcpy(&it->oid, &tree->object.oid);
762 +
763 init_tree_desc(&desc, tree->buffer, tree->size);
764 cnt = 0;
765 while (tree_entry(&desc, &entry)) {
@@ -757,14 +768,40 @@ static void prime_cache_tree_rec(struct repository *r,
768 else {
769 struct cache_tree_sub *sub;
770 struct tree *subtree = lookup_tree(r, &entry.oid);
771 +
772 if (!subtree->object.parsed)
773 parse_tree(subtree);
774 sub = cache_tree_sub(it, entry.path);
775 sub->cache_tree = cache_tree();
764 - prime_cache_tree_rec(r, sub->cache_tree, subtree);
776 +
777 + /*
778 + * Recursively-constructed subtree path is only needed when working
779 + * in a sparse index (where it's used to determine whether the
780 + * subtree is a sparse directory in the index).
781 + */
782 + if (r->index->sparse_index) {
783 + strbuf_setlen(tree_path, base_path_len);
784 + strbuf_grow(tree_path, base_path_len + entry.pathlen + 1);
785 + strbuf_add(tree_path, entry.path, entry.pathlen);
786 + strbuf_addch(tree_path, '/');
787 + }
788 +
789 + /*
790 + * If a sparse index is in use, the directory being processed may be
791 + * sparse. To confirm that, we can check whether an entry with that
792 + * exact name exists in the index. If it does, the created subtree
793 + * should be sparse. Otherwise, cache tree expansion should continue
794 + * as normal.
795 + */
796 + if (r->index->sparse_index &&
797 + index_entry_exists(r->index, tree_path->buf, tree_path->len))
798 + prime_cache_tree_sparse_dir(sub->cache_tree, subtree);
799 + else
800 + prime_cache_tree_rec(r, sub->cache_tree, subtree, tree_path);
801 cnt += sub->cache_tree->entry_count;
802 }
803 }
804 +
805 it->entry_count = cnt;
806 }
807
@@ -772,12 +809,14 @@ void prime_cache_tree(struct repository *r,
809 struct index_state *istate,
810 struct tree *tree)
811 {
812 + struct strbuf tree_path = STRBUF_INIT;
813 +
814 trace2_region_enter("cache-tree", "prime_cache_tree", the_repository);
815 cache_tree_free(&istate->cache_tree);
816 istate->cache_tree = cache_tree();
817
779 - ensure_full_index(istate);
780 - prime_cache_tree_rec(r, istate->cache_tree, tree);
818 + prime_cache_tree_rec(r, istate->cache_tree, tree, &tree_path);
819 + strbuf_release(&tree_path);
820 istate->cache_changed |= CACHE_TREE_CHANGED;
821 trace2_region_leave("cache-tree", "prime_cache_tree", the_repository);
822 }
cache.h
+10
@@ -816,6 +816,16 @@ struct cache_entry *index_file_exists(struct index_state *istate, const char *na
816 */
817 int index_name_pos(struct index_state *, const char *name, int namelen);
818
819 +/*
820 + * Determines whether an entry with the given name exists within the
821 + * given index. The return value is 1 if an exact match is found, otherwise
822 + * it is 0. Note that, unlike index_name_pos, this function does not expand
823 + * the index if it is sparse. If an item exists within the full index but it
824 + * is contained within a sparse directory (and not in the sparse index), 0 is
825 + * returned.
826 + */
827 +int index_entry_exists(struct index_state *, const char *name, int namelen);
828 +
829 /*
830 * Some functions return the negative complement of an insert position when a
831 * precise match was not found but a position was found where the entry would
read-cache.c
+20 -7
@@ -68,6 +68,11 @@
68 */
69 #define CACHE_ENTRY_PATH_LENGTH 80
70
71 +enum index_search_mode {
72 + NO_EXPAND_SPARSE = 0,
73 + EXPAND_SPARSE = 1
74 +};
75 +
76 static inline struct cache_entry *mem_pool__ce_alloc(struct mem_pool *mem_pool, size_t len)
77 {
78 struct cache_entry *ce;
@@ -551,7 +556,10 @@ int cache_name_stage_compare(const char *name1, int len1, int stage1, const char
556 return 0;
557 }
558
554 -static int index_name_stage_pos(struct index_state *istate, const char *name, int namelen, int stage)
559 +static int index_name_stage_pos(struct index_state *istate,
560 + const char *name, int namelen,
561 + int stage,
562 + enum index_search_mode search_mode)
563 {
564 int first, last;
565
@@ -570,7 +578,7 @@ static int index_name_stage_pos(struct index_state *istate, const char *name, in
578 first = next+1;
579 }
580
573 - if (istate->sparse_index &&
581 + if (search_mode == EXPAND_SPARSE && istate->sparse_index &&
582 first > 0) {
583 /* Note: first <= istate->cache_nr */
584 struct cache_entry *ce = istate->cache[first - 1];
@@ -586,7 +594,7 @@ static int index_name_stage_pos(struct index_state *istate, const char *name, in
594 ce_namelen(ce) < namelen &&
595 !strncmp(name, ce->name, ce_namelen(ce))) {
596 ensure_full_index(istate);
589 - return index_name_stage_pos(istate, name, namelen, stage);
597 + return index_name_stage_pos(istate, name, namelen, stage, search_mode);
598 }
599 }
600
@@ -595,7 +603,12 @@ static int index_name_stage_pos(struct index_state *istate, const char *name, in
603
604 int index_name_pos(struct index_state *istate, const char *name, int namelen)
605 {
598 - return index_name_stage_pos(istate, name, namelen, 0);
606 + return index_name_stage_pos(istate, name, namelen, 0, EXPAND_SPARSE);
607 +}
608 +
609 +int index_entry_exists(struct index_state *istate, const char *name, int namelen)
610 +{
611 + return index_name_stage_pos(istate, name, namelen, 0, NO_EXPAND_SPARSE) >= 0;
612 }
613
614 int remove_index_entry_at(struct index_state *istate, int pos)
@@ -1222,7 +1235,7 @@ static int has_dir_name(struct index_state *istate,
1235 */
1236 }
1237
1225 - pos = index_name_stage_pos(istate, name, len, stage);
1238 + pos = index_name_stage_pos(istate, name, len, stage, EXPAND_SPARSE);
1239 if (pos >= 0) {
1240 /*
1241 * Found one, but not so fast. This could
@@ -1322,7 +1335,7 @@ static int add_index_entry_with_check(struct index_state *istate, struct cache_e
1335 strcmp(ce->name, istate->cache[istate->cache_nr - 1]->name) > 0)
1336 pos = index_pos_to_insert_pos(istate->cache_nr);
1337 else
1325 - pos = index_name_stage_pos(istate, ce->name, ce_namelen(ce), ce_stage(ce));
1338 + pos = index_name_stage_pos(istate, ce->name, ce_namelen(ce), ce_stage(ce), EXPAND_SPARSE);
1339
1340 /* existing match? Just replace it. */
1341 if (pos >= 0) {
@@ -1357,7 +1370,7 @@ static int add_index_entry_with_check(struct index_state *istate, struct cache_e
1370 if (!ok_to_replace)
1371 return error(_("'%s' appears as both a file and as a directory"),
1372 ce->name);
1360 - pos = index_name_stage_pos(istate, ce->name, ce_namelen(ce), ce_stage(ce));
1373 + pos = index_name_stage_pos(istate, ce->name, ce_namelen(ce), ce_stage(ce), EXPAND_SPARSE);
1374 pos = -pos-1;
1375 }
1376 return pos + 1;
t/t1092-sparse-checkout-compatibility.sh
+13 -2
@@ -777,9 +777,9 @@ test_expect_success 'sparse-index is not expanded' '
777 ensure_not_expanded checkout - &&
778 ensure_not_expanded switch rename-out-to-out &&
779 ensure_not_expanded switch - &&
780 - git -C sparse-index reset --hard &&
780 + ensure_not_expanded reset --hard &&
781 ensure_not_expanded checkout rename-out-to-out -- deep/deeper1 &&
782 - git -C sparse-index reset --hard &&
782 + ensure_not_expanded reset --hard &&
783 ensure_not_expanded restore -s rename-out-to-out -- deep/deeper1 &&
784
785 echo >>sparse-index/README.md &&
@@ -789,6 +789,17 @@ test_expect_success 'sparse-index is not expanded' '
789 echo >>sparse-index/untracked.txt &&
790 ensure_not_expanded add . &&
791
792 + for ref in update-deep update-folder1 update-folder2 update-deep
793 + do
794 + echo >>sparse-index/README.md &&
795 + ensure_not_expanded reset --hard $ref || return 1
796 + done &&
797 +
798 + ensure_not_expanded reset --hard update-deep &&
799 + ensure_not_expanded reset --keep base &&
800 + ensure_not_expanded reset --merge update-deep &&
801 + ensure_not_expanded reset --hard &&
802 +
803 ensure_not_expanded checkout -f update-deep &&
804 test_config -C sparse-index pull.twohead ort &&
805 (