Skip to content

Commit ea696e5

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. Helped-by: Victoria Dye <[email protected]> Helped-by: Derrick Stolee <[email protected]> Signed-off-by: Shaoxuan Yuan <[email protected]>
1 parent 6fd7626 commit ea696e5

File tree

2 files changed

+22
-2
lines changed

2 files changed

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

+19
Original file line numberDiff line numberDiff line change
@@ -1910,4 +1910,23 @@ test_expect_failure 'rm pathspec outside sparse definition' '
19101910
test_sparse_match git status --porcelain=v2
19111911
'
19121912

1913+
test_expect_failure 'rm pathspec expands index when necessary' '
1914+
init_repos &&
1915+
1916+
# in-cone pathspec (do not expand)
1917+
ensure_not_expanded rm deep/deep* 2>sparse-index-err &&
1918+
test_must_be_empty sparse-index-err &&
1919+
1920+
# out-of-cone pathspec (expand)
1921+
! ensure_not_expanded rm --sparse folder1/a* 2>sparse-index-err &&
1922+
test_must_be_empty sparse-index-err &&
1923+
1924+
# pathspec that should expand index
1925+
! ensure_not_expanded rm "*/a" 2>sparse-index-err &&
1926+
test_must_be_empty sparse-index-err &&
1927+
1928+
! ensure_not_expanded rm "**a" 2>sparse-index-err 1>stdout &&
1929+
test_must_be_empty sparse-index-err
1930+
'
1931+
19131932
test_done

0 commit comments

Comments
 (0)