{"thread":{"id":"66917","subject":"[PATCH] dir: skip prep_exclude() when fsmonitor is trusted","startedAt":"2026-11-22T22:29:51Z","lastAt":"2026-11-22T22:29:51Z","messageCount":1,"participants":["Stepan Koltsov via GitGitGadget"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"559118","messageId":"pull.2501.git.git.1795386591089.gitgitgadget@gmail.com","threadId":"66917","inReplyTo":null,"subject":"[PATCH] dir: skip prep_exclude() when fsmonitor is trusted","fromName":"Stepan Koltsov via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-11-22T22:29:51Z","receivedAt":"2026-11-22T22:29:51Z","isPatch":true,"sender":{"key":"stepan.koltsov@jetbrains.com","avatar":null},"body":"From: Stepan Koltsov <stepan.koltsov@gmail.com>\n\n\"git status\" calls prep_exclude() for every directory in the\nuntracked cache, to drop a cache entry if its .gitignore rules\nchanged. That matches every directory against the exclude patterns\nand reads every .gitignore. With fsmonitor it is unnecessary:\nfsmonitor already drops the cache entry when anything in the\ndirectory changes, including .gitignore.\n\nSkip the call in valid_cached_dir() when fsmonitor is on and the\ncache entry is still valid.\n\nOne case is left: a .gitignore is edited while the monitor is not\nwatching, for example while the daemon is stopped. The monitor then\nsends a trivial response. Since 1cf98cb4d9 (fsmonitor: check the\nuntracked cache after a trivial response, 2026-11-09) the cached\ndirectories are rechecked after one, but only by their stat data.\nEditing a file in place does not change the stat data of its\ndirectory, so only prep_exclude() notices the edit. Make that\nrecheck also invalidate every directory that has a .gitignore.\n\nThis is a trade-off: each such directory is read again once per\ntrivial response. Hashing its .gitignore instead would avoid that,\nbut would have to reproduce how add_patterns() chooses the object\nname it records, so prefer the simpler check.\n\nOn macOS with the builtin fsmonitor daemon, in a tree of about\n89,000 directories with a warm untracked cache,\n\"git status --untracked-files=all\" went from about 310 ms to about\n65 ms, and the time spent in read_directory() from about 255 ms to\nabout 8 ms.\n\nAssisted-by: LLM\nSigned-off-by: Stepan Koltsov <stepan.koltsov@gmail.com>\n---\n    dir: skip prep_exclude() when fsmonitor is trusted\n\nPublished-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-2501%2Fstepancheg%2Ffsmonitor-skip-prep-exclude-v1\nFetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-2501/stepancheg/fsmonitor-skip-prep-exclude-v1\nPull-Request: https://github.com/git/git/pull/2501\n\n dir.c                        | 15 ++++++-\n dir.h                        |  7 +--\n t/t7527-builtin-fsmonitor.sh | 82 ++++++++++++++++++++++++++++++++++++\n 3 files changed, 100 insertions(+), 4 deletions(-)\n\ndiff --git a/dir.c b/dir.c\nindex 4f748800b1..48b11b1636 100644\n--- a/dir.c\n+++ b/dir.c\n@@ -2568,6 +2568,14 @@ static int valid_cached_dir(struct dir_struct *dir,\n \tif (untracked->check_only != !!check_only)\n \t\treturn 0;\n \n+\t/*\n+\t * With fsmonitor, a still-valid entry has had no working-tree\n+\t * changes, including to .gitignore, so the prep_exclude()\n+\t * check below is redundant.\n+\t */\n+\tif (dir->untracked->use_fsmonitor && untracked->valid)\n+\t\treturn 1;\n+\n \t/*\n \t * prep_exclude will be called eventually on this directory,\n \t * but it's called much later in last_matching_pattern(). We\n@@ -4076,8 +4084,13 @@ static int invalidate_stale_dirs(struct untracked_cache *uc,\n \tint nr_invalidated = 0;\n \tunsigned int i;\n \n+\t/*\n+\t * Editing .gitignore in place does not change the stat data of\n+\t * its directory, so do not trust a directory that has one.\n+\t */\n \tif (ucd->valid &&\n-\t    (lstat(path->buf, &st) ||\n+\t    (!is_null_oid(&ucd->exclude_oid) ||\n+\t     lstat(path->buf, &st) ||\n \t     match_stat_data_racy(istate, &ucd->stat_data, &st))) {\n \t\tinvalidate_one_directory(uc, ucd);\n \t\tnr_invalidated++;\ndiff --git a/dir.h b/dir.h\nindex 7af1562b44..b945068072 100644\n--- a/dir.h\n+++ b/dir.h\n@@ -605,9 +605,10 @@ void untracked_cache_invalidate_trimmed_path(struct index_state *,\n \t\t\t\t\t     const char *path,\n \t\t\t\t\t     int safe_path);\n /*\n- * Invalidate every cached directory that no longer exists or whose\n- * stat data no longer matches the working tree. valid_cached_dir()\n- * skips this check while the file system monitor is trusted.\n+ * Invalidate every cached directory that no longer exists, whose\n+ * stat data no longer matches the working tree, or that has a\n+ * .gitignore, which may have been edited in place. valid_cached_dir()\n+ * skips these checks while the file system monitor is trusted.\n  * Returns the number of invalidated directories.\n  */\n int untracked_cache_invalidate_stale_dirs(struct index_state *);\ndiff --git a/t/t7527-builtin-fsmonitor.sh b/t/t7527-builtin-fsmonitor.sh\nindex f2770e9056..d46b90739f 100755\n--- a/t/t7527-builtin-fsmonitor.sh\n+++ b/t/t7527-builtin-fsmonitor.sh\n@@ -1429,4 +1429,86 @@ test_expect_success UNTRACKED_CACHE 'untracked cache is checked after a trivial\n \ttest_cmp expect actual\n '\n \n+test_expect_success UNTRACKED_CACHE 'untracked cache sees .gitignore edit after a trivial response' '\n+\ttest_when_finished \"stop_daemon_delete_repo test_trivial_gi\" &&\n+\n+\tgit init test_trivial_gi &&\n+\tmkdir -p test_trivial_gi/dir/sub &&\n+\techo secret >test_trivial_gi/dir/sub/.gitignore &&\n+\techo x >test_trivial_gi/dir/sub/secret &&\n+\techo y >test_trivial_gi/dir/sub/visible &&\n+\tgit -C test_trivial_gi add dir/sub/.gitignore &&\n+\tgit -C test_trivial_gi commit -m initial &&\n+\tgit -C test_trivial_gi config core.fsmonitor true &&\n+\tgit -C test_trivial_gi config core.untrackedCache true &&\n+\n+\t# A directory changed in the same second as the index was\n+\t# written is rechecked anyway. Make dir/sub older, so that\n+\t# only its .gitignore can get it invalidated.\n+\ttest-tool chmtime =-60 test_trivial_gi/dir/sub &&\n+\n+\techo \"?? dir/sub/visible\" >expect &&\n+\tgit -C test_trivial_gi status --porcelain >actual &&\n+\ttest_cmp expect actual &&\n+\tgit -C test_trivial_gi status --porcelain >actual &&\n+\ttest_cmp expect actual &&\n+\n+\t# Edit .gitignore in place while no daemon is running. \"git add\"\n+\t# then starts a new daemon, receives its trivial response, and\n+\t# does not look for untracked files.\n+\tgit -C test_trivial_gi fsmonitor--daemon stop &&\n+\techo visible >test_trivial_gi/dir/sub/.gitignore &&\n+\techo other >test_trivial_gi/other &&\n+\tGIT_TRACE2_EVENT=\"$PWD/trace_trivial_gi\" \\\n+\t\tgit -C test_trivial_gi add other &&\n+\thave_t2_data_event fsm_client query/trivial-response <trace_trivial_gi &&\n+\tgit -C test_trivial_gi fsmonitor--daemon status &&\n+\n+\tcat >expect <<-\\EOF &&\n+\t M dir/sub/.gitignore\n+\tA  other\n+\t?? dir/sub/secret\n+\tEOF\n+\tgit -C test_trivial_gi status --porcelain >actual &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success UNTRACKED_CACHE 'untracked cache sees .gitignore edit reported by fsmonitor' '\n+\ttest_when_finished \"stop_daemon_delete_repo test_gitignore\" &&\n+\n+\tgit init test_gitignore &&\n+\tmkdir -p test_gitignore/dir/sub &&\n+\techo secret >test_gitignore/dir/.gitignore &&\n+\techo tracked >test_gitignore/dir/sub/tracked &&\n+\techo x >test_gitignore/dir/sub/secret &&\n+\techo y >test_gitignore/dir/sub/visible &&\n+\tgit -C test_gitignore add dir/.gitignore dir/sub/tracked &&\n+\tgit -C test_gitignore commit -m initial &&\n+\tgit -C test_gitignore config core.fsmonitor true &&\n+\tgit -C test_gitignore config core.untrackedCache true &&\n+\n+\t# The first status starts the daemon and builds the untracked\n+\t# cache, the second one saves what the trivial response of the\n+\t# new daemon left invalid, and the third one trusts all of it.\n+\techo \"?? dir/sub/visible\" >expect &&\n+\tgit -C test_gitignore status --porcelain >actual &&\n+\ttest_cmp expect actual &&\n+\tgit -C test_gitignore status --porcelain >actual &&\n+\ttest_cmp expect actual &&\n+\tGIT_TRACE2_EVENT=\"$PWD/trace_gitignore\" \\\n+\t\tgit -C test_gitignore status --porcelain >actual &&\n+\ttest_cmp expect actual &&\n+\ttest_trace2_data read_directory opendir 0 <trace_gitignore &&\n+\n+\t# dir/sub has no .gitignore of its own, the rules for it change\n+\t# with the one in its parent directory.\n+\techo visible >test_gitignore/dir/.gitignore &&\n+\tcat >expect <<-\\EOF &&\n+\t M dir/.gitignore\n+\t?? dir/sub/secret\n+\tEOF\n+\tgit -C test_gitignore status --porcelain >actual &&\n+\ttest_cmp expect actual\n+'\n+\n test_done\n\nbase-commit: f7de81456940dd6f3274e461149bba12965de609\n-- \ngitgitgadget\n\n"}]}