From 01c324e20af8ee4d5b8cf4ba330afd084895acfe Mon Sep 17 00:00:00 2001 From: Anirudh Oppiliappan Date: Wed, 16 Apr 2025 14:48:38 +0300 Subject: [PATCH] knotserver: merge using git-am if format-patch is detected --- knotserver/git/merge.go | 13 ++++++------- knotserver/routes.go | 4 ++++ 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/knotserver/git/merge.go b/knotserver/git/merge.go index aa9edb5a..9057650d 100644 --- a/knotserver/git/merge.go +++ b/knotserver/git/merge.go @@ -31,6 +31,7 @@ type MergeOptions struct { CommitBody string AuthorName string AuthorEmail string + FormatPatch bool } func (e ErrMerge) Error() string { @@ -86,17 +87,12 @@ func (g *GitRepo) cloneRepository(targetBranch string) (string, error) { func (g *GitRepo) applyPatch(tmpDir, patchFile string, checkOnly bool, opts *MergeOptions) error { var stderr bytes.Buffer var cmd *exec.Cmd - var formatPatch = false - - if patchutil.IsFormatPatch(patchFile) { - formatPatch = true - } if checkOnly { cmd = exec.Command("git", "-C", tmpDir, "apply", "--check", "-v", patchFile) } else { // if patch is a format-patch, apply using 'git am' - if formatPatch { + if opts.FormatPatch { amCmd := exec.Command("git", "-C", tmpDir, "am", patchFile) amCmd.Stderr = &stderr if err := amCmd.Run(); err != nil { @@ -169,6 +165,9 @@ func (g *GitRepo) applyPatch(tmpDir, patchFile string, checkOnly bool, opts *Mer } func (g *GitRepo) MergeCheck(patchData []byte, targetBranch string) error { + var opts MergeOptions + opts.FormatPatch = patchutil.IsFormatPatch(string(patchData)) + patchFile, err := g.createTempFileWithPatch(patchData) if err != nil { return &ErrMerge{ @@ -187,7 +186,7 @@ func (g *GitRepo) MergeCheck(patchData []byte, targetBranch string) error { } defer os.RemoveAll(tmpDir) - return g.applyPatch(tmpDir, patchFile, true, nil) + return g.applyPatch(tmpDir, patchFile, true, &opts) } func (g *GitRepo) Merge(patchData []byte, targetBranch string) error { diff --git a/knotserver/routes.go b/knotserver/routes.go index a2a1dc3e..33408b0d 100644 --- a/knotserver/routes.go +++ b/knotserver/routes.go @@ -24,6 +24,7 @@ import ( "github.com/go-git/go-git/v5/plumbing/object" "tangled.sh/tangled.sh/core/knotserver/db" "tangled.sh/tangled.sh/core/knotserver/git" + "tangled.sh/tangled.sh/core/patchutil" "tangled.sh/tangled.sh/core/types" ) @@ -687,6 +688,9 @@ func (h *Handle) Merge(w http.ResponseWriter, r *http.Request) { notFound(w) return } + + mo.FormatPatch = patchutil.IsFormatPatch(patch) + if err := gr.MergeWithOptions([]byte(patch), branch, mo); err != nil { var mergeErr *git.ErrMerge if errors.As(err, &mergeErr) { -- 2.51.2