+144
-31
knotserver/git/merge.go
+144
-31
knotserver/git/merge.go
···
4
4
"bytes"
5
5
"crypto/sha256"
6
6
"fmt"
7
+
"log"
7
8
"os"
8
9
"os/exec"
9
10
"regexp"
···
12
13
"github.com/dgraph-io/ristretto"
13
14
"github.com/go-git/go-git/v5"
14
15
"github.com/go-git/go-git/v5/plumbing"
16
+
"tangled.org/core/patchutil"
17
+
"tangled.org/core/types"
15
18
)
16
19
17
20
type MergeCheckCache struct {
···
162
165
return nil
163
166
}
164
167
165
-
func (g *GitRepo) applyPatch(tmpDir, patchFile string, opts MergeOptions) error {
168
+
func (g *GitRepo) applyPatch(patchData, patchFile string, opts MergeOptions) error {
166
169
var stderr bytes.Buffer
167
170
var cmd *exec.Cmd
168
171
169
172
// configure default git user before merge
170
-
exec.Command("git", "-C", tmpDir, "config", "user.name", opts.CommitterName).Run()
171
-
exec.Command("git", "-C", tmpDir, "config", "user.email", opts.CommitterEmail).Run()
172
-
exec.Command("git", "-C", tmpDir, "config", "advice.mergeConflict", "false").Run()
173
+
exec.Command("git", "-C", g.path, "config", "user.name", opts.CommitterName).Run()
174
+
exec.Command("git", "-C", g.path, "config", "user.email", opts.CommitterEmail).Run()
175
+
exec.Command("git", "-C", g.path, "config", "advice.mergeConflict", "false").Run()
173
176
174
177
// if patch is a format-patch, apply using 'git am'
175
178
if opts.FormatPatch {
176
-
cmd = exec.Command("git", "-C", tmpDir, "am", patchFile)
177
-
} else {
178
-
// else, apply using 'git apply' and commit it manually
179
-
applyCmd := exec.Command("git", "-C", tmpDir, "apply", patchFile)
180
-
applyCmd.Stderr = &stderr
181
-
if err := applyCmd.Run(); err != nil {
182
-
return fmt.Errorf("patch application failed: %s", stderr.String())
183
-
}
179
+
return g.applyMailbox(patchData)
180
+
}
184
181
185
-
stageCmd := exec.Command("git", "-C", tmpDir, "add", ".")
186
-
if err := stageCmd.Run(); err != nil {
187
-
return fmt.Errorf("failed to stage changes: %w", err)
188
-
}
182
+
// else, apply using 'git apply' and commit it manually
183
+
applyCmd := exec.Command("git", "-C", g.path, "apply", patchFile)
184
+
applyCmd.Stderr = &stderr
185
+
if err := applyCmd.Run(); err != nil {
186
+
return fmt.Errorf("patch application failed: %s", stderr.String())
187
+
}
189
188
190
-
commitArgs := []string{"-C", tmpDir, "commit"}
189
+
stageCmd := exec.Command("git", "-C", g.path, "add", ".")
190
+
if err := stageCmd.Run(); err != nil {
191
+
return fmt.Errorf("failed to stage changes: %w", err)
192
+
}
191
193
192
-
// Set author if provided
193
-
authorName := opts.AuthorName
194
-
authorEmail := opts.AuthorEmail
194
+
commitArgs := []string{"-C", g.path, "commit"}
195
195
196
-
if authorName != "" && authorEmail != "" {
197
-
commitArgs = append(commitArgs, "--author", fmt.Sprintf("%s <%s>", authorName, authorEmail))
198
-
}
199
-
// else, will default to knot's global user.name & user.email configured via `KNOT_GIT_USER_*` env variables
196
+
// Set author if provided
197
+
authorName := opts.AuthorName
198
+
authorEmail := opts.AuthorEmail
200
199
201
-
commitArgs = append(commitArgs, "-m", opts.CommitMessage)
200
+
if authorName != "" && authorEmail != "" {
201
+
commitArgs = append(commitArgs, "--author", fmt.Sprintf("%s <%s>", authorName, authorEmail))
202
+
}
203
+
// else, will default to knot's global user.name & user.email configured via `KNOT_GIT_USER_*` env variables
202
204
203
-
if opts.CommitBody != "" {
204
-
commitArgs = append(commitArgs, "-m", opts.CommitBody)
205
-
}
205
+
commitArgs = append(commitArgs, "-m", opts.CommitMessage)
206
206
207
-
cmd = exec.Command("git", commitArgs...)
207
+
if opts.CommitBody != "" {
208
+
commitArgs = append(commitArgs, "-m", opts.CommitBody)
208
209
}
210
+
211
+
cmd = exec.Command("git", commitArgs...)
209
212
210
213
cmd.Stderr = &stderr
211
214
···
216
219
return nil
217
220
}
218
221
219
-
func (g *GitRepo) MergeCheck(patchData []byte, targetBranch string) error {
222
+
func (g *GitRepo) applyMailbox(patchData string) error {
223
+
fps, err := patchutil.ExtractPatches(patchData)
224
+
if err != nil {
225
+
return fmt.Errorf("failed to extract patches: %w", err)
226
+
}
227
+
228
+
// apply each patch one by one
229
+
// update the newly created commit object to add the change-id header
230
+
total := len(fps)
231
+
for i, p := range fps {
232
+
newCommit, err := g.applySingleMailbox(p)
233
+
if err != nil {
234
+
return err
235
+
}
236
+
237
+
log.Printf("applying mailbox patch %d/%d: committed %s\n", i+1, total, newCommit.String())
238
+
}
239
+
240
+
return nil
241
+
}
242
+
243
+
func (g *GitRepo) applySingleMailbox(singlePatch types.FormatPatch) (plumbing.Hash, error) {
244
+
tmpPatch, err := g.createTempFileWithPatch(singlePatch.Raw)
245
+
if err != nil {
246
+
return plumbing.ZeroHash, fmt.Errorf("failed to create temporary patch file for singluar mailbox patch: %w", err)
247
+
}
248
+
249
+
var stderr bytes.Buffer
250
+
cmd := exec.Command("git", "-C", g.path, "am", tmpPatch)
251
+
cmd.Stderr = &stderr
252
+
253
+
head, err := g.r.Head()
254
+
if err != nil {
255
+
return plumbing.ZeroHash, err
256
+
}
257
+
log.Println("head before apply", head.Hash().String())
258
+
259
+
if err := cmd.Run(); err != nil {
260
+
return plumbing.ZeroHash, fmt.Errorf("patch application failed: %s", stderr.String())
261
+
}
262
+
263
+
if err := g.Refresh(); err != nil {
264
+
return plumbing.ZeroHash, fmt.Errorf("failed to refresh repository state: %w", err)
265
+
}
266
+
267
+
head, err = g.r.Head()
268
+
if err != nil {
269
+
return plumbing.ZeroHash, err
270
+
}
271
+
log.Println("head after apply", head.Hash().String())
272
+
273
+
newHash := head.Hash()
274
+
if changeId, err := singlePatch.ChangeId(); err != nil {
275
+
// no change ID
276
+
} else if updatedHash, err := g.setChangeId(head.Hash(), changeId); err != nil {
277
+
return plumbing.ZeroHash, err
278
+
} else {
279
+
newHash = updatedHash
280
+
}
281
+
282
+
return newHash, nil
283
+
}
284
+
285
+
func (g *GitRepo) setChangeId(hash plumbing.Hash, changeId string) (plumbing.Hash, error) {
286
+
log.Printf("updating change ID of %s to %s\n", hash.String(), changeId)
287
+
obj, err := g.r.CommitObject(hash)
288
+
if err != nil {
289
+
return plumbing.ZeroHash, fmt.Errorf("failed to get commit object for hash %s: %w", hash.String(), err)
290
+
}
291
+
292
+
// write the change-id header
293
+
obj.ExtraHeaders["change-id"] = []byte(changeId)
294
+
295
+
// create a new object
296
+
dest := g.r.Storer.NewEncodedObject()
297
+
if err := obj.Encode(dest); err != nil {
298
+
return plumbing.ZeroHash, fmt.Errorf("failed to create new object: %w", err)
299
+
}
300
+
301
+
// store the new object
302
+
newHash, err := g.r.Storer.SetEncodedObject(dest)
303
+
if err != nil {
304
+
return plumbing.ZeroHash, fmt.Errorf("failed to store new object: %w", err)
305
+
}
306
+
307
+
log.Printf("hash changed from %s to %s\n", obj.Hash.String(), newHash.String())
308
+
309
+
// find the branch that HEAD is pointing to
310
+
ref, err := g.r.Head()
311
+
if err != nil {
312
+
return plumbing.ZeroHash, fmt.Errorf("failed to fetch HEAD: %w", err)
313
+
}
314
+
315
+
// and update that branch to point to new commit
316
+
if ref.Name().IsBranch() {
317
+
err = g.r.Storer.SetReference(plumbing.NewHashReference(ref.Name(), newHash))
318
+
if err != nil {
319
+
return plumbing.ZeroHash, fmt.Errorf("failed to update HEAD: %w", err)
320
+
}
321
+
}
322
+
323
+
// new hash of commit
324
+
return newHash, nil
325
+
}
326
+
327
+
func (g *GitRepo) MergeCheck(patchData string, targetBranch string) error {
220
328
if val, ok := mergeCheckCache.Get(g, patchData, targetBranch); ok {
221
329
return val
222
330
}
···
263
371
}
264
372
defer os.RemoveAll(tmpDir)
265
373
266
-
if err := g.applyPatch(tmpDir, patchFile, opts); err != nil {
374
+
tmpRepo, err := PlainOpen(tmpDir)
375
+
if err != nil {
376
+
return err
377
+
}
378
+
379
+
if err := tmpRepo.applyPatch(patchData, patchFile, opts); err != nil {
267
380
return err
268
381
}
269
382
+1
-1
knotserver/xrpc/merge.go
+1
-1
knotserver/xrpc/merge.go
···
85
85
mo.CommitterEmail = x.Config.Git.UserEmail
86
86
mo.FormatPatch = patchutil.IsFormatPatch(data.Patch)
87
87
88
-
err = gr.MergeWithOptions([]byte(data.Patch), data.Branch, mo)
88
+
err = gr.MergeWithOptions(data.Patch, data.Branch, mo)
89
89
if err != nil {
90
90
var mergeErr *git.ErrMerge
91
91
if errors.As(err, &mergeErr) {