Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
git_panel: Fix stale Git status entries for moved directories (#59934)
Bing Wang33473c1cd3b6
diff --git a/crates/project/src/git_store.rs b/crates/project/src/git_store.rsindex 1056e45f3bb..1edd3780307 100644--- a/crates/project/src/git_store.rs+++ b/crates/project/src/git_store.rs@@ -8636,8 +8636,14 @@ impl Repository {let changed_path_statuses = cx.background_spawn(async move {- let mut changed_paths =- changed_paths.into_iter().flatten().collect::<BTreeSet<_>>();+ let changed_paths = GitStore::coalesce_repo_paths(+ changed_paths+ .into_iter()+ .flatten()+ .collect::<BTreeSet<_>>()+ .into_iter()+ .collect(),+ );let changed_paths_vec = changed_paths.iter().cloned().collect::<Vec<_>>();let status_task = backend.status(&changed_paths_vec);@@ -8658,12 +8664,34 @@ impl Repository {let mut changed_path_statuses = Vec::new();let prev_statuses = prev_snapshot.statuses_by_path.clone();+ let current_status_paths = statuses+ .entries+ .iter()+ .map(|(repo_path, _)| repo_path.clone())+ .collect::<BTreeSet<_>>();++ for path in &changed_paths {+ let mut cursor = prev_statuses.cursor::<PathProgress>(());+ cursor.seek_forward(&PathTarget::Path(path), Bias::Left);+ while let Some(entry) = cursor.item() {+ if !entry.repo_path.starts_with(path) {+ break;+ }++ if !current_status_paths.contains(&entry.repo_path) {+ changed_path_statuses.push(Edit::Remove(PathKey(+ entry.repo_path.as_ref().clone(),+ )));+ }+ cursor.next();+ }+ }+let mut cursor = prev_statuses.cursor::<PathProgress>(());for (repo_path, status) in &*statuses.entries {let current_diff_stat = diff_stats.get(repo_path).copied();- changed_paths.remove(repo_path);if cursor.seek_forward(&PathTarget::Path(repo_path), Bias::Left)&& cursor.item().is_some_and(|entry| {entry.status == *status && entry.diff_stat == current_diff_stat@@ -8678,13 +8706,6 @@ impl Repository {diff_stat: current_diff_stat,}));}- let mut cursor = prev_statuses.cursor::<PathProgress>(());- for path in changed_paths.into_iter() {- if cursor.seek_forward(&PathTarget::Path(&path), Bias::Left) {- changed_path_statuses- .push(Edit::Remove(PathKey(path.as_ref().clone())));- }- }anyhow::Ok(changed_path_statuses)}).await?;diff --git a/crates/project/tests/integration/project_tests.rs b/crates/project/tests/integration/project_tests.rsindex 037e62c6e0c..16e948f3a73 100644--- a/crates/project/tests/integration/project_tests.rs+++ b/crates/project/tests/integration/project_tests.rs@@ -11316,6 +11316,96 @@ async fn test_git_repository_status(cx: &mut gpui::TestAppContext) {});}+#[gpui::test]+async fn test_git_repository_status_removes_directory_descendants(cx: &mut gpui::TestAppContext) {+ init_test(cx);+ cx.executor().allow_parking();++ let fs = FakeFs::new(cx.background_executor.clone());+ fs.insert_tree(+ path!("/root"),+ json!({+ "project": {+ ".git": {},+ "ci2": {+ "Dockerfile.namespace": "untracked",+ },+ },+ }),+ )+ .await;+ fs.set_status_for_repo(+ path!("/root/project/.git").as_ref(),+ &[("ci2/Dockerfile.namespace", FileStatus::Untracked)],+ );++ let project = Project::test(fs.clone(), [path!("/root/project").as_ref()], cx).await;++ let tree = project.read_with(cx, |project, cx| project.worktrees(cx).next().unwrap());+ tree.flush_fs_events(cx).await;+ project+ .update(cx, |project, cx| project.git_scans_complete(cx))+ .await;+ cx.executor().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.cached_status().collect::<Vec<_>>(),+ [StatusEntry {+ repo_path: repo_path("ci2/Dockerfile.namespace"),+ status: FileStatus::Untracked,+ diff_stat: None,+ }]+ );+ });++ fs.pause_events();+ fs.create_dir(path!("/root/project/ci3").as_ref())+ .await+ .unwrap();+ fs.copy_file(+ path!("/root/project/ci2/Dockerfile.namespace").as_ref(),+ path!("/root/project/ci3/Dockerfile.namespace").as_ref(),+ Default::default(),+ )+ .await+ .unwrap();+ fs.remove_dir(+ path!("/root/project/ci2").as_ref(),+ RemoveOptions {+ recursive: true,+ ..Default::default()+ },+ )+ .await+ .unwrap();+ fs.clear_buffered_events();+ fs.unpause_events_and_flush();+ fs.emit_fs_event(path!("/root/project/ci2"), Some(PathEventKind::Removed));+ fs.emit_fs_event(path!("/root/project/ci3"), Some(PathEventKind::Created));++ tree.flush_fs_events(cx).await;+ project+ .update(cx, |project, cx| project.git_scans_complete(cx))+ .await;+ cx.executor().run_until_parked();++ repository.read_with(cx, |repository, _| {+ assert_eq!(+ repository.cached_status().collect::<Vec<_>>(),+ [StatusEntry {+ repo_path: repo_path("ci3/Dockerfile.namespace"),+ status: FileStatus::Untracked,+ diff_stat: None,+ }]+ );+ });+}+#[cfg(target_os = "linux")]#[gpui::test(retries = 5)]async fn test_git_events_after_project_excludes_dot_git(cx: &mut gpui::TestAppContext) {