applyPatchToRepo applies a patch to the target repository and returns the branch name
(patchFile string, prInfo *PRInfo, targetOwner, targetRepo string, verbose bool)
| 255 | return patchFile, nil |
| 256 | } // applyPatchToRepo applies a patch to the target repository and returns the branch name |
| 257 | func applyPatchToRepo(patchFile string, prInfo *PRInfo, targetOwner, targetRepo string, verbose bool) (string, error) { |
| 258 | // Get current branch to restore later |
| 259 | currentBranch, err := getCurrentBranch() |
| 260 | if err != nil { |
| 261 | return "", fmt.Errorf("failed to get current branch: %w", err) |
| 262 | } |
| 263 | |
| 264 | // Get the default branch of the target repository |
| 265 | defaultBranchOutput, err := workflow.RunGH("Fetching default branch...", "api", fmt.Sprintf("/repos/%s/%s", targetOwner, targetRepo), "--jq", ".default_branch") |
| 266 | if err != nil { |
| 267 | return "", fmt.Errorf("failed to get default branch: %w", err) |
| 268 | } |
| 269 | defaultBranch := strings.TrimSpace(string(defaultBranchOutput)) |
| 270 | |
| 271 | // Ensure we're on the latest version of the default branch |
| 272 | if verbose { |
| 273 | fmt.Fprintln(os.Stderr, console.FormatInfoMessage(fmt.Sprintf("Checking out and updating %s branch...", defaultBranch))) |
| 274 | } |
| 275 | |
| 276 | cmd := exec.Command("git", "checkout", defaultBranch) |
| 277 | if err := cmd.Run(); err != nil { |
| 278 | return "", fmt.Errorf("failed to checkout default branch %s: %w", defaultBranch, err) |
| 279 | } |
| 280 | |
| 281 | cmd = exec.Command("git", "pull", "origin", defaultBranch) |
| 282 | if err := cmd.Run(); err != nil { |
| 283 | return "", fmt.Errorf("failed to pull latest %s: %w", defaultBranch, err) |
| 284 | } |
| 285 | |
| 286 | // Create a new branch for the transfer based on the updated default branch |
| 287 | branchName := fmt.Sprintf("transfer-pr-%d-%d", prInfo.Number, time.Now().Unix()) |
| 288 | if verbose { |
| 289 | fmt.Fprintln(os.Stderr, console.FormatInfoMessage("Creating branch: "+branchName)) |
| 290 | } |
| 291 | |
| 292 | if err := createAndSwitchBranch(branchName, verbose); err != nil { |
| 293 | return "", fmt.Errorf("failed to create new branch: %w", err) |
| 294 | } |
| 295 | |
| 296 | // Apply the patch |
| 297 | if verbose { |
| 298 | fmt.Fprintln(os.Stderr, console.FormatInfoMessage("Applying patch...")) |
| 299 | |
| 300 | // Show some info about the patch file |
| 301 | patchContent, err := os.ReadFile(patchFile) |
| 302 | if err == nil { |
| 303 | lines := strings.Split(string(patchContent), "\n") |
| 304 | fmt.Fprintln(os.Stderr, console.FormatInfoMessage(fmt.Sprintf("Patch file has %d lines", len(lines)))) |
| 305 | if len(lines) > 0 { |
| 306 | fmt.Fprintln(os.Stderr, console.FormatInfoMessage("First line: "+lines[0])) |
| 307 | } |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | // Check if patch looks like a mailbox format (starts with "From ") |
| 312 | patchContent, err := os.ReadFile(patchFile) |
| 313 | if err != nil { |
| 314 | return "", fmt.Errorf("failed to read patch file: %w", err) |
no test coverage detected