From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=100.103.45.18 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== 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== 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 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 ; Thu, 3 Dec 2026 19:33:50 +0000 (UTC) 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 Authentication-Results: smtp.kernel.org; arc=none smtp.remote-ip=100.80.115.77 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== 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== ARC-Authentication-Results: i=1; smtp.kernel.org; arc=none smtp.remote-ip=100.80.115.77 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) 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== 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) From: Elvis Pranskevichus via B4 Relay Date: Thu, 03 Dec 2026 11:32:57 -0800 Subject: [PATCH] diff: honor --no-optional-locks when refreshing the index X-Mailing-List: git@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Message-Id: <20261203-diff-optional-locks-v1-1-ca3fa3f08175@prans.net> X-B4-Tracking: v=1; b=H4sIAAAAAAAC/yXMQQqDMBCF4avIrB3QtFXjVcSFTSbtqBjJWBHEu xvr8oP3/h2EApNAnewQaGVhP0XkaQLm200fQrbRoDJV5Cp7oGXn0M9L3HUjjt4MgtpZren5qsi WEJ9zIMfbv9q0t+X37sksVwqO4wRFqzqCdwAAAA== X-Change-ID: 20261203-diff-optional-locks-9fd99e458ed7 To: git@vger.kernel.org Cc: Junio C Hamano , Jeff King , Elijah Newren , Elvis Pranskevichus 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 Reply-To: el@prans.net Content-Transfer-Encoding: 7bit From: Elvis Pranskevichus 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 --- 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