Skip to content

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

refactor: Simplify command expansion variable handling

Maximilian Roose930e6f4a1fa
diff --git a/src/commands/command_executor.rs b/src/commands/command_executor.rs
index 39f36f5fe..0e639573a 100644
--- a/src/commands/command_executor.rs
+++ b/src/commands/command_executor.rs
@@ -60,28 +60,27 @@ fn expand_commands(
.and_then(|n| n.to_str())
.unwrap_or("unknown");
- let mut base_extras = HashMap::new();
- base_extras.insert(
+ let mut extras_owned = HashMap::new();
+ extras_owned.insert(
"worktree".to_string(),
ctx.worktree_path.to_string_lossy().to_string(),
);
- base_extras.insert(
+ extras_owned.insert(
"repo_root".to_string(),
repo_root.to_string_lossy().to_string(),
);
for &(key, value) in extra_vars {
- base_extras.insert(key.to_string(), value.to_string());
+ extras_owned.insert(key.to_string(), value.to_string());
}
+ let extras_ref: HashMap<&str, &str> = extras_owned
+ .iter()
+ .map(|(k, v)| (k.as_str(), v.as_str()))
+ .collect();
+
let mut expanded_commands = Vec::new();
for cmd in commands {
- let extras_owned = base_extras.clone();
- let extras_ref: HashMap<&str, &str> = extras_owned
- .iter()
- .map(|(k, v)| (k.as_str(), v.as_str()))
- .collect();
-
let expanded_str = expand_template(&cmd.template, repo_name, ctx.branch, &extras_ref)
.map_err(|e| {
GitError::message(format!(
diff --git a/src/commands/list/collect.rs b/src/commands/list/collect.rs
index 8fc01cd14..3d1399fd5 100644
--- a/src/commands/list/collect.rs
+++ b/src/commands/list/collect.rs
@@ -89,15 +89,15 @@ impl CellUpdate {
/// Get the item index for this update
fn item_idx(&self) -> usize {
match self {
- CellUpdate::CommitDetails { item_idx, .. } => *item_idx,
- CellUpdate::AheadBehind { item_idx, .. } => *item_idx,
- CellUpdate::BranchDiff { item_idx, .. } => *item_idx,
- CellUpdate::WorkingTreeDiff { item_idx, .. } => *item_idx,
- CellUpdate::Conflicts { item_idx, .. } => *item_idx,
- CellUpdate::WorktreeState { item_idx, .. } => *item_idx,
- CellUpdate::UserStatus { item_idx, .. } => *item_idx,
- CellUpdate::Upstream { item_idx, .. } => *item_idx,
- CellUpdate::CiStatus { item_idx, .. } => *item_idx,
+ CellUpdate::CommitDetails { item_idx, .. }
+ | CellUpdate::AheadBehind { item_idx, .. }
+ | CellUpdate::BranchDiff { item_idx, .. }
+ | CellUpdate::WorkingTreeDiff { item_idx, .. }
+ | CellUpdate::Conflicts { item_idx, .. }
+ | CellUpdate::WorktreeState { item_idx, .. }
+ | CellUpdate::UserStatus { item_idx, .. }
+ | CellUpdate::Upstream { item_idx, .. }
+ | CellUpdate::CiStatus { item_idx, .. } => *item_idx,
}
}
}
@@ -115,23 +115,40 @@ pub(super) fn detect_worktree_state(repo: &Repository) -> Option<String> {
}
}
+#[derive(Clone, Copy, Debug, PartialEq, Eq)]
+enum DivergenceKind {
+ None,
+ Ahead,
+ Behind,
+ Diverged,
+}
+
+fn classify_divergence(ahead: usize, behind: usize) -> DivergenceKind {
+ match (ahead, behind) {
+ (0, 0) => DivergenceKind::None,
+ (a, 0) if a > 0 => DivergenceKind::Ahead,
+ (0, b) if b > 0 => DivergenceKind::Behind,
+ _ => DivergenceKind::Diverged,
+ }
+}
+
/// 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,
- (a, 0) if a > 0 => MainDivergence::Ahead,
- (0, b) if b > 0 => MainDivergence::Behind,
- _ => MainDivergence::Diverged,
+ match classify_divergence(ahead, behind) {
+ DivergenceKind::None => MainDivergence::None,
+ DivergenceKind::Ahead => MainDivergence::Ahead,
+ DivergenceKind::Behind => MainDivergence::Behind,
+ DivergenceKind::Diverged => 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,
- (a, 0) if a > 0 => UpstreamDivergence::Ahead,
- (0, b) if b > 0 => UpstreamDivergence::Behind,
- _ => UpstreamDivergence::Diverged,
+ match classify_divergence(ahead, behind) {
+ DivergenceKind::None => UpstreamDivergence::None,
+ DivergenceKind::Ahead => UpstreamDivergence::Ahead,
+ DivergenceKind::Behind => UpstreamDivergence::Behind,
+ DivergenceKind::Diverged => UpstreamDivergence::Diverged,
}
}