All of lore.kernel.org
 help / color / mirror / Atom feed
From: "adam torres encarnacion via GitGitGadget" <gitgitgadget@gmail.com>
To: git@vger.kernel.org
Cc: "Junio C Hamano" <gitster@pobox.com>,
	"Elijah Newren" <newren@gmail.com>,
	"Nguyễn Thái Ngọc Duy" <pclouds@gmail.com>,
	"Adam T" <admr0805@gmail.com>,
	"adam torres encarnacion" <admr0805@gmail.com>
Subject: [PATCH 2/2] rm: relax --worktree-only restrictions
Date: Sat, 05 Dec 2026 07:19:04 +0000	[thread overview]
Message-ID: <9fdb6498df0ae57035bd336695fcaf4bc1cdbf7a.1796455144.git.gitgitgadget@gmail.com> (raw)
In-Reply-To: <pull.2349.git.1796455144.gitgitgadget@gmail.com>

From: adam torres encarnacion <admr0805@gmail.com>

`git rm` refuses to delete paths from the working tree
and index if information will be lost. This guard is
too restrictive when `--worktree` is used without
`--staged` (since the changes will remain in the
index & therefore won't be lost).

Add a new branch in `check_local_mod` to handle
`--worktree` only behavior. Block only if the file
differs from the index.

Signed-off-by: adam torres encarnacion <admr0805@gmail.com>
---
 builtin/rm.c  | 28 +++++++++++++++++-----------
 t/t3600-rm.sh | 25 +++++++++++++++++++++++++
 2 files changed, 42 insertions(+), 11 deletions(-)

diff --git a/builtin/rm.c b/builtin/rm.c
index 4160bdefe4..1f0bfbec8a 100644
--- a/builtin/rm.c
+++ b/builtin/rm.c
@@ -98,7 +98,7 @@ static void submodules_absorb_gitdir_if_needed(void)
 	}
 }
 
-static int check_local_mod(struct object_id *head, int index_only)
+static int check_local_mod(struct object_id *head, int worktree, int staged)
 {
 	/*
 	 * Items in list are already sorted in the cache order,
@@ -197,16 +197,21 @@ static int check_local_mod(struct object_id *head, int index_only)
 		/*
 		 * If the index does not match the file in the work
 		 * tree and if it does not match the HEAD commit
-		 * either, (1) "git rm" without --staged definitely
-		 * will lose information; (2) "git rm --staged" will
-		 * lose information unless it is about removing an
-		 * "intent to add" entry.
+		 * either, (1) "git rm" definitely will lose information;
+		 * (2) "git rm --staged" will lose information unless it
+		 * is about removing an "intent to add" entry.
+		 * "git rm --worktree" keeps the index entry, so only
+		 * local changes are at risk.
 		 */
-		if (local_changes && staged_changes) {
-			if (!index_only || !ce_intent_to_add(ce))
+		if (worktree && !staged) {
+			if (local_changes)
+				string_list_append(&files_local, name);
+		}
+		else if (local_changes && staged_changes) {
+			if (worktree || !ce_intent_to_add(ce))
 				string_list_append(&files_staged, name);
 		}
-		else if (!index_only) {
+		else if (worktree) {
 			if (staged_changes)
 				string_list_append(&files_cached, name);
 			if (local_changes)
@@ -236,8 +241,9 @@ static int check_local_mod(struct object_id *head, int index_only)
 			  Q_("the following file has local modifications:",
 			     "the following files have local modifications:",
 			     files_local.nr),
-			  _("\n(use --staged to keep the file,"
-			    " or -f to force removal)"),
+			  staged ? _("\n(use --staged to keep the file,"
+				     " or -f to force removal)")
+				 : _("\n(use -f to force removal)"),
 			  &errs);
 	string_list_clear(&files_local, 0);
 
@@ -403,7 +409,7 @@ int cmd_rm(int argc,
 		struct object_id oid;
 		if (repo_get_oid(the_repository, "HEAD", &oid))
 			oidclr(&oid, the_repository->hash_algo);
-		if (check_local_mod(&oid, !worktree))
+		if (check_local_mod(&oid, worktree, staged))
 			exit(1);
 	}
 
diff --git a/t/t3600-rm.sh b/t/t3600-rm.sh
index d12c3448d4..a1bc8cca22 100755
--- a/t/t3600-rm.sh
+++ b/t/t3600-rm.sh
@@ -203,6 +203,31 @@ test_expect_success 'rm --worktree deletes the file and keeps it in the index' '
 	git ls-files --error-unmatch worktree-only
 '
 
+test_expect_success 'rm --worktree allows changes staged in the index' '
+	echo content >worktree-staged &&
+	git add worktree-staged &&
+	git rm --worktree worktree-staged &&
+	test_path_is_missing worktree-staged &&
+	git ls-files --error-unmatch worktree-staged
+'
+
+test_expect_success 'rm --worktree refuses to drop local modifications' '
+	cat >expect <<-\EOF &&
+	error: the following file has local modifications:
+	    worktree-local
+	(use -f to force removal)
+	EOF
+	echo content >worktree-local &&
+	git add worktree-local &&
+	echo changed >worktree-local &&
+	test_must_fail git rm --worktree worktree-local 2>actual &&
+	test_cmp expect actual &&
+	test_path_is_file worktree-local &&
+	git rm -f --worktree worktree-local &&
+	test_path_is_missing worktree-local &&
+	git ls-files --error-unmatch worktree-local
+'
+
 test_expect_success 'rm --staged --worktree removes the file from both' '
 	echo content >staged-and-worktree &&
 	git add staged-and-worktree &&
-- 
gitgitgadget

  parent reply	other threads:[~2026-12-05  7:19 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-05  7:19 [PATCH 0/2] rm: revisiting --staged and --worktree Adam T  via GitGitGadget
2026-12-05  7:19 ` [PATCH 1/2] rm: add " adam torres encarnacion via GitGitGadget
2026-12-05 15:15   ` D. Ben Knoble
2026-12-05 16:32     ` Adam
2026-12-05  7:19 ` adam torres encarnacion via GitGitGadget [this message]
2026-12-05 15:15 ` [PATCH 0/2] rm: revisiting " D. Ben Knoble
2026-12-05 16:32   ` Adam

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=9fdb6498df0ae57035bd336695fcaf4bc1cdbf7a.1796455144.git.gitgitgadget@gmail.com \
    --to=gitgitgadget@gmail.com \
    --cc=admr0805@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=newren@gmail.com \
    --cc=pclouds@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.