diff options
| -rw-r--r-- | makima/src/daemon/worktree/manager.rs | 79 |
1 files changed, 72 insertions, 7 deletions
diff --git a/makima/src/daemon/worktree/manager.rs b/makima/src/daemon/worktree/manager.rs index ea26767..2caa86a 100644 --- a/makima/src/daemon/worktree/manager.rs +++ b/makima/src/daemon/worktree/manager.rs @@ -491,16 +491,81 @@ impl WorktreeManager { .output() .await; - // Create the worktree with a new branch based on the local base_branch - let output = Command::new("git") + // Prefer origin/{base_branch} to get latest remote state. + // If neither origin/{base_branch} nor {base_branch} exist (e.g. PR branch + // was deleted after merge), fall back to the repo's default branch. + let origin_ref = format!("origin/{}", base_branch); + let has_origin_ref = Command::new("git") .args([ - "worktree", - "add", - "-b", - &branch_name, + "rev-parse", + "--verify", + &format!("refs/remotes/{}", origin_ref), ]) + .current_dir(source_repo) + .output() + .await + .map(|o| o.status.success()) + .unwrap_or(false); + + let has_local_ref = if !has_origin_ref { + Command::new("git") + .args([ + "rev-parse", + "--verify", + &format!("refs/heads/{}", base_branch), + ]) + .current_dir(source_repo) + .output() + .await + .map(|o| o.status.success()) + .unwrap_or(false) + } else { + false // don't need to check — we already have origin ref + }; + + let start_point: String = if has_origin_ref { + origin_ref + } else if has_local_ref { + base_branch.to_string() + } else { + // Branch doesn't exist (likely deleted after PR merge) — use default branch + tracing::warn!( + task_id = %task_id, + base_branch = %base_branch, + "Base branch ref not found, falling back to default branch" + ); + let default_branch = + self.detect_default_branch(source_repo).await?; + let default_origin = format!("origin/{}", default_branch); + let has_default_origin = Command::new("git") + .args([ + "rev-parse", + "--verify", + &format!("refs/remotes/{}", default_origin), + ]) + .current_dir(source_repo) + .output() + .await + .map(|o| o.status.success()) + .unwrap_or(false); + if has_default_origin { + default_origin + } else { + default_branch + } + }; + + tracing::info!( + task_id = %task_id, + start_point = %start_point, + "Using start point for new worktree branch" + ); + + // Create the worktree with a new branch based on the start point + let output = Command::new("git") + .args(["worktree", "add", "-b", &branch_name]) .arg(&worktree_path) - .arg(start_point) + .arg(&start_point) .current_dir(source_repo) .output() .await?; |
