🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: linux.kernel.git
Article #4353

[PATCH 2/2] rm: relax --worktree-only restrictions

#4353
From: "adam torres encarnacion via Git...
Date: Sat, 05 Dec 2026 07:19
120 lines
15150 bytes
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
Technical Details
Message-ID: <9fdb6498df0ae57035bd336695fcaf4bc1cdbf7a.1796455144.git.gitgitgadget@gmail.com>
Path: mbox-mo1.pugleaf.net!feed-us-txt.newsdeef.eu!feed-us-mo.newsdeef.eu!srv.newsdeef.eu!news.mb-net.net!open-news-network.org!news.nntp4.net!nntp.xenet.de!news.neodome.net!usenet.network!.POSTED!vger.kernel.org!git+bounces-77043-linux-kernel-git=dewy.ru
References: <pull.2349.git.1796455144.gitgitgadget@gmail.com>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!feed-us-txt.newsdeef.eu!feed-us-mo.newsdeef.eu!srv.newsdeef.eu!news.mb-net.net!open-news-network.org!news.nntp4.net!nntp.xenet.de!news.neodome.net!usenet.network!.POSTED!vger.kernel.org!git+bounces-77043-linux-kernel-git=dewy.ru
From: gitgitgadget@gmail.com ("adam torres encarnacion via GitGitGadget")
Newsgroups: linux.kernel.git
Subject: [PATCH 2/2] rm: relax --worktree-only restrictions
Date: Sat, 05 Dec 2026 07:19:04 +0000
Organization: Usenet Network
Message-ID: <9fdb6498df0ae57035bd336695fcaf4bc1cdbf7a.1796455144.git.gitgitgadget@gmail.com>
References: <pull.2349.git.1796455144.gitgitgadget@gmail.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Info: usenet.network;
	logging-data="2974986"; mail-complaints-to="abuse@usenet.network"
Cancel-Lock: sha1:NizjTc85xRHCIAvlQZC8KJRiK6Q= sha256:bLVNojQSmhn87o6EspiF+Tfl7rsmhxeqsSRGvveAXvY=
	sha1:4XCIgfePPzoF9d6fFxCmUGx3OlQ= sha256:7VxvBCzlZmAUbTl2sCYqFOrrzkogvq2rAkTe7I92PwY=
List-ID: <git.vger.kernel.org>
X-Mailpost-Empty-Hdrs: X-Spam-Level
X-Received: from localhost (localhost [127.0.0.1])
	by mail.dewy.ru (Postfix) with ESMTP id B8130117D92; Sat,  5 Dec 2026 10:19:30 +0300 (MSK)
X-Virus-Scanned: Debian amavisd-new at usenet.network
X-Spam-Flag: NO
X-Spam-Score: -2.297
X-Spam-Status: No, score=-2.297 tagged_above=-9999 required=6.31
	tests=[DCC_CHECK=1.1, DKIM_SIGNED=0.1, DKIM_VALID=-0.1,
	DKIM_VALID_AU=-0.1, FREEMAIL_FORGED_FROMDOMAIN=0.001,
	FREEMAIL_FROM=0.001, HEADER_FROM_DIFFERENT_DOMAINS=0.001,
	MAILING_LIST_MULTI=-1, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001,
	SPF_PASS=-0.001] autolearn=ham autolearn_force=no
X-Authentication-Results: mail.dewy.ru (amavisd-new); dkim=pass (2048-bit key)
	header.d=gmail.com
X-Received: from mail.dewy.ru ([127.0.0.1])
	by localhost (mail.dewy.ru [127.0.0.1]) (amavisd-new, port 10024)
	with ESMTP id y-T3hCMnzZAs;
	Sat,  5 Dec 2026 10:19:27 +0300 (MSK)
X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=172.105.105.114; helo=tor.lore.kernel.org; envelope-from=git+bounces-77043-linux-kernel-git=dewy.ru@vger.kernel.org; receiver=<UNKNOWN> 
X-Authentication-Results: OpenDMARC; dmarc=fail (p=none dis=none) header.from=gmail.com
X-Received: from tor.lore.kernel.org (tor.lore.kernel.org [172.105.105.114])
	by mail.dewy.ru (Postfix) with ESMTPS id 3B2D1117D93; Sat,  5 Dec 2026 10:19:18 +0300 (MSK)
X-Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.77.114.92])
	by tor.lore.kernel.org (Postfix) with ESMTP id BBA3F2A192; Sat,  5 Dec 2026 07:19:17 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1
X-ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass;
	t=1796455155;
	b=XkK+j/NS8TzOymye3ywbunAX5ndh8T5tPzk1auGGMeXLfvvMR7QUSfhRRhgAZcu+JsgY
	 bSj7gqk26gXty/gZXpLdU0ThADZ/S1sVfp7HRgx8KdpGQf0bzhHIanHJO7rfMODn0ULFL
	 12g8gp8AVbTMmU//gEjhjXDki7lLr/A/SwpQ9sgwKz6LYJOYLQejeOhdCnGkVCxp6/JyA
	 K0Gm1f3fPYkV+pytyz/P6R5tewt6A6TZLo1kLAtt6CpQilsabTKaEx4HVrXldzn3nEkdT
	 rsnS/pToL7gIOh9DX2nYW1rMzgXQmYToVk8CFrc4Gz5XPe/BKrvcXKEO1aFm5AmyzIw==
X-ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1796455155;
	h=X-Original-To:Received:DMARC-Filter:Received:DKIM-Signature:
	 X-Google-DKIM-Signature:X-Gm-Message-State:X-Gm-Gg:X-Received:
	 Received:Message-Id:In-Reply-To:References:From:Date:Subject:Fcc:
	 Content-Type:Content-Transfer-Encoding:X-Mailing-List:List-Id:
	 List-Post:List-Help:List-Subscribe:List-Unsubscribe:List-Owner:
	 Precedence:MIME-Version:To:Cc;
	bh=Q0W9C5f5PKBnUwAx2bklwKpzu/arAD6AY0cS9v/lFz8=;
	b=TMMh+LgKmyza6KrW/5k558gdC5j+LkV9JQgxMHF2Ww5Uu+3RpsWNbDoqKtTiz/DGxt+q
	 k+4TuskLxU+eagoHJ0nSVJLe+I5y6hjmrZyiMmmU+7DsVe3s0UBKa2NuxrMCfVV5Zai+Y
	 fXjGCW1i18y95BYuib13bEPdDWiR/cLWfvQPfn/Gj3xu6+XYqLfCgq/novn+8JysQ7F7Y
	 SRJKQfSEHHMS5oipoZDZikbqNmWftMCzuAs1uBrVh8aPybZAy1FDmFCuA7iQkQKU1FzGe
	 y3FDZFqRPsZHuRD94uot/Tp8AtvP+ePO+gHa4Vjao4NIJTkbSoYexXUSczzen2RbMYg==
X-ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; 
	dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=uWQkWfzl;
	dmarc=pass header.from=gmail.com;
	spf=pass smtp.mailfrom=gmail.com;
	arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1
X-Received: from localhost.localdomain (localhost [127.0.0.1])
	by smtp.subspace.kernel.org (Postfix) with ESMTP id 433F9606A334;
	Sat,  5 Dec 2026 07:19:15 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org;
	dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=uWQkWfzl
X-Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.222.174
X-ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none;
	t=1796455149;
	b=2YbwmW1AikOs8mypmUv3rw93LhVjn2jt8K4uJmIAzAx6B0YckLGc9vcO8gYFPKzJ0EJ/
	 h0CqcquXpmTfEeEk1iWiICFP3vc9S6E8206M7Q89P8FeeRDhlpO3pH7RO2h+LcxsPGE/J
	 AdYUmiSS2p/dn2MJGIOMcNOQHUmlO2n+gHPg71rWzhKB4J/aJZoc3MIqraTru3F0GWZgQ
	 B9N3OmzNnHCa23HDS2cz9HwZEqJMvUNukg3bQJncE7r5GistKrQzI4XnZR8zGyXeasDFe
	 yZI2aWM0KegIV53bpu2MeFAa5sb6siJipqp2w8txtTLcG11G4jjmviFXSLgfaKUzQ2g==
X-ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1796455149;
	h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature:
	 X-Gm-Message-State:X-Gm-Gg:X-Received:Received:Message-Id:In-Reply-To:
	 References:From:Date:Subject:Fcc:Content-Type:
	 Content-Transfer-Encoding:MIME-Version:To:Cc;
	bh=Q0W9C5f5PKBnUwAx2bklwKpzu/arAD6AY0cS9v/lFz8=;
	b=aT6iKmG35Fy5G5MuJKgDNaOrHDXmEzShEkStSNq4dDlFSqq8Xcb7iSCUHt17R789pPpm
	 AwR7Q9ePnLNhFyb+KdB2J7ywoTXDbhaIPDrvjyCY/AeJsnrhMXt/SRvoDCLghl2n3I6Up
	 Uw4LEu9g/KiXcsalWRx0yHEHjU1FHIDzVA6R6kbGD/F0E3JeIbZ2CndbV6TZ4iICSEdDZ
	 oUXOnY5E1eO1sk0xzQsRS7jSt5LkNvmFeAMB8Fik8+B2OtoVzFk1e2ddsZcXw7UzseTxQ
	 kZqzv2CQzku3OCkG1srkU97jOccQPpYrndVWalxg0D0lLhvhx7XAs4Ggrh7tMCNS2Pg==
X-ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; 
	dmarc=pass header.from=gmail.com;
	spf=pass smtp.mailfrom=gmail.com;
	dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=uWQkWfzl;
	arc=none smtp.remote-ip=209.85.222.174
X-Received: from mail-qk1-f174.google.com (mail-qk1-f174.google.com [209.85.222.174])
	(using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)
	 key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256)
	(No client certificate requested)
	by smtp.subspace.kernel.org (Postfix) with ESMTPS id 148B6606A681
	for <git@vger.kernel.org>; Sat,  5 Dec 2026 07:19:08 +0000 (UTC)
X-DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 148B6606A681
X-Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com
X-Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com
X-Received: by mail-qk1-f174.google.com with SMTP id af79cd13be357-9495f083657so196801385a.0
        for <git@vger.kernel.org>; Fri, 04 Dec 2026 23:19:08 -0800 (PST)
X-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=gmail.com; s=20260707; t=1796455148; x=1797059948; darn=vger.kernel.org;
        h=cc:to:mime-version:content-transfer-encoding:content-type:fcc
         :subject:date:from:references:in-reply-to:message-id:from:to:cc
         :subject:date:message-id:reply-to:content-type;
        bh=Q0W9C5f5PKBnUwAx2bklwKpzu/arAD6AY0cS9v/lFz8=;
        b=uWQkWfzl+tzDfR+Ud6RGdjA17RP11HCoz10aBlyDvWFFZyZ8d0BdeRDW5gTvMzmfZU
         g1PLNNazitya2Pqz6PqQD6UxDlgFmgr5tXBNK1n+r+3+Z6kedz33XbunIU/wHZuLg/bb
         WFrwlV9uiR95HTghKXFF8Sm/JuiEwol68ucx9GFo5oiqUsbfOmIK3k5nAhhhhzDiYh80
         lNHm8st7+THoL+y0xp1f0TEjRsj55HRWay8ualNNuAgy2gJl6XFIkeCt5nFmOLaC3B4N
         0uvyhTDzIgCbwn9GCWw2/UiNC6XJ80Tydzy2NHbjMq4Pll8W0eO1QwsiTi8Pogot4hQ8
         yLNQ==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20260707; t=1796455148; x=1797059948;
        h=cc:to:mime-version:content-transfer-encoding:content-type:fcc
         :subject:date:from:references:in-reply-to:message-id:x-gm-gg
         :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to
         :content-type;
        bh=Q0W9C5f5PKBnUwAx2bklwKpzu/arAD6AY0cS9v/lFz8=;
        b=MrH2wp/w1z/rWwWK0m2JW/OkUVRZssP7Hl9TgBtPWakEmrFXdnbZmB2UpbHoaCkjhl
         2ky0KQHITfYAyRCr/cKM8a0tJd1VG05t3ZUjdvvnMjdZYlrdIiuHP5Op/yPh2BqP4DvH
         qkHgZdPmDCy+/xoeMZgRZWM3B0b61uVGKFUrFqxhpOPxuteQcgFaSwDsZARDOQk23Gb8
         CLwgzCFqn6E6eBsFHyAUlsbK7O+RsKqz94+RlkO+dX5m42l2RluI4LVjToWxA/q83Y3L
         SwepwkqZ04WFLdnQ1RGSIBZvWu2EVLV/Pa80sc+7ZFCVRtlV+s5ovemoU6Lm3aN2Hny9
         jK9A==
X-Gm-Message-State: APuU4Z9/pjITwg5j6tx3zrwIGd/ZhZviNO9S5Oy8OYlyQ4Usi4L7dhnu
	Ic+m7Ge8wgOx+3r4HEHoEC1aL0eLlbArDeJN+HEHr5Y/8s+5+huwgrwlzxGBOJkc
X-Gm-Gg: AZFdTZdgf7riazR4m7nIxahflJaYkVKAk++l52UbLIdUiKrZHhzM+ZZndIGMHqCpgUr
	m7mE3/scNo0IYywalm4/ZV2NxepcTtq2rBz9JgLADCNbvV2EkiWMYWwO6omild6uu62CvrgLTJd
	C88m+GhIq0tmlbE4vFxUYM6Sfynvkczu4XqGHYIuAIvn9am1btf31ChhQGl93+M+/ojCJtXsYlq
	86+R1WljVpg1BWbk1aI8bKH4qSiWP99+pTqD9UOKaqXrPnNn1KI8FWGdRqMQh0Em3eG7kaJlqA8
	FXgmA9nvlxFJcyzda/Dypzz+ACn48G9vwPxliP4Nc4elCM3nt5mzBKlftRScxxYjaGG9HqP6f4J
	W66qs8DqyhWPyP3J+ePtLUc/WQnaQR4cRxZCuaUVXbFOMOJq6wQRE8e2BzfwLsceP1lXrFbCI7x
	0oL9LMR8sSC47ztsTdyYuNWDCmrUtcC0a172ZL4ArUb0udb7sr7fgcYLo3C1/NbWHdcucNammkw
	I30M8AhFnjj/fbQHRZTUTgAZECH+wRQHvAl54ioo1RWlqGLy4QWsCQS0Kz1SDDOimK49iH02trb
	wYP7hqRij1fuCP8HZXzzB3Lh24HnKCtbFiNiNR0dtCT7DkEA+DXJ6g==
X-Received: by 2002:a05:620a:4625:b0:949:a71d:82e0 with SMTP id af79cd13be357-94a8279b772mr696862185a.13.1796455147664;
        Fri, 04 Dec 2026 23:19:07 -0800 (PST)
X-Received: from [127.0.0.1] ([20.81.230.132])
        by smtp.gmail.com with ESMTPSA id af79cd13be357-94a7f4dba49sm424225685a.27.2026.12.04.23.19.07
        (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);
        Fri, 04 Dec 2026 23:19:07 -0800 (PST)
X-In-Reply-To: <pull.2349.git.1796455144.gitgitgadget@gmail.com>
X-Fcc: Sent
X-Mailing-List: git@vger.kernel.org
X-List-Post: <mailto:git@vger.kernel.org>
X-List-Help: <mailto:git+help@vger.kernel.org>
X-List-Subscribe: <mailto:git+subscribe@vger.kernel.org>
X-List-Unsubscribe: <mailto:git+unsubscribe@vger.kernel.org>
X-List-Owner: <mailto:git+owner@vger.kernel.org>
X-Precedence: list
X-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>