···
-
func (g *GitRepo) applyPatch(tmpDir, patchFile string, checkOnly bool, opts *MergeOptions) error {
-
cmd = exec.Command("git", "-C", tmpDir, "apply", "--check", "-v", patchFile)
-
// if patch is a format-patch, apply using 'git am'
-
amCmd := exec.Command("git", "-C", tmpDir, "am", patchFile)
-
if err := amCmd.Run(); err != nil {
-
return fmt.Errorf("patch application failed: %s", stderr.String())
-
// else, apply using 'git apply' and commit it manually
-
exec.Command("git", "-C", tmpDir, "config", "advice.mergeConflict", "false").Run()
-
applyCmd := exec.Command("git", "-C", tmpDir, "apply", patchFile)
-
applyCmd.Stderr = &stderr
-
if err := applyCmd.Run(); err != nil {
-
return fmt.Errorf("patch application failed: %s", stderr.String())
-
stageCmd := exec.Command("git", "-C", tmpDir, "add", ".")
-
if err := stageCmd.Run(); err != nil {
-
return fmt.Errorf("failed to stage changes: %w", err)
-
commitArgs := []string{"-C", tmpDir, "commit"}
-
// Set author if provided
-
authorName := opts.AuthorName
-
authorEmail := opts.AuthorEmail
-
authorEmail = "noreply@tangled.sh"
-
commitArgs = append(commitArgs, "--author", fmt.Sprintf("%s <%s>", authorName, authorEmail))
-
commitArgs = append(commitArgs, "-m", opts.CommitMessage)
-
if opts.CommitBody != "" {
-
commitArgs = append(commitArgs, "-m", opts.CommitBody)
-
cmd = exec.Command("git", commitArgs...)
-
// If no commit message specified, use git-am which automatically creates a commit
-
cmd = exec.Command("git", "-C", tmpDir, "am", patchFile)
if err := cmd.Run(); err != nil {
-
conflicts := parseGitApplyErrors(stderr.String())
-
Message: "patch cannot be applied cleanly",
-
HasConflict: len(conflicts) > 0,
return fmt.Errorf("patch application failed: %s", stderr.String())
···
-
opts.FormatPatch = patchutil.IsFormatPatch(string(patchData))
patchFile, err := g.createTempFileWithPatch(patchData)
···
defer os.RemoveAll(tmpDir)
-
result := g.applyPatch(tmpDir, patchFile, true, &opts)
mergeCheckCache.Set(g, patchData, targetBranch, result)
···
defer os.RemoveAll(tmpDir)
-
if err := g.applyPatch(tmpDir, patchFile, false, opts); err != nil {
···
+
func (g *GitRepo) checkPatch(tmpDir, patchFile string) error {
+
cmd := exec.Command("git", "-C", tmpDir, "apply", "--check", "-v", patchFile)
+
if err := cmd.Run(); err != nil {
+
conflicts := parseGitApplyErrors(stderr.String())
+
Message: "patch cannot be applied cleanly",
+
HasConflict: len(conflicts) > 0,
+
func (g *GitRepo) applyPatch(tmpDir, patchFile string, opts *MergeOptions) error {
+
var stderr bytes.Buffer
+
// if patch is a format-patch, apply using 'git am'
+
amCmd := exec.Command("git", "-C", tmpDir, "am", patchFile)
+
if err := amCmd.Run(); err != nil {
+
return fmt.Errorf("patch application failed: %s", stderr.String())
+
// else, apply using 'git apply' and commit it manually
+
exec.Command("git", "-C", tmpDir, "config", "advice.mergeConflict", "false").Run()
+
applyCmd := exec.Command("git", "-C", tmpDir, "apply", patchFile)
+
applyCmd.Stderr = &stderr
+
if err := applyCmd.Run(); err != nil {
+
return fmt.Errorf("patch application failed: %s", stderr.String())
+
stageCmd := exec.Command("git", "-C", tmpDir, "add", ".")
+
if err := stageCmd.Run(); err != nil {
+
return fmt.Errorf("failed to stage changes: %w", err)
+
commitArgs := []string{"-C", tmpDir, "commit"}
+
// Set author if provided
+
authorName := opts.AuthorName
+
authorEmail := opts.AuthorEmail
+
authorEmail = "noreply@tangled.sh"
+
commitArgs = append(commitArgs, "--author", fmt.Sprintf("%s <%s>", authorName, authorEmail))
+
commitArgs = append(commitArgs, "-m", opts.CommitMessage)
+
if opts.CommitBody != "" {
+
commitArgs = append(commitArgs, "-m", opts.CommitBody)
+
cmd = exec.Command("git", commitArgs...)
+
// If no commit message specified, use git-am which automatically creates a commit
+
cmd = exec.Command("git", "-C", tmpDir, "am", patchFile)
if err := cmd.Run(); err != nil {
return fmt.Errorf("patch application failed: %s", stderr.String())
···
patchFile, err := g.createTempFileWithPatch(patchData)
···
defer os.RemoveAll(tmpDir)
+
result := g.checkPatch(tmpDir, patchFile)
mergeCheckCache.Set(g, patchData, targetBranch, result)
···
defer os.RemoveAll(tmpDir)
+
if err := g.applyPatch(tmpDir, patchFile, opts); err != nil {