Skip to content

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

fix: don't report branch creation when local branch already exists (#656) (#660)

Maximilian Roosfbf5a4eff33c
diff --git a/src/commands/worktree.rs b/src/commands/worktree.rs
index eaa84f1cc..eb46f65ac 100644
--- a/src/commands/worktree.rs
+++ b/src/commands/worktree.rs
@@ -376,6 +376,8 @@ 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,
},
}
@@ -563,6 +565,11 @@ 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() {
@@ -646,6 +653,7 @@ pub fn plan_switch(
base_branch,
clobber_backup,
new_previous,
+ local_branch_exists,
})
}
@@ -706,6 +714,7 @@ 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 {
@@ -745,7 +754,8 @@ pub fn execute_switch(
}
// Check if git's DWIM created a tracking branch from a remote
- let from_remote = if !create_branch {
+ // Only report this if the local branch didn't exist before (DWIM created it)
+ let from_remote = if !create_branch && !local_branch_exists {
repo.upstream_branch(&branch)?
} else {
None
diff --git a/tests/integration_tests/switch.rs b/tests/integration_tests/switch.rs
index 43e1ebce5..b67175392 100644
--- a/tests/integration_tests/switch.rs
+++ b/tests/integration_tests/switch.rs
@@ -91,6 +91,39 @@ fn test_switch_create_with_remote_branch_only(#[from(repo_with_remote)] repo: Te
);
}
+/// Git's DWIM creates local tracking branch from remote when no local branch exists.
+/// Should report "Created branch X (tracking remote)" since DWIM actually created the branch.
+#[rstest]
+fn test_switch_dwim_from_remote(#[from(repo_with_remote)] repo: TestRepo) {
+ // Create a branch on the remote only (no local branch)
+ repo.run_git(&["branch", "dwim-feature"]);
+ repo.run_git(&["push", "origin", "dwim-feature"]);
+ repo.run_git(&["branch", "-D", "dwim-feature"]);
+
+ // Now we have origin/dwim-feature but no local dwim-feature
+ // DWIM should create local branch from remote
+ snapshot_switch("switch_dwim_from_remote", &repo, &["dwim-feature"]);
+}
+
+/// When local branch already exists and tracks a remote, should report
+/// "Created worktree for X" NOT "Created branch X (tracking remote)".
+/// This is the bug fix for GitHub issue #656.
+#[rstest]
+fn test_switch_existing_local_branch_with_upstream(#[from(repo_with_remote)] repo: TestRepo) {
+ // Create local branch tracking remote
+ repo.run_git(&["checkout", "-b", "tracked-feature"]);
+ repo.run_git(&["commit", "--allow-empty", "-m", "feature commit"]);
+ repo.run_git(&["push", "-u", "origin", "tracked-feature"]);
+ repo.run_git(&["checkout", "main"]);
+
+ // Switch to the existing local branch (should NOT say "Created branch")
+ snapshot_switch(
+ "switch_existing_local_with_upstream",
+ &repo,
+ &["tracked-feature"],
+ );
+}
+
#[rstest]
fn test_switch_existing_branch(mut repo: TestRepo) {
repo.add_worktree("feature-z");
diff --git a/tests/snapshots/integration__integration_tests__switch__switch_dwim_from_remote.snap b/tests/snapshots/integration__integration_tests__switch__switch_dwim_from_remote.snap
new file mode 100644
index 000000000..bcb201bee
--- /dev/null
+++ b/tests/snapshots/integration__integration_tests__switch__switch_dwim_from_remote.snap
@@ -0,0 +1,38 @@
+---
+source: tests/integration_tests/switch.rs
+info:
+ program: wt
+ args:
+ - switch
+ - dwim-feature
+ env:
+ APPDATA: "[TEST_CONFIG_HOME]"
+ CLICOLOR_FORCE: "1"
+ COLUMNS: "500"
+ GIT_AUTHOR_DATE: "2025-01-01T00:00:00Z"
+ GIT_COMMITTER_DATE: "2025-01-01T00:00:00Z"
+ GIT_CONFIG_GLOBAL: "[TEST_GIT_CONFIG]"
+ GIT_CONFIG_SYSTEM: /dev/null
+ GIT_EDITOR: ""
+ GIT_TERMINAL_PROMPT: "0"
+ HOME: "[TEST_HOME]"
+ LANG: C
+ LC_ALL: C
+ MOCK_CONFIG_DIR: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpk2rrMV/mock-bin
+ PATH: "[PATH]"
+ RUST_LOG: warn
+ SOURCE_DATE_EPOCH: "1735776000"
+ TERM: alacritty
+ USERPROFILE: "[TEST_HOME]"
+ WORKTRUNK_CONFIG_PATH: "[TEST_CONFIG]"
+ WORKTRUNK_TEST_SKIP_URL_HEALTH_CHECK: "1"
+ XDG_CONFIG_HOME: "[TEST_CONFIG_HOME]"
+---
+success: true
+exit_code: 0
+----- stdout -----
+
+----- stderr -----
+✓ Created branch dwim-feature (tracking origin/dwim-feature) and worktree @ _REPO_.dwim-feature
+▲ Cannot change directory — shell integration not installed
+↳ To enable automatic cd, run wt config shell install
diff --git a/tests/snapshots/integration__integration_tests__switch__switch_existing_local_with_upstream.snap b/tests/snapshots/integration__integration_tests__switch__switch_existing_local_with_upstream.snap
new file mode 100644
index 000000000..bca237fce
--- /dev/null
+++ b/tests/snapshots/integration__integration_tests__switch__switch_existing_local_with_upstream.snap
@@ -0,0 +1,38 @@
+---
+source: tests/integration_tests/switch.rs
+info:
+ program: wt
+ args:
+ - switch
+ - tracked-feature
+ env:
+ APPDATA: "[TEST_CONFIG_HOME]"
+ CLICOLOR_FORCE: "1"
+ COLUMNS: "500"
+ GIT_AUTHOR_DATE: "2025-01-01T00:00:00Z"
+ GIT_COMMITTER_DATE: "2025-01-01T00:00:00Z"
+ GIT_CONFIG_GLOBAL: "[TEST_GIT_CONFIG]"
+ GIT_CONFIG_SYSTEM: /dev/null
+ GIT_EDITOR: ""
+ GIT_TERMINAL_PROMPT: "0"
+ HOME: "[TEST_HOME]"
+ LANG: C
+ LC_ALL: C
+ MOCK_CONFIG_DIR: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmptQDDmu/mock-bin
+ PATH: "[PATH]"
+ RUST_LOG: warn
+ SOURCE_DATE_EPOCH: "1735776000"
+ TERM: alacritty
+ USERPROFILE: "[TEST_HOME]"
+ WORKTRUNK_CONFIG_PATH: "[TEST_CONFIG]"
+ WORKTRUNK_TEST_SKIP_URL_HEALTH_CHECK: "1"
+ XDG_CONFIG_HOME: "[TEST_CONFIG_HOME]"
+---
+success: true
+exit_code: 0
+----- stdout -----
+
+----- stderr -----
+✓ Created worktree for tracked-feature @ _REPO_.tracked-feature
+▲ Cannot change directory — shell integration not installed
+↳ To enable automatic cd, run wt config shell install