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.

sidebar: Fix archive loop when a thread's worktree folder was deleted (#65578)

Smit Barmasedf134bf1ecdf
diff --git a/crates/sidebar/src/sidebar.rs b/crates/sidebar/src/sidebar.rs
index f35151937fc..55b6c9309c3 100644
--- a/crates/sidebar/src/sidebar.rs
+++ b/crates/sidebar/src/sidebar.rs
@@ -5374,8 +5374,58 @@ impl Sidebar {
window: &mut Window,
cx: &mut Context<Self>,
) {
- let store = ThreadMetadataStore::global(cx);
- let metadata = store.read(cx).entry_by_session(session_id).cloned();
+ let (metadata, thread_entry) = self.thread_to_archive(session_id, cx);
+
+ if let (
+ Some(metadata),
+ Some(ThreadEntryWorkspace::Closed {
+ folder_paths,
+ project_group_key,
+ }),
+ ) = (
+ metadata.as_ref(),
+ thread_entry.as_ref().map(|thread| &thread.workspace),
+ ) && self.should_load_closed_workspace_for_archive(
+ folder_paths,
+ project_group_key,
+ metadata.remote_connection.as_ref(),
+ Some(metadata.thread_id),
+ None,
+ cx,
+ ) {
+ let session_id = session_id.clone();
+ self.open_workspace_for_archive(
+ folder_paths.clone(),
+ project_group_key.clone(),
+ window,
+ cx,
+ move |this, _workspace, window, cx| {
+ this.update_entries(cx);
+ let (metadata, thread_entry) = this.thread_to_archive(&session_id, cx);
+ this.archive_thread_in_loaded_workspace(
+ &session_id,
+ metadata,
+ thread_entry,
+ window,
+ cx,
+ );
+ },
+ );
+ return;
+ }
+
+ self.archive_thread_in_loaded_workspace(session_id, metadata, thread_entry, window, cx);
+ }
+
+ fn thread_to_archive(
+ &self,
+ session_id: &acp::SessionId,
+ cx: &App,
+ ) -> (Option<ThreadMetadata>, Option<Arc<ThreadEntry>>) {
+ let metadata = ThreadMetadataStore::global(cx)
+ .read(cx)
+ .entry_by_session(session_id)
+ .cloned();
let metadata_thread_id = metadata.as_ref().map(|metadata| metadata.thread_id);
let thread_entry = self.contents.entries.iter().find_map(|entry| match entry {
ListEntry::Thread(thread) => metadata_thread_id
@@ -5386,6 +5436,18 @@ impl Sidebar {
.then(|| thread.clone()),
_ => None,
});
+ (metadata, thread_entry)
+ }
+
+ fn archive_thread_in_loaded_workspace(
+ &mut self,
+ session_id: &acp::SessionId,
+ metadata: Option<ThreadMetadata>,
+ thread_entry: Option<Arc<ThreadEntry>>,
+ window: &mut Window,
+ cx: &mut Context<Self>,
+ ) {
+ let metadata_thread_id = metadata.as_ref().map(|metadata| metadata.thread_id);
let thread_id = metadata_thread_id.or_else(|| {
thread_entry
.as_ref()
@@ -5413,37 +5475,6 @@ impl Sidebar {
.as_ref()
.map(|workspace| PathList::new(&workspace.read(cx).root_paths(cx)))
});
- let thread_entry_workspace = thread_entry.map(|thread| thread.workspace.clone());
-
- if let (
- Some(metadata),
- Some(ThreadEntryWorkspace::Closed {
- folder_paths,
- project_group_key,
- }),
- ) = (metadata.as_ref(), thread_entry_workspace)
- && self.should_load_closed_workspace_for_archive(
- &folder_paths,
- &project_group_key,
- metadata.remote_connection.as_ref(),
- Some(metadata.thread_id),
- None,
- cx,
- )
- {
- let session_id = session_id.clone();
- self.open_workspace_for_archive(
- folder_paths,
- project_group_key,
- window,
- cx,
- move |this, _workspace, window, cx| {
- this.update_entries(cx);
- this.archive_thread(&session_id, window, cx);
- },
- );
- return;
- }
// Compute which linked worktree roots should be archived from disk if
// this thread is archived. This must happen before we remove any
diff --git a/crates/sidebar/src/sidebar_tests.rs b/crates/sidebar/src/sidebar_tests.rs
index 4f22a0e9aba..6ec961c2bd1 100644
--- a/crates/sidebar/src/sidebar_tests.rs
+++ b/crates/sidebar/src/sidebar_tests.rs
@@ -4197,6 +4197,82 @@ async fn test_archive_selected_thread_archives_closed_linked_worktree(cx: &mut T
);
}
+#[gpui::test]
+async fn test_archive_thread_with_deleted_closed_linked_worktree(cx: &mut TestAppContext) {
+ let (main_project, _fs) = init_test_project_with_git("/project", cx).await;
+ main_project
+ .update(cx, |project, cx| project.git_scans_complete(cx))
+ .await;
+
+ let (multi_workspace, cx) =
+ cx.add_window_view(|window, cx| MultiWorkspace::test_new(main_project.clone(), window, cx));
+ let sidebar = setup_sidebar(&multi_workspace, cx);
+ let main_workspace =
+ multi_workspace.read_with(cx, |multi_workspace, _| multi_workspace.workspace().clone());
+
+ // A thread on a linked worktree that no longer exists on disk.
+ let worktree_session_id = acp::SessionId::new(Arc::from("worktree-thread"));
+ let worktree_folder_paths =
+ PathList::new(&[PathBuf::from("/worktrees/project/feature-a/project")]);
+ save_thread_metadata_with_main_paths(
+ "worktree-thread",
+ "Worktree Thread",
+ worktree_folder_paths,
+ PathList::new(&[PathBuf::from("/project")]),
+ chrono::TimeZone::with_ymd_and_hms(&Utc, 2024, 1, 1, 0, 0, 0).unwrap(),
+ cx,
+ );
+ sidebar.update(cx, |sidebar, cx| sidebar.update_entries(cx));
+ cx.run_until_parked();
+
+ sidebar.read_with(cx, |sidebar, _cx| {
+ let thread = sidebar
+ .contents
+ .entries
+ .iter()
+ .find_map(|entry| match entry {
+ ListEntry::Thread(thread)
+ if thread.metadata.session_id.as_ref() == Some(&worktree_session_id) =>
+ {
+ Some(thread)
+ }
+ _ => None,
+ })
+ .expect("worktree thread should be visible in sidebar");
+ assert!(
+ matches!(thread.workspace, ThreadEntryWorkspace::Closed { .. }),
+ "thread should start closed so archiving has to open its workspace"
+ );
+ });
+
+ // Opening the missing worktree falls back to the main workspace, so the
+ // thread's entry is still closed after archiving opens it.
+ sidebar.update_in(cx, |sidebar, window, cx| {
+ sidebar.archive_thread(&worktree_session_id, window, cx);
+ });
+ cx.run_until_parked();
+
+ let thread_archived = cx.update(|_, cx| {
+ ThreadMetadataStore::global(cx)
+ .read(cx)
+ .entry_by_session(&worktree_session_id)
+ .map(|thread| thread.archived)
+ });
+ assert_eq!(
+ thread_archived,
+ Some(true),
+ "thread should be archived even though its worktree no longer exists"
+ );
+ assert_eq!(
+ multi_workspace.read_with(cx, |multi_workspace, _| multi_workspace
+ .workspaces()
+ .cloned()
+ .collect::<Vec<_>>()),
+ vec![main_workspace],
+ "archiving should leave only the main workspace open"
+ );
+}
+
#[gpui::test]
async fn test_archive_selected_thread_deletes_empty_draft_when_linked_worktree_has_no_archive_root(
cx: &mut TestAppContext,