diff --git a/codex-rs/core/src/git_info.rs b/codex-rs/core/src/git_info.rs index 52d029f669..01eab70939 100644 --- a/codex-rs/core/src/git_info.rs +++ b/codex-rs/core/src/git_info.rs @@ -83,6 +83,36 @@ pub async fn collect_git_info(cwd: &Path) -> Option { Some(git_info) } +/// Blocking convenience wrapper around `collect_git_info` suitable for +/// synchronous contexts. If called from within an active Tokio runtime, +/// spawns a separate thread to avoid blocking the runtime's executor thread. +pub fn collect_git_info_blocking(cwd: &Path) -> Option { + // If we're already on a Tokio runtime, avoid `block_on` on the same thread. + if tokio::runtime::Handle::try_current().is_ok() { + let cwd = cwd.to_path_buf(); + let (tx, rx) = std::sync::mpsc::channel(); + std::thread::spawn(move || { + // Use a lightweight current-thread runtime. + let rt = tokio::runtime::Builder::new_current_thread() + .enable_all() + .build(); + let res = match rt { + Ok(rt) => rt.block_on(collect_git_info(&cwd)), + Err(_) => None, + }; + let _ = tx.send(res); + }); + rx.recv().ok().flatten() + } else { + // Safe to block here – we're not inside a runtime. + let rt = tokio::runtime::Builder::new_current_thread() + .enable_all() + .build() + .ok()?; + rt.block_on(collect_git_info(cwd)) + } +} + /// Run a git command with a timeout to prevent blocking on large repositories async fn run_git_command_with_timeout(args: &[&str], cwd: &Path) -> Option { let result = timeout( diff --git a/codex-rs/tui/src/history_cell.rs b/codex-rs/tui/src/history_cell.rs index 3658dc7ed7..2794e2b8b3 100644 --- a/codex-rs/tui/src/history_cell.rs +++ b/codex-rs/tui/src/history_cell.rs @@ -212,6 +212,19 @@ impl HistoryCell { None => config.cwd.display().to_string(), }; + // Determine the current Git branch (if any) for display using + // the shared Git info collector in a way that is safe in sync contexts. + let branch_suffix = codex_core::git_info::collect_git_info_blocking(&config.cwd) + .and_then(|g| g.branch) + .map(|b| format!(" ({b})")) + .unwrap_or_default(); + + let path_and_branch = if branch_suffix.is_empty() { + format!(" {cwd_str}") + } else { + format!(" {cwd_str}{branch_suffix}") + }; + let lines: Vec> = vec![ Line::from(vec![ Span::raw(">_ ").dim(), @@ -219,7 +232,7 @@ impl HistoryCell { "You are using OpenAI Codex in", Style::default().add_modifier(Modifier::BOLD), ), - Span::raw(format!(" {cwd_str}")).dim(), + Span::raw(path_and_branch).dim(), ]), Line::from("".dim()), Line::from(" To get started, describe a task or try one of these commands:".dim()), @@ -834,6 +847,8 @@ impl HistoryCell { } } +// + impl WidgetRef for &HistoryCell { fn render_ref(&self, area: Rect, buf: &mut Buffer) { Paragraph::new(Text::from(self.plain_lines()))