Skip to content

Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows

Simplify CI status and render mode detection logic

Maximilian Roosa17bee7f2555
diff --git a/src/commands/list/ci_status.rs b/src/commands/list/ci_status.rs
index 7ff787cb0..a6048dd5a 100644
--- a/src/commands/list/ci_status.rs
+++ b/src/commands/list/ci_status.rs
@@ -553,7 +553,7 @@ impl PrStatus {
pub fn detect(branch: &str, local_head: &str, repo_path: &std::path::Path) -> Option<Self> {
// We run gh/glab commands from the repo directory to let them auto-detect the correct repo
// (including upstream repos for forks)
- let repo_root = repo_path.to_str().expect("repo path is not valid UTF-8");
+ let repo_root = repo_path.to_str()?;
// Check cache first to avoid hitting API rate limits
use std::time::{SystemTime, UNIX_EPOCH};
diff --git a/src/commands/list/progressive.rs b/src/commands/list/progressive.rs
index 7e28c2568..5ee674cfd 100644
--- a/src/commands/list/progressive.rs
+++ b/src/commands/list/progressive.rs
@@ -13,11 +13,9 @@ impl RenderMode {
/// # Arguments
///
/// * `progressive` - Rendering mode (Some(true) = --progressive, Some(false) = --no-progressive, None = auto)
- /// * `directive_mode` - True if in directive mode (--internal), affects which stream to check
///
- /// In directive mode, table output goes to stderr, so we check stderr's TTY status.
- /// In interactive mode, table output goes to stdout, so we check stdout's TTY status.
- pub fn detect(progressive: Option<bool>, directive_mode: bool) -> Self {
+ /// Table output always goes to stderr (via output::table()), so we check stderr's TTY status.
+ pub fn detect(progressive: Option<bool>) -> Self {
// Priority 1: Explicit CLI flag
match progressive {
Some(true) => return RenderMode::Progressive,
@@ -25,18 +23,9 @@ impl RenderMode {
None => {} // Fall through to auto-detection
}
- // Priority 2: Auto-detect based on TTY
- // Check the appropriate stream based on output mode:
- // - Directive mode: check stderr (where table output goes via output::table())
- // - Interactive mode: check stdout (where table output goes)
+ // Priority 2: Auto-detect based on stderr TTY (table output always goes to stderr)
use std::io::IsTerminal;
- let is_tty = if directive_mode {
- std::io::stderr().is_terminal()
- } else {
- std::io::stdout().is_terminal()
- };
-
- if is_tty {
+ if std::io::stderr().is_terminal() {
// TODO: Check for pager in environment
RenderMode::Progressive
} else {
@@ -52,18 +41,10 @@ mod tests {
#[test]
fn test_render_mode_detect_explicit_flags() {
// --progressive (Some(true)) should force progressive mode
- assert_eq!(
- RenderMode::detect(Some(true), false),
- RenderMode::Progressive
- );
- assert_eq!(
- RenderMode::detect(Some(true), true),
- RenderMode::Progressive
- );
+ assert_eq!(RenderMode::detect(Some(true)), RenderMode::Progressive);
// --no-progressive (Some(false)) should force buffered mode
- assert_eq!(RenderMode::detect(Some(false), false), RenderMode::Buffered);
- assert_eq!(RenderMode::detect(Some(false), true), RenderMode::Buffered);
+ assert_eq!(RenderMode::detect(Some(false)), RenderMode::Buffered);
// None should auto-detect (tested via TTY checks in runtime)
}
diff --git a/src/git/mod.rs b/src/git/mod.rs
index ae82630b6..85b27e541 100644
--- a/src/git/mod.rs
+++ b/src/git/mod.rs
@@ -158,8 +158,12 @@ pub struct Worktree {
///
/// This type ensures:
/// - Bare worktrees are filtered out (only worktrees with working trees are included)
-/// - The main worktree is always at index 0 (accessible via `.main()`)
+/// - The main worktree is at index 0 (accessible via `.main()`) for non-bare repos
/// - Construction fails if no valid worktrees exist
+///
+/// Git guarantees that the main worktree is listed first in `git worktree list` output,
+/// so index 0 is always the main worktree after filtering. For bare repositories where
+/// the main worktree is filtered out, index 0 will be the first linked worktree.
#[derive(Debug, Clone)]
pub struct WorktreeList {
pub worktrees: Vec<Worktree>,
@@ -167,6 +171,8 @@ pub struct WorktreeList {
impl WorktreeList {
/// Create from raw worktrees, filtering bare entries.
+ ///
+ /// Preserves git's ordering where the main worktree is first.
pub(crate) fn from_raw(raw_worktrees: Vec<Worktree>) -> anyhow::Result<Self> {
let worktrees: Vec<_> = raw_worktrees.into_iter().filter(|wt| !wt.bare).collect();
@@ -180,7 +186,11 @@ impl WorktreeList {
Ok(Self { worktrees })
}
- /// Returns the main worktree (always at index 0).
+ /// Returns the main worktree (at index 0).
+ ///
+ /// For non-bare repositories, this is the original working tree created by
+ /// `git init` or `git clone`. For bare repositories, this returns the first
+ /// linked worktree (since bare worktrees are filtered out).
pub fn main(&self) -> &Worktree {
&self.worktrees[0]
}
diff --git a/src/main.rs b/src/main.rs
index b8130abc4..1bc293990 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1016,7 +1016,7 @@ fn main() {
(_, true) => Some(false),
_ => None,
};
- let render_mode = RenderMode::detect(progressive_opt, cli.internal);
+ let render_mode = RenderMode::detect(progressive_opt);
handle_list(
format,
show_branches,