🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: linux.kernel.git
Article #4311

[PATCH] diff: honor --no-optional-locks when refreshing the index

#4311
From: Elvis Pranskevichus via B4 Relay
Date: Thu, 03 Dec 2026 19:32
125 lines
15504 bytes
From: Elvis Pranskevichus <el@prans.net>

When "git diff" compares the work tree with the index and finds paths
whose stat information changed but whose contents did not, it takes the
index lock and writes the refreshed stat information back.  27344d6a6c
(git: add --no-optional-locks option, 2017-09-27) taught "git status" to
skip this opportunistic write, so that tools running in the background
do not hold index.lock while the user runs a command, but "git diff"
still takes the lock and ignores --no-optional-locks.  Fix this.

Signed-off-by: Elvis Pranskevichus <el@prans.net>
---
 Documentation/config/diff.adoc |  3 ++-
 Documentation/git.adoc         |  4 ++--
 builtin/diff.c                 |  4 ++++
 t/meson.build                  |  1 +
 t/t4075-diff-optional-locks.sh | 32 ++++++++++++++++++++++++++++++++
 5 files changed, 41 insertions(+), 3 deletions(-)

diff --git a/Documentation/config/diff.adoc b/Documentation/config/diff.adoc
index 1135a62a0a..9a8fae2f3b 100644
--- a/Documentation/config/diff.adoc
+++ b/Documentation/config/diff.adoc
@@ -6,7 +6,8 @@
 	contents in the work tree match the contents in the
 	index.  This option defaults to `true`.  Note that this
 	affects only `git diff` Porcelain, and not lower level
-	`diff` commands such as `git diff-files`.
+	`diff` commands such as `git diff-files`.  The refresh is an
+	optional operation, so `--no-optional-locks` skips it.
 
 `diff.dirstat`::
 ifdef::git-diff[]
diff --git a/Documentation/git.adoc b/Documentation/git.adoc
index 1aa0608bae..82ceb8771a 100644
--- a/Documentation/git.adoc
+++ b/Documentation/git.adoc
@@ -1042,8 +1042,8 @@ be in the future).
 `GIT_OPTIONAL_LOCKS`::
 	If this Boolean environment variable is set to false, Git will complete any requested operation without
 	performing any optional sub-operations that require taking a lock.
-	For example, this will prevent `git status` from refreshing the
-	index as a side effect. This is useful for processes running in
+	For example, this will prevent `git status` and `git diff` from
+	refreshing the index as a side effect. This is useful for processes running in
 	the background which do not want to cause lock contention with
 	other operations on the repository.  Defaults to `1`.
 
diff --git a/builtin/diff.c b/builtin/diff.c
index 18b1083e98..c70f93ba29 100644
--- a/builtin/diff.c
+++ b/builtin/diff.c
@@ -18,6 +18,7 @@
 #include "diff.h"
 #include "diff-merges.h"
 #include "diffcore.h"
+#include "environment.h"
 #include "preload-index.h"
 #include "read-cache-ll.h"
 #include "revision.h"
@@ -239,6 +240,9 @@ static void refresh_index_quietly(void)
 	struct lock_file lock_file = LOCK_INIT;
 	int fd;
 
+	if (!use_optional_locks())
+		return;
+
 	fd = repo_hold_locked_index(the_repository, &lock_file, 0);
 	if (fd < 0)
 		return;
diff --git a/t/meson.build b/t/meson.build
index 1cf1d95f70..88ba38b027 100644
--- a/t/meson.build
+++ b/t/meson.build
@@ -527,6 +527,7 @@ integration_tests = [
   't4072-diff-max-depth.sh',
   't4073-diff-stat-name-width.sh',
   't4074-diff-shifted-matched-group.sh',
+  't4075-diff-optional-locks.sh',
   't4100-apply-stat.sh',
   't4101-apply-nonl.sh',
   't4102-apply-rename.sh',
diff --git a/t/t4075-diff-optional-locks.sh b/t/t4075-diff-optional-locks.sh
new file mode 100755
index 0000000000..0c111bf6fe
--- /dev/null
+++ b/t/t4075-diff-optional-locks.sh
@@ -0,0 +1,32 @@
+#!/bin/sh
+
+test_description='git diff refreshes the index only when optional locks are allowed'
+
+. ./test-lib.sh
+
+test_expect_success 'setup' '
+	echo content >file &&
+	git add file &&
+	git commit -m initial
+'
+
+test_expect_success '--no-optional-locks prevents index refresh' '
+	test-tool chmtime -10 file &&
+	test_set_magic_mtime .git/index &&
+	git --no-optional-locks diff >out &&
+	test_must_be_empty out &&
+	test_is_magic_mtime .git/index &&
+	git diff >out &&
+	test_must_be_empty out &&
+	! test_is_magic_mtime .git/index
+'
+
+test_expect_success 'GIT_OPTIONAL_LOCKS=0 prevents index refresh' '
+	test-tool chmtime -10 file &&
+	test_set_magic_mtime .git/index &&
+	GIT_OPTIONAL_LOCKS=0 git diff >out &&
+	test_must_be_empty out &&
+	test_is_magic_mtime .git/index
+'
+
+test_done

---
base-commit: 566b6997dd4710b625c0882cf10a7443bf52d183
change-id: 20261203-diff-optional-locks-9fd99e458ed7
Technical Details
Message-ID: <20261203-diff-optional-locks-v1-1-ca3fa3f08175@prans.net>
Path: mbox-mo1.pugleaf.net!feed-us-txt.newsdeef.eu!feed-us-mo.newsdeef.eu!srv.newsdeef.eu!newsfeed.endofthelinebbs.com!usenet.network!.POSTED!vger.kernel.org!git+bounces-76968-linux-kernel-git=dewy.ru

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!feed-us-txt.newsdeef.eu!feed-us-mo.newsdeef.eu!srv.newsdeef.eu!newsfeed.endofthelinebbs.com!usenet.network!.POSTED!vger.kernel.org!git+bounces-76968-linux-kernel-git=dewy.ru
From: devnull+el.prans.net@kernel.org (Elvis Pranskevichus via B4 Relay)
Newsgroups: linux.kernel.git
Subject: [PATCH] diff: honor --no-optional-locks when refreshing the index
Date: Thu, 03 Dec 2026 11:32:57 -0800
Organization: Usenet Network
Message-ID: <20261203-diff-optional-locks-v1-1-ca3fa3f08175@prans.net>
MIME-Version: 1.0
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: 7bit
Injection-Info: usenet.network;
	logging-data="2577725"; mail-complaints-to="abuse@usenet.network"
Cancel-Lock: sha1:5AxVStVdGIC05fiiJUghBT0Spxo= sha256:KQLYwKT0sR+kXtyqL+DLeIVVrJubZw+WyYSI7ThR94M=
	sha1:Z6apMjhYXzwmWaFEwQad46lGQIQ= sha256:Np8IRu7CalpsGnjGJPkY8M4tVUxA6I78oyZXWs0HJMM=
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 320AF1179BD; Thu,  3 Dec 2026 22:34:30 +0300 (MSK)
X-Virus-Scanned: Debian amavisd-new at usenet.network
X-Spam-Flag: NO
X-Spam-Score: -3.64
X-Spam-Status: No, score=-3.64 tagged_above=-9999 required=6.31
	tests=[DCC_REPUT_70_89=0.1, DKIMWL_WL_HIGH=-0.24, DKIM_SIGNED=0.1,
	DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1,
	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=kernel.org
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 X74cYk6kE1y0;
	Thu,  3 Dec 2026 22:34:23 +0300 (MSK)
X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=104.64.211.4; helo=sin.lore.kernel.org; envelope-from=git+bounces-76968-linux-kernel-git=dewy.ru@vger.kernel.org; receiver=<UNKNOWN> 
X-Authentication-Results: OpenDMARC; dmarc=pass (p=quarantine dis=none) header.from=kernel.org
X-Received: from sin.lore.kernel.org (sin.lore.kernel.org [104.64.211.4])
	by mail.dewy.ru (Postfix) with ESMTPS id B2D74117A1D; Thu,  3 Dec 2026 22:34:11 +0300 (MSK)
X-Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.77.114.92])
	by sin.lore.kernel.org (Postfix) with ESMTP id 77DF022283; Thu,  3 Dec 2026 19:34:07 +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=3; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass;
	t=1796326431;
	b=TQu7sqVYBfffHYnKxC+JyQzExktUSLMuWb2OjYiqETGy5/9TxOdzDMFcHfbB29jPhjou
	 haLg1kWKM/6wZfFNoxqNMUqd7YDZIfcwfStYaWjQHfTvAhf74DOkgCtmwQtfsTuuhrD00
	 t9DG4GwBkbx68l9rQfSvifwyFVY8SNKaLv9/vaGSSIIIV9nS/kkpGP9+tr0Nfpp5s3ZXC
	 pCHSVebNJio27ar0A9P3PgB75iv/fnyobUp19yO2mrLjwTQUrddJPKzG2p1Y4Qa0nEmiw
	 mSInmyZY0JaDE8y7O/ToPn/LciB4CPfr3fN1jnYknyODM+C4MfDn7MAwpAhLL+5chSA==
X-ARC-Message-Signature: i=3; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1796326431;
	h=X-Original-To:Received:Received:DKIM-Signature:Received:From:Date:
	 Subject:X-Mailing-List:List-Id:List-Post:List-Help:List-Subscribe:
	 List-Unsubscribe:List-Owner:Precedence:MIME-Version:Content-Type:
	 Message-Id:X-B4-Tracking:X-Change-ID:To:Cc:X-Mailer:
	 X-Developer-Signature:X-Developer-Key:X-Endpoint-Received:
	 X-Original-From:Reply-To:Content-Transfer-Encoding;
	bh=uooslKVweLRe5QvPZnPlJXeiQ9iTCMQNsEpEmsyWJaA=;
	b=KBX71OhhvJIep6otNcPAuTMgJaS5QooiIyWFDrnmS13r2DUmXyBLynQLjXQu4fdoiV+P
	 3U+RabY8f1VuYyf+ha4a9FfIgyjN4fhQoycgXbZ76FvwX0RkhvGxfO4MZzRTDaHCSfQbC
	 cCJIAlGlTAgV5Bo3lURIV6MCRyLUtx5dGf+FY0/BwFo5MDGaXYXfJpCsSuEpm2rMMRX0T
	 EjW47Pdn5kI8iRd335f1pErElPmk5ogaJS7JQConoEIvq8fHdeycFvoAfoCx7uHTdiSg/
	 cVRudvB/Ab694f13H9f1YFFK6j5DgKxqccQCyNeZwV7eAVksf9HNX9b32xd09pq+W7w==
X-ARC-Authentication-Results: i=3; smtp.subspace.kernel.org; 
	dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=I9RtXlLk;
	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 B07F5606A6A4;
	Thu,  3 Dec 2026 19:33:51 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org;
	dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=I9RtXlLk
X-Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=100.103.45.18
X-ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass;
	t=1796326430;
	b=e4Bo3T8xJKHHrvhJqed0541kxppiXgu4x/n4k0aTUw80g6cd5k8zgVB6qUKpxyzr7lIj
	 C28MR2b8lQZ9i5cDTZo2NR7P6tGB3BgEqX9fHhAxkntI2bWgclTbEwvn4bdPQSrnTVvtr
	 OwICkvuY91v4DIWMfrftSHHBUWdlanPsCr+gc3BeNoktvIYcHZzkg4TT7Tt9ovjcmvO8o
	 1f1sm8UZdXivTktOfO2axmAXi/5tg/5Kk/4HGrIN342/BPbEmPzVW6excStc7foQh69AI
	 1tgtZRz2gh7j2VUy1mT9GRcYROZdmELSB5ET1lnUVu6U6REwSONZDdSJp4uHNoA80cQ==
X-ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1796326430;
	h=Received:DKIM-Signature:Received:From:Date:Subject:MIME-Version:
	 Content-Type:Message-Id:X-B4-Tracking:X-Change-ID:To:Cc:X-Mailer:
	 X-Developer-Signature:X-Developer-Key:X-Endpoint-Received:
	 X-Original-From:Reply-To:Content-Transfer-Encoding;
	bh=uooslKVweLRe5QvPZnPlJXeiQ9iTCMQNsEpEmsyWJaA=;
	b=jdOqADHBc+Pkp7ukzGvo6YzmTwHkUVL8/9I+VBhMGp2Wu0lPUJpCFMxUotfVCD3f7VK7
	 viE/XeUbhRbUNZ2fzz89oJeiSsBntZeQCrK6diFI377bpbZYYkVm2cpR4PAOoIr0O/w2g
	 MYu/QYtKIX+8DeYuOQFpszvi2FHJekyATguw+cRzBL724sz+7YWY+Zjmp0elW+RBTL1k0
	 l8OJ3N1OA3PoUdRgIG00UNLfe15kdZgr+Hbbg0IEES1+0y0816t0Wrmw/qI3DQ9mi71MI
	 JVucJZ+UUQYD4WkiDW1dF1QhRPjn1MimfSIqrjigR0QwwwOMyxbvnom04E7NWbJF4dA==
X-ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; 
	dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=I9RtXlLk;
	arc=pass header.oldest-pass=0 smtp.remote-ip=100.103.45.18
X-Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18])
	(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
	 key-exchange X25519 server-signature ECDSA (prime256v1))
	(No client certificate requested)
	by smtp.subspace.kernel.org (Postfix) with ESMTPS id C384D606A322
	for <git@vger.kernel.org>; Thu,  3 Dec 2026 19:33:50 +0000 (UTC)
X-Authentication-Results: smtp.kernel.org; arc=none smtp.remote-ip=100.80.115.77
X-ARC-Seal: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; cv=none;
	t=1796326430;
	b=KVUYhnWV2cgDnAdX0/A0c4x/bv/+h2zBsA+VGkGpSrxqKsXQ9GFimeRyE2TbKu5z3OEY
	 LHiyr3AQAXPEk+FzQIqVCm8J+c3LTHqMKYYcFVt6OHqQnY6Gc0GJz2xg2YLRaFsCen/Gc
	 2xrcB7zecRUmI6OvkAH4zEeojA6dDZg2igH0YfHzuQP0M+7M7lK7axpz/aWuovtig8dn1
	 Ad3C97wbhi0ZjSkVy0LyopeWBIO6zg1iASMUMbgZptnZFb5GEiemN4t1MKroehnRCeEVt
	 4ABQ4mgNTUL8GXoMYhIntGZ2QsQF1pWhunAF4Nezkkf0MM3Mk98rsKKn7G7M6A3IMVw==
X-ARC-Message-Signature: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256;
	c=relaxed/relaxed; t=1796326430;
	h=DKIM-Signature:Received:From:Date:Subject:MIME-Version:Content-Type:
	 Message-Id:X-B4-Tracking:X-Change-ID:To:Cc:X-Mailer:
	 X-Developer-Signature:X-Developer-Key:X-Endpoint-Received:
	 X-Original-From:Reply-To:Content-Transfer-Encoding;
	bh=uooslKVweLRe5QvPZnPlJXeiQ9iTCMQNsEpEmsyWJaA=;
	b=TwyMnANxafZrD3W0PVqlHqopd0XQfpSlJdeTwJ1h3YfeNaRbBae4c4r3FcPJyQXvD3yv
	 TQxRUcuPOz4h4D0fnb0TZNa8tX/X5xjsC4dN7xRTCNhz8HtOvZL6GaMFYC8G7V3NTbRZO
	 Rbz0UZHcnkkVEWGkfPm7FPaxQCWrIHYqgQ/5CZPVQkjKiGyTNskILgYEC7M5e/9EaVWfT
	 XVcNw8PzWfkIWGafX25LIqtRiYpl/0J53i/UxKsWgo8O/mKZK0hq90iM7uSo8Q3mM4sRn
	 gwqRRSv7TLj/qZ5js03wBffua8fVYaXMfEiUn8JqDTYRgoH/8LTbOCvYbE28YtVoDmg==
X-ARC-Authentication-Results: i=1; smtp.kernel.org; 
	arc=none smtp.remote-ip=100.80.115.77
X-Received: from lkml.kernel.org (nexus.subspace.kernel.org [100.80.115.77])
	(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
	 key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256)
	(No client certificate requested)
	by smtp.kernel.org (Postfix) with ESMTPS id 6DB231F000C2;
	Thu,  3 Dec 2026 19:33:47 +0000 (UTC)
X-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org;
	s=k20260515; t=1796326430;
	bh=uooslKVweLRe5QvPZnPlJXeiQ9iTCMQNsEpEmsyWJaA=;
	h=From:Date:Subject:To:Cc:Reply-To;
	b=I9RtXlLkkJ5vzsUaixni0GefMI+u0XKmy9qknjDXEgFaiLoG0lXLSz84JqudoDvPZ
	 CbTBHdm2nFJss9ohNb7t7z8ivzahVdoL40mF1xBrDRIvVlEloXhr0KnVva/QTMou0w
	 biiwaT7vBVBe7WcoivP01UXVw4VbEv2oFHFYS01lgOlSeTA455joS1jTerylbcr1Ol
	 9iqcC2ErXxEZq7mBF3/x+8cc5pwWKXiI0rV4eRCQMCNvv/SskJI2G3mZJ2pK6morO4
	 XItJm1DpwaNXfCYSuCsoAAYjJ8N957+/bbZpOR+9npsXJSmUbZgJ8/ivxaarI/Xcql
	 iPDBu5gfwUfoQ==
X-Received: from nexus.subspace.kernel.org (localhost [IPv6:::1])
	by lkml.kernel.org (Postfix) with ESMTP id C25DB4FAC2;
	Thu,  3 Dec 2026 19:33:46 +0000 (UTC)
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-B4-Tracking: v=1; b=H4sIAAAAAAAC/yXMQQqDMBCF4avIrB3QtFXjVcSFTSbtqBjJWBHEu
 xvr8oP3/h2EApNAnewQaGVhP0XkaQLm200fQrbRoDJV5Cp7oGXn0M9L3HUjjt4MgtpZren5qsi
 WEJ9zIMfbv9q0t+X37sksVwqO4wRFqzqCdwAAAA==
X-Change-ID: 20261203-diff-optional-locks-9fd99e458ed7
X-Cc: Junio C Hamano <gitster@pobox.com>, Jeff King <peff@peff.net>, 
 Elijah Newren <newren@gmail.com>, Elvis Pranskevichus <el@prans.net>
X-Mailer: b4 0.15.2
X-Developer-Signature: v=1; a=openpgp-sha256; l=4378; i=el@prans.net;
 h=from:subject:message-id;
 bh=W5Fv5eQLA9oLxm9Mnf97ySPFwVIU0yfz849mCgEO4dQ=;
 b=owEBiQJ2/ZANAwAIAVA2MjqpipzsAcsmYgBrEcQavWUOL247mhqBCbawmWboua27VVm51jZT3
 a7mNWO8eeCJAk8EAAEIADkWIQTBxwvgDTX/s9V480tQNjI6qYqc7AUCaxHEGhsUgAAAAAAEAA5t
 YW51MiwyLjUrMS4xMiwzLDIACgkQUDYyOqmKnOyGPRAAunTaPHxxEwnQp7KVmgOkWs95LgbVtov
 V89P6we7Yl3syaalSmF27f6vvW5yBnXbQfhPCsMFPK0cMEGHHAsxAycyXaui1QkmeiX1zIhcg9F
 VfeP5KKjKy7M449+Y1w5QCCBcNK/uWlmGSNI/UJMQzaYXq3H/fK54TGKeYT8HT8R1eZGAZFReO0
 pwwNp9BiUL8sGm60ZDHcGF1ntT3p9fVzVoXaF5742sjYMNrT08IZMg5mgV8e0yiDtKhrsK/ZuXh
 PiGcY9kv5eCenxRlc+NbxA5WGBzlEgDPFXJueVSZyjMRrijFIcnxYLPly9nk8//BdE8oKe7kzCA
 4xN8gIiOsAUZOvY9m1cymW+g9EhkMkwEkNqkcAEfnPp+G+G6RWWzijtpV4ypYIBwgOVMA9Ep52l
 xhChROWATxORFUEmNaJVGZFJXixDRuqz3xFAqVRPoLqT9PLAgWo9YZorvZVvyN9iK+cyMHfAzFz
 98upqieH7zqOHgxPHQXURU/18SKIjskRpTdQkab8q66e4Rt4gcpQUtA+2ZdnvXbLIL33I1w+Jwj
 Jmcr4D4LilCg5D8pYgn1j7BswOFzRvoSqzriGsj8G5dJxGoMxPU282ck+B6OcQqxp96uR4e4N80
 PpOpqFualiS2q3tAbwCGyH8/8sqIrxX+hfKPMhcyAItPIIEnFPKk=
X-Developer-Key: i=el@prans.net; a=openpgp;
 fpr=DC5DF68AFF7AB9EFA5AD2E09C105B3AAFCEA6BB1
X-Endpoint-Received: by B4 Relay for el@prans.net/default with auth_id=1735
X-Original-From: Elvis Pranskevichus <el@prans.net>
X-Reply-To: el@prans.net