diff options
| author | Ogulcan Celik <ogulcancelik@gmail.com> | 2026-06-22 00:51:00 +0300 |
|---|---|---|
| committer | Ogulcan Celik <ogulcancelik@gmail.com> | 2026-06-22 00:51:00 +0300 |
| commit | 89ca3ba933eaf493234912972be8236c9c2493e5 (patch) | |
| tree | 508a0489cf3aa15f6d454a1c86697b0a7ba4ed72 /src/app/worktrees.rs | |
| parent | a5182282a490cfb60b22776257edc5ac0fce6adc (diff) | |
fix: check out existing branches for worktree create
refs #729
Diffstat (limited to 'src/app/worktrees.rs')
| -rw-r--r-- | src/app/worktrees.rs | 50 |
1 files changed, 31 insertions, 19 deletions
diff --git a/src/app/worktrees.rs b/src/app/worktrees.rs index e1c30a77..12e7ae84 100644 --- a/src/app/worktrees.rs +++ b/src/app/worktrees.rs @@ -521,12 +521,6 @@ impl App { create.creating = true; create.error = None; - let command = crate::worktree::build_worktree_add_new_branch_command( - &create.source_checkout_path, - &create.checkout_path, - &create.branch, - "HEAD", - ); let parent_dir = create .checkout_path .parent() @@ -538,15 +532,23 @@ impl App { "starting git worktree add" ); let path = create.checkout_path.clone(); + let source_checkout_path = create.source_checkout_path.clone(); + let branch = create.branch.clone(); let event_tx = self.event_tx.clone(); std::thread::spawn(move || { let result = if let Some(parent_dir) = parent_dir { - std::fs::create_dir_all(&parent_dir) - .map_err(|err| err.to_string()) - .and_then(|()| crate::worktree::run_worktree_command(&command)) + std::fs::create_dir_all(&parent_dir).map_err(|err| err.to_string()) } else { - crate::worktree::run_worktree_command(&command) - }; + Ok(()) + } + .and_then(|()| { + crate::worktree::run_worktree_add_command( + &source_checkout_path, + &path, + &branch, + "HEAD", + ) + }); let _ = event_tx.blocking_send(AppEvent::WorktreeAddFinished(Box::new( WorktreeAddResult { path, @@ -1632,7 +1634,7 @@ mod tests { } #[test] - fn start_worktree_add_existing_branch_clears_creating_and_shows_fatal_error() { + fn start_worktree_add_existing_branch_checks_out_branch() { let repo = create_committed_repo("app-worktree-add-existing-branch-repo"); let worktree_root = unique_temp_path("app-worktree-add-existing-branch-root"); let branch = "foo"; @@ -1664,18 +1666,28 @@ mod tests { let event = wait_for_worktree_event(&mut app); match event { AppEvent::WorktreeAddFinished(result) => { - app.handle_worktree_add_finished(*result); + let result = *result; + assert_eq!(result.path, checkout); + assert_eq!(result.result, Ok(())); } other => panic!("unexpected event: {other:?}"), } - let create = app.state.worktree_create.as_ref().unwrap(); - assert!(!create.creating); - let error = create.error.as_deref().unwrap(); - assert!(error.contains("Preparing worktree")); - assert!(error.contains("fatal: a branch named 'foo' already exists")); - assert!(!checkout.exists()); + assert!(checkout.join("README.md").exists()); + let branch_name = std::process::Command::new("git") + .arg("-C") + .arg(&checkout) + .args(["branch", "--show-current"]) + .output() + .unwrap(); + assert!(branch_name.status.success()); + assert_eq!( + String::from_utf8(branch_name.stdout).unwrap().trim(), + branch + ); + let remove = crate::worktree::build_worktree_remove_command(&repo, &checkout, false); + crate::worktree::run_worktree_command(&remove).unwrap(); let _ = std::fs::remove_dir_all(worktree_root); let _ = std::fs::remove_dir_all(repo); } |
