rm: add --sparse option

As we did previously in 'git add', add a '--sparse' option to 'git rm' that allows modifying paths outside of the sparse-checkout definition. The existing checks in 'git rm' are restricted to tracked files that have the SKIP_WORKTREE bit in the current index. Future changes will cause 'git rm' to reject removing paths outside of the sparse-checkout definition, even if they are untracked or do not have the SKIP_WORKTREE bit. Signed-off-by: Derrick Stolee <dstolee@microsoft.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Derrick Stolee committed Sep 24, 2021 at 15:39 UTC f9786f9b85a6930a711e0ca8ba317c619f02bd8b
3 files changed +24 -2
Documentation/git-rm.txt
+6
@@ -72,6 +72,12 @@ For more details, see the 'pathspec' entry in linkgit:gitglossary[7].
72 --ignore-unmatch::
73 Exit with a zero status even if no files matched.
74
75 +--sparse::
76 + Allow updating index entries outside of the sparse-checkout cone.
77 + Normally, `git rm` refuses to update index entries whose paths do
78 + not fit within the sparse-checkout cone. See
79 + linkgit:git-sparse-checkout[1] for more.
80 +
81 -q::
82 --quiet::
83 `git rm` normally outputs one line (in the form of an `rm` command)
builtin/rm.c
+6 -2
@@ -237,6 +237,7 @@ static int check_local_mod(struct object_id *head, int index_only)
237
238 static int show_only = 0, force = 0, index_only = 0, recursive = 0, quiet = 0;
239 static int ignore_unmatch = 0, pathspec_file_nul;
240 +static int include_sparse;
241 static char *pathspec_from_file;
242
243 static struct option builtin_rm_options[] = {
@@ -247,6 +248,7 @@ static struct option builtin_rm_options[] = {
248 OPT_BOOL('r', NULL, &recursive, N_("allow recursive removal")),
249 OPT_BOOL( 0 , "ignore-unmatch", &ignore_unmatch,
250 N_("exit with a zero status even if nothing matched")),
251 + OPT_BOOL(0, "sparse", &include_sparse, N_("allow updating entries outside of the sparse-checkout cone")),
252 OPT_PATHSPEC_FROM_FILE(&pathspec_from_file),
253 OPT_PATHSPEC_FILE_NUL(&pathspec_file_nul),
254 OPT_END(),
@@ -298,7 +300,8 @@ int cmd_rm(int argc, const char **argv, const char *prefix)
300 ensure_full_index(&the_index);
301 for (i = 0; i < active_nr; i++) {
302 const struct cache_entry *ce = active_cache[i];
301 - if (ce_skip_worktree(ce))
303 +
304 + if (!include_sparse && ce_skip_worktree(ce))
305 continue;
306 if (!ce_path_match(&the_index, ce, &pathspec, seen))
307 continue;
@@ -322,7 +325,8 @@ int cmd_rm(int argc, const char **argv, const char *prefix)
325 seen_any = 1;
326 else if (ignore_unmatch)
327 continue;
325 - else if (matches_skip_worktree(&pathspec, i, &skip_worktree_seen))
328 + else if (!include_sparse &&
329 + matches_skip_worktree(&pathspec, i, &skip_worktree_seen))
330 string_list_append(&only_match_skip_worktree, original);
331 else
332 die(_("pathspec '%s' did not match any files"), original);
t/t3602-rm-sparse-checkout.sh
+12
@@ -43,6 +43,18 @@ test_expect_success 'recursive rm does not remove sparse entries' '
43 test_cmp expected actual
44 '
45
46 +test_expect_success 'recursive rm --sparse removes sparse entries' '
47 + git reset --hard &&
48 + git sparse-checkout set "sub/dir" &&
49 + git rm --sparse -r sub &&
50 + git status --porcelain -uno >actual &&
51 + cat >expected <<-\EOF &&
52 + D sub/d
53 + D sub/dir/e
54 + EOF
55 + test_cmp expected actual
56 +'
57 +
58 test_expect_success 'rm obeys advice.updateSparsePath' '
59 git reset --hard &&
60 git sparse-checkout set a &&