Skip to content

Commit 20ec2d0

Browse files
vdyegitster
authored andcommitted
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 <[email protected]> Signed-off-by: Victoria Dye <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent c01b1cb commit 20ec2d0

File tree

4 files changed

+86
-13
lines changed

4 files changed

+86
-13
lines changed

cache-tree.c

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -740,15 +740,26 @@ int write_index_as_tree(struct object_id *oid, struct index_state *index_state,
740740
return ret;
741741
}
742742

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+
743751
static void prime_cache_tree_rec(struct repository *r,
744752
struct cache_tree *it,
745-
struct tree *tree)
753+
struct tree *tree,
754+
struct strbuf *tree_path)
746755
{
747756
struct tree_desc desc;
748757
struct name_entry entry;
749758
int cnt;
759+
int base_path_len = tree_path->len;
750760

751761
oidcpy(&it->oid, &tree->object.oid);
762+
752763
init_tree_desc(&desc, tree->buffer, tree->size);
753764
cnt = 0;
754765
while (tree_entry(&desc, &entry)) {
@@ -757,27 +768,55 @@ static void prime_cache_tree_rec(struct repository *r,
757768
else {
758769
struct cache_tree_sub *sub;
759770
struct tree *subtree = lookup_tree(r, &entry.oid);
771+
760772
if (!subtree->object.parsed)
761773
parse_tree(subtree);
762774
sub = cache_tree_sub(it, entry.path);
763775
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);
765801
cnt += sub->cache_tree->entry_count;
766802
}
767803
}
804+
768805
it->entry_count = cnt;
769806
}
770807

771808
void prime_cache_tree(struct repository *r,
772809
struct index_state *istate,
773810
struct tree *tree)
774811
{
812+
struct strbuf tree_path = STRBUF_INIT;
813+
775814
trace2_region_enter("cache-tree", "prime_cache_tree", the_repository);
776815
cache_tree_free(&istate->cache_tree);
777816
istate->cache_tree = cache_tree();
778817

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);
781820
istate->cache_changed |= CACHE_TREE_CHANGED;
782821
trace2_region_leave("cache-tree", "prime_cache_tree", the_repository);
783822
}

cache.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -816,6 +816,16 @@ struct cache_entry *index_file_exists(struct index_state *istate, const char *na
816816
*/
817817
int index_name_pos(struct index_state *, const char *name, int namelen);
818818

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+
819829
/*
820830
* Some functions return the negative complement of an insert position when a
821831
* precise match was not found but a position was found where the entry would

read-cache.c

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@
6868
*/
6969
#define CACHE_ENTRY_PATH_LENGTH 80
7070

71+
enum index_search_mode {
72+
NO_EXPAND_SPARSE = 0,
73+
EXPAND_SPARSE = 1
74+
};
75+
7176
static inline struct cache_entry *mem_pool__ce_alloc(struct mem_pool *mem_pool, size_t len)
7277
{
7378
struct cache_entry *ce;
@@ -551,7 +556,10 @@ int cache_name_stage_compare(const char *name1, int len1, int stage1, const char
551556
return 0;
552557
}
553558

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)
555563
{
556564
int first, last;
557565

@@ -570,7 +578,7 @@ static int index_name_stage_pos(struct index_state *istate, const char *name, in
570578
first = next+1;
571579
}
572580

573-
if (istate->sparse_index &&
581+
if (search_mode == EXPAND_SPARSE && istate->sparse_index &&
574582
first > 0) {
575583
/* Note: first <= istate->cache_nr */
576584
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
586594
ce_namelen(ce) < namelen &&
587595
!strncmp(name, ce->name, ce_namelen(ce))) {
588596
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);
590598
}
591599
}
592600

@@ -595,7 +603,12 @@ static int index_name_stage_pos(struct index_state *istate, const char *name, in
595603

596604
int index_name_pos(struct index_state *istate, const char *name, int namelen)
597605
{
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;
599612
}
600613

601614
int remove_index_entry_at(struct index_state *istate, int pos)
@@ -1222,7 +1235,7 @@ static int has_dir_name(struct index_state *istate,
12221235
*/
12231236
}
12241237

1225-
pos = index_name_stage_pos(istate, name, len, stage);
1238+
pos = index_name_stage_pos(istate, name, len, stage, EXPAND_SPARSE);
12261239
if (pos >= 0) {
12271240
/*
12281241
* 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
13221335
strcmp(ce->name, istate->cache[istate->cache_nr - 1]->name) > 0)
13231336
pos = index_pos_to_insert_pos(istate->cache_nr);
13241337
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);
13261339

13271340
/* existing match? Just replace it. */
13281341
if (pos >= 0) {
@@ -1357,7 +1370,7 @@ static int add_index_entry_with_check(struct index_state *istate, struct cache_e
13571370
if (!ok_to_replace)
13581371
return error(_("'%s' appears as both a file and as a directory"),
13591372
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);
13611374
pos = -pos-1;
13621375
}
13631376
return pos + 1;

t/t1092-sparse-checkout-compatibility.sh

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -777,9 +777,9 @@ test_expect_success 'sparse-index is not expanded' '
777777
ensure_not_expanded checkout - &&
778778
ensure_not_expanded switch rename-out-to-out &&
779779
ensure_not_expanded switch - &&
780-
git -C sparse-index reset --hard &&
780+
ensure_not_expanded reset --hard &&
781781
ensure_not_expanded checkout rename-out-to-out -- deep/deeper1 &&
782-
git -C sparse-index reset --hard &&
782+
ensure_not_expanded reset --hard &&
783783
ensure_not_expanded restore -s rename-out-to-out -- deep/deeper1 &&
784784
785785
echo >>sparse-index/README.md &&
@@ -789,6 +789,17 @@ test_expect_success 'sparse-index is not expanded' '
789789
echo >>sparse-index/untracked.txt &&
790790
ensure_not_expanded add . &&
791791
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+
792803
ensure_not_expanded checkout -f update-deep &&
793804
test_config -C sparse-index pull.twohead ort &&
794805
(

0 commit comments

Comments
 (0)