Git development
 help / color / mirror / Atom feed
* [PATCH] diff-lib: skip leading path check for fsmonitor-valid entries
@ 2026-11-18 18:22 Maik Riechert via GitGitGadget
  2026-11-18 19:01 ` Junio C Hamano
  0 siblings, 1 reply; 2+ messages in thread
From: Maik Riechert via GitGitGadget @ 2026-11-18 18:22 UTC (permalink / raw)
  To: git; +Cc: Josip Sokcevic, Maik Riechert, Maik Riechert

From: Maik Riechert <letmaik@outlook.com>

Since 4f3d6d0261 (fsmonitor: skip lstat deletion check during git
diff-index, 2021-03-17), check_removed() does not lstat() entries
that fsmonitor says are unchanged. But it still calls
has_symlink_leading_path() for them, which lstat()s the directories
leading up to each path.

That check is there to notice a leading directory that was replaced
by a symlink. fsmonitor would have reported such a change, and
refresh_fsmonitor() would have cleared CE_FSMONITOR_VALID for every
entry below it, so we would not get here. preload_index() already
trusts the flag this way and skips these entries altogether.

The S_ISDIR() check that follows cannot trigger either: the mode
comes from the index, and gitlinks are never marked valid.

So return as soon as fake_lstat() has filled in the stat data.

The first new test checks the shortcut itself by replacing a parent
directory with a symlink without telling fsmonitor. The other two make
sure we still notice when fsmonitor does report that directory, with
and without a trailing slash, since nothing else checks for it now.

On Linux, with 100,000 files (ten per directory), a hook that
reports nothing and a warm cache, "git diff HEAD" goes from 57ms to
26ms.

Signed-off-by: Maik Riechert <letmaik@outlook.com>
---
    diff-lib: skip leading path check for fsmonitor-valid entries

Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-2318%2Fletmaik%2Fdiff-lib-fsmonitor-leading-path-v1
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-2318/letmaik/diff-lib-fsmonitor-leading-path-v1
Pull-Request: https://github.com/gitgitgadget/git/pull/2318

 diff-lib.c                  |  8 +++----
 t/t7519-status-fsmonitor.sh | 44 +++++++++++++++++++++++++++++++++++++
 2 files changed, 48 insertions(+), 4 deletions(-)

diff --git a/diff-lib.c b/diff-lib.c
index 086476bd77..3730e04149 100644
--- a/diff-lib.c
+++ b/diff-lib.c
@@ -43,10 +43,10 @@ static int check_removed(const struct cache_entry *ce, struct stat *st)
 {
 	int stat_err;
 
-	if (!(ce->ce_flags & CE_FSMONITOR_VALID))
-		stat_err = lstat(ce->name, st);
-	else
-		stat_err = fake_lstat(ce, st);
+	if (ce->ce_flags & CE_FSMONITOR_VALID)
+		return fake_lstat(ce, st);
+
+	stat_err = lstat(ce->name, st);
 	if (stat_err < 0) {
 		if (!is_missing_file_error(errno))
 			return -1;
diff --git a/t/t7519-status-fsmonitor.sh b/t/t7519-status-fsmonitor.sh
index 93973ed25a..6c9df9f333 100755
--- a/t/t7519-status-fsmonitor.sh
+++ b/t/t7519-status-fsmonitor.sh
@@ -336,6 +336,50 @@ do
 	done
 done
 
+# Create a repository whose only entry, dir1/tracked, is marked valid
+# by an fsmonitor hook that reports no changes, then replace dir1 with
+# a symlink to a directory that has the same contents.
+setup_symlinked_leading_dir () {
+	test_when_finished "rm -rf repo" &&
+	git init repo &&
+	mkdir repo/dir1 &&
+	: >repo/dir1/tracked &&
+	git -C repo add dir1/tracked &&
+	git -C repo commit -m initial &&
+	test_hook -C repo --setup fsmonitor-test <<-\EOF &&
+	printf "last_update_token\0"
+	EOF
+	git -C repo config core.fsmonitor .git/hooks/fsmonitor-test &&
+	git -C repo update-index --refresh --force-write-index &&
+	echo "h dir1/tracked" >expect &&
+	git -C repo ls-files -f >actual &&
+	test_cmp expect actual &&
+	mv repo/dir1 repo/dir2 &&
+	ln -s dir2 repo/dir1
+}
+
+# Like "status" above, diff-index should trust fsmonitor and not look
+# for symlinks in leading directories that it was not told about.
+test_expect_success SYMLINKS "diff-index doesn't detect unreported symlinked directories" '
+	setup_symlinked_leading_dir &&
+	git -C repo diff-index --name-status HEAD >actual &&
+	test_must_be_empty actual
+'
+
+for report in dir1 dir1/
+do
+	test_expect_success SYMLINKS "diff-index detects symlinked directory reported as $report" '
+		setup_symlinked_leading_dir &&
+		test_hook -C repo --clobber fsmonitor-test <<-EOF &&
+		printf "last_update_token\0"
+		printf "$report\0"
+		EOF
+		printf "D\tdir1/tracked\n" >expect &&
+		git -C repo diff-index --name-status HEAD >actual &&
+		test_cmp expect actual
+	'
+done
+
 # test that splitting the index doesn't interfere
 test_expect_success 'splitting the index results in the same state' '
 	write_integration_script &&

base-commit: e1ec09b23c8e0deb21a0b95ac5996422277395d4
-- 
gitgitgadget

^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] diff-lib: skip leading path check for fsmonitor-valid entries
  2026-11-18 18:22 [PATCH] diff-lib: skip leading path check for fsmonitor-valid entries Maik Riechert via GitGitGadget
@ 2026-11-18 19:01 ` Junio C Hamano
  0 siblings, 0 replies; 2+ messages in thread
From: Junio C Hamano @ 2026-11-18 19:01 UTC (permalink / raw)
  To: Maik Riechert via GitGitGadget; +Cc: git, Josip Sokcevic, Maik Riechert

"Maik Riechert via GitGitGadget" <gitgitgadget@gmail.com> writes:

> diff --git a/diff-lib.c b/diff-lib.c
> index 086476bd77..3730e04149 100644
> --- a/diff-lib.c
> +++ b/diff-lib.c
> @@ -43,10 +43,10 @@ static int check_removed(const struct cache_entry *ce, struct stat *st)
>  {
>  	int stat_err;
>  
> -	if (!(ce->ce_flags & CE_FSMONITOR_VALID))
> -		stat_err = lstat(ce->name, st);
> -	else
> -		stat_err = fake_lstat(ce, st);
> +	if (ce->ce_flags & CE_FSMONITOR_VALID)
> +		return fake_lstat(ce, st);
> +
> +	stat_err = lstat(ce->name, st);
>  	if (stat_err < 0) {
>  		if (!is_missing_file_error(errno))
>  			return -1;

Now that stat_err is used only once, we can retire the variable and
inline the lstat() call inside the if() condition.

Other than that, assuming that the CE_FSMONITOR_VALID bit is
trustworthy, the change looks OK.  has_symlink_leading_path() would
be fairly expensive and being able to avoid it is a big plus, which
is done in other code paths like read-cache.c:refresh_cache_ent()
and preload-index.c:preload_thread() already.


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-11-18 19:01 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-18 18:22 [PATCH] diff-lib: skip leading path check for fsmonitor-valid entries Maik Riechert via GitGitGadget
2026-11-18 19:01 ` Junio C Hamano

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox