diff options
author | Patrick Steinhardt <psteinhardt@gitlab.com> | 2021-08-26 13:21:13 +0300 |
---|---|---|
committer | Patrick Steinhardt <psteinhardt@gitlab.com> | 2021-08-30 12:10:58 +0300 |
commit | 6c68e8cf4ac518847183b6e44419d5bc8647951e (patch) | |
tree | 063441b89245cc3fd3e1d1ae9aa727d29b0bd949 /cmd | |
parent | 40fae4205d3ad62ca9341620146486bee8d31b28 (diff) |
gitaly-git2go: Split merge code into runner and actual logic
The gitaly-git2go merge code handles deserialization of params,
deserialization of results and the actual merge logic in a single
function. This makes it hard to reason about inputs and outputs of this
function and thus prohibits an easy refactor of them.
Refactor the code and pull out the merge logic into its own function,
where all parameters are obtained as input and output is handled by the
calling function.
No change in behaviour is expected from this refactoring.
Diffstat (limited to 'cmd')
-rw-r--r-- | cmd/gitaly-git2go/merge.go | 45 |
1 files changed, 27 insertions, 18 deletions
diff --git a/cmd/gitaly-git2go/merge.go b/cmd/gitaly-git2go/merge.go index 3e264d8ad..43922272f 100644 --- a/cmd/gitaly-git2go/merge.go +++ b/cmd/gitaly-git2go/merge.go @@ -37,64 +37,73 @@ func (cmd *mergeSubcommand) Run(context.Context, io.Reader, io.Writer) error { request.AuthorDate = time.Now() } + commitID, err := merge(request) + if err != nil { + return err + } + + response := git2go.MergeResult{ + CommitID: commitID, + } + + if err := response.SerializeTo(os.Stdout); err != nil { + return err + } + + return nil +} + +func merge(request git2go.MergeCommand) (string, error) { repo, err := git2goutil.OpenRepository(request.Repository) if err != nil { - return fmt.Errorf("could not open repository: %w", err) + return "", fmt.Errorf("could not open repository: %w", err) } defer repo.Free() ours, err := lookupCommit(repo, request.Ours) if err != nil { - return fmt.Errorf("ours commit lookup: %w", err) + 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) + return "", fmt.Errorf("theirs commit lookup: %w", err) } mergeOpts, err := git.DefaultMergeOptions() if err != nil { - return fmt.Errorf("could not create merge options: %w", err) + return "", fmt.Errorf("could not create merge options: %w", err) } mergeOpts.RecursionLimit = git2go.MergeRecursionLimit index, err := repo.MergeCommits(ours, theirs, &mergeOpts) if err != nil { - return fmt.Errorf("could not merge commits: %w", err) + return "", fmt.Errorf("could not merge commits: %w", err) } defer index.Free() if index.HasConflicts() { if !request.AllowConflicts { - return errors.New("could not auto-merge due to conflicts") + return "", errors.New("could not auto-merge due to conflicts") } if err := resolveConflicts(repo, index); err != nil { - return fmt.Errorf("could not resolve conflicts: %w", err) + return "", fmt.Errorf("could not resolve conflicts: %w", err) } } tree, err := index.WriteTreeTo(repo) if err != nil { - return fmt.Errorf("could not write tree: %w", err) + return "", fmt.Errorf("could not write tree: %w", err) } committer := git.Signature(git2go.NewSignature(request.AuthorName, request.AuthorMail, 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(), + return "", fmt.Errorf("could not create merge commit: %w", err) } - if err := response.SerializeTo(os.Stdout); err != nil { - return err - } - - return nil + return commit.String(), nil } func resolveConflicts(repo *git.Repository, index *git.Index) error { |