Skip to content

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

refactor: Consolidate branch resolution and output messages

Maximilian Roos4ea69a3c59bc
diff --git a/src/commands/dev.rs b/src/commands/dev.rs
index 32190c04f..f60cc1036 100644
--- a/src/commands/dev.rs
+++ b/src/commands/dev.rs
@@ -171,7 +171,7 @@ pub fn handle_dev_squash(
.ok_or_else(|| GitError::CommandFailed("Not on a branch (detached HEAD)".to_string()))?;
// Get target branch (default to default branch if not provided)
- let target_branch = target.map_or_else(|| repo.default_branch(), |b| Ok(b.to_string()))?;
+ let target_branch = repo.resolve_target_branch(target)?;
// Run pre-squash hook unless --no-hooks was specified
if !no_hooks && let Ok(Some(project_config)) = ProjectConfig::load(&repo.worktree_root()?) {
@@ -310,7 +310,7 @@ pub fn handle_dev_rebase(target: Option<&str>) -> Result<(), GitError> {
let repo = Repository::current();
// Get target branch (default to default branch if not provided)
- let target_branch = target.map_or_else(|| repo.default_branch(), |b| Ok(b.to_string()))?;
+ let target_branch = repo.resolve_target_branch(target)?;
// Rebase onto target
crate::output::progress(format!(
diff --git a/src/commands/merge.rs b/src/commands/merge.rs
index 2437eb75b..e5f4243de 100644
--- a/src/commands/merge.rs
+++ b/src/commands/merge.rs
@@ -60,7 +60,7 @@ pub fn handle_merge(
let current_branch = repo.current_branch()?.ok_or(GitError::DetachedHead)?;
// Get target branch (default to default branch if not provided)
- let target_branch = target.map_or_else(|| repo.default_branch(), |b| Ok(b.to_string()))?;
+ let target_branch = repo.resolve_target_branch(target)?;
// Check if already on target branch
if current_branch == target_branch {
diff --git a/src/commands/worktree.rs b/src/commands/worktree.rs
index c2e36b844..3d12245b0 100644
--- a/src/commands/worktree.rs
+++ b/src/commands/worktree.rs
@@ -565,7 +565,7 @@ pub fn handle_push(
let repo = Repository::current();
// Get target branch (default to default branch if not provided)
- let target_branch = target.map_or_else(|| repo.default_branch(), |b| Ok(b.to_string()))?;
+ let target_branch = repo.resolve_target_branch(target)?;
// Check if it's a fast-forward
if !repo.is_ancestor(&target_branch, "HEAD")? {
diff --git a/src/git.rs b/src/git.rs
index 75bfdfabc..f5317ec37 100644
--- a/src/git.rs
+++ b/src/git.rs
@@ -443,6 +443,14 @@ impl Repository {
self.infer_default_branch_locally()
}
+ /// Resolve a target branch from an optional override
+ ///
+ /// If target is Some, returns it as a String. Otherwise, queries the default branch.
+ /// This is a common pattern used throughout commands that accept an optional --target flag.
+ pub fn resolve_target_branch(&self, target: Option<&str>) -> Result<String, GitError> {
+ target.map_or_else(|| self.default_branch(), |b| Ok(b.to_string()))
+ }
+
/// Infer the default branch locally (without remote).
///
/// Uses local heuristics when no remote is available:
diff --git a/src/output/directive.rs b/src/output/directive.rs
index f66de258f..de77655c2 100644
--- a/src/output/directive.rs
+++ b/src/output/directive.rs
@@ -88,20 +88,7 @@ impl DirectiveOutput {
/// In directive mode, the shell wrapper will actually change directories,
/// so we can say "changed directory to {path}"
pub fn format_switch_success(&self, branch: &str, path: &Path, created_branch: bool) -> String {
- use worktrunk::styling::{GREEN, SUCCESS_EMOJI};
- let green_bold = GREEN.bold();
-
- if created_branch {
- format!(
- "{SUCCESS_EMOJI} {GREEN}Created new worktree for {green_bold}{branch}{green_bold:#}{GREEN}, changed directory to {green_bold}{}{green_bold:#}",
- path.display()
- )
- } else {
- format!(
- "{SUCCESS_EMOJI} {GREEN}Switched to worktree for {green_bold}{branch}{green_bold:#}{GREEN}, changed directory to {green_bold}{}{green_bold:#}",
- path.display()
- )
- }
+ super::format_switch_success_message(branch, path, created_branch, true)
}
}
diff --git a/src/output/interactive.rs b/src/output/interactive.rs
index e858d6b04..e4b0374eb 100644
--- a/src/output/interactive.rs
+++ b/src/output/interactive.rs
@@ -72,20 +72,7 @@ impl InteractiveOutput {
///
/// In interactive mode, we can't actually change directories, so we say "at {path}"
pub fn format_switch_success(&self, branch: &str, path: &Path, created_branch: bool) -> String {
- use worktrunk::styling::{GREEN, SUCCESS_EMOJI};
- let green_bold = GREEN.bold();
-
- if created_branch {
- format!(
- "{SUCCESS_EMOJI} {GREEN}Created new worktree for {green_bold}{branch}{green_bold:#}{GREEN} at {green_bold}{}{green_bold:#}",
- path.display()
- )
- } else {
- format!(
- "{SUCCESS_EMOJI} {GREEN}Switched to worktree for {green_bold}{branch}{green_bold:#}{GREEN} at {green_bold}{}{green_bold:#}",
- path.display()
- )
- }
+ super::format_switch_success_message(branch, path, created_branch, false)
}
}
diff --git a/src/output/mod.rs b/src/output/mod.rs
index 4a432e23d..876f1de58 100644
--- a/src/output/mod.rs
+++ b/src/output/mod.rs
@@ -69,3 +69,36 @@ pub use global::{
// Re-export output handlers
pub use handlers::{execute_command_in_worktree, handle_remove_output, handle_switch_output};
+
+use std::path::Path;
+
+/// Format a switch success message with mode-specific location phrase
+///
+/// The message format differs between interactive and directive modes:
+/// - Interactive: "Created new worktree for {branch} at {path}"
+/// - Directive: "Created new worktree for {branch}, changed directory to {path}"
+pub(crate) fn format_switch_success_message(
+ branch: &str,
+ path: &Path,
+ created_branch: bool,
+ use_past_tense: bool,
+) -> String {
+ use worktrunk::styling::{GREEN, SUCCESS_EMOJI};
+ let green_bold = GREEN.bold();
+
+ let action = if created_branch {
+ "Created new worktree for"
+ } else {
+ "Switched to worktree for"
+ };
+ let location = if use_past_tense {
+ ", changed directory to"
+ } else {
+ " at"
+ };
+
+ format!(
+ "{SUCCESS_EMOJI} {GREEN}{action} {green_bold}{branch}{green_bold:#}{GREEN}{location} {green_bold}{}{green_bold:#}",
+ path.display()
+ )
+}