Skip to content

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

Move divergence computation to enum impls

Maximilian Roos092dc706d1f7
diff --git a/src/commands/list/collect.rs b/src/commands/list/collect.rs
index d6622f4ae..485150bd2 100644
--- a/src/commands/list/collect.rs
+++ b/src/commands/list/collect.rs
@@ -123,34 +123,14 @@ pub(super) fn detect_worktree_state(repo: &Repository) -> Option<String> {
}
}
-/// Compute main branch divergence state from ahead/behind counts.
-fn compute_main_divergence(ahead: usize, behind: usize) -> MainDivergence {
- match (ahead, behind) {
- (0, 0) => MainDivergence::None,
- (_, 0) => MainDivergence::Ahead,
- (0, _) => MainDivergence::Behind,
- _ => MainDivergence::Diverged,
- }
-}
-
-/// Compute upstream divergence state from ahead/behind counts.
-fn compute_upstream_divergence(ahead: usize, behind: usize) -> UpstreamDivergence {
- match (ahead, behind) {
- (0, 0) => UpstreamDivergence::None,
- (_, 0) => UpstreamDivergence::Ahead,
- (0, _) => UpstreamDivergence::Behind,
- _ => UpstreamDivergence::Diverged,
- }
-}
-
fn compute_divergences(
counts: &AheadBehind,
upstream: &UpstreamStatus,
) -> (MainDivergence, UpstreamDivergence) {
- let main_divergence = compute_main_divergence(counts.ahead, counts.behind);
+ let main_divergence = MainDivergence::from_counts(counts.ahead, counts.behind);
let (upstream_ahead, upstream_behind) =
upstream.active().map(|(_, a, b)| (a, b)).unwrap_or((0, 0));
- let upstream_divergence = compute_upstream_divergence(upstream_ahead, upstream_behind);
+ let upstream_divergence = UpstreamDivergence::from_counts(upstream_ahead, upstream_behind);
(main_divergence, upstream_divergence)
}
diff --git a/src/commands/list/model.rs b/src/commands/list/model.rs
index b65eec95f..de3726455 100644
--- a/src/commands/list/model.rs
+++ b/src/commands/list/model.rs
@@ -325,6 +325,21 @@ impl serde::Serialize for MainDivergence {
}
}
+impl MainDivergence {
+ /// Compute divergence state from ahead/behind counts.
+ ///
+ /// Note: This cannot produce `IsMain` - that variant is set explicitly
+ /// when the worktree is on the main branch.
+ pub fn from_counts(ahead: usize, behind: usize) -> Self {
+ match (ahead, behind) {
+ (0, 0) => Self::None,
+ (_, 0) => Self::Ahead,
+ (0, _) => Self::Behind,
+ _ => Self::Diverged,
+ }
+ }
+}
+
/// Upstream/remote divergence state
///
/// Represents relationship to the remote tracking branch.
@@ -362,6 +377,18 @@ impl serde::Serialize for UpstreamDivergence {
}
}
+impl UpstreamDivergence {
+ /// Compute divergence state from ahead/behind counts.
+ pub fn from_counts(ahead: usize, behind: usize) -> Self {
+ match (ahead, behind) {
+ (0, 0) => Self::None,
+ (_, 0) => Self::Ahead,
+ (0, _) => Self::Behind,
+ _ => Self::Diverged,
+ }
+ }
+}
+
/// Combined branch and operation state
///
/// Represents the primary state of a branch/worktree in a single position.