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
|
package conflicts
import (
"bytes"
"errors"
"fmt"
"io"
"unicode/utf8"
"gitlab.com/gitlab-org/gitaly/internal/git"
"gitlab.com/gitlab-org/gitaly/internal/git2go"
"gitlab.com/gitlab-org/gitaly/internal/helper"
"gitlab.com/gitlab-org/gitaly/proto/go/gitalypb"
"gitlab.com/gitlab-org/gitaly/streamio"
)
func (s *server) ListConflictFiles(request *gitalypb.ListConflictFilesRequest, stream gitalypb.ConflictsService_ListConflictFilesServer) error {
ctx := stream.Context()
if err := validateListConflictFilesRequest(request); err != nil {
return helper.ErrInvalidArgument(err)
}
repo := s.localrepo(request.GetRepository())
ours, err := repo.ResolveRevision(ctx, git.Revision(request.OurCommitOid+"^{commit}"))
if err != nil {
return helper.ErrPreconditionFailedf("could not lookup 'our' OID: %s", err)
}
theirs, err := repo.ResolveRevision(ctx, git.Revision(request.TheirCommitOid+"^{commit}"))
if err != nil {
return helper.ErrPreconditionFailedf("could not lookup 'their' OID: %s", err)
}
repoPath, err := s.locator.GetPath(request.Repository)
if err != nil {
return err
}
conflicts, err := git2go.ConflictsCommand{
Repository: repoPath,
Ours: ours.String(),
Theirs: theirs.String(),
}.Run(ctx, s.cfg)
if err != nil {
if errors.Is(err, git2go.ErrInvalidArgument) {
return helper.ErrInvalidArgument(err)
}
return helper.ErrInternal(err)
}
var conflictFiles []*gitalypb.ConflictFile
msgSize := 0
for _, conflict := range conflicts.Conflicts {
if conflict.Their.Path == "" || conflict.Our.Path == "" {
return helper.ErrPreconditionFailedf("conflict side missing")
}
if !utf8.Valid(conflict.Content) {
return helper.ErrPreconditionFailed(errors.New("unsupported encoding"))
}
conflictFiles = append(conflictFiles, &gitalypb.ConflictFile{
ConflictFilePayload: &gitalypb.ConflictFile_Header{
Header: &gitalypb.ConflictFileHeader{
CommitOid: request.OurCommitOid,
TheirPath: []byte(conflict.Their.Path),
OurPath: []byte(conflict.Our.Path),
AncestorPath: []byte(conflict.Ancestor.Path),
OurMode: conflict.Our.Mode,
},
},
})
contentReader := bytes.NewReader(conflict.Content)
for {
chunk := make([]byte, streamio.WriteBufferSize-msgSize)
bytesRead, err := contentReader.Read(chunk)
if err != nil && err != io.EOF {
return helper.ErrInternal(err)
}
if bytesRead > 0 {
conflictFiles = append(conflictFiles, &gitalypb.ConflictFile{
ConflictFilePayload: &gitalypb.ConflictFile_Content{
Content: chunk[:bytesRead],
},
})
}
if err == io.EOF {
break
}
// We don't send a message for each chunk because the content of
// a file may be smaller than the size limit, which means we can
// keep adding data to the message
msgSize += bytesRead
if msgSize < streamio.WriteBufferSize {
continue
}
if err := stream.Send(&gitalypb.ListConflictFilesResponse{
Files: conflictFiles,
}); err != nil {
return helper.ErrInternal(err)
}
conflictFiles = conflictFiles[:0]
msgSize = 0
}
}
// Send leftover data, if any
if len(conflictFiles) > 0 {
if err := stream.Send(&gitalypb.ListConflictFilesResponse{
Files: conflictFiles,
}); err != nil {
return helper.ErrInternal(err)
}
}
return nil
}
func validateListConflictFilesRequest(in *gitalypb.ListConflictFilesRequest) error {
if in.GetRepository() == nil {
return fmt.Errorf("empty Repository")
}
if in.GetOurCommitOid() == "" {
return fmt.Errorf("empty OurCommitOid")
}
if in.GetTheirCommitOid() == "" {
return fmt.Errorf("empty TheirCommitOid")
}
return nil
}
|