Skip to content

Commit

Permalink
sparse-index: make mixed reset sparse-aware
Browse files Browse the repository at this point in the history
Due to the introduction of sparse directory entries in the `diff_cache`
execution (used internally by `reset --mixed`), explicit `change` and
`add_remove` functions are specified for merging of the interior contents of
those sparse directories.

Additionally, to handle cases in which `reset --mixed` must merge files nested
multiple levels deep in directories outside the sparse checkout cone, the
`recursive` diff option is added.

Signed-off-by: Victoria Dye <vdye@github.com>
  • Loading branch information
vdye authored and ldennington committed Jan 19, 2022
1 parent 927b179 commit eeef9d6
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 3 deletions.
74 changes: 73 additions & 1 deletion builtin/reset.c
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,73 @@ static void update_index_from_diff(struct diff_queue_struct *q,
}
}

static int pathspec_needs_expanded_index(const struct pathspec *pathspec)
{
unsigned int i, pos;
int res = 0;
char *skip_worktree_seen = NULL;

/*
* When using a magic pathspec, assume for the sake of simplicity that
* the index needs to be expanded to match all matchable files.
*/
if (pathspec->magic)
return 1;

for (i = 0; i < pathspec->nr; i++) {
struct pathspec_item item = pathspec->items[i];

/*
* If the pathspec item has a wildcard, the index should be expanded
* if the pathspec has the possibility of matching a subset of entries inside
* of a sparse directory (but not the entire directory).
*
* If the pathspec item is a literal path, the index only needs to be expanded
* if a) the pathspec isn't in the sparse checkout cone (to make sure we don't
* expand for in-cone files) and b) it doesn't match any sparse directories
* (since we can reset whole sparse directories without expanding them).
*/
if (item.nowildcard_len < item.len) {
for (pos = 0; pos < active_nr; pos++) {
struct cache_entry *ce = active_cache[pos];

if (!S_ISSPARSEDIR(ce->ce_mode))
continue;

/*
* If the pre-wildcard length is longer than the sparse
* directory name and the sparse directory is the first
* component of the pathspec, need to expand the index.
*/
if (item.nowildcard_len > ce_namelen(ce) &&
!strncmp(item.original, ce->name, ce_namelen(ce))) {
res = 1;
break;
}

/*
* If the pre-wildcard length is shorter than the sparse
* directory and the pathspec does not match the whole
* directory, need to expand the index.
*/
if (!strncmp(item.original, ce->name, item.nowildcard_len) &&
wildmatch(item.original, ce->name, 0)) {
res = 1;
break;
}
}
} else if (!path_in_cone_mode_sparse_checkout(item.original, &the_index) &&
!matches_skip_worktree(pathspec, i, &skip_worktree_seen))
res = 1;

if (res > 0)
break;
}

free(skip_worktree_seen);
return res;
}

static int read_from_tree(const struct pathspec *pathspec,
struct object_id *tree_oid,
int intent_to_add)
Expand All @@ -208,9 +275,14 @@ static int read_from_tree(const struct pathspec *pathspec,
opt.format_callback = update_index_from_diff;
opt.format_callback_data = &intent_to_add;
opt.flags.override_submodule_config = 1;
opt.flags.recursive = 1;
opt.repo = the_repository;
opt.change = diff_change;
opt.add_remove = diff_addremove;

if (pathspec->nr && the_index.sparse_index && pathspec_needs_expanded_index(pathspec))
ensure_full_index(&the_index);

ensure_full_index(&the_index);
if (do_diff_cache(tree_oid, &opt))
return 1;
diffcore_std(&opt);
Expand Down
32 changes: 30 additions & 2 deletions t/t1092-sparse-checkout-compatibility.sh
Original file line number Diff line number Diff line change
Expand Up @@ -599,7 +599,7 @@ test_expect_success 'reset with sparse directory pathspec outside definition' '
test_all_match git status --porcelain=v2
'

test_expect_success 'reset with file pathspec outside sparse definition' '
test_expect_success 'reset with pathspec match in sparse directory' '
init_repos &&
test_all_match git checkout -b reset-test update-deep &&
Expand Down Expand Up @@ -854,10 +854,38 @@ test_expect_success 'sparse-index is not expanded' '
ensure_not_expanded reset --hard $ref || return 1
done &&
ensure_not_expanded reset --mixed base &&
ensure_not_expanded reset --hard update-deep &&
ensure_not_expanded reset --keep base &&
ensure_not_expanded reset --merge update-deep &&
ensure_not_expanded reset --hard &&
ensure_not_expanded reset base -- deep/a &&
ensure_not_expanded reset base -- nonexistent-file &&
ensure_not_expanded reset deepest -- deep &&
# Although folder1 is outside the sparse definition, it exists as a
# directory entry in the index, so it will be reset without needing to
# expand the full index.
ensure_not_expanded reset --hard update-folder1 &&
ensure_not_expanded reset base -- folder1 &&
ensure_not_expanded reset --hard update-deep &&
ensure_not_expanded reset base -- deep/a &&
ensure_not_expanded reset base -- nonexistent-file &&
ensure_not_expanded reset deepest -- deep &&
# Although folder1 is outside the sparse definition, it exists as a
# directory entry in the index, so the pathspec will not force the
# index to be expanded.
ensure_not_expanded reset deepest -- folder1 &&
ensure_not_expanded reset deepest -- folder1/ &&
# Wildcard identifies only in-cone files, no index expansion
ensure_not_expanded reset deepest -- deep/\* &&
# Wildcard identifies only full sparse directories, no index expansion
ensure_not_expanded reset deepest -- folder\* &&
ensure_not_expanded checkout -f update-deep &&
test_config -C sparse-index pull.twohead ort &&
Expand Down

0 comments on commit eeef9d6

Please sign in to comment.