Skip to content

Commit ece68c7

Browse files
committed
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]>
1 parent 6fd7626 commit ece68c7

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

builtin/rm.c

+3-2
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

+25-2
Original file line numberDiff line numberDiff line change
@@ -1340,10 +1340,14 @@ ensure_not_expanded () {
13401340
shift &&
13411341
test_must_fail env \
13421342
GIT_TRACE2_EVENT="$(pwd)/trace2.txt" \
1343-
git -C sparse-index "$@" || return 1
1343+
git -C sparse-index "$@" \
1344+
>sparse-index-out \
1345+
2>sparse-index-error || return 1
13441346
else
13451347
GIT_TRACE2_EVENT="$(pwd)/trace2.txt" \
1346-
git -C sparse-index "$@" || return 1
1348+
git -C sparse-index "$@" \
1349+
>sparse-index-out \
1350+
2>sparse-index-error || return 1
13471351
fi &&
13481352
test_region ! index ensure_full_index trace2.txt
13491353
}
@@ -1910,4 +1914,23 @@ test_expect_failure 'rm pathspec outside sparse definition' '
19101914
test_sparse_match git status --porcelain=v2
19111915
'
19121916

1917+
test_expect_failure 'rm pathspec expands index when necessary' '
1918+
init_repos &&
1919+
1920+
# in-cone pathspec (do not expand)
1921+
ensure_not_expanded rm "deep/deep*" &&
1922+
test_must_be_empty sparse-index-err &&
1923+
1924+
# out-of-cone pathspec (expand)
1925+
! ensure_not_expanded rm --sparse "folder1/a*" &&
1926+
test_must_be_empty sparse-index-err &&
1927+
1928+
# pathspec that should expand index
1929+
! ensure_not_expanded rm "*/a" &&
1930+
test_must_be_empty sparse-index-err &&
1931+
1932+
! ensure_not_expanded rm "**a" &&
1933+
test_must_be_empty sparse-index-err
1934+
'
1935+
19131936
test_done

0 commit comments

Comments
 (0)