Skip to content

Commit f9786f9

Browse files
derrickstoleegitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 61d450f commit f9786f9

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

Documentation/git-rm.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,12 @@ For more details, see the 'pathspec' entry in linkgit:gitglossary[7].
7272
--ignore-unmatch::
7373
Exit with a zero status even if no files matched.
7474

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+
7581
-q::
7682
--quiet::
7783
`git rm` normally outputs one line (in the form of an `rm` command)

builtin/rm.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,7 @@ static int check_local_mod(struct object_id *head, int index_only)
237237

238238
static int show_only = 0, force = 0, index_only = 0, recursive = 0, quiet = 0;
239239
static int ignore_unmatch = 0, pathspec_file_nul;
240+
static int include_sparse;
240241
static char *pathspec_from_file;
241242

242243
static struct option builtin_rm_options[] = {
@@ -247,6 +248,7 @@ static struct option builtin_rm_options[] = {
247248
OPT_BOOL('r', NULL, &recursive, N_("allow recursive removal")),
248249
OPT_BOOL( 0 , "ignore-unmatch", &ignore_unmatch,
249250
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")),
250252
OPT_PATHSPEC_FROM_FILE(&pathspec_from_file),
251253
OPT_PATHSPEC_FILE_NUL(&pathspec_file_nul),
252254
OPT_END(),
@@ -298,7 +300,8 @@ int cmd_rm(int argc, const char **argv, const char *prefix)
298300
ensure_full_index(&the_index);
299301
for (i = 0; i < active_nr; i++) {
300302
const struct cache_entry *ce = active_cache[i];
301-
if (ce_skip_worktree(ce))
303+
304+
if (!include_sparse && ce_skip_worktree(ce))
302305
continue;
303306
if (!ce_path_match(&the_index, ce, &pathspec, seen))
304307
continue;
@@ -322,7 +325,8 @@ int cmd_rm(int argc, const char **argv, const char *prefix)
322325
seen_any = 1;
323326
else if (ignore_unmatch)
324327
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))
326330
string_list_append(&only_match_skip_worktree, original);
327331
else
328332
die(_("pathspec '%s' did not match any files"), original);

t/t3602-rm-sparse-checkout.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,18 @@ test_expect_success 'recursive rm does not remove sparse entries' '
4343
test_cmp expected actual
4444
'
4545

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+
4658
test_expect_success 'rm obeys advice.updateSparsePath' '
4759
git reset --hard &&
4860
git sparse-checkout set a &&

0 commit comments

Comments
 (0)