Skip to content

armgit / zed

Public Imported from GitHub
0

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.

worktree: Fix linked worktree git dir event handling (#57782)

Lukas Wirth954ee58c15be
diff --git a/crates/worktree/src/worktree.rs b/crates/worktree/src/worktree.rs
index 0ee6f0cb310..9e6f58e0042 100644
--- a/crates/worktree/src/worktree.rs
+++ b/crates/worktree/src/worktree.rs
@@ -5496,6 +5496,7 @@ impl BackgroundScanner {
if SanitizedPath::new(repo.common_dir_abs_path.as_ref()) == dot_git_dir
|| SanitizedPath::new(repo.repository_dir_abs_path.as_ref())
== dot_git_dir
+ || SanitizedPath::new(repo.dot_git_abs_path.as_ref()) == dot_git_dir
{
Some(repo.clone())
} else {
@@ -5551,10 +5552,7 @@ impl BackgroundScanner {
});
if exists_in_snapshot
- || matches!(
- self.fs.metadata(&entry.common_dir_abs_path).await,
- Ok(Some(_))
- )
+ || matches!(self.fs.metadata(&entry.dot_git_abs_path).await, Ok(Some(_)))
{
ids_to_preserve.insert(work_directory_id);
}
diff --git a/crates/worktree/tests/integration/worktree_tests.rs b/crates/worktree/tests/integration/worktree_tests.rs
index 7691a68392c..b98f4517696 100644
--- a/crates/worktree/tests/integration/worktree_tests.rs
+++ b/crates/worktree/tests/integration/worktree_tests.rs
@@ -3391,7 +3391,7 @@ async fn test_invisible_worktree_does_not_track_ancestor_git_repository(
}
#[gpui::test]
-async fn test_linked_worktree_git_file_event_does_not_panic(
+async fn test_linked_worktree_gitfile_event_preserves_repo(
executor: BackgroundExecutor,
cx: &mut TestAppContext,
) {
@@ -3405,19 +3405,11 @@ async fn test_linked_worktree_git_file_event_does_not_panic(
// and `update_git_repositories` panics because the path is outside the
// worktree root.
init_test(cx);
-
use git::repository::Worktree as GitWorktree;
let fs = FakeFs::new(executor);
-
- fs.insert_tree(
- path!("/main_repo"),
- json!({
- ".git": {},
- "file.txt": "content",
- }),
- )
- .await;
+ fs.insert_tree(path!("/main_repo"), json!({ ".git": {}, "file.txt": "" }))
+ .await;
fs.add_linked_worktree_for_repo(
Path::new(path!("/main_repo/.git")),
false,
@@ -3430,12 +3422,9 @@ async fn test_linked_worktree_git_file_event_does_not_panic(
},
)
.await;
- fs.write(
- path!("/linked_worktree/file.txt").as_ref(),
- "content".as_bytes(),
- )
- .await
- .unwrap();
+ fs.write(path!("/linked_worktree/file.txt").as_ref(), b"content")
+ .await
+ .unwrap();
let tree = Worktree::local(
path!("/linked_worktree").as_ref(),
@@ -3452,17 +3441,19 @@ async fn test_linked_worktree_git_file_event_does_not_panic(
.await;
cx.run_until_parked();
- // Trigger a filesystem event inside the main repo's .git directory
- // (which the linked worktree scanner watches via the commondir). This
- // uses the sentinel-file helper to ensure the event goes through the
- // real watcher path, exactly as it would in production.
- tree.flush_fs_events_in_root_git_repository(cx).await;
+ // Overwrite the .git gitfile with garbage to trigger an event for the
+ // gitfile path itself, which only matches `dot_git_abs_path`.
+ fs.write(path!("/linked_worktree/.git").as_ref(), b"garbage")
+ .await
+ .unwrap();
+ tree.flush_fs_events(cx).await;
// The worktree should still be intact.
tree.read_with(cx, |tree, _| {
assert_eq!(
tree.snapshot().root_repo_common_dir().map(|p| p.as_ref()),
Some(Path::new(path!("/main_repo/.git"))),
+ "linked worktree repo should survive a gitfile change event"
);
});
}