From: Elvis Pranskevichus <e…@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 <e…@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
[PATCH] diff: honor --no-optional-locks when refreshing the index
- From
- devn…@kernel.org (Elvis Pranskevichus via B4 Relay)
- Organization
- Usenet Network
- Message-ID
- <2026…@prans.net>
- Date
- Newsgroups
- linux.kernel.git