mirror of
https://github.com/openai/codex.git
synced 2026-11-27 23:31:09 +00:00
Show worktree owner details and add confirmed deletion (#43942)
## What changed - Display owner thread titles, relative update times, and archived or unavailable status in the managed worktree browser. Offer resume only for resumable owners, including those with compressed thread history. - Add a delete action with a confirmation dialog that defaults to Cancel and preserves thread history. - Restrict removal to managed worktrees in the current repository. Refuse the current checkout, including path aliases, and checkouts containing local changes, untracked files, or ignored files. ## Testing Add browser snapshots and tests for owner states, deletion confirmation, and compressed thread history. Add worktree removal coverage for unrelated checkouts, current-directory aliases, untracked and ignored files, and successful removal of a clean checkout. GitOrigin-RevId: e5867f1540d5d5bc8130acc76875e51e3d8b3b6a
This commit is contained in:
1 parent
c53f342fec
commit
973dcd80fc
12 files changed
+611
-31
No files matched your search
+3
-2
@@ -1,12 +1,13 @@
|
||||
---
|
||||
source: tui/src/chatwidget/tests/worktree_picker_tests.rs
|
||||
assertion_line: 114
|
||||
assertion_line: 137
|
||||
expression: "render_bottom_popup(&chat, 80)"
|
||||
---
|
||||
Worktree
|
||||
Worktree: Fix login form
|
||||
/repo/worktree
|
||||
|
||||
› 1. Resume owner thread
|
||||
2. Copy working directory
|
||||
3. Delete worktree
|
||||
|
||||
Press enter to confirm or esc to go back
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
---
|
||||
source: tui/src/chatwidget/tests/worktree_picker_tests.rs
|
||||
assertion_line: 170
|
||||
expression: "render_bottom_popup(&chat, 80)"
|
||||
---
|
||||
Delete this worktree?
|
||||
/repo/worktree
|
||||
|
||||
› 1. Cancel
|
||||
2. Delete worktree Keeps thread history; may disrupt other sessions
|
||||
|
||||
Press enter to confirm or esc to go back
|
||||
+3
-3
@@ -1,12 +1,12 @@
|
||||
---
|
||||
source: tui/src/chatwidget/tests/worktree_picker_tests.rs
|
||||
assertion_line: 100
|
||||
assertion_line: 120
|
||||
expression: "render_bottom_popup(&chat, 80)"
|
||||
---
|
||||
Managed worktrees
|
||||
Select a worktree to resume its owner or copy its working directory
|
||||
Select a worktree to resume, copy its path, or delete it
|
||||
|
||||
|
||||
› /repo/worktree Owner: 00000000-0000-0000-0000-000000000001
|
||||
› Fix login form updated 2h ago · /repo/worktree
|
||||
|
||||
Press enter to confirm or esc to go back
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
---
|
||||
source: tui/src/chatwidget/tests/worktree_picker_tests.rs
|
||||
assertion_line: 237
|
||||
expression: "render_bottom_popup(&chat, 80)"
|
||||
---
|
||||
Managed worktrees
|
||||
Select a worktree to resume, copy its path, or delete it
|
||||
|
||||
|
||||
› Fix settings Archived · updated 1d ago · /repo/archived
|
||||
/repo/missing Owner thread unavailable
|
||||
/repo/empty No attached thread
|
||||
|
||||
Press enter to confirm or esc to go back
|
||||
@@ -93,6 +93,8 @@ async fn slash_worktree_offers_current_or_new_conversation() {
|
||||
#[tokio::test]
|
||||
async fn worktree_browser_actions_and_stale_results() {
|
||||
use crate::worktree_browser::Entry;
|
||||
use crate::worktree_browser::Owner;
|
||||
use crate::worktree_browser::ThreadSummary;
|
||||
let (mut chat, mut rx, _op_rx) = make_chatwidget_manual(/*model_override*/ None).await;
|
||||
let checkout = tempdir().unwrap();
|
||||
std::fs::create_dir(checkout.path().join(".git")).unwrap();
|
||||
@@ -106,8 +108,13 @@ async fn worktree_browser_actions_and_stale_results() {
|
||||
);
|
||||
let owner = ThreadId::from_string("00000000-0000-0000-0000-000000000001").unwrap();
|
||||
let entry = Entry {
|
||||
root: PathBuf::from("/repo/worktree"),
|
||||
cwd: PathBuf::from("/repo/worktree"),
|
||||
owner: Some(owner),
|
||||
owner: Owner::Resumable(ThreadSummary {
|
||||
id: owner,
|
||||
title: "Fix login form".to_string(),
|
||||
updated_at: chrono::Utc::now().timestamp() - 7_200,
|
||||
}),
|
||||
};
|
||||
chat.on_managed_worktrees_loaded(request, Ok(vec![entry.clone()]));
|
||||
assert_chatwidget_snapshot!(
|
||||
@@ -149,7 +156,7 @@ async fn worktree_browser_actions_and_stale_results() {
|
||||
chat.show_managed_worktree_actions(
|
||||
request,
|
||||
Entry {
|
||||
owner: None,
|
||||
owner: Owner::None,
|
||||
..entry
|
||||
},
|
||||
);
|
||||
@@ -159,6 +166,24 @@ async fn worktree_browser_actions_and_stale_results() {
|
||||
panic!("copy action");
|
||||
};
|
||||
assert_matches!(chat.managed_worktree_action(&request, action), Some(AppEvent::CopySelection { text, .. }) if &*text == "/repo/worktree");
|
||||
chat.confirm_managed_worktree_removal(request.clone(), PathBuf::from("/repo/worktree"));
|
||||
assert_chatwidget_snapshot!(
|
||||
"worktree_browser_delete_confirmation",
|
||||
render_bottom_popup(&chat, /*width*/ 80)
|
||||
);
|
||||
chat.handle_key_event(KeyEvent::from(KeyCode::Enter));
|
||||
assert_matches!(rx.try_recv(), Err(TryRecvError::Empty));
|
||||
chat.confirm_managed_worktree_removal(request, PathBuf::from("/repo/worktree"));
|
||||
chat.handle_key_event(KeyEvent::from(KeyCode::Down));
|
||||
chat.handle_key_event(KeyEvent::from(KeyCode::Enter));
|
||||
let AppEvent::ManagedWorktreeAction {
|
||||
request: selected,
|
||||
action,
|
||||
} = rx.try_recv().unwrap()
|
||||
else {
|
||||
panic!("delete action");
|
||||
};
|
||||
assert_matches!(chat.managed_worktree_action(&selected, action), Some(AppEvent::RemoveManagedWorktree { root, .. }) if root.as_path() == std::path::Path::new("/repo/worktree"));
|
||||
let first = chat.request_managed_worktrees().unwrap();
|
||||
chat.handle_key_event(KeyEvent::from(KeyCode::Esc));
|
||||
chat.on_managed_worktrees_loaded(first, Ok(Vec::new()));
|
||||
@@ -171,3 +196,62 @@ async fn worktree_browser_actions_and_stale_results() {
|
||||
chat.on_managed_worktrees_loaded(current, Ok(Vec::new()));
|
||||
assert!(!chat.bottom_pane.has_active_view());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn worktree_browser_names_archived_and_missing_owners() {
|
||||
use crate::worktree_browser::Entry;
|
||||
use crate::worktree_browser::Owner;
|
||||
use crate::worktree_browser::ThreadSummary;
|
||||
let (mut chat, _rx, _op_rx) = make_chatwidget_manual(/*model_override*/ None).await;
|
||||
let checkout = tempdir().unwrap();
|
||||
std::fs::create_dir(checkout.path().join(".git")).unwrap();
|
||||
std::fs::write(checkout.path().join(".git/HEAD"), "ref: refs/heads/main\n").unwrap();
|
||||
chat.config.cwd = AbsolutePathBuf::from_absolute_path(checkout.path()).unwrap();
|
||||
chat.set_feature_enabled(Feature::Worktrees, /*enabled*/ true);
|
||||
let request = chat.request_managed_worktrees().unwrap();
|
||||
let owner = ThreadId::from_string("00000000-0000-0000-0000-000000000002").unwrap();
|
||||
chat.on_managed_worktrees_loaded(
|
||||
request.clone(),
|
||||
Ok(vec![
|
||||
Entry {
|
||||
root: PathBuf::from("/repo/archived"),
|
||||
cwd: PathBuf::from("/repo/archived"),
|
||||
owner: Owner::Archived(ThreadSummary {
|
||||
id: owner,
|
||||
title: "Fix settings".to_string(),
|
||||
updated_at: chrono::Utc::now().timestamp() - 86_400,
|
||||
}),
|
||||
},
|
||||
Entry {
|
||||
root: PathBuf::from("/repo/missing"),
|
||||
cwd: PathBuf::from("/repo/missing"),
|
||||
owner: Owner::Unavailable(owner),
|
||||
},
|
||||
Entry {
|
||||
root: PathBuf::from("/repo/empty"),
|
||||
cwd: PathBuf::from("/repo/empty"),
|
||||
owner: Owner::None,
|
||||
},
|
||||
]),
|
||||
);
|
||||
assert_chatwidget_snapshot!(
|
||||
"worktree_browser_owner_states",
|
||||
render_bottom_popup(&chat, /*width*/ 80)
|
||||
);
|
||||
chat.handle_key_event(KeyEvent::from(KeyCode::Enter));
|
||||
chat.show_managed_worktree_actions(
|
||||
request,
|
||||
Entry {
|
||||
root: PathBuf::from("/repo/archived"),
|
||||
cwd: PathBuf::from("/repo/archived"),
|
||||
owner: Owner::Archived(ThreadSummary {
|
||||
id: owner,
|
||||
title: "Fix settings".to_string(),
|
||||
updated_at: 0,
|
||||
}),
|
||||
},
|
||||
);
|
||||
let popup = render_bottom_popup(&chat, /*width*/ 80);
|
||||
assert!(popup.contains("Worktree: Fix settings"), "popup: {popup}");
|
||||
assert!(!popup.contains("Resume owner"));
|
||||
}
|
||||
@@ -4,6 +4,7 @@ use super::*;
|
||||
use crate::app_event::ManagedWorktreeMode;
|
||||
use crate::worktree_browser::Action;
|
||||
use crate::worktree_browser::Entry;
|
||||
use crate::worktree_browser::Owner;
|
||||
use crate::worktree_browser::Request;
|
||||
|
||||
const BROWSER_VIEW_ID: &str = "managed-worktrees";
|
||||
@@ -158,7 +159,7 @@ impl ChatWidget {
|
||||
Some(request)
|
||||
}
|
||||
|
||||
fn worktree_request_is_current(&self, request: &Request) -> bool {
|
||||
pub(crate) fn worktree_request_is_current(&self, request: &Request) -> bool {
|
||||
self.worktree_popup_request_id == Some(request.id)
|
||||
&& request.cwd == self.config.cwd.as_path()
|
||||
&& request.thread_id == self.thread_id
|
||||
@@ -194,7 +195,7 @@ impl ChatWidget {
|
||||
if entries.is_empty() {
|
||||
"No worktrees in this repository's configured pool"
|
||||
} else {
|
||||
"Select a worktree to resume its owner or copy its working directory"
|
||||
"Select a worktree to resume, copy its path, or delete it"
|
||||
}
|
||||
.to_string(),
|
||||
),
|
||||
@@ -203,13 +204,38 @@ impl ChatWidget {
|
||||
.into_iter()
|
||||
.map(|entry| {
|
||||
let request = request.clone();
|
||||
let (name, description) = match &entry.owner {
|
||||
Owner::None => (
|
||||
entry.cwd.display().to_string(),
|
||||
"No attached thread".to_string(),
|
||||
),
|
||||
Owner::Unavailable(_) => (
|
||||
entry.cwd.display().to_string(),
|
||||
"Owner thread unavailable".to_string(),
|
||||
),
|
||||
Owner::Archived(thread) | Owner::Resumable(thread) => {
|
||||
let status = match &entry.owner {
|
||||
Owner::Archived(_) => "Archived · ",
|
||||
Owner::Resumable(_) => "",
|
||||
Owner::None | Owner::Unavailable(_) => unreachable!(),
|
||||
};
|
||||
(
|
||||
thread.title.clone(),
|
||||
format!(
|
||||
"{status}updated {} · {}",
|
||||
worktree_updated_ago(
|
||||
thread.updated_at,
|
||||
chrono::Utc::now().timestamp()
|
||||
),
|
||||
entry.cwd.display()
|
||||
),
|
||||
)
|
||||
}
|
||||
};
|
||||
SelectionItem {
|
||||
name: entry.cwd.display().to_string(),
|
||||
search_value: Some(entry.cwd.display().to_string()),
|
||||
description: Some(entry.owner.map_or_else(
|
||||
|| "No owner metadata".to_string(),
|
||||
|owner| format!("Owner: {owner}"),
|
||||
)),
|
||||
name: name.clone(),
|
||||
search_value: Some(format!("{name} {}", entry.cwd.display())),
|
||||
description: Some(description),
|
||||
actions: vec![Box::new(move |tx| {
|
||||
tx.send(AppEvent::ShowManagedWorktreeActions {
|
||||
request: request.clone(),
|
||||
@@ -241,6 +267,10 @@ impl ChatWidget {
|
||||
label: "Worktree working directory".to_string(),
|
||||
format: crate::clipboard_copy::CopyFormat::PlainText,
|
||||
},
|
||||
Action::Remove(root) => AppEvent::RemoveManagedWorktree {
|
||||
request: request.clone(),
|
||||
root,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
@@ -249,7 +279,8 @@ impl ChatWidget {
|
||||
return;
|
||||
}
|
||||
let mut items = Vec::new();
|
||||
if let Some(owner) = entry.owner {
|
||||
if let Owner::Resumable(thread) = &entry.owner {
|
||||
let owner = thread.id;
|
||||
let request = request.clone();
|
||||
items.push(SelectionItem {
|
||||
name: "Resume owner thread".to_string(),
|
||||
@@ -264,6 +295,7 @@ impl ChatWidget {
|
||||
});
|
||||
}
|
||||
let cwd = entry.cwd.clone();
|
||||
let copy_request = request.clone();
|
||||
items.push(SelectionItem {
|
||||
name: "Copy working directory".to_string(),
|
||||
is_disabled: entry.cwd.to_str().is_none(),
|
||||
@@ -274,19 +306,89 @@ impl ChatWidget {
|
||||
.then(|| "Path is not valid UTF-8".to_string()),
|
||||
actions: vec![Box::new(move |tx| {
|
||||
tx.send(AppEvent::ManagedWorktreeAction {
|
||||
request: request.clone(),
|
||||
request: copy_request.clone(),
|
||||
action: Action::Copy(cwd.clone()),
|
||||
})
|
||||
})],
|
||||
dismiss_on_select: true,
|
||||
..Default::default()
|
||||
});
|
||||
let can_delete = !request.cwd.starts_with(&entry.root);
|
||||
let root = entry.root.clone();
|
||||
let delete_request = request;
|
||||
items.push(SelectionItem {
|
||||
name: "Delete worktree".to_string(),
|
||||
is_disabled: !can_delete,
|
||||
disabled_reason: (!can_delete)
|
||||
.then(|| "Switch to another checkout before deleting this one".to_string()),
|
||||
actions: vec![Box::new(move |tx| {
|
||||
tx.send(AppEvent::ConfirmManagedWorktreeRemoval {
|
||||
request: delete_request.clone(),
|
||||
root: root.clone(),
|
||||
})
|
||||
})],
|
||||
dismiss_on_select: true,
|
||||
..Default::default()
|
||||
});
|
||||
let title = match &entry.owner {
|
||||
Owner::Archived(thread) | Owner::Resumable(thread) => {
|
||||
format!("Worktree: {}", thread.title)
|
||||
}
|
||||
Owner::None | Owner::Unavailable(_) => "Worktree".to_string(),
|
||||
};
|
||||
self.bottom_pane.show_selection_view(SelectionViewParams {
|
||||
title: Some("Worktree".to_string()),
|
||||
title: Some(title),
|
||||
subtitle: Some(entry.cwd.display().to_string()),
|
||||
items,
|
||||
footer_hint: Some(standard_popup_hint_line()),
|
||||
..Default::default()
|
||||
});
|
||||
}
|
||||
|
||||
pub(crate) fn confirm_managed_worktree_removal(&mut self, request: Request, root: PathBuf) {
|
||||
if !self.worktree_request_is_current(&request) || request.cwd.starts_with(&root) {
|
||||
return;
|
||||
}
|
||||
self.bottom_pane.show_selection_view(SelectionViewParams {
|
||||
title: Some("Delete this worktree?".to_string()),
|
||||
subtitle: Some(root.display().to_string()),
|
||||
items: vec![
|
||||
SelectionItem {
|
||||
name: "Cancel".to_string(),
|
||||
actions: vec![],
|
||||
dismiss_on_select: true,
|
||||
..Default::default()
|
||||
},
|
||||
SelectionItem {
|
||||
name: "Delete worktree".to_string(),
|
||||
description: Some(
|
||||
"Keeps thread history; may disrupt other sessions".to_string(),
|
||||
),
|
||||
actions: vec![Box::new(move |tx| {
|
||||
tx.send(AppEvent::ManagedWorktreeAction {
|
||||
request: request.clone(),
|
||||
action: Action::Remove(root.clone()),
|
||||
})
|
||||
})],
|
||||
dismiss_on_select: true,
|
||||
..Default::default()
|
||||
},
|
||||
],
|
||||
footer_hint: Some(standard_popup_hint_line()),
|
||||
..Default::default()
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
fn worktree_updated_ago(updated_at: i64, now: i64) -> String {
|
||||
let seconds = now.saturating_sub(updated_at).max(0);
|
||||
if seconds < 60 {
|
||||
"just now".to_string()
|
||||
} else if seconds < 3_600 {
|
||||
format!("{}m ago", seconds / 60)
|
||||
} else if seconds < 86_400 {
|
||||
format!("{}h ago", seconds / 3_600)
|
||||
} else {
|
||||
format!("{}d ago", seconds / 86_400)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user