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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
|
//go:build static && system_libgit2
package main
import (
"bytes"
"context"
"encoding/gob"
"errors"
"flag"
"fmt"
"strings"
"time"
git "github.com/libgit2/git2go/v33"
"gitlab.com/gitlab-org/gitaly/v15/cmd/gitaly-git2go/git2goutil"
"gitlab.com/gitlab-org/gitaly/v15/internal/git/conflict"
"gitlab.com/gitlab-org/gitaly/v15/internal/git2go"
)
type resolveSubcommand struct{}
func (cmd *resolveSubcommand) Flags() *flag.FlagSet {
return flag.NewFlagSet("resolve", flag.ExitOnError)
}
func (cmd resolveSubcommand) Run(_ context.Context, decoder *gob.Decoder, encoder *gob.Encoder) error {
var request git2go.ResolveCommand
if err := decoder.Decode(&request); err != nil {
return err
}
if request.AuthorDate.IsZero() {
request.AuthorDate = time.Now()
}
repo, err := git2goutil.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("ours commit lookup: %w", err)
}
theirs, err := lookupCommit(repo, request.Theirs)
if err != nil {
return fmt.Errorf("theirs commit lookup: %w", err)
}
index, err := repo.MergeCommits(ours, theirs, nil)
if err != nil {
return fmt.Errorf("could not merge commits: %w", err)
}
ci, err := index.ConflictIterator()
if err != nil {
return err
}
type paths struct {
theirs, ours string
}
conflicts := map[paths]git.IndexConflict{}
for {
c, err := ci.Next()
if git.IsErrorCode(err, git.ErrorCodeIterOver) {
break
}
if err != nil {
return err
}
if c.Our.Path == "" || c.Their.Path == "" {
return errors.New("conflict side missing")
}
k := paths{
theirs: c.Their.Path,
ours: c.Our.Path,
}
conflicts[k] = c
}
odb, err := repo.Odb()
if err != nil {
return err
}
for _, r := range request.Resolutions {
c, ok := conflicts[paths{
theirs: r.OldPath,
ours: r.NewPath,
}]
if !ok {
// Note: this emulates the Ruby error that occurs when
// there are no conflicts for a resolution
return errors.New("NoMethodError: undefined method `resolve_lines' for nil:NilClass")
}
switch {
case c.Our == nil:
return fmt.Errorf("missing our-part of merge file input for new path %q", r.NewPath)
case c.Their == nil:
return fmt.Errorf("missing their-part of merge file input for new path %q", r.NewPath)
}
ancestor, our, their, err := readConflictEntries(odb, c)
if err != nil {
return fmt.Errorf("read conflict entries: %w", err)
}
mfr, err := mergeFileResult(ancestor, our, their)
if err != nil {
return fmt.Errorf("merge file result for %q: %w", r.NewPath, err)
}
if r.Content != "" && bytes.Equal([]byte(r.Content), mfr.Contents) {
return fmt.Errorf("Resolved content has no changes for file %s", r.NewPath) //nolint
}
conflictFile, err := conflict.Parse(
bytes.NewReader(mfr.Contents),
ancestor,
our,
their,
)
if err != nil {
return fmt.Errorf("parse conflict for %q: %w", c.Ancestor.Path, err)
}
resolvedBlob, err := conflictFile.Resolve(r)
if err != nil {
return err // do not decorate this error to satisfy old test
}
resolvedBlobOID, err := odb.Write(resolvedBlob, git.ObjectBlob)
if err != nil {
return fmt.Errorf("write object for %q: %w", c.Ancestor.Path, err)
}
ourResolvedEntry := *c.Our // copy by value
ourResolvedEntry.Id = resolvedBlobOID
if err := index.Add(&ourResolvedEntry); err != nil {
return fmt.Errorf("add index for %q: %w", c.Ancestor.Path, err)
}
if err := index.RemoveConflict(ourResolvedEntry.Path); err != nil {
return fmt.Errorf("remove conflict from index for %q: %w", c.Ancestor.Path, err)
}
}
if index.HasConflicts() {
ci, err := index.ConflictIterator()
if err != nil {
return fmt.Errorf("iterating unresolved conflicts: %w", err)
}
var conflictPaths []string
for {
c, err := ci.Next()
if git.IsErrorCode(err, git.ErrorCodeIterOver) {
break
}
if err != nil {
return fmt.Errorf("next unresolved conflict: %w", err)
}
var conflictingPath string
if c.Ancestor != nil {
conflictingPath = c.Ancestor.Path
} else {
conflictingPath = c.Our.Path
}
conflictPaths = append(conflictPaths, conflictingPath)
}
return fmt.Errorf("Missing resolutions for the following files: %s", strings.Join(conflictPaths, ", ")) //nolint
}
treeOID, err := index.WriteTreeTo(repo)
if err != nil {
return fmt.Errorf("write tree to repo: %w", err)
}
tree, err := repo.LookupTree(treeOID)
if err != nil {
return fmt.Errorf("lookup tree: %w", err)
}
sign := git2go.NewSignature(request.AuthorName, request.AuthorMail, request.AuthorDate)
committer := &git.Signature{
Name: sign.Name,
Email: sign.Email,
When: request.AuthorDate,
}
commitID, err := git2goutil.NewCommitSubmitter(repo, request.SigningKey).
Commit(committer, committer, git.MessageEncodingUTF8, request.Message, tree, ours, theirs)
if err != nil {
return fmt.Errorf("create commit: %w", err)
}
response := git2go.ResolveResult{
MergeResult: git2go.MergeResult{
CommitID: commitID.String(),
},
}
return encoder.Encode(response)
}
func readConflictEntries(odb *git.Odb, c git.IndexConflict) (*conflict.Entry, *conflict.Entry, *conflict.Entry, error) {
var ancestor, our, their *conflict.Entry
for _, part := range []struct {
entry *git.IndexEntry
result **conflict.Entry
}{
{entry: c.Ancestor, result: &ancestor},
{entry: c.Our, result: &our},
{entry: c.Their, result: &their},
} {
if part.entry == nil {
continue
}
blob, err := odb.Read(part.entry.Id)
if err != nil {
return nil, nil, nil, err
}
*part.result = &conflict.Entry{
Path: part.entry.Path,
Mode: uint(part.entry.Mode),
Contents: blob.Data(),
}
}
return ancestor, our, their, nil
}
func mergeFileResult(ancestor, our, their *conflict.Entry) (*git.MergeFileResult, error) {
mfr, err := git.MergeFile(
conflictEntryToMergeFileInput(ancestor),
conflictEntryToMergeFileInput(our),
conflictEntryToMergeFileInput(their),
nil,
)
if err != nil {
return nil, err
}
return mfr, nil
}
func conflictEntryToMergeFileInput(e *conflict.Entry) git.MergeFileInput {
if e == nil {
return git.MergeFileInput{}
}
return git.MergeFileInput{
Path: e.Path,
Mode: e.Mode,
Contents: e.Contents,
}
}
|