Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
feat(switch): Allow --create with remote-only branches
Maximilian Roosed0d14fa2eb2
diff --git a/src/commands/worktree.rs b/src/commands/worktree.rsindex c5e2482fb..f0f350eb7 100644--- a/src/commands/worktree.rs+++ b/src/commands/worktree.rs@@ -153,7 +153,7 @@ pub fn handle_switch(let repo = Repository::current();// Check for conflicting conditions- if create && repo.branch_exists(branch)? {+ if create && repo.local_branch_exists(branch)? {return Err(GitError::BranchAlreadyExists {branch: branch.to_string(),});diff --git a/src/git.rs b/src/git.rsindex d4e97bd3d..3af3ea6de 100644--- a/src/git.rs+++ b/src/git.rs@@ -356,13 +356,17 @@ impl Repository {Ok(first_remote.unwrap_or("origin").to_string())}+ /// Check if a local git branch exists.+ pub fn local_branch_exists(&self, branch: &str) -> Result<bool, GitError> {+ Ok(self+ .run_command(&["rev-parse", "--verify", &format!("refs/heads/{}", branch)])+ .is_ok())+ }+/// Check if a git branch exists (local or remote).pub fn branch_exists(&self, branch: &str) -> Result<bool, GitError> {// Try local branch first- if self- .run_command(&["rev-parse", "--verify", &format!("refs/heads/{}", branch)])- .is_ok()- {+ if self.local_branch_exists(branch)? {return Ok(true);}diff --git a/tests/integration_tests/switch.rs b/tests/integration_tests/switch.rsindex 4c6576639..ea19a9656 100644--- a/tests/integration_tests/switch.rs+++ b/tests/integration_tests/switch.rs@@ -59,6 +59,48 @@ fn test_switch_create_existing_branch_error() {);}+#[test]+fn test_switch_create_with_remote_branch_only() {+ use std::process::Command;++ let mut repo = TestRepo::new();+ repo.commit("Initial commit");++ // Set up a remote+ repo.setup_remote("main");++ // Create a branch on the remote only (no local branch)+ let mut cmd = Command::new("git");+ repo.configure_git_cmd(&mut cmd);+ cmd.args(["branch", "remote-feature"])+ .current_dir(repo.root_path())+ .output()+ .expect("Failed to create branch");++ let mut cmd = Command::new("git");+ repo.configure_git_cmd(&mut cmd);+ cmd.args(["push", "origin", "remote-feature"])+ .current_dir(repo.root_path())+ .output()+ .expect("Failed to push to remote");++ // Delete the local branch+ let mut cmd = Command::new("git");+ repo.configure_git_cmd(&mut cmd);+ cmd.args(["branch", "-D", "remote-feature"])+ .current_dir(repo.root_path())+ .output()+ .expect("Failed to delete local branch");++ // Now we have origin/remote-feature but no local remote-feature+ // This should succeed with --create (previously would fail)+ snapshot_switch(+ "switch_create_remote_only",+ &repo,+ &["--create", "remote-feature"],+ );+}+#[test]fn test_switch_existing_branch() {let mut repo = TestRepo::new();diff --git a/tests/snapshots/integration__integration_tests__switch__switch_create_remote_only.snap b/tests/snapshots/integration__integration_tests__switch__switch_create_remote_only.snapnew file mode 100644index 000000000..76386184e--- /dev/null+++ b/tests/snapshots/integration__integration_tests__switch__switch_create_remote_only.snap@@ -0,0 +1,29 @@+---+source: tests/integration_tests/switch.rs+info:+ program: wt+ args:+ - switch+ - "--create"+ - remote-feature+ env:+ CLICOLOR_FORCE: "1"+ COLUMNS: "150"+ GIT_AUTHOR_DATE: "2025-01-01T00:00:00Z"+ GIT_COMMITTER_DATE: "2025-01-01T00:00:00Z"+ GIT_CONFIG_GLOBAL: /dev/null+ GIT_CONFIG_SYSTEM: /dev/null+ GIT_EDITOR: ""+ LANG: C+ LC_ALL: C+ SOURCE_DATE_EPOCH: "1761609600"+ WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpboPJAp/test-config.toml+---+success: true+exit_code: 0+----- stdout -----+✅ [32mCreated new worktree for [1m[32mremote-feature[0m at [1m[REPO].remote-feature[0m[0m++To enable automatic cd, run: wt config shell++----- stderr -----