From 15b6e5fba161a194fe5427d7d29b0c4286423260 Mon Sep 17 00:00:00 2001 From: soryu Date: Tue, 10 Feb 2026 14:50:07 +0000 Subject: Add auto-PR creation for remote repos in directives --- makima/src/daemon/worktree/manager.rs | 92 +++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) (limited to 'makima/src/daemon/worktree') diff --git a/makima/src/daemon/worktree/manager.rs b/makima/src/daemon/worktree/manager.rs index 310627c..20c93b1 100644 --- a/makima/src/daemon/worktree/manager.rs +++ b/makima/src/daemon/worktree/manager.rs @@ -1417,6 +1417,98 @@ impl WorktreeManager { Ok(()) } + /// Push a worktree branch to origin (the upstream GitHub remote). + /// Simpler than push_to_target_repo — just pushes to origin. + pub async fn push_branch_to_origin( + &self, + worktree_path: &Path, + branch_name: &str, + task_name: &str, + ) -> Result<(), WorktreeError> { + tracing::info!( + worktree = %worktree_path.display(), + branch = %branch_name, + "Pushing branch to origin" + ); + + // Stage all changes + let output = Command::new("git") + .args(["add", "-A"]) + .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 stage changes: {}", + stderr + ))); + } + + // Check if there are staged changes to commit + let output = Command::new("git") + .args(["diff", "--cached", "--quiet"]) + .current_dir(worktree_path) + .output() + .await?; + + // Exit code 1 means there are staged changes + if !output.status.success() { + tracing::info!("Committing staged changes before push to origin"); + + let commit_message = format!("feat: {}", task_name); + let output = Command::new("git") + .args(["commit", "-m", &commit_message]) + .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 commit changes: {}", + stderr + ))); + } + } + + // Ensure there are commits to push + let output = Command::new("git") + .args(["log", "--oneline", "-1"]) + .current_dir(worktree_path) + .output() + .await?; + + if !output.status.success() { + return Err(WorktreeError::GitCommand( + "No commits in worktree".to_string(), + )); + } + + // Push to origin + let output = Command::new("git") + .args(["push", "-u", "origin", &format!("HEAD:{}", branch_name)]) + .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 to origin: {}", + stderr + ))); + } + + tracing::info!( + branch = %branch_name, + "Branch pushed to origin successfully" + ); + + Ok(()) + } + /// Merge a branch into the target branch in the target repository. /// /// This pushes the branch first (if needed), then performs a merge in the target repo. -- cgit v1.2.3