Git development
 help / color / mirror / Atom feed
From: Elvis Pranskevichus via B4 Relay <devnull+el.prans.net@kernel.org>
To: git@vger.kernel.org
Cc: Junio C Hamano <gitster@pobox.com>, Jeff King <peff@peff.net>,
	 Elijah Newren <newren@gmail.com>,
	Elvis Pranskevichus <el@prans.net>
Subject: [PATCH] diff: honor --no-optional-locks when refreshing the index
Date: Thu, 03 Dec 2026 11:32:57 -0800	[thread overview]
Message-ID: <20261203-diff-optional-locks-v1-1-ca3fa3f08175@prans.net> (raw)

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



                 reply	other threads:[~2026-12-03 19:33 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20261203-diff-optional-locks-v1-1-ca3fa3f08175@prans.net \
    --to=devnull+el.prans.net@kernel.org \
    --cc=el@prans.net \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=newren@gmail.com \
    --cc=peff@peff.net \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox