Skip to content

Commit bcf96cf

Browse files
ffyuandagitster
authored andcommitted
rm: expand the index only when necessary
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]>
1 parent b29ad38 commit bcf96cf

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

builtin/rm.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -296,8 +296,9 @@ int cmd_rm(int argc, const char **argv, const char *prefix)
296296

297297
seen = xcalloc(pathspec.nr, 1);
298298

299-
/* TODO: audit for interaction with sparse-index. */
300-
ensure_full_index(&the_index);
299+
if (pathspec_needs_expanded_index(&the_index, &pathspec))
300+
ensure_full_index(&the_index);
301+
301302
for (i = 0; i < active_nr; i++) {
302303
const struct cache_entry *ce = active_cache[i];
303304

t/t1092-sparse-checkout-compatibility.sh

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1365,10 +1365,14 @@ ensure_not_expanded () {
13651365
shift &&
13661366
test_must_fail env \
13671367
GIT_TRACE2_EVENT="$(pwd)/trace2.txt" \
1368-
git -C sparse-index "$@" || return 1
1368+
git -C sparse-index "$@" \
1369+
>sparse-index-out \
1370+
2>sparse-index-error || return 1
13691371
else
13701372
GIT_TRACE2_EVENT="$(pwd)/trace2.txt" \
1371-
git -C sparse-index "$@" || return 1
1373+
git -C sparse-index "$@" \
1374+
>sparse-index-out \
1375+
2>sparse-index-error || return 1
13721376
fi &&
13731377
test_region ! index ensure_full_index trace2.txt
13741378
}
@@ -1935,4 +1939,23 @@ test_expect_failure 'rm pathspec outside sparse definition' '
19351939
test_sparse_match git status --porcelain=v2
19361940
'
19371941

1942+
test_expect_failure 'rm pathspec expands index when necessary' '
1943+
init_repos &&
1944+
1945+
# in-cone pathspec (do not expand)
1946+
ensure_not_expanded rm "deep/deep*" &&
1947+
test_must_be_empty sparse-index-err &&
1948+
1949+
# out-of-cone pathspec (expand)
1950+
! ensure_not_expanded rm --sparse "folder1/a*" &&
1951+
test_must_be_empty sparse-index-err &&
1952+
1953+
# pathspec that should expand index
1954+
! ensure_not_expanded rm "*/a" &&
1955+
test_must_be_empty sparse-index-err &&
1956+
1957+
! ensure_not_expanded rm "**a" &&
1958+
test_must_be_empty sparse-index-err
1959+
'
1960+
19381961
test_done

0 commit comments

Comments
 (0)