1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
|
// +build static,system_libgit2
package main
import (
"errors"
"flag"
"fmt"
"strings"
"time"
git "github.com/libgit2/git2go/v30"
"gitlab.com/gitlab-org/gitaly/internal/git2go"
)
type mergeSubcommand struct {
request string
}
func (cmd *mergeSubcommand) Flags() *flag.FlagSet {
flags := flag.NewFlagSet("merge", flag.ExitOnError)
flags.StringVar(&cmd.request, "request", "", "git2go.MergeCommand")
return flags
}
func lookupCommit(repo *git.Repository, ref string) (*git.Commit, error) {
object, err := repo.RevparseSingle(ref)
if err != nil {
return nil, fmt.Errorf("could not lookup reference: %w", err)
}
peeled, err := object.Peel(git.ObjectCommit)
if err != nil {
return nil, fmt.Errorf("could not peel reference: %w", err)
}
commit, err := peeled.AsCommit()
if err != nil {
return nil, fmt.Errorf("could not cast to commit: %w", err)
}
return commit, nil
}
func sanitizeSignatureInfo(info string) string {
return strings.Map(func(r rune) rune {
switch r {
case '<', '>', '\n':
return -1
default:
return r
}
}, info)
}
func (cmd *mergeSubcommand) Run() error {
request, err := git2go.MergeCommandFromSerialized(cmd.request)
if err != nil {
return err
}
if request.AuthorDate.IsZero() {
request.AuthorDate = time.Now()
}
repo, err := git.OpenRepository(request.Repository)
if err != nil {
return fmt.Errorf("could not open repository: %w", err)
}
ours, err := lookupCommit(repo, request.Ours)
if err != nil {
return fmt.Errorf("could not lookup commit %q: %w", request.Ours, err)
}
theirs, err := lookupCommit(repo, request.Theirs)
if err != nil {
return fmt.Errorf("could not lookup commit %q: %w", request.Theirs, err)
}
index, err := repo.MergeCommits(ours, theirs, nil)
if err != nil {
return fmt.Errorf("could not merge commits: %w", err)
}
if index.HasConflicts() {
return errors.New("could not auto-merge due to conflicts")
}
tree, err := index.WriteTreeTo(repo)
if err != nil {
return fmt.Errorf("could not write tree: %w", err)
}
committer := git.Signature{
Name: sanitizeSignatureInfo(request.AuthorName),
Email: sanitizeSignatureInfo(request.AuthorMail),
When: request.AuthorDate,
}
commit, err := repo.CreateCommitFromIds("", &committer, &committer, request.Message, tree, ours.Id(), theirs.Id())
if err != nil {
return fmt.Errorf("could not create merge commit: %w", err)
}
response := git2go.MergeResult{
CommitID: commit.String(),
}
serialized, err := response.Serialize()
if err != nil {
return err
}
fmt.Println(serialized)
return nil
}
|