MergeBranch merges a branch into the current branch, returning conflicting file list on failure.
(repoDir, branch string)
| 218 | |
| 219 | // MergeBranch merges a branch into the current branch, returning conflicting file list on failure. |
| 220 | func MergeBranch(repoDir, branch string) ([]string, error) { |
| 221 | cmd := exec.Command("git", "merge", branch) |
| 222 | cmd.Dir = repoDir |
| 223 | out, err := cmd.CombinedOutput() |
| 224 | if err != nil { |
| 225 | // Parse conflicting files from merge output |
| 226 | conflicts := parseConflicts(repoDir) |
| 227 | if len(conflicts) > 0 { |
| 228 | // Abort the merge to leave a clean state |
| 229 | abortCmd := exec.Command("git", "merge", "--abort") |
| 230 | abortCmd.Dir = repoDir |
| 231 | _ = abortCmd.Run() |
| 232 | return conflicts, fmt.Errorf("merge conflict: %s", strings.TrimSpace(string(out))) |
| 233 | } |
| 234 | return nil, fmt.Errorf("merge failed: %s", strings.TrimSpace(string(out))) |
| 235 | } |
| 236 | return nil, nil |
| 237 | } |
| 238 | |
| 239 | // parseConflicts uses `git diff --name-only --diff-filter=U` to find conflicting files. |
| 240 | func parseConflicts(repoDir string) []string { |