Skip to content

add branch name w/ sync function for git info #2142

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions codex-rs/core/src/git_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,36 @@ pub async fn collect_git_info(cwd: &Path) -> Option<GitInfo> {
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<GitInfo> {
// 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<std::process::Output> {
let result = timeout(
Expand Down
17 changes: 16 additions & 1 deletion codex-rs/tui/src/history_cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,14 +212,27 @@ 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)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we make this part of the SessionConfiguredEvent so we can do all this work async?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks! should be better now

.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<Line<'static>> = vec![
Line::from(vec![
Span::raw(">_ ").dim(),
Span::styled(
"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()),
Expand Down Expand Up @@ -834,6 +847,8 @@ impl HistoryCell {
}
}

//
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove?


impl WidgetRef for &HistoryCell {
fn render_ref(&self, area: Rect, buf: &mut Buffer) {
Paragraph::new(Text::from(self.plain_lines()))
Expand Down
Loading