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
|
//go:build static && system_libgit2
package main
import (
"context"
"encoding/gob"
"errors"
"flag"
"fmt"
git "github.com/libgit2/git2go/v34"
"gitlab.com/gitlab-org/gitaly/v16/cmd/gitaly-git2go/git2goutil"
"gitlab.com/gitlab-org/gitaly/v16/internal/git2go"
"gitlab.com/gitlab-org/gitaly/v16/internal/structerr"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
type conflictsSubcommand struct{}
func (cmd *conflictsSubcommand) Flags() *flag.FlagSet {
return flag.NewFlagSet("conflicts", flag.ExitOnError)
}
func (cmd *conflictsSubcommand) Run(_ context.Context, decoder *gob.Decoder, encoder *gob.Encoder) error {
var request git2go.ConflictsCommand
if err := decoder.Decode(&request); err != nil {
return err
}
res := cmd.conflicts(request)
return encoder.Encode(res)
}
func (*conflictsSubcommand) conflicts(request git2go.ConflictsCommand) git2go.ConflictsResult {
repo, err := git2goutil.OpenRepository(request.Repository)
if err != nil {
return conflictError(codes.Internal, fmt.Errorf("could not open repository: %w", err).Error())
}
oursOid, err := git.NewOid(request.Ours)
if err != nil {
return conflictError(codes.InvalidArgument, err.Error())
}
ours, err := repo.LookupCommit(oursOid)
if err != nil {
return convertError(err, git.ErrorCodeNotFound, codes.InvalidArgument)
}
theirsOid, err := git.NewOid(request.Theirs)
if err != nil {
return conflictError(codes.InvalidArgument, err.Error())
}
theirs, err := repo.LookupCommit(theirsOid)
if err != nil {
return convertError(err, git.ErrorCodeNotFound, codes.InvalidArgument)
}
index, err := repo.MergeCommits(ours, theirs, nil)
if err != nil {
return conflictError(codes.FailedPrecondition, fmt.Sprintf("could not merge commits: %v", err))
}
iterator, err := index.ConflictIterator()
if err != nil {
return conflictError(codes.Internal, fmt.Errorf("could not get conflicts: %w", err).Error())
}
var result git2go.ConflictsResult
for {
conflict, err := iterator.Next()
if err != nil {
var gitError *git.GitError
if errors.As(err, &gitError) && gitError.Code == git.ErrorCodeIterOver {
break
}
return conflictError(codes.Internal, err.Error())
}
merge, err := Merge(repo, conflict)
if err != nil {
if s, ok := status.FromError(err); ok {
return conflictError(s.Code(), s.Message())
}
return conflictError(codes.Internal, err.Error())
}
result.Conflicts = append(result.Conflicts, git2go.Conflict{
Ancestor: conflictEntryFromIndex(conflict.Ancestor),
Our: conflictEntryFromIndex(conflict.Our),
Their: conflictEntryFromIndex(conflict.Their),
Content: merge.Contents,
})
}
return result
}
// Merge will merge the given index conflict and produce a file with conflict
// markers.
func Merge(repo *git.Repository, conflict git.IndexConflict) (*git.MergeFileResult, error) {
var ancestor, our, their git.MergeFileInput
for _, val := range []struct {
entry *git.IndexEntry
input *git.MergeFileInput
}{
{conflict.Ancestor, &ancestor},
{conflict.Our, &our},
{conflict.Their, &their},
} {
if val.entry == nil {
continue
}
blob, err := repo.LookupBlob(val.entry.Id)
if err != nil {
return nil, structerr.NewFailedPrecondition("could not get conflicting blob: %w", err)
}
val.input.Path = val.entry.Path
val.input.Mode = uint(val.entry.Mode)
val.input.Contents = blob.Contents()
}
merge, err := git.MergeFile(ancestor, our, their, nil)
if err != nil {
return nil, fmt.Errorf("could not compute conflicts: %w", err)
}
// In a case of tree-based conflicts (e.g. no ancestor), fallback to `Path`
// of `their` side. If that's also blank, fallback to `Path` of `our` side.
// This is to ensure that there's always a `Path` when we try to merge
// conflicts.
if merge.Path == "" {
if their.Path != "" {
merge.Path = their.Path
} else {
merge.Path = our.Path
}
}
return merge, nil
}
func conflictEntryFromIndex(entry *git.IndexEntry) git2go.ConflictEntry {
if entry == nil {
return git2go.ConflictEntry{}
}
return git2go.ConflictEntry{
Path: entry.Path,
Mode: int32(entry.Mode),
}
}
func conflictError(code codes.Code, message string) git2go.ConflictsResult {
err := git2go.ConflictError{
Code: code,
Message: message,
}
return git2go.ConflictsResult{
Err: err,
}
}
func convertError(err error, errorCode git.ErrorCode, returnCode codes.Code) git2go.ConflictsResult {
var gitError *git.GitError
if errors.As(err, &gitError) && gitError.Code == errorCode {
return conflictError(returnCode, err.Error())
}
return conflictError(codes.Internal, err.Error())
}
|