mirror of
https://github.com/openai/codex.git
synced 2026-11-26 23:40:43 +00:00
feat(tui): create worktrees from slash command
This commit is contained in:
@@ -191,6 +191,10 @@ impl App {
|
||||
self.open_worktree_picker(tui);
|
||||
tui.frame_requester().schedule_frame();
|
||||
}
|
||||
AppEvent::OpenWorktreeCreatePrompt => {
|
||||
self.open_worktree_create_prompt();
|
||||
tui.frame_requester().schedule_frame();
|
||||
}
|
||||
AppEvent::WorktreesLoaded { cwd, result } => {
|
||||
self.on_worktrees_loaded(cwd, result);
|
||||
tui.frame_requester().schedule_frame();
|
||||
@@ -200,8 +204,11 @@ impl App {
|
||||
base_ref,
|
||||
dirty_policy,
|
||||
} => {
|
||||
self.create_worktree_and_switch(tui, app_server, branch, base_ref, dirty_policy)
|
||||
.await;
|
||||
self.create_worktree_and_switch(tui, branch, base_ref, dirty_policy);
|
||||
tui.frame_requester().schedule_frame();
|
||||
}
|
||||
AppEvent::WorktreeCreated { cwd, result } => {
|
||||
self.on_worktree_created(tui, app_server, cwd, result).await;
|
||||
tui.frame_requester().schedule_frame();
|
||||
}
|
||||
AppEvent::SwitchToWorktree { target } => {
|
||||
@@ -217,10 +224,13 @@ impl App {
|
||||
info,
|
||||
config,
|
||||
forked,
|
||||
warnings,
|
||||
result,
|
||||
} => {
|
||||
self.on_worktree_session_ready(tui, app_server, info, config, forked, result)
|
||||
.await;
|
||||
self.on_worktree_session_ready(
|
||||
tui, app_server, info, config, forked, warnings, result,
|
||||
)
|
||||
.await;
|
||||
tui.frame_requester().schedule_frame();
|
||||
}
|
||||
AppEvent::ShowWorktreePath { target } => {
|
||||
|
||||
@@ -6,12 +6,14 @@ use codex_worktree::WorktreeInfo;
|
||||
use codex_worktree::WorktreeListQuery;
|
||||
use codex_worktree::WorktreeRemoveRequest;
|
||||
use codex_worktree::WorktreeRequest;
|
||||
use codex_worktree::WorktreeResolution;
|
||||
use codex_worktree::WorktreeSource;
|
||||
use std::path::Path;
|
||||
use std::path::PathBuf;
|
||||
use std::time::Duration;
|
||||
|
||||
use super::*;
|
||||
use crate::bottom_pane::custom_prompt_view::CustomPromptView;
|
||||
|
||||
const WORKTREE_SWITCH_RENDER_DELAY: Duration = Duration::from_millis(20);
|
||||
|
||||
@@ -52,6 +54,32 @@ impl App {
|
||||
self.fetch_worktrees_for_picker();
|
||||
}
|
||||
|
||||
pub(super) fn open_worktree_create_prompt(&mut self) {
|
||||
if self.remote_app_server_url.is_some() {
|
||||
self.chat_widget.add_error_message(
|
||||
"/worktree is not supported for remote sessions yet.".to_string(),
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
let tx = self.app_event_tx.clone();
|
||||
let view = CustomPromptView::new(
|
||||
"New worktree".to_string(),
|
||||
"Type a branch name and press Enter".to_string(),
|
||||
/*initial_text*/ String::new(),
|
||||
/*context_label*/
|
||||
Some("Creates a sibling worktree and starts this chat there.".to_string()),
|
||||
Box::new(move |branch: String| {
|
||||
tx.send(AppEvent::CreateWorktreeAndSwitch {
|
||||
branch: branch.trim().to_string(),
|
||||
base_ref: None,
|
||||
dirty_policy: None,
|
||||
});
|
||||
}),
|
||||
);
|
||||
self.chat_widget.show_bottom_pane_view(Box::new(view));
|
||||
}
|
||||
|
||||
pub(super) fn on_worktrees_loaded(
|
||||
&mut self,
|
||||
cwd: PathBuf,
|
||||
@@ -68,10 +96,9 @@ impl App {
|
||||
self.replace_worktree_view(params);
|
||||
}
|
||||
|
||||
pub(super) async fn create_worktree_and_switch(
|
||||
pub(super) fn create_worktree_and_switch(
|
||||
&mut self,
|
||||
tui: &mut tui::Tui,
|
||||
app_server: &mut AppServerSession,
|
||||
branch: String,
|
||||
base_ref: Option<String>,
|
||||
dirty_policy: Option<DirtyPolicy>,
|
||||
@@ -99,36 +126,50 @@ impl App {
|
||||
},
|
||||
};
|
||||
|
||||
let resolution = match codex_worktree::ensure_worktree(WorktreeRequest {
|
||||
self.show_worktree_creating_view(tui, branch.clone());
|
||||
self.spawn_worktree_create_request(WorktreeRequest {
|
||||
codex_home: self.config.codex_home.to_path_buf(),
|
||||
source_cwd: self.config.cwd.to_path_buf(),
|
||||
branch,
|
||||
base_ref,
|
||||
dirty_policy,
|
||||
}) {
|
||||
});
|
||||
}
|
||||
|
||||
pub(super) async fn on_worktree_created(
|
||||
&mut self,
|
||||
tui: &mut tui::Tui,
|
||||
app_server: &mut AppServerSession,
|
||||
cwd: PathBuf,
|
||||
result: Result<WorktreeResolution, String>,
|
||||
) {
|
||||
if cwd.as_path() != self.config.cwd.as_path() {
|
||||
return;
|
||||
}
|
||||
let resolution = match result {
|
||||
Ok(resolution) => resolution,
|
||||
Err(err) => {
|
||||
self.chat_widget
|
||||
.add_error_message(format!("Failed to create worktree: {err}"));
|
||||
self.show_worktree_error("Failed to create worktree.".to_string(), err);
|
||||
return;
|
||||
}
|
||||
};
|
||||
let warnings = resolution
|
||||
.warnings
|
||||
.iter()
|
||||
.map(|warning| warning.message.clone())
|
||||
.collect::<Vec<_>>();
|
||||
let target = resolution
|
||||
.info
|
||||
.branch
|
||||
.clone()
|
||||
.unwrap_or_else(|| resolution.info.name.clone());
|
||||
self.show_worktree_switching_view(tui, target);
|
||||
self.switch_to_worktree_info(tui, app_server, resolution.info)
|
||||
.await;
|
||||
for warning in warnings {
|
||||
self.chat_widget.add_info_message(warning, /*hint*/ None);
|
||||
}
|
||||
self.switch_to_worktree_info(
|
||||
tui,
|
||||
app_server,
|
||||
resolution.info,
|
||||
resolution
|
||||
.warnings
|
||||
.into_iter()
|
||||
.map(|warning| warning.message)
|
||||
.collect(),
|
||||
)
|
||||
.await;
|
||||
}
|
||||
|
||||
pub(super) fn begin_switch_to_worktree_target(&mut self, tui: &mut tui::Tui, target: String) {
|
||||
@@ -168,7 +209,8 @@ impl App {
|
||||
return;
|
||||
}
|
||||
};
|
||||
self.switch_to_worktree_info(tui, app_server, info).await;
|
||||
self.switch_to_worktree_info(tui, app_server, info, Vec::new())
|
||||
.await;
|
||||
}
|
||||
|
||||
pub(super) fn show_worktree_path(&mut self, target: String) {
|
||||
@@ -279,11 +321,21 @@ impl App {
|
||||
});
|
||||
}
|
||||
|
||||
fn spawn_worktree_create_request(&self, request: WorktreeRequest) {
|
||||
let cwd = request.source_cwd.clone();
|
||||
let app_event_tx = self.app_event_tx.clone();
|
||||
tokio::task::spawn_blocking(move || {
|
||||
let result = codex_worktree::ensure_worktree(request).map_err(|err| err.to_string());
|
||||
app_event_tx.send(AppEvent::WorktreeCreated { cwd, result });
|
||||
});
|
||||
}
|
||||
|
||||
async fn switch_to_worktree_info(
|
||||
&mut self,
|
||||
tui: &mut tui::Tui,
|
||||
app_server: &mut AppServerSession,
|
||||
info: WorktreeInfo,
|
||||
warnings: Vec<String>,
|
||||
) {
|
||||
let mut config = match self
|
||||
.rebuild_config_for_cwd(info.workspace_cwd.clone())
|
||||
@@ -301,7 +353,7 @@ impl App {
|
||||
self.apply_runtime_policy_overrides(&mut config);
|
||||
|
||||
let mode = self.worktree_switch_mode().await;
|
||||
self.spawn_worktree_session_request(app_server, info, config, mode);
|
||||
self.spawn_worktree_session_request(app_server, info, config, mode, warnings);
|
||||
tui.frame_requester().schedule_frame();
|
||||
}
|
||||
|
||||
@@ -312,6 +364,7 @@ impl App {
|
||||
info: WorktreeInfo,
|
||||
config: Config,
|
||||
forked: bool,
|
||||
warnings: Vec<String>,
|
||||
result: Result<AppServerStartedThread, String>,
|
||||
) {
|
||||
match result {
|
||||
@@ -335,6 +388,9 @@ impl App {
|
||||
WorktreeSessionTransition::Started
|
||||
};
|
||||
self.add_worktree_session_message(&info, transition);
|
||||
for warning in warnings {
|
||||
self.chat_widget.add_info_message(warning, /*hint*/ None);
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(err) => {
|
||||
@@ -355,6 +411,7 @@ impl App {
|
||||
info: WorktreeInfo,
|
||||
config: Config,
|
||||
mode: WorktreeSwitchMode,
|
||||
warnings: Vec<String>,
|
||||
) {
|
||||
let request_handle = app_server.request_handle();
|
||||
let remote_cwd_override = app_server.remote_cwd_override().map(Path::to_path_buf);
|
||||
@@ -385,6 +442,7 @@ impl App {
|
||||
info,
|
||||
config,
|
||||
forked,
|
||||
warnings,
|
||||
result,
|
||||
});
|
||||
});
|
||||
@@ -436,9 +494,26 @@ impl App {
|
||||
}
|
||||
|
||||
fn show_worktree_switching_view(&mut self, tui: &mut tui::Tui, target: String) {
|
||||
let params = crate::worktree::switching_params(
|
||||
target.clone(),
|
||||
tui.frame_requester(),
|
||||
self.config.animations,
|
||||
);
|
||||
if !self.replace_worktree_view(params) {
|
||||
self.chat_widget
|
||||
.show_selection_view(crate::worktree::switching_params(
|
||||
target,
|
||||
tui.frame_requester(),
|
||||
self.config.animations,
|
||||
));
|
||||
}
|
||||
tui.frame_requester().schedule_frame();
|
||||
}
|
||||
|
||||
fn show_worktree_creating_view(&mut self, tui: &mut tui::Tui, branch: String) {
|
||||
self.chat_widget
|
||||
.show_selection_view(crate::worktree::switching_params(
|
||||
target,
|
||||
.show_selection_view(crate::worktree::creating_params(
|
||||
branch,
|
||||
tui.frame_requester(),
|
||||
self.config.animations,
|
||||
));
|
||||
|
||||
@@ -197,6 +197,9 @@ pub(crate) enum AppEvent {
|
||||
/// Open the managed worktree picker.
|
||||
OpenWorktreePicker,
|
||||
|
||||
/// Open the prompt for creating a managed worktree.
|
||||
OpenWorktreeCreatePrompt,
|
||||
|
||||
/// Result of loading worktrees for the managed worktree picker.
|
||||
WorktreesLoaded {
|
||||
cwd: PathBuf,
|
||||
@@ -210,6 +213,12 @@ pub(crate) enum AppEvent {
|
||||
dirty_policy: Option<DirtyPolicy>,
|
||||
},
|
||||
|
||||
/// Result of creating or reusing a managed worktree.
|
||||
WorktreeCreated {
|
||||
cwd: PathBuf,
|
||||
result: Result<codex_worktree::WorktreeResolution, String>,
|
||||
},
|
||||
|
||||
/// Switch the TUI into an existing worktree.
|
||||
SwitchToWorktree {
|
||||
target: String,
|
||||
@@ -225,6 +234,7 @@ pub(crate) enum AppEvent {
|
||||
info: codex_worktree::WorktreeInfo,
|
||||
config: Config,
|
||||
forked: bool,
|
||||
warnings: Vec<String>,
|
||||
result: Result<AppServerStartedThread, String>,
|
||||
},
|
||||
|
||||
|
||||
@@ -5377,6 +5377,15 @@ impl ChatWidget {
|
||||
self.request_redraw();
|
||||
}
|
||||
|
||||
pub(crate) fn show_bottom_pane_view(
|
||||
&mut self,
|
||||
view: Box<dyn crate::bottom_pane::BottomPaneView>,
|
||||
) {
|
||||
self.bottom_pane.show_view(view);
|
||||
self.refresh_plan_mode_nudge();
|
||||
self.request_redraw();
|
||||
}
|
||||
|
||||
pub(crate) fn replace_selection_view_if_active(
|
||||
&mut self,
|
||||
view_id: &'static str,
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
---
|
||||
source: tui/src/worktree.rs
|
||||
expression: "render_selection(creating_params(\"fcoury/demo\".to_string(),\nFrameRequester::test_dummy(), false), 92)"
|
||||
---
|
||||
|
||||
Worktrees
|
||||
• Creating fcoury/demo...
|
||||
Codex is creating the worktree before starting the chat in that workspace.
|
||||
|
||||
› Preparing worktree... Codex is creating the worktree before starting the chat in
|
||||
that workspace.
|
||||
|
||||
Press enter to confirm or esc to go back
|
||||
@@ -0,0 +1,10 @@
|
||||
---
|
||||
source: tui/src/worktree.rs
|
||||
expression: "render_selection(empty_params(), 84)"
|
||||
---
|
||||
|
||||
Worktrees
|
||||
|
||||
› 1. New worktree... Type the branch name for the new worktree.
|
||||
|
||||
Press enter to confirm or esc to go back
|
||||
@@ -4,10 +4,11 @@ expression: "render_selection(params, 86)"
|
||||
---
|
||||
|
||||
Worktrees
|
||||
Select a worktree to fork this chat into that workspace.
|
||||
Create a worktree or fork this chat into an existing workspace.
|
||||
|
||||
Search worktrees
|
||||
› fcoury/demo (current) Fork this chat into /repo/codex.fcoury-demo
|
||||
› New worktree... Type the branch name for the new worktree.
|
||||
fcoury/demo (current) clean · cli · /repo/codex.fcoury-demo
|
||||
codex clean · app · /repo/codex.codex
|
||||
main dirty · git · /repo/codex.main
|
||||
|
||||
|
||||
+118
-43
@@ -297,6 +297,33 @@ pub(crate) fn switching_params(
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn creating_params(
|
||||
branch: String,
|
||||
frame_requester: FrameRequester,
|
||||
animations_enabled: bool,
|
||||
) -> SelectionViewParams {
|
||||
let status = format!("Creating {branch}...");
|
||||
let note =
|
||||
"Codex is creating the worktree before starting the chat in that workspace.".to_string();
|
||||
SelectionViewParams {
|
||||
view_id: Some(WORKTREE_SELECTION_VIEW_ID),
|
||||
header: Box::new(WorktreeLoadingHeader::new(
|
||||
frame_requester,
|
||||
animations_enabled,
|
||||
status,
|
||||
note.clone(),
|
||||
)),
|
||||
footer_hint: Some(standard_popup_hint_line()),
|
||||
items: vec![SelectionItem {
|
||||
name: "Preparing worktree...".to_string(),
|
||||
description: Some(note),
|
||||
is_disabled: true,
|
||||
..Default::default()
|
||||
}],
|
||||
..Default::default()
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn empty_params() -> SelectionViewParams {
|
||||
let mut header = ColumnRenderable::new();
|
||||
header.push(Line::from("Worktrees".bold()));
|
||||
@@ -305,12 +332,7 @@ pub(crate) fn empty_params() -> SelectionViewParams {
|
||||
view_id: Some(WORKTREE_SELECTION_VIEW_ID),
|
||||
header: Box::new(header),
|
||||
footer_hint: Some(standard_popup_hint_line()),
|
||||
items: vec![SelectionItem {
|
||||
name: "No worktrees found for this repository.".to_string(),
|
||||
description: Some("Use /worktree new <branch> to create one.".to_string()),
|
||||
is_disabled: true,
|
||||
..Default::default()
|
||||
}],
|
||||
items: vec![new_worktree_item()],
|
||||
..Default::default()
|
||||
}
|
||||
}
|
||||
@@ -338,48 +360,46 @@ pub(crate) fn error_with_summary_params(summary: String, error: String) -> Selec
|
||||
}
|
||||
|
||||
pub(crate) fn picker_params(entries: Vec<WorktreeInfo>, current_cwd: &Path) -> SelectionViewParams {
|
||||
let items = entries
|
||||
.into_iter()
|
||||
.map(|entry| {
|
||||
let target = entry.branch.clone().unwrap_or_else(|| entry.name.clone());
|
||||
let source = source_label(entry.source);
|
||||
let status = if entry.dirty.is_dirty() {
|
||||
"dirty"
|
||||
} else {
|
||||
"clean"
|
||||
};
|
||||
let description = format!("{status} · {source} · {}", entry.workspace_cwd.display());
|
||||
let search_value = Some(format!(
|
||||
"{} {} {} {}",
|
||||
target,
|
||||
entry.name,
|
||||
source,
|
||||
let mut items = vec![new_worktree_item()];
|
||||
items.extend(entries.into_iter().map(|entry| {
|
||||
let target = entry.branch.clone().unwrap_or_else(|| entry.name.clone());
|
||||
let source = source_label(entry.source);
|
||||
let status = if entry.dirty.is_dirty() {
|
||||
"dirty"
|
||||
} else {
|
||||
"clean"
|
||||
};
|
||||
let description = format!("{status} · {source} · {}", entry.workspace_cwd.display());
|
||||
let search_value = Some(format!(
|
||||
"{} {} {} {}",
|
||||
target,
|
||||
entry.name,
|
||||
source,
|
||||
entry.workspace_cwd.display()
|
||||
));
|
||||
SelectionItem {
|
||||
name: target.clone(),
|
||||
description: Some(description),
|
||||
selected_description: Some(format!(
|
||||
"Fork this chat into {}",
|
||||
entry.workspace_cwd.display()
|
||||
));
|
||||
SelectionItem {
|
||||
name: target.clone(),
|
||||
description: Some(description),
|
||||
selected_description: Some(format!(
|
||||
"Fork this chat into {}",
|
||||
entry.workspace_cwd.display()
|
||||
)),
|
||||
is_current: paths_match(current_cwd, &entry.workspace_cwd),
|
||||
actions: vec![Box::new(move |tx| {
|
||||
tx.send(AppEvent::SwitchToWorktree {
|
||||
target: target.clone(),
|
||||
});
|
||||
})],
|
||||
dismiss_on_select: true,
|
||||
search_value,
|
||||
..Default::default()
|
||||
}
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
)),
|
||||
is_current: paths_match(current_cwd, &entry.workspace_cwd),
|
||||
actions: vec![Box::new(move |tx| {
|
||||
tx.send(AppEvent::SwitchToWorktree {
|
||||
target: target.clone(),
|
||||
});
|
||||
})],
|
||||
dismiss_on_select: true,
|
||||
search_value,
|
||||
..Default::default()
|
||||
}
|
||||
}));
|
||||
|
||||
let mut header = ColumnRenderable::new();
|
||||
header.push(Line::from("Worktrees".bold()));
|
||||
header.push(Line::from(
|
||||
"Select a worktree to fork this chat into that workspace.".dim(),
|
||||
"Create a worktree or fork this chat into an existing workspace.".dim(),
|
||||
));
|
||||
|
||||
SelectionViewParams {
|
||||
@@ -395,6 +415,20 @@ pub(crate) fn picker_params(entries: Vec<WorktreeInfo>, current_cwd: &Path) -> S
|
||||
}
|
||||
}
|
||||
|
||||
fn new_worktree_item() -> SelectionItem {
|
||||
SelectionItem {
|
||||
name: "New worktree...".to_string(),
|
||||
description: Some("Create a sibling worktree and start this chat there.".to_string()),
|
||||
selected_description: Some("Type the branch name for the new worktree.".to_string()),
|
||||
actions: vec![Box::new(|tx| {
|
||||
tx.send(AppEvent::OpenWorktreeCreatePrompt);
|
||||
})],
|
||||
dismiss_on_select: false,
|
||||
search_value: Some("new worktree create branch".to_string()),
|
||||
..Default::default()
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn dirty_policy_prompt_params(
|
||||
branch: String,
|
||||
base_ref: Option<String>,
|
||||
@@ -619,6 +653,47 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn worktree_creating_snapshot() {
|
||||
insta::assert_snapshot!(
|
||||
"worktree_creating",
|
||||
render_selection(
|
||||
creating_params(
|
||||
"fcoury/demo".to_string(),
|
||||
FrameRequester::test_dummy(),
|
||||
/*animations_enabled*/ false
|
||||
),
|
||||
/*width*/ 92
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn worktree_empty_snapshot() {
|
||||
insta::assert_snapshot!(
|
||||
"worktree_empty",
|
||||
render_selection(empty_params(), /*width*/ 84)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn new_worktree_item_dispatches_create_prompt_event() {
|
||||
let (tx_raw, mut rx) = unbounded_channel::<AppEvent>();
|
||||
let tx = AppEventSender::new(tx_raw);
|
||||
let item = new_worktree_item();
|
||||
|
||||
assert!(
|
||||
!item.dismiss_on_select,
|
||||
"picker should stay behind the branch-name prompt"
|
||||
);
|
||||
(item.actions[0])(&tx);
|
||||
|
||||
assert!(matches!(
|
||||
rx.try_recv(),
|
||||
Ok(AppEvent::OpenWorktreeCreatePrompt)
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn worktree_dirty_policy_prompt_snapshot() {
|
||||
insta::assert_snapshot!(
|
||||
|
||||
Reference in New Issue
Block a user