···
"github.com/dgraph-io/ristretto"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
15
-
"tangled.sh/tangled.sh/core/patchutil"
type MergeCheckCache struct {
···
146
-
func (g *GitRepo) applyPatch(tmpDir, patchFile string, checkOnly bool, opts *MergeOptions) error {
145
+
func (g *GitRepo) checkPatch(tmpDir, patchFile string) error {
151
-
cmd = exec.Command("git", "-C", tmpDir, "apply", "--check", "-v", patchFile)
153
-
// if patch is a format-patch, apply using 'git am'
154
-
if opts.FormatPatch {
155
-
amCmd := exec.Command("git", "-C", tmpDir, "am", patchFile)
156
-
amCmd.Stderr = &stderr
157
-
if err := amCmd.Run(); err != nil {
158
-
return fmt.Errorf("patch application failed: %s", stderr.String())
148
+
cmd := exec.Command("git", "-C", tmpDir, "apply", "--check", "-v", patchFile)
149
+
cmd.Stderr = &stderr
151
+
if err := cmd.Run(); err != nil {
152
+
conflicts := parseGitApplyErrors(stderr.String())
154
+
Message: "patch cannot be applied cleanly",
155
+
Conflicts: conflicts,
156
+
HasConflict: len(conflicts) > 0,
163
-
// else, apply using 'git apply' and commit it manually
164
-
exec.Command("git", "-C", tmpDir, "config", "advice.mergeConflict", "false").Run()
166
-
applyCmd := exec.Command("git", "-C", tmpDir, "apply", patchFile)
167
-
applyCmd.Stderr = &stderr
168
-
if err := applyCmd.Run(); err != nil {
169
-
return fmt.Errorf("patch application failed: %s", stderr.String())
163
+
func (g *GitRepo) applyPatch(tmpDir, patchFile string, opts *MergeOptions) error {
164
+
var stderr bytes.Buffer
172
-
stageCmd := exec.Command("git", "-C", tmpDir, "add", ".")
173
-
if err := stageCmd.Run(); err != nil {
174
-
return fmt.Errorf("failed to stage changes: %w", err)
167
+
// if patch is a format-patch, apply using 'git am'
168
+
if opts.FormatPatch {
169
+
amCmd := exec.Command("git", "-C", tmpDir, "am", patchFile)
170
+
amCmd.Stderr = &stderr
171
+
if err := amCmd.Run(); err != nil {
172
+
return fmt.Errorf("patch application failed: %s", stderr.String())
177
-
commitArgs := []string{"-C", tmpDir, "commit"}
177
+
// else, apply using 'git apply' and commit it manually
178
+
exec.Command("git", "-C", tmpDir, "config", "advice.mergeConflict", "false").Run()
180
+
applyCmd := exec.Command("git", "-C", tmpDir, "apply", patchFile)
181
+
applyCmd.Stderr = &stderr
182
+
if err := applyCmd.Run(); err != nil {
183
+
return fmt.Errorf("patch application failed: %s", stderr.String())
179
-
// Set author if provided
180
-
authorName := opts.AuthorName
181
-
authorEmail := opts.AuthorEmail
186
+
stageCmd := exec.Command("git", "-C", tmpDir, "add", ".")
187
+
if err := stageCmd.Run(); err != nil {
188
+
return fmt.Errorf("failed to stage changes: %w", err)
183
-
if authorEmail == "" {
184
-
authorEmail = "noreply@tangled.sh"
191
+
commitArgs := []string{"-C", tmpDir, "commit"}
187
-
if authorName == "" {
188
-
authorName = "Tangled"
193
+
// Set author if provided
194
+
authorName := opts.AuthorName
195
+
authorEmail := opts.AuthorEmail
191
-
if authorName != "" {
192
-
commitArgs = append(commitArgs, "--author", fmt.Sprintf("%s <%s>", authorName, authorEmail))
197
+
if authorEmail == "" {
198
+
authorEmail = "noreply@tangled.sh"
195
-
commitArgs = append(commitArgs, "-m", opts.CommitMessage)
201
+
if authorName == "" {
202
+
authorName = "Tangled"
197
-
if opts.CommitBody != "" {
198
-
commitArgs = append(commitArgs, "-m", opts.CommitBody)
205
+
if authorName != "" {
206
+
commitArgs = append(commitArgs, "--author", fmt.Sprintf("%s <%s>", authorName, authorEmail))
209
+
commitArgs = append(commitArgs, "-m", opts.CommitMessage)
201
-
cmd = exec.Command("git", commitArgs...)
203
-
// If no commit message specified, use git-am which automatically creates a commit
204
-
cmd = exec.Command("git", "-C", tmpDir, "am", patchFile)
211
+
if opts.CommitBody != "" {
212
+
commitArgs = append(commitArgs, "-m", opts.CommitBody)
215
+
cmd = exec.Command("git", commitArgs...)
217
+
// If no commit message specified, use git-am which automatically creates a commit
218
+
cmd = exec.Command("git", "-C", tmpDir, "am", patchFile)
if err := cmd.Run(); err != nil {
212
-
conflicts := parseGitApplyErrors(stderr.String())
214
-
Message: "patch cannot be applied cleanly",
215
-
Conflicts: conflicts,
216
-
HasConflict: len(conflicts) > 0,
return fmt.Errorf("patch application failed: %s", stderr.String())
···
231
-
var opts MergeOptions
232
-
opts.FormatPatch = patchutil.IsFormatPatch(string(patchData))
patchFile, err := g.createTempFileWithPatch(patchData)
···
defer os.RemoveAll(tmpDir)
252
-
result := g.applyPatch(tmpDir, patchFile, true, &opts)
253
+
result := g.checkPatch(tmpDir, patchFile)
mergeCheckCache.Set(g, patchData, targetBranch, result)
···
defer os.RemoveAll(tmpDir)
280
-
if err := g.applyPatch(tmpDir, patchFile, false, opts); err != nil {
281
+
if err := g.applyPatch(tmpDir, patchFile, opts); err != nil {