Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
Add ∅ symbol for orphan branches in main↕ column (#571)
Maximilian Roosa13a092f6131
diff --git a/src/commands/list/collect.rs b/src/commands/list/collect.rsindex b81c237f4..a5ab94a2b 100644--- a/src/commands/list/collect.rs+++ b/src/commands/list/collect.rs@@ -172,6 +172,8 @@ pub(super) enum TaskResult {AheadBehind {item_idx: usize,counts: AheadBehind,+ /// True if this is an orphan branch (no common ancestor with default branch)+ is_orphan: bool,},/// Whether HEAD's tree SHA matches integration target's tree SHA (committed content identical)CommittedTreesMatch {@@ -386,6 +388,8 @@ fn apply_default(items: &mut [ListItem], status_contexts: &mut [StatusContext],}TaskKind::AheadBehind => {items[idx].counts = Some(AheadBehind::default());+ // Conservative: don't claim orphan if we couldn't check+ items[idx].is_orphan = Some(false);}TaskKind::CommittedTreesMatch => {// Conservative: don't claim integrated if we couldn't check@@ -561,8 +565,11 @@ fn drain_results(TaskResult::CommitDetails { commit, .. } => {item.commit = Some(commit);}- TaskResult::AheadBehind { counts, .. } => {+ TaskResult::AheadBehind {+ counts, is_orphan, ..+ } => {item.counts = Some(counts);+ item.is_orphan = Some(is_orphan);}TaskResult::CommittedTreesMatch {committed_trees_match,@@ -867,6 +874,7 @@ pub fn collect(has_file_changes: None,would_merge_add: None,is_ancestor: None,+ is_orphan: None,upstream: None,pr_status: None,url: None,@@ -1453,6 +1461,7 @@ pub fn build_worktree_item(has_file_changes: None,would_merge_add: None,is_ancestor: None,+ is_orphan: None,upstream: None,pr_status: None,url: None,diff --git a/src/commands/list/collect_progressive_impl.rs b/src/commands/list/collect_progressive_impl.rsindex ae1d50e7f..12f25d739 100644--- a/src/commands/list/collect_progressive_impl.rs+++ b/src/commands/list/collect_progressive_impl.rs@@ -402,10 +402,26 @@ impl Task for AheadBehindTask {return Ok(TaskResult::AheadBehind {item_idx: ctx.item_idx,counts: AheadBehind::default(),+ is_orphan: false,});};let repo = &ctx.repo;+ // Check for orphan branch (no common ancestor with default branch).+ // merge_base() is cached, so this is cheap after first call.+ let is_orphan = repo+ .merge_base(&base, &ctx.branch_ref.commit_sha)+ .map_err(|e| ctx.error(Self::KIND, e))?+ .is_none();++ if is_orphan {+ return Ok(TaskResult::AheadBehind {+ item_idx: ctx.item_idx,+ counts: AheadBehind::default(),+ is_orphan: true,+ });+ }+// Check cache first (populated by batch_ahead_behind if it ran).// Cache lookup has minor overhead (rev-parse for cache key + allocations),// but saves the expensive ahead_behind computation on cache hit.@@ -424,6 +440,7 @@ impl Task for AheadBehindTask {Ok(TaskResult::AheadBehind {item_idx: ctx.item_idx,counts: AheadBehind { ahead, behind },+ is_orphan: false,})}}diff --git a/src/commands/list/layout.rs b/src/commands/list/layout.rsindex 038b309dc..091fe3f38 100644--- a/src/commands/list/layout.rs+++ b/src/commands/list/layout.rs@@ -1229,6 +1229,7 @@ mod tests {has_file_changes: Some(true),would_merge_add: None,is_ancestor: None,+ is_orphan: None,upstream: Some(UpstreamStatus::from_parts(Some("origin".to_string()), 4, 2)),pr_status: None,url: None,@@ -1327,6 +1328,7 @@ mod tests {has_file_changes: Some(true),would_merge_add: None,is_ancestor: None,+ is_orphan: None,upstream: Some(UpstreamStatus::default()),pr_status: None,url: None,diff --git a/src/commands/list/model.rs b/src/commands/list/model.rsindex 9fa17680b..c2a6d2dc7 100644--- a/src/commands/list/model.rs+++ b/src/commands/list/model.rs@@ -354,6 +354,10 @@ pub struct ListItem {/// This is the cheapest integration check (~1ms).#[serde(skip)]pub is_ancestor: Option<bool>,+ /// Whether this branch is an orphan (no common ancestor with default branch).+ /// Orphan branches have independent history and can't compute meaningful ahead/behind counts.+ #[serde(skip)]+ pub is_orphan: Option<bool>,// TODO: Same concern as counts/branch_diff above - should upstream fields always be present?#[serde(flatten, skip_serializing_if = "Option::is_none")]@@ -403,6 +407,7 @@ impl ListItem {has_file_changes: None,would_merge_add: None,is_ancestor: None,+ is_orphan: None,upstream: None,pr_status: None,url: None,@@ -706,6 +711,7 @@ impl ListItem {has_merge_tree_conflicts,integration,is_same_commit_dirty,+ self.is_orphan.unwrap_or(false),counts.ahead,counts.behind,);@@ -738,6 +744,7 @@ impl ListItem {has_merge_tree_conflicts,integration,false, // branches have no working tree, can't be dirty+ self.is_orphan.unwrap_or(false),counts.ahead,counts.behind,);@@ -923,13 +930,14 @@ impl serde::Serialize for WorktreeState {////// Priority order determines which symbol is shown:/// 1. IsMain (^) - this IS the main worktree-/// 2. WouldConflict (✗) - merge-tree simulation shows conflicts-/// 3. Empty (_) - same commit as default branch AND clean working tree (safe to delete)-/// 4. SameCommit (–) - same commit as default branch with uncommitted changes-/// 5. Integrated (⊂) - content is in default branch via different history-/// 6. Diverged (↕) - both ahead and behind default branch-/// 7. Ahead (↑) - has commits default branch doesn't have-/// 8. Behind (↓) - missing commits from default branch+/// 2. Orphan (∅) - no common ancestor with default branch+/// 3. WouldConflict (✗) - merge-tree simulation shows conflicts+/// 4. Empty (_) - same commit as default branch AND clean working tree (safe to delete)+/// 5. SameCommit (–) - same commit as default branch with uncommitted changes+/// 6. Integrated (⊂) - content is in default branch via different history+/// 7. Diverged (↕) - both ahead and behind default branch+/// 8. Ahead (↑) - has commits default branch doesn't have+/// 9. Behind (↓) - missing commits from default branch////// The `Integrated` variant carries an [`IntegrationReason`] explaining how the/// content was integrated (ancestor, trees match, no added changes, or merge adds nothing).@@ -951,6 +959,8 @@ pub enum MainState {/// Content is integrated into default branch via different history#[strum(serialize = "integrated")]Integrated(IntegrationReason),+ /// No common ancestor with default branch (orphan branch)+ Orphan,/// Both ahead and behind default branchDiverged,/// Has commits default branch doesn't have@@ -969,6 +979,7 @@ impl std::fmt::Display for MainState {Self::Empty => write!(f, "_"),Self::SameCommit => write!(f, "–"), // en-dash U+2013Self::Integrated(_) => write!(f, "⊂"),+ Self::Orphan => write!(f, "∅"), // U+2205 empty setSelf::Diverged => write!(f, "↕"),Self::Ahead => write!(f, "↑"),Self::Behind => write!(f, "↓"),@@ -1007,17 +1018,25 @@ impl MainState {/// Compute from divergence counts, integration state, and same-commit-dirty flag.///- /// Priority: IsMain > WouldConflict > integration > SameCommit > Diverged > Ahead > Behind+ /// Priority: IsMain > Orphan > WouldConflict > integration > SameCommit > Diverged > Ahead > Behind+ ///+ /// Orphan takes priority over WouldConflict because:+ /// - Orphan is a fundamental property (no common ancestor)+ /// - Merge conflicts for orphan branches are expected but not actionable normally+ /// - Users should understand "this is an orphan branch" rather than "this would conflict"pub fn from_integration_and_counts(is_main: bool,would_conflict: bool,integration: Option<MainState>,is_same_commit_dirty: bool,+ is_orphan: bool,ahead: usize,behind: usize,) -> Self {if is_main {Self::IsMain+ } else if is_orphan {+ Self::Orphan} else if would_conflict {Self::WouldConflict} else if let Some(state) = integration {@@ -1572,13 +1591,19 @@ mod tests {fn test_main_state_from_integration_and_counts() {// IsMain takes priorityassert!(matches!(- MainState::from_integration_and_counts(true, false, None, false, 5, 3),+ MainState::from_integration_and_counts(true, false, None, false, false, 5, 3),MainState::IsMain));- // WouldConflict next+ // Orphan takes priority over WouldConflict (orphan is root cause)+ assert!(matches!(+ MainState::from_integration_and_counts(false, true, None, false, true, 0, 0),+ MainState::Orphan+ ));++ // WouldConflict when not orphanassert!(matches!(- MainState::from_integration_and_counts(false, true, None, false, 5, 3),+ MainState::from_integration_and_counts(false, true, None, false, false, 5, 3),MainState::WouldConflict));@@ -1589,6 +1614,7 @@ mod tests {false,Some(MainState::Empty),false,+ false,0,0),@@ -1602,6 +1628,7 @@ mod tests {false,Some(MainState::Integrated(IntegrationReason::Ancestor)),false,+ false,0,5),@@ -1610,31 +1637,37 @@ mod tests {// SameCommit (via is_same_commit_dirty flag, NOT integration)assert!(matches!(- MainState::from_integration_and_counts(false, false, None, true, 0, 0),+ MainState::from_integration_and_counts(false, false, None, true, false, 0, 0),MainState::SameCommit));+ // Orphan (no common ancestor with default branch)+ assert!(matches!(+ MainState::from_integration_and_counts(false, false, None, false, true, 0, 0),+ MainState::Orphan+ ));+// Diverged (both ahead and behind)assert!(matches!(- MainState::from_integration_and_counts(false, false, None, false, 3, 2),+ MainState::from_integration_and_counts(false, false, None, false, false, 3, 2),MainState::Diverged));// Ahead onlyassert!(matches!(- MainState::from_integration_and_counts(false, false, None, false, 3, 0),+ MainState::from_integration_and_counts(false, false, None, false, false, 3, 0),MainState::Ahead));// Behind onlyassert!(matches!(- MainState::from_integration_and_counts(false, false, None, false, 0, 2),+ MainState::from_integration_and_counts(false, false, None, false, false, 0, 2),MainState::Behind));// None (in sync)assert!(matches!(- MainState::from_integration_and_counts(false, false, None, false, 0, 0),+ MainState::from_integration_and_counts(false, false, None, false, false, 0, 0),MainState::None));}@@ -1813,6 +1846,7 @@ mod tests {format!("{}", MainState::Integrated(IntegrationReason::Ancestor)),"⊂");+ assert_eq!(format!("{}", MainState::Orphan), "∅"); // empty setassert_eq!(format!("{}", MainState::Diverged), "↕");assert_eq!(format!("{}", MainState::Ahead), "↑");assert_eq!(format!("{}", MainState::Behind), "↓");@@ -1833,6 +1867,10 @@ mod tests {let styled = MainState::Ahead.styled().unwrap();assert!(styled.contains("↑"));++ // Orphan is dimmed (informational, not a warning)+ let styled = MainState::Orphan.styled().unwrap();+ assert!(styled.contains("∅"));}#[test]@@ -1845,6 +1883,9 @@ mod tests {let json = serde_json::to_string(&MainState::Diverged).unwrap();assert_eq!(json, "\"↕\"");++ let json = serde_json::to_string(&MainState::Orphan).unwrap();+ assert_eq!(json, "\"∅\"");}// ============================================================================diff --git a/src/git/repository/diff.rs b/src/git/repository/diff.rsindex ac1c608bd..1d6f05928 100644--- a/src/git/repository/diff.rs+++ b/src/git/repository/diff.rs@@ -186,7 +186,7 @@ impl Repository {/// and behind is commits in base not in head.////// For orphan branches with no common ancestor, returns `(0, 0)`.- /// TODO: Consider distinct display for orphan branches (e.g., "–" or "∅")+ /// Caller should check for orphan status separately via `merge_base()`.////// Uses `merge_base()` internally (which is cached) to compute the common/// ancestor, then counts commits using two-dot syntax. This allows thediff --git a/tests/snapshots/integration__integration_tests__list__list_handles_orphan_branch.snap b/tests/snapshots/integration__integration_tests__list__list_handles_orphan_branch.snapindex ae075aa88..77095678d 100644--- a/tests/snapshots/integration__integration_tests__list__list_handles_orphan_branch.snap+++ b/tests/snapshots/integration__integration_tests__list__list_handles_orphan_branch.snap@@ -32,7 +32,7 @@ exit_code: 0----- stdout -----[1mBranch[0m [1mStatus[0m [1mHEAD±[0m [1mmain↕[0m [1mPath[0m [1mRemote⇅[0m [1mCommit[0m [1mAge[0m [1mMessage@ main [2m^[22m . [2ma1e809f5[0m [2m1d[0m [2mInitial commit- assets [2m/[22m[33m✗[39m [2m50209039[0m [2m1d[0m [2mAdd asset+ assets [2m/[22m[2m∅[22m [2m50209039[0m [2m1d[0m [2mAdd asset[2m○[22m [2mShowing 1 worktrees, 1 branches