Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
Refresh git state on bare .git events (#59876)
Jiby Jose137c981cb03b
diff --git a/crates/project/tests/integration/project_tests.rs b/crates/project/tests/integration/project_tests.rsindex 1b373941d24..1a5d6d358c9 100644--- a/crates/project/tests/integration/project_tests.rs+++ b/crates/project/tests/integration/project_tests.rs@@ -13958,6 +13958,80 @@ async fn test_file_status(cx: &mut gpui::TestAppContext) {});}+#[gpui::test]+async fn test_bare_dot_git_changed_event_refreshes_git_state(cx: &mut gpui::TestAppContext) {+ init_test(cx);++ let fs = FakeFs::new(cx.executor());+ fs.insert_tree(+ path!("/repo"),+ json!({+ ".git": {},+ "file.txt": "new contents",+ }),+ )+ .await;++ let dot_git = Path::new(path!("/repo/.git"));+ fs.set_head_for_repo(dot_git, &[("file.txt", "old contents".into())], "old-sha");+ fs.set_index_for_repo(dot_git, &[("file.txt", "old contents".into())]);++ let project = Project::test(fs.clone(), [path!("/repo").as_ref()], cx).await;+ project+ .update(cx, |project, cx| project.git_scans_complete(cx))+ .await;+ cx.run_until_parked();++ let repository = project.read_with(cx, |project, cx| {+ project.repositories(cx).values().next().unwrap().clone()+ });++ repository.read_with(cx, |repository, _| {+ assert_eq!(+ repository+ .head_commit+ .as_ref()+ .map(|commit| commit.sha.as_ref()),+ Some("old-sha")+ );+ assert_eq!(+ repository+ .status_for_path(&repo_path("file.txt"))+ .map(|entry| entry.status),+ Some(StatusCode::Modified.worktree())+ );+ });++ fs.with_git_state(dot_git, false, |state| {+ state+ .head_contents+ .insert(repo_path("file.txt"), "new contents".into());+ state+ .index_contents+ .insert(repo_path("file.txt"), "new contents".into());+ state.refs.insert("HEAD".into(), "new-sha".into());+ })+ .unwrap();+ fs.emit_fs_event(dot_git, Some(PathEventKind::Changed));++ cx.run_until_parked();+ project+ .update(cx, |project, cx| project.git_scans_complete(cx))+ .await;+ cx.run_until_parked();++ repository.read_with(cx, |repository, _| {+ assert_eq!(+ repository+ .head_commit+ .as_ref()+ .map(|commit| commit.sha.as_ref()),+ Some("new-sha")+ );+ assert_eq!(repository.status_for_path(&repo_path("file.txt")), None);+ });+}+#[gpui::test]#[ignore]async fn test_ignored_dirs_events(cx: &mut gpui::TestAppContext) {diff --git a/crates/worktree/src/worktree.rs b/crates/worktree/src/worktree.rsindex 1edef7e5b66..9350d193a8a 100644--- a/crates/worktree/src/worktree.rs+++ b/crates/worktree/src/worktree.rs@@ -4790,6 +4790,14 @@ impl BackgroundScanner {skip_ix(&mut ranges_to_drop, ix);continue;}++ if !dot_git_abs_paths.contains(&dot_git_abs_path) {+ log::debug!(+ "detected update within git repo at {dot_git_abs_path:?}: {abs_path:?}"+ );+ dot_git_abs_paths.push(dot_git_abs_path);+ }+if is_dot_git {log::debug!("ignoring event {abs_path:?} for .git directory itself (kind: {:?})",@@ -4816,13 +4824,6 @@ impl BackgroundScanner {).await;}-- if !dot_git_abs_paths.contains(&dot_git_abs_path) {- log::debug!(- "detected update within git repo at {dot_git_abs_path:?}: {abs_path:?}"- );- dot_git_abs_paths.push(dot_git_abs_path);- }}if self.track_git_repositoriesdiff --git a/crates/worktree/tests/integration/worktree_tests.rs b/crates/worktree/tests/integration/worktree_tests.rsindex 6e2c2a94705..41b27cb09c3 100644--- a/crates/worktree/tests/integration/worktree_tests.rs+++ b/crates/worktree/tests/integration/worktree_tests.rs@@ -4682,12 +4682,11 @@ async fn test_dot_git_dir_event_does_not_suppress_children(let dot_git = project_dir.join(DOT_GIT);- // Case 1: Events for .git AND .git/index.lock should NOT emit UpdatedGitRepositories+ // Case 1: Event for .git/index.lock only should NOT emit UpdatedGitRepositories// (index.lock is in the skipped files list){let mut events = cx.events(&worktree);fs.pause_events();- fs.emit_fs_event(dot_git.clone(), Some(PathEventKind::Changed));fs.emit_fs_event(dot_git.join("index.lock"), Some(PathEventKind::Created));fs.unpause_events_and_flush();executor.run_until_parked();@@ -4699,7 +4698,7 @@ async fn test_dot_git_dir_event_does_not_suppress_children();}- // Case 2: Event for just .git (bare directory event) should NOT emit UpdatedGitRepositories+ // Case 2: Event for just .git (bare directory event) should emit UpdatedGitRepositories{let mut events = cx.events(&worktree);fs.pause_events();@@ -4709,8 +4708,8 @@ async fn test_dot_git_dir_event_does_not_suppress_children(let got_git_update = drain_git_repo_updates(&mut events);assert!(- !got_git_update,- "should NOT emit UpdatedGitRepositories for a bare .git directory event"+ got_git_update,+ "should emit UpdatedGitRepositories for a bare .git directory event");}