[PATCH] dir: skip prep_exclude() when fsmonitor is trusted
- From
- Stepan Koltsov via GitGitGadget <gitgitgadget@gmail.com>
- Date
- Nov 22, 2026, 22:29 UTC
- Message-ID
- <pull.2501.git.git.1795386591089.gitgitgadget@gmail.com>
From: Stepan Koltsov <stepan.koltsov@gmail.com>
"git status" calls prep_exclude() for every directory in the untracked cache, to drop a cache entry if its .gitignore rules changed. That matches every directory against the exclude patterns and reads every .gitignore. With fsmonitor it is unnecessary: fsmonitor already drops the cache entry when anything in the directory changes, including .gitignore.
Skip the call in valid_cached_dir() when fsmonitor is on and the cache entry is still valid.
One case is left: a .gitignore is edited while the monitor is not watching, for example while the daemon is stopped. The monitor then sends a trivial response. Since 1cf98cb4d9 (fsmonitor: check the untracked cache after a trivial response, 2026-11-09) the cached directories are rechecked after one, but only by their stat data. Editing a file in place does not change the stat data of its directory, so only prep_exclude() notices the edit. Make that recheck also invalidate every directory that has a .gitignore.
This is a trade-off: each such directory is read again once per trivial response. Hashing its .gitignore instead would avoid that, but would have to reproduce how add_patterns() chooses the object name it records, so prefer the simpler check.
On macOS with the builtin fsmonitor daemon, in a tree of about 89,000 directories with a warm untracked cache, "git status --untracked-files=all" went from about 310 ms to about 65 ms, and the time spent in read_directory() from about 255 ms to about 8 ms.
Assisted-by: LLM
Signed-off-by: Stepan Koltsov <stepan.koltsov@gmail.com>
---
dir: skip prep_exclude() when fsmonitor is trustedPublished-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-2501%2Fstepancheg%2Ffsmonitor-skip-prep-exclude-v1 Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-2501/stepancheg/fsmonitor-skip-prep-exclude-v1 Pull-Request: https://github.com/git/git/pull/2501
dir.c | 15 ++++++- dir.h | 7 +-- t/t7527-builtin-fsmonitor.sh | 82 ++++++++++++++++++++++++++++++++++++ 3 files changed, 100 insertions(+), 4 deletions(-)
diff --git a/dir.c b/dir.c index 4f748800b1..48b11b1636 100644 --- a/dir.c +++ b/dir.c @@ -2568,6 +2568,14 @@ static int valid_cached_dir(struct dir_struct *dir, if (untracked->check_only != !!check_only) return 0; + /* + * With fsmonitor, a still-valid entry has had no working-tree + * changes, including to .gitignore, so the prep_exclude() + * check below is redundant. + */ + if (dir->untracked->use_fsmonitor && untracked->valid) + return 1; + /* * prep_exclude will be called eventually on this directory, * but it's called much later in last_matching_pattern(). We @@ -4076,8 +4084,13 @@ static int invalidate_stale_dirs(struct untracked_cache *uc, int nr_invalidated = 0; unsigned int i; + /* + * Editing .gitignore in place does not change the stat data of + * its directory, so do not trust a directory that has one. + */ if (ucd->valid && - (lstat(path->buf, &st) || + (!is_null_oid(&ucd->exclude_oid) || + lstat(path->buf, &st) || match_stat_data_racy(istate, &ucd->stat_data, &st))) { invalidate_one_directory(uc, ucd); nr_invalidated++; diff --git a/dir.h b/dir.h index 7af1562b44..b945068072 100644 --- a/dir.h +++ b/dir.h @@ -605,9 +605,10 @@ void untracked_cache_invalidate_trimmed_path(struct index_state *, const char *path, int safe_path); /* - * Invalidate every cached directory that no longer exists or whose - * stat data no longer matches the working tree. valid_cached_dir() - * skips this check while the file system monitor is trusted. + * Invalidate every cached directory that no longer exists, whose + * stat data no longer matches the working tree, or that has a + * .gitignore, which may have been edited in place. valid_cached_dir() + * skips these checks while the file system monitor is trusted. * Returns the number of invalidated directories. */ int untracked_cache_invalidate_stale_dirs(struct index_state *); diff --git a/t/t7527-builtin-fsmonitor.sh b/t/t7527-builtin-fsmonitor.sh index f2770e9056..d46b90739f 100755 --- a/t/t7527-builtin-fsmonitor.sh +++ b/t/t7527-builtin-fsmonitor.sh @@ -1429,4 +1429,86 @@ test_expect_success UNTRACKED_CACHE 'untracked cache is checked after a trivial test_cmp expect actual ' +test_expect_success UNTRACKED_CACHE 'untracked cache sees .gitignore edit after a trivial response' ' + test_when_finished "stop_daemon_delete_repo test_trivial_gi" && + + git init test_trivial_gi && + mkdir -p test_trivial_gi/dir/sub && + echo secret >test_trivial_gi/dir/sub/.gitignore && + echo x >test_trivial_gi/dir/sub/secret && + echo y >test_trivial_gi/dir/sub/visible && + git -C test_trivial_gi add dir/sub/.gitignore && + git -C test_trivial_gi commit -m initial && + git -C test_trivial_gi config core.fsmonitor true && + git -C test_trivial_gi config core.untrackedCache true && + + # A directory changed in the same second as the index was + # written is rechecked anyway. Make dir/sub older, so that + # only its .gitignore can get it invalidated. + test-tool chmtime =-60 test_trivial_gi/dir/sub && + + echo "?? dir/sub/visible" >expect && + git -C test_trivial_gi status --porcelain >actual && + test_cmp expect actual && + git -C test_trivial_gi status --porcelain >actual && + test_cmp expect actual && + + # Edit .gitignore in place while no daemon is running. "git add" + # then starts a new daemon, receives its trivial response, and + # does not look for untracked files. + git -C test_trivial_gi fsmonitor--daemon stop && + echo visible >test_trivial_gi/dir/sub/.gitignore && + echo other >test_trivial_gi/other && + GIT_TRACE2_EVENT="$PWD/trace_trivial_gi" \ + git -C test_trivial_gi add other && + have_t2_data_event fsm_client query/trivial-response <trace_trivial_gi && + git -C test_trivial_gi fsmonitor--daemon status && + + cat >expect <<-\EOF && + M dir/sub/.gitignore + A other + ?? dir/sub/secret + EOF + git -C test_trivial_gi status --porcelain >actual && + test_cmp expect actual +' + +test_expect_success UNTRACKED_CACHE 'untracked cache sees .gitignore edit reported by fsmonitor' ' + test_when_finished "stop_daemon_delete_repo test_gitignore" && + + git init test_gitignore && + mkdir -p test_gitignore/dir/sub && + echo secret >test_gitignore/dir/.gitignore && + echo tracked >test_gitignore/dir/sub/tracked && + echo x >test_gitignore/dir/sub/secret && + echo y >test_gitignore/dir/sub/visible && + git -C test_gitignore add dir/.gitignore dir/sub/tracked && + git -C test_gitignore commit -m initial && + git -C test_gitignore config core.fsmonitor true && + git -C test_gitignore config core.untrackedCache true && + + # The first status starts the daemon and builds the untracked + # cache, the second one saves what the trivial response of the + # new daemon left invalid, and the third one trusts all of it. + echo "?? dir/sub/visible" >expect && + git -C test_gitignore status --porcelain >actual && + test_cmp expect actual && + git -C test_gitignore status --porcelain >actual && + test_cmp expect actual && + GIT_TRACE2_EVENT="$PWD/trace_gitignore" \ + git -C test_gitignore status --porcelain >actual && + test_cmp expect actual && + test_trace2_data read_directory opendir 0 <trace_gitignore && + + # dir/sub has no .gitignore of its own, the rules for it change + # with the one in its parent directory. + echo visible >test_gitignore/dir/.gitignore && + cat >expect <<-\EOF && + M dir/.gitignore + ?? dir/sub/secret + EOF + git -C test_gitignore status --porcelain >actual && + test_cmp expect actual +' + test_done base-commit: f7de81456940dd6f3274e461149bba12965de609
-- gitgitgadget