Skip to content

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

refactor: simplify local branch check for DWIM detection (#663)

Maximilian Roos5b86fdf0cb6f
diff --git a/src/commands/worktree.rs b/src/commands/worktree.rs
index eb46f65ac..fc40b04f9 100644
--- a/src/commands/worktree.rs
+++ b/src/commands/worktree.rs
@@ -376,8 +376,6 @@ pub enum SwitchPlan {
clobber_backup: Option<PathBuf>,
/// Branch to record as "previous" for `wt switch -`
new_previous: Option<String>,
- /// True if local branch already exists (for DWIM detection)
- local_branch_exists: bool,
},
}
@@ -565,11 +563,6 @@ pub fn plan_switch(
.into());
}
- // Track whether local branch exists (for DWIM detection in output messages)
- // If !create and we got here, branch_exists() returned true, which could mean
- // either local branch exists OR only remote branch exists (DWIM will create local)
- let local_branch_exists = create || repo.local_branch_exists(&resolved_branch)?;
-
// Check if expected path is occupied by a different branch's worktree
if let Some((existing_path, path_branch)) = repo.worktree_at_path(&expected_path)? {
if !existing_path.exists() {
@@ -653,7 +646,6 @@ pub fn plan_switch(
base_branch,
clobber_backup,
new_previous,
- local_branch_exists,
})
}
@@ -714,7 +706,6 @@ pub fn execute_switch(
base_branch,
clobber_backup,
new_previous,
- local_branch_exists,
} => {
// Handle --clobber backup if needed
if let Some(backup_path) = clobber_backup {
@@ -729,6 +720,11 @@ pub fn execute_switch(
})?;
}
+ // Check if local branch exists BEFORE git worktree add (for DWIM detection).
+ // Only relevant when not using --create (which explicitly creates the branch).
+ let local_branch_existed =
+ !create_branch && repo.local_branch_exists(&branch).unwrap_or(false);
+
// Build git worktree add command
let worktree_path_str = worktree_path.to_string_lossy();
let mut args = vec!["worktree", "add", worktree_path_str.as_ref()];
@@ -753,9 +749,8 @@ pub fn execute_switch(
.into());
}
- // Check if git's DWIM created a tracking branch from a remote
- // Only report this if the local branch didn't exist before (DWIM created it)
- let from_remote = if !create_branch && !local_branch_exists {
+ // Report tracking info only if git's DWIM created the branch from a remote
+ let from_remote = if !create_branch && !local_branch_existed {
repo.upstream_branch(&branch)?
} else {
None