@@ -6,7 +6,9 @@
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`. If
+ `--no-optional-locks` is set (see linkgit:git[1] for
+ details), the index file is not updated.
`diff.dirstat`::
ifdef::git-diff[]
@@ -190,7 +190,8 @@ If you just want to run git as if it was started in `<path>` then use
--no-optional-locks::
Do not perform optional operations that require locks. This is
equivalent to setting the `GIT_OPTIONAL_LOCKS` to `0`. This
- functionality is implemented for `git status` and `git describe`.
+ functionality is implemented for `git status`, `git describe`,
+ and `git diff`.
--no-advice::
Disable all advice hints from being printed.
@@ -9,6 +9,7 @@
#include "builtin.h"
#include "config.h"
+#include "environment.h"
#include "ewah/ewok.h"
#include "lockfile.h"
#include "color.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;
@@ -500,6 +500,7 @@ integration_tests = [
't4067-diff-partial-clone.sh',
't4068-diff-symmetric-merge-base.sh',
't4069-remerge-diff.sh',
+ 't4070-diff-auto-refresh-index.sh',
't4100-apply-stat.sh',
't4101-apply-nonl.sh',
't4102-apply-rename.sh',
new file mode 100755
@@ -0,0 +1,46 @@
+#!/bin/sh
+#
+# Copyright (c) 2025 Benjamin Woodruff
+#
+
+test_description='diff.autoRefreshIndex config option'
+
+. ./test-lib.sh
+. "$TEST_DIRECTORY"/lib-diff.sh
+
+test_expect_success 'index is updated when autoRefreshIndex is true' '
+ >tracked &&
+ git add tracked &&
+
+ # stat() must change (but not file contents) to trigger an index update
+ test_set_magic_mtime tracked &&
+
+ # check the mtime of .git/index does not change without autoRefreshIndex
+ test_set_magic_mtime .git/index &&
+ git config diff.autoRefreshIndex false &&
+ git diff &&
+ test_is_magic_mtime .git/index &&
+
+ # but it does change when autoRefreshIndex is true (the default)
+ git config diff.autoRefreshIndex true &&
+ git diff &&
+ ! test_is_magic_mtime .git/index
+'
+
+test_expect_success '--no-optional-locks overrides autoRefreshIndex' '
+ >tracked &&
+ git add tracked &&
+ test_set_magic_mtime tracked &&
+
+ # `--no-optional-locks` overrides `autoRefreshIndex`
+ test_set_magic_mtime .git/index &&
+ git config diff.autoRefreshIndex true &&
+ git --no-optional-locks diff &&
+
+ # sanity check that without `--no-optional-locks` it still updates
+ test_is_magic_mtime .git/index &&
+ git diff &&
+ ! test_is_magic_mtime .git/index
+'
+
+test_done