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 <vdye@github.com> Helped-by: Derrick Stolee <derrickstolee@github.com> Signed-off-by: Shaoxuan Yuan <shaoxuan.yuan02@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Shaoxuan Yuan committed Aug 7, 2022 at 12:13 UTC bcf96cfca6a9cfc62cea3998d9bb23d1c728a463
2 files changed +28 -4
builtin/rm.c
+3 -2
@@ -296,8 +296,9 @@ int cmd_rm(int argc, const char **argv, const char *prefix)
296
297 seen = xcalloc(pathspec.nr, 1);
298
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 +
302 for (i = 0; i < active_nr; i++) {
303 const struct cache_entry *ce = active_cache[i];
304
t/t1092-sparse-checkout-compatibility.sh
+25 -2
@@ -1365,10 +1365,14 @@ ensure_not_expanded () {
1365 shift &&
1366 test_must_fail env \
1367 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
1371 else
1372 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
1376 fi &&
1377 test_region ! index ensure_full_index trace2.txt
1378 }
@@ -1935,4 +1939,23 @@ test_expect_failure 'rm pathspec outside sparse definition' '
1939 test_sparse_match git status --porcelain=v2
1940 '
1941
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 +
1961 test_done