summaryrefslogtreecommitdiff
path: root/makima/src/daemon/worktree
diff options
context:
space:
mode:
Diffstat (limited to 'makima/src/daemon/worktree')
-rw-r--r--makima/src/daemon/worktree/manager.rs131
1 files changed, 84 insertions, 47 deletions
diff --git a/makima/src/daemon/worktree/manager.rs b/makima/src/daemon/worktree/manager.rs
index d370828..5edd7b1 100644
--- a/makima/src/daemon/worktree/manager.rs
+++ b/makima/src/daemon/worktree/manager.rs
@@ -1470,10 +1470,11 @@ impl WorktreeManager {
/// Create a GitHub pull request using the gh CLI.
///
/// This pushes the branch first, then creates a PR.
+ /// If target_repo is None, uses the worktree's origin remote directly (for repos already cloned from remote).
pub async fn create_pull_request(
&self,
worktree_path: &Path,
- target_repo: &Path,
+ target_repo: Option<&Path>,
source_branch: &str,
target_branch: &str,
title: &str,
@@ -1481,7 +1482,7 @@ impl WorktreeManager {
) -> Result<String, WorktreeError> {
tracing::info!(
worktree = %worktree_path.display(),
- target_repo = %target_repo.display(),
+ target_repo = ?target_repo.map(|p| p.display().to_string()),
source_branch = %source_branch,
target_branch = %target_branch,
title = %title,
@@ -1504,60 +1505,96 @@ impl WorktreeManager {
source_branch.to_string()
};
- // Push to the target repo's origin
- // First, check if target_repo has an origin remote
- let output = Command::new("git")
- .args(["remote", "get-url", "origin"])
- .current_dir(target_repo)
- .output()
- .await?;
+ // Get the origin URL - either from target_repo or from worktree directly
+ let (origin_url, gh_working_dir) = if let Some(target_repo) = target_repo {
+ // Use target_repo's origin
+ let output = Command::new("git")
+ .args(["remote", "get-url", "origin"])
+ .current_dir(target_repo)
+ .output()
+ .await?;
- if !output.status.success() {
- return Err(WorktreeError::GitCommand(
- "Target repository has no origin remote configured".to_string(),
- ));
- }
+ if !output.status.success() {
+ return Err(WorktreeError::GitCommand(
+ "Target repository has no origin remote configured".to_string(),
+ ));
+ }
+
+ let url = String::from_utf8_lossy(&output.stdout).trim().to_string();
+ (url, target_repo.to_path_buf())
+ } else {
+ // Check if worktree has an origin remote directly
+ let output = Command::new("git")
+ .args(["remote", "get-url", "origin"])
+ .current_dir(worktree_path)
+ .output()
+ .await?;
- let origin_url = String::from_utf8_lossy(&output.stdout).trim().to_string();
+ if !output.status.success() {
+ return Err(WorktreeError::GitCommand(
+ "Repository has no origin remote configured. Either set target_repo_path or ensure the worktree was cloned from a remote repository.".to_string(),
+ ));
+ }
+
+ let url = String::from_utf8_lossy(&output.stdout).trim().to_string();
+ (url, worktree_path.to_path_buf())
+ };
// Push the branch from worktree to the remote
- // First add the remote to worktree
- let _ = Command::new("git")
- .args(["remote", "remove", "pr-origin"])
- .current_dir(worktree_path)
- .output()
- .await;
+ // First add the remote to worktree (if not using worktree's origin directly)
+ if target_repo.is_some() {
+ let _ = Command::new("git")
+ .args(["remote", "remove", "pr-origin"])
+ .current_dir(worktree_path)
+ .output()
+ .await;
- let output = Command::new("git")
- .args(["remote", "add", "pr-origin", &origin_url])
- .current_dir(worktree_path)
- .output()
- .await?;
+ let output = Command::new("git")
+ .args(["remote", "add", "pr-origin", &origin_url])
+ .current_dir(worktree_path)
+ .output()
+ .await?;
- if !output.status.success() {
- let stderr = String::from_utf8_lossy(&output.stderr);
- return Err(WorktreeError::GitCommand(format!(
- "Failed to add remote: {}",
- stderr
- )));
- }
+ if !output.status.success() {
+ let stderr = String::from_utf8_lossy(&output.stderr);
+ return Err(WorktreeError::GitCommand(format!(
+ "Failed to add remote: {}",
+ stderr
+ )));
+ }
- // Push to the remote
- let output = Command::new("git")
- .args(["push", "-u", "pr-origin", &format!("{}:{}", current_branch, source_branch)])
- .current_dir(worktree_path)
- .output()
- .await?;
+ // Push to the remote
+ let output = Command::new("git")
+ .args(["push", "-u", "pr-origin", &format!("{}:{}", current_branch, source_branch)])
+ .current_dir(worktree_path)
+ .output()
+ .await?;
- if !output.status.success() {
- let stderr = String::from_utf8_lossy(&output.stderr);
- return Err(WorktreeError::GitCommand(format!(
- "Failed to push branch: {}",
- stderr
- )));
+ if !output.status.success() {
+ let stderr = String::from_utf8_lossy(&output.stderr);
+ return Err(WorktreeError::GitCommand(format!(
+ "Failed to push branch: {}",
+ stderr
+ )));
+ }
+ } else {
+ // Push directly to origin
+ let output = Command::new("git")
+ .args(["push", "-u", "origin", &format!("{}:{}", current_branch, source_branch)])
+ .current_dir(worktree_path)
+ .output()
+ .await?;
+
+ if !output.status.success() {
+ let stderr = String::from_utf8_lossy(&output.stderr);
+ return Err(WorktreeError::GitCommand(format!(
+ "Failed to push branch: {}",
+ stderr
+ )));
+ }
}
- // Create PR using gh CLI in the target repo
+ // Create PR using gh CLI
let output = Command::new("gh")
.args([
"pr",
@@ -1567,7 +1604,7 @@ impl WorktreeManager {
"--head", source_branch,
"--base", target_branch,
])
- .current_dir(target_repo)
+ .current_dir(&gh_working_dir)
.output()
.await?;