Skip to content

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

Detect when already at target worktree in switch

Maximilian Roosd0da511aa61e
diff --git a/src/commands/worktree.rs b/src/commands/worktree.rs
index 810c82273..5a9c1330e 100644
--- a/src/commands/worktree.rs
+++ b/src/commands/worktree.rs
@@ -118,10 +118,12 @@ pub struct MergeOperations {
/// Result of a worktree switch operation
pub enum SwitchResult {
+ /// Already at the target worktree (no action taken)
+ AlreadyAt(PathBuf),
/// Switched to existing worktree at the given path
- ExistingWorktree(PathBuf),
+ Existing(PathBuf),
/// Created new worktree at the given path
- CreatedWorktree {
+ Created {
path: PathBuf,
created_branch: bool,
base_branch: Option<String>,
@@ -132,8 +134,9 @@ impl SwitchResult {
/// Get the worktree path
pub fn path(&self) -> &PathBuf {
match self {
- SwitchResult::ExistingWorktree(path) => path,
- SwitchResult::CreatedWorktree { path, .. } => path,
+ SwitchResult::AlreadyAt(path) => path,
+ SwitchResult::Existing(path) => path,
+ SwitchResult::Created { path, .. } => path,
}
}
}
@@ -218,8 +221,23 @@ pub fn handle_switch(
// Canonicalize the path for cleaner display
let canonical_existing_path = existing_path.canonicalize().unwrap_or(existing_path);
+
+ // Check if we're already at this worktree
+ let current_dir = std::env::current_dir()
+ .ok()
+ .and_then(|p| p.canonicalize().ok());
+
+ let already_at_worktree = current_dir
+ .as_ref()
+ .map(|cur| cur == &canonical_existing_path)
+ .unwrap_or(false);
+
return Ok((
- SwitchResult::ExistingWorktree(canonical_existing_path),
+ if already_at_worktree {
+ SwitchResult::AlreadyAt(canonical_existing_path)
+ } else {
+ SwitchResult::Existing(canonical_existing_path)
+ },
resolved_branch,
));
}
@@ -341,7 +359,7 @@ pub fn handle_switch(
let _ = repo.record_switch_history(&new_current, new_previous.as_deref());
Ok((
- SwitchResult::CreatedWorktree {
+ SwitchResult::Created {
path: worktree_path,
created_branch: create,
base_branch: base_for_creation,
diff --git a/src/main.rs b/src/main.rs
index b7f086cab..ced5c43f5 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -219,7 +219,7 @@ fn main() {
// Now spawn post-start hooks (background processes, after success message)
// Only run post-start commands when creating a NEW worktree, not when switching to existing
// Note: If user declines post-start commands, continue anyway - they're optional
- if !no_verify && let SwitchResult::CreatedWorktree { path, .. } = &result {
+ if !no_verify && let SwitchResult::Created { path, .. } = &result {
let repo = Repository::current();
let repo_root = repo.worktree_base()?;
let ctx = CommandContext::new(
diff --git a/src/output/handlers.rs b/src/output/handlers.rs
index b0900a460..5b5e103dc 100644
--- a/src/output/handlers.rs
+++ b/src/output/handlers.rs
@@ -6,19 +6,33 @@ use worktrunk::git::GitError;
use worktrunk::styling::{GREEN, GREEN_BOLD, WARNING, WARNING_BOLD, format_with_gutter};
/// Format message for switch operation (mode-specific via output system)
-fn format_switch_message(result: &SwitchResult, branch: &str) -> String {
+fn format_switch_message(result: &SwitchResult, branch: &str) -> (String, bool) {
match result {
- SwitchResult::ExistingWorktree(path) => {
+ SwitchResult::AlreadyAt(path) => {
+ // Note: output::info() adds the INFO_EMOJI automatically
+ let bold = worktrunk::styling::AnstyleStyle::new().bold();
+ (
+ format!(
+ "Already on worktree for {bold}{branch}{bold:#} at {bold}{}{bold:#}",
+ path.display()
+ ),
+ true, // is_info
+ )
+ }
+ SwitchResult::Existing(path) => {
// created_branch=false means we switched to existing worktree
- format_switch_success(branch, path, false, None)
+ (format_switch_success(branch, path, false, None), false)
}
- SwitchResult::CreatedWorktree {
+ SwitchResult::Created {
path,
created_branch,
base_branch,
} => {
// Pass through whether we created a new branch and the base branch
- format_switch_success(branch, path, *created_branch, base_branch.as_deref())
+ (
+ format_switch_success(branch, path, *created_branch, base_branch.as_deref()),
+ false,
+ )
}
}
}
@@ -91,8 +105,13 @@ pub fn handle_switch_output(
// Set target directory for command execution
super::change_directory(result.path())?;
- // Show success message (includes emoji and color)
- super::success(format_switch_message(result, branch))?;
+ // Show message (success or info based on result)
+ let (message, is_info) = format_switch_message(result, branch);
+ if is_info {
+ super::info(message)?;
+ } else {
+ super::success(message)?;
+ }
// If no execute command provided: show shell integration hint
// (suppressed in directive mode since user already has integration)
diff --git a/tests/snapshots/integration__integration_tests__switch__switch_base_without_create.snap b/tests/snapshots/integration__integration_tests__switch__switch_base_without_create.snap
index 695a65932..b8b7783a8 100644
--- a/tests/snapshots/integration__integration_tests__switch__switch_base_without_create.snap
+++ b/tests/snapshots/integration__integration_tests__switch__switch_base_without_create.snap
@@ -18,13 +18,13 @@ info:
LANG: C
LC_ALL: C
SOURCE_DATE_EPOCH: "1761609600"
- WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpxg95yO/test-config.toml
+ WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpBGb1tD/test-config.toml
---
success: true
exit_code: 0
----- stdout -----
🟡 --base flag is only used with --create, ignoring
-✅ Switched to worktree for main at [REPO]
+⚪ Already on worktree for main at [REPO]
💡 To enable automatic cd, run: wt config shell
----- stderr -----