Skip to content
/ git Public
forked from git/git

Commit

Permalink
rm: expand the index only when necessary
Browse files Browse the repository at this point in the history
Remove the `ensure_full_index()` method so `git-rm` does not always
expand the index when the expansion is unnecessary, i.e. when
<pathspec> does not have any possibilities to match anything outside
of sparse-checkout definition.

Expand the index when the <pathspec> needs an expanded index, i.e. the
<pathspec> contains wildcard that may need a full-index or the
<pathspec> is simply outside of sparse-checkout definition.

Notice that the test 'rm pathspec expands index when necessary' in
t1092 *is* testing this code change behavior, though it will be marked
as 'test_expect_success' only in the next patch, where we officially
mark `command_requires_full_index = 0`, so the index does not expand
unless we tell it to do so.

Notice that because we also want `ensure_full_index` to record the
stdout and stderr from Git command, a corresponding modification
is also included in this patch. The reason we want the "sparse-index-out"
and "sparse-index-err", is that we need to make sure there is no error
from Git command itself, so we can rely on the `test_region` result
and determine if the index is expanded or not.

Helped-by: Victoria Dye <[email protected]>
Helped-by: Derrick Stolee <[email protected]>
Signed-off-by: Shaoxuan Yuan <[email protected]>
Signed-off-by: Junio C Hamano <[email protected]>
  • Loading branch information
ffyuanda authored and gitster committed Aug 7, 2022
1 parent 0defb5c commit 46fc9ff
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
5 changes: 3 additions & 2 deletions builtin/rm.c
Original file line number Diff line number Diff line change
Expand Up @@ -296,8 +296,9 @@ int cmd_rm(int argc, const char **argv, const char *prefix)

seen = xcalloc(pathspec.nr, 1);

/* TODO: audit for interaction with sparse-index. */
ensure_full_index(&the_index);
if (pathspec_needs_expanded_index(&the_index, &pathspec))
ensure_full_index(&the_index);

for (i = 0; i < active_nr; i++) {
const struct cache_entry *ce = active_cache[i];

Expand Down
27 changes: 25 additions & 2 deletions t/t1092-sparse-checkout-compatibility.sh
Original file line number Diff line number Diff line change
Expand Up @@ -1365,10 +1365,14 @@ ensure_not_expanded () {
shift &&
test_must_fail env \
GIT_TRACE2_EVENT="$(pwd)/trace2.txt" \
git -C sparse-index "$@" || return 1
git -C sparse-index "$@" \
>sparse-index-out \
2>sparse-index-error || return 1
else
GIT_TRACE2_EVENT="$(pwd)/trace2.txt" \
git -C sparse-index "$@" || return 1
git -C sparse-index "$@" \
>sparse-index-out \
2>sparse-index-error || return 1
fi &&
test_region ! index ensure_full_index trace2.txt
}
Expand Down Expand Up @@ -1935,4 +1939,23 @@ test_expect_failure 'rm pathspec outside sparse definition' '
test_sparse_match git status --porcelain=v2
'

test_expect_failure 'rm pathspec expands index when necessary' '
init_repos &&
# in-cone pathspec (do not expand)
ensure_not_expanded rm "deep/deep*" &&
test_must_be_empty sparse-index-err &&
# out-of-cone pathspec (expand)
! ensure_not_expanded rm --sparse "folder1/a*" &&
test_must_be_empty sparse-index-err &&
# pathspec that should expand index
! ensure_not_expanded rm "*/a" &&
test_must_be_empty sparse-index-err &&
! ensure_not_expanded rm "**a" &&
test_must_be_empty sparse-index-err
'

test_done

0 comments on commit 46fc9ff

Please sign in to comment.