From ff11932a650192f7ee0f0cf09d6f4b03e9dcfa33 Mon Sep 17 00:00:00 2001 From: l0ng-ai Date: Sat, 21 Nov 2026 01:45:09 +0800 Subject: [PATCH] fix(vcs): keep git status from rewriting the index (#4880) Refs #4792 The dirty-scope status refresh (and the full one when gitoxide can't read the tree) ran git status, which writes refreshed stat data back to .git/index. The watcher can't tell that write from an external one, so it scheduled another refresh that could run git again. git status now runs with GIT_OPTIONAL_LOCKS=0 and leaves the index alone; our own gitoxide write-back is unchanged. --- crates/ide-vcs/src/status.rs | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/crates/ide-vcs/src/status.rs b/crates/ide-vcs/src/status.rs index d427a7af6..3a04c41b1 100644 --- a/crates/ide-vcs/src/status.rs +++ b/crates/ide-vcs/src/status.rs @@ -235,6 +235,12 @@ fn gix_status(r: &Repo, ignored: &IgnoredNames) -> IdeResult { Ok((map, conflicts)) } +/// Keeps `git status` from rewriting the index with refreshed stat data. Such a write is not +/// one of ours to the watcher, so it would schedule another refresh, which runs git again (#4792). +fn read_only() -> [(&'static str, &'static std::ffi::OsStr); 1] { + [("GIT_OPTIONAL_LOCKS", std::ffi::OsStr::new("0"))] +} + /// `git status --porcelain=v2`: what git lists, unreadable directories skipped. /// In Restricted Mode git compares content without the repository's clean/process /// filters and doesn't ask submodules for their own status (which runs their config). @@ -245,7 +251,7 @@ fn cli_status(r: &Repo, ignored: &IgnoredNames) -> IdeResult { if !crate::execution_allowed() { args.push("--ignore-submodules=dirty"); } - let out = crate::cli::run(r.root(), &args, None)?; + let out = crate::cli::run_env(r.root(), &args, None, &read_only())?; Ok(parse_porcelain_v2(&String::from_utf8_lossy(&out.stdout), ignored)) } @@ -260,7 +266,7 @@ fn cli_status_paths(r: &Repo, ignored: &IgnoredNames, paths: &[String]) -> IdeRe } args.push("--"); args.extend(paths.iter().map(String::as_str)); - let out = crate::cli::run(r.root(), &args, None)?; + let out = crate::cli::run_env(r.root(), &args, None, &read_only())?; Ok(parse_porcelain_v2(&String::from_utf8_lossy(&out.stdout), ignored)) } @@ -397,6 +403,29 @@ mod tests { assert!(staged.stdout.is_empty(), "{}", String::from_utf8_lossy(&staged.stdout)); } + /// #4792: the dirty-scope `git status` must not rewrite the index after refreshing stale + /// stat data, or the watcher sees a foreign index write and refreshes again. + #[test] + fn git_status_of_paths_leaves_the_index_alone() { + let tmp = tempfile::tempdir().unwrap(); + let dir = tmp.path(); + git(dir, &["init", "-q"]); + git(dir, &["config", "user.name", "Test User"]); + git(dir, &["config", "user.email", "test@example.com"]); + git(dir, &["config", "commit.gpgsign", "false"]); + std::fs::write(dir.join("a.txt"), "a\n").unwrap(); + commit_all(dir, "base"); + // Same content, new stat data: plain `git status` would refresh and write the index. + std::thread::sleep(std::time::Duration::from_millis(1100)); + std::fs::write(dir.join("a.txt"), "a\n").unwrap(); + let index = dir.join(".git/index"); + let before = std::fs::read(&index).unwrap(); + + let repo = Repo::discover(dir).unwrap().expect("repository found"); + assert!(status_paths(&repo, &["a.txt".to_owned()]).unwrap().is_empty()); + assert_eq!(std::fs::read(&index).unwrap(), before, "git status rewrote the index"); + } + /// git's porcelain answer (used when gitoxide can't read the tree) matches gitoxide's. #[test] fn git_and_gitoxide_report_the_same_status() {