fix(vcs): only report state files from linked worktree gitdirs (#5730)

The project watcher reported every file below .git/worktrees/<id>/ except locks, so scratch files Git and the IDE write during read-only work (fsmonitor cookies, temporary indexes, reflogs, rebase state) each scheduled another VCS refresh. Linked worktree gitdirs now use the same allowlist as the main .git: HEAD, index, config.worktree, gitdir, commondir, locked, refs/** and the worktree entry itself.

The external metadata watcher only recognised our own status write-backs to .git/index, so a linked worktree's index refreshed VCS after every status run. The check now covers any index file.
Цей коміт міститься в:
l0ng-ai authored and GitHub committed 2026-11-25 09:26:12 +08:00
1 джерело 8e9a0e48cf
коміт 4bc46baddb
2 files changed
+68 -8

No files matched your search

+38 -1
Переглянути файл
@@ -2361,8 +2361,11 @@ fn git_metadata_watch(root: &Path, sink: &SharedSink) -> Option<Arc<ide_vcs::Met
.ok()
}
/// Whether a VCS metadata path changed for a reason other than our own status run
/// refreshing an index: the main repository's `.git/index` or a linked worktree's
/// `.git/worktrees/<id>/index`.
fn vcs_path_changed(path: &Path) -> bool {
!path.ends_with(".git/index") || !ide_vcs::is_own_index_write(path)
path.file_name().is_none_or(|n| n != "index") || !ide_vcs::is_own_index_write(path)
}
#[cfg(test)]
@@ -2446,6 +2449,40 @@ mod index_event_tests {
std::fs::remove_file(&index).unwrap();
assert!(vcs_path_changed(&index));
}
#[test]
fn status_write_in_a_linked_worktree_is_suppressed() {
let tmp = tempfile::tempdir().unwrap();
let main = tmp.path().join("main");
std::fs::create_dir(&main).unwrap();
let git = |dir: &std::path::Path, args: &[&str]| {
let output = std::process::Command::new("git")
.current_dir(dir)
.args(["-c", "user.name=t", "-c", "user.email=t@t"])
.args(args)
.output()
.unwrap();
assert!(output.status.success(), "{}", String::from_utf8_lossy(&output.stderr));
};
git(&main, &["init", "-q"]);
std::fs::write(main.join("file.txt"), "original\n").unwrap();
git(&main, &["add", "file.txt"]);
git(&main, &["commit", "-qm", "init"]);
let linked = tmp.path().join("linked");
git(&main, &["worktree", "add", "-q", linked.to_str().unwrap()]);
std::fs::File::options()
.write(true)
.open(linked.join("file.txt"))
.unwrap()
.set_modified(std::time::SystemTime::UNIX_EPOCH + std::time::Duration::from_secs(1))
.unwrap();
let index = main.join(".git/worktrees/linked/index");
let before = std::fs::read(&index).unwrap();
ide_vcs::Repo::discover(&linked).unwrap().unwrap().status().unwrap();
assert_ne!(before, std::fs::read(&index).unwrap(), "status must actually write the index");
assert!(!vcs_path_changed(&index));
assert!(vcs_path_changed(&main.join(".git/worktrees/linked/HEAD")));
}
}
#[cfg(all(test, unix))]
+30 -7
Переглянути файл
@@ -784,10 +784,25 @@ impl EventFilter {
}
}
/// Relative to `.git/worktrees`: the first component is the worktree ID. Even read-only
/// Git queries create fsmonitor cookies; reporting those would schedule another query.
fn watch_worktree_entry(parts: &[&OsStr]) -> bool {
!matches!(parts, [_, name, ..] if *name == "fsmonitor--daemon" || *name == "fsmonitor--daemon.ipc")
/// Relative to `.git/worktrees`: the first component is the worktree ID. Like the main
/// gitdir, a linked worktree's gitdir only reports the files that describe its state; the
/// rest is Git's (and our own) scratch space. Even read-only queries write there
/// (fsmonitor cookies, temporary indexes, reflogs), and reporting those would schedule
/// another query.
fn worktree_metadata(parts: &[&OsStr]) -> bool {
match parts {
[] | [_] => true,
[_, name] => matches!(
name.to_str(),
Some("HEAD" | "index" | "config.worktree" | "gitdir" | "commondir" | "locked" | "refs")
),
[_, first, ..] => *first == "refs",
}
}
/// Directories below `.git/worktrees` that need a watch: the worktree entries and their refs.
fn watch_worktree_dir(parts: &[&OsStr]) -> bool {
matches!(parts, [] | [_]) || matches!(parts, [_, first, ..] if *first == "refs")
}
/// `Some(true)` for interesting VCS metadata, `Some(false)` for other `.git`
@@ -815,7 +830,7 @@ impl EventFilter {
let metadata = *first == "refs"
// Linked-worktree gitdirs: a worktree added or removed below
// the project writes its HEAD here.
|| (*first == "worktrees" && Self::watch_worktree_entry(parts));
|| (*first == "worktrees" && Self::worktree_metadata(parts));
metadata && !rest.last().is_some_and(|l| l.to_string_lossy().ends_with(".lock"))
}
// The `.git` path itself: a linked worktree's `.git` is a plain
@@ -848,7 +863,7 @@ impl EventFilter {
return match rest.as_slice() {
[] => true,
[first, ..] if *first == "refs" => true,
[first, parts @ ..] if *first == "worktrees" => Self::watch_worktree_entry(parts),
[first, parts @ ..] if *first == "worktrees" => Self::watch_worktree_dir(parts),
[only] => *only == "info",
_ => false,
};
@@ -1502,6 +1517,13 @@ mod tests {
// Linked-worktree gitdir metadata below the main repository.
assert_eq!(g(".git/worktrees/wt/HEAD"), Some(true));
assert_eq!(g(".git/worktrees/wt/index.lock"), Some(false));
for path in ["index", "config.worktree", "gitdir", "commondir", "locked", "refs", "refs/bisect/bad"] {
assert_eq!(g(&format!(".git/worktrees/wt/{path}")), Some(true), "{path}");
}
// Scratch files Git and the IDE write during read-only queries.
for path in ["openidea-index-1-2", "openidea-partial-index", "logs/HEAD", "ORIG_HEAD", "rebase-merge/done"] {
assert_eq!(g(&format!(".git/worktrees/wt/{path}")), Some(false), "{path}");
}
// The depth-1 directories themselves: `git init` or the first
// `git worktree add` creates them in an already-watched gitdir.
assert_eq!(g(".git/refs"), Some(true));
@@ -1719,7 +1741,8 @@ mod tests {
assert!(!w(".git/objects") && !w(".git/objects/ab") && !w(".git/logs"));
// Linked-worktree gitdirs are watched so worktrees added below the
// project while it is open are discovered.
assert!(w(".git/worktrees") && w(".git/worktrees/wt") && w(".git/worktrees/wt/rebase-merge"));
assert!(w(".git/worktrees") && w(".git/worktrees/wt") && w(".git/worktrees/wt/refs/bisect"));
assert!(!w(".git/worktrees/wt/rebase-merge") && !w(".git/worktrees/wt/logs"));
assert!(!filter.should_watch_dir(Path::new("/elsewhere")));
// Excludes are replaced in place (Watcher::set_excludes).
*filter.excludes.write() = ExcludeMatcher::new(&["gen".into()]);