mirror of
https://github.com/harness/drone.git
synced 2025-05-31 11:43:15 +00:00
merge operation refactored (#172)
This commit is contained in:
parent
63de576d08
commit
2f5339ece5
@ -166,6 +166,10 @@ func (g Adapter) GetCommit(ctx context.Context, repoPath string, ref string) (*t
|
|||||||
return mapGiteaCommit(commit)
|
return mapGiteaCommit(commit)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (g Adapter) GetFullCommitID(ctx context.Context, repoPath, shortID string) (string, error) {
|
||||||
|
return gitea.GetFullCommitID(ctx, repoPath, shortID)
|
||||||
|
}
|
||||||
|
|
||||||
// GetCommits returns the (latest) commits for a specific list of refs.
|
// GetCommits returns the (latest) commits for a specific list of refs.
|
||||||
// Note: ref can be Branch / Tag / CommitSHA.
|
// Note: ref can be Branch / Tag / CommitSHA.
|
||||||
func (g Adapter) GetCommits(ctx context.Context, repoPath string, refs []string) ([]types.Commit, error) {
|
func (g Adapter) GetCommits(ctx context.Context, repoPath string, refs []string) ([]types.Commit, error) {
|
||||||
|
28
gitrpc/internal/gitea/config.go
Normal file
28
gitrpc/internal/gitea/config.go
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
// Copyright 2022 Harness Inc. All rights reserved.
|
||||||
|
// Use of this source code is governed by the Polyform Free Trial License
|
||||||
|
// that can be found in the LICENSE.md file for this repository.
|
||||||
|
|
||||||
|
package gitea
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"code.gitea.io/gitea/modules/git"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Config set local git key and value configuration.
|
||||||
|
func (g Adapter) Config(ctx context.Context, repoPath, key, value string) error {
|
||||||
|
var outbuf, errbuf strings.Builder
|
||||||
|
if err := git.NewCommand(ctx, "config", "--local").AddArguments(key, value).
|
||||||
|
Run(&git.RunOpts{
|
||||||
|
Dir: repoPath,
|
||||||
|
Stdout: &outbuf,
|
||||||
|
Stderr: &errbuf,
|
||||||
|
}); err != nil {
|
||||||
|
return fmt.Errorf("git config [%s -> <%s> ]: %w\n%s\n%s",
|
||||||
|
key, value, err, outbuf.String(), errbuf.String())
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
@ -13,6 +13,7 @@ import (
|
|||||||
// GitAdapter for accessing git commands from gitea.
|
// GitAdapter for accessing git commands from gitea.
|
||||||
type GitAdapter interface {
|
type GitAdapter interface {
|
||||||
InitRepository(ctx context.Context, path string, bare bool) error
|
InitRepository(ctx context.Context, path string, bare bool) error
|
||||||
|
Config(ctx context.Context, repoPath, key, value string) error
|
||||||
SetDefaultBranch(ctx context.Context, repoPath string, defaultBranch string, allowEmpty bool) error
|
SetDefaultBranch(ctx context.Context, repoPath string, defaultBranch string, allowEmpty bool) error
|
||||||
Clone(ctx context.Context, from, to string, opts types.CloneRepoOptions) error
|
Clone(ctx context.Context, from, to string, opts types.CloneRepoOptions) error
|
||||||
AddFiles(repoPath string, all bool, files ...string) error
|
AddFiles(repoPath string, all bool, files ...string) error
|
||||||
@ -30,6 +31,7 @@ type GitAdapter interface {
|
|||||||
ListCommits(ctx context.Context, repoPath string,
|
ListCommits(ctx context.Context, repoPath string,
|
||||||
ref string, afterRef string, page int, limit int) ([]types.Commit, error)
|
ref string, afterRef string, page int, limit int) ([]types.Commit, error)
|
||||||
GetLatestCommit(ctx context.Context, repoPath string, ref string, treePath string) (*types.Commit, error)
|
GetLatestCommit(ctx context.Context, repoPath string, ref string, treePath string) (*types.Commit, error)
|
||||||
|
GetFullCommitID(ctx context.Context, repoPath, shortID string) (string, error)
|
||||||
GetAnnotatedTag(ctx context.Context, repoPath string, sha string) (*types.Tag, error)
|
GetAnnotatedTag(ctx context.Context, repoPath string, sha string) (*types.Tag, error)
|
||||||
GetAnnotatedTags(ctx context.Context, repoPath string, shas []string) ([]types.Tag, error)
|
GetAnnotatedTags(ctx context.Context, repoPath string, shas []string) ([]types.Tag, error)
|
||||||
CreateBranch(ctx context.Context, repoPath string, branchName string, target string) (*types.Branch, error)
|
CreateBranch(ctx context.Context, repoPath string, branchName string, target string) (*types.Branch, error)
|
||||||
|
@ -81,68 +81,26 @@ func (s MergeService) MergeBranch(
|
|||||||
return nil, fmt.Errorf("unable to write .git/info/sparse-checkout file in tmpBasePath: %w", err)
|
return nil, fmt.Errorf("unable to write .git/info/sparse-checkout file in tmpBasePath: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
gitConfigCommand := func() *git.Command {
|
|
||||||
return git.NewCommand(ctx, "config", "--local")
|
|
||||||
}
|
|
||||||
|
|
||||||
// Switch off LFS process (set required, clean and smudge here also)
|
// Switch off LFS process (set required, clean and smudge here also)
|
||||||
if err = gitConfigCommand().AddArguments("filter.lfs.process", "").
|
if err = s.adapter.Config(ctx, tmpBasePath, "filter.lfs.process", ""); err != nil {
|
||||||
Run(&git.RunOpts{
|
return nil, err
|
||||||
Dir: tmpBasePath,
|
|
||||||
Stdout: &outbuf,
|
|
||||||
Stderr: &errbuf,
|
|
||||||
}); err != nil {
|
|
||||||
return nil, fmt.Errorf("git config [filter.lfs.process -> <> ]: %w\n%s\n%s",
|
|
||||||
err, outbuf.String(), errbuf.String())
|
|
||||||
}
|
}
|
||||||
outbuf.Reset()
|
|
||||||
errbuf.Reset()
|
|
||||||
|
|
||||||
if err = gitConfigCommand().AddArguments("filter.lfs.required", "false").
|
if err = s.adapter.Config(ctx, tmpBasePath, "filter.lfs.required", "false"); err != nil {
|
||||||
Run(&git.RunOpts{
|
return nil, err
|
||||||
Dir: tmpBasePath,
|
|
||||||
Stdout: &outbuf,
|
|
||||||
Stderr: &errbuf,
|
|
||||||
}); err != nil {
|
|
||||||
return nil, fmt.Errorf("git config [filter.lfs.required -> <false> ]: %w\n%s\n%s",
|
|
||||||
err, outbuf.String(), errbuf.String())
|
|
||||||
}
|
}
|
||||||
outbuf.Reset()
|
|
||||||
errbuf.Reset()
|
|
||||||
|
|
||||||
if err = gitConfigCommand().AddArguments("filter.lfs.clean", "").
|
if err = s.adapter.Config(ctx, tmpBasePath, "filter.lfs.clean", ""); err != nil {
|
||||||
Run(&git.RunOpts{
|
return nil, err
|
||||||
Dir: tmpBasePath,
|
|
||||||
Stdout: &outbuf,
|
|
||||||
Stderr: &errbuf,
|
|
||||||
}); err != nil {
|
|
||||||
return nil, fmt.Errorf("git config [filter.lfs.clean -> <> ]: %w\n%s\n%s",
|
|
||||||
err, outbuf.String(), errbuf.String())
|
|
||||||
}
|
}
|
||||||
outbuf.Reset()
|
|
||||||
errbuf.Reset()
|
|
||||||
|
|
||||||
if err = gitConfigCommand().AddArguments("filter.lfs.smudge", "").
|
if err = s.adapter.Config(ctx, tmpBasePath, "filter.lfs.smudge", ""); err != nil {
|
||||||
Run(&git.RunOpts{
|
return nil, err
|
||||||
Dir: tmpBasePath,
|
|
||||||
Stdout: &outbuf,
|
|
||||||
Stderr: &errbuf,
|
|
||||||
}); err != nil {
|
|
||||||
return nil, fmt.Errorf("git config [filter.lfs.smudge -> <> ]: %w\n%s\n%s", err, outbuf.String(), errbuf.String())
|
|
||||||
}
|
}
|
||||||
outbuf.Reset()
|
|
||||||
errbuf.Reset()
|
|
||||||
|
|
||||||
if err = gitConfigCommand().AddArguments("core.sparseCheckout", "true").
|
if err = s.adapter.Config(ctx, tmpBasePath, "core.sparseCheckout", "true"); err != nil {
|
||||||
Run(&git.RunOpts{
|
return nil, err
|
||||||
Dir: tmpBasePath,
|
|
||||||
Stdout: &outbuf,
|
|
||||||
Stderr: &errbuf,
|
|
||||||
}); err != nil {
|
|
||||||
return nil, fmt.Errorf("git config [core.sparsecheckout -> true]: %w\n%s\n%s", err, outbuf.String(), errbuf.String())
|
|
||||||
}
|
}
|
||||||
outbuf.Reset()
|
|
||||||
errbuf.Reset()
|
|
||||||
|
|
||||||
// Read base branch index
|
// Read base branch index
|
||||||
if err = git.NewCommand(ctx, "read-tree", "HEAD").
|
if err = git.NewCommand(ctx, "read-tree", "HEAD").
|
||||||
@ -178,38 +136,19 @@ func (s MergeService) MergeBranch(
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
mergeCommitID, err := git.GetFullCommitID(ctx, tmpBasePath, baseBranch)
|
mergeCommitID, err := s.adapter.GetFullCommitID(ctx, tmpBasePath, baseBranch)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to get full commit id for the new merge: %w", err)
|
return nil, fmt.Errorf("failed to get full commit id for the new merge: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
pushCmd := git.NewCommand(ctx, "push", "origin", baseBranch+":"+git.BranchPrefix+pr.BaseBranch)
|
if err = s.adapter.Push(ctx, tmpBasePath, types.PushOptions{
|
||||||
|
Remote: "origin",
|
||||||
if err = pushCmd.Run(&git.RunOpts{
|
Branch: baseBranch + ":" + git.BranchPrefix + pr.BaseBranch,
|
||||||
|
Force: request.Force,
|
||||||
Env: env,
|
Env: env,
|
||||||
Dir: tmpBasePath,
|
|
||||||
Stdout: &outbuf,
|
|
||||||
Stderr: &errbuf,
|
|
||||||
}); err != nil {
|
}); err != nil {
|
||||||
if strings.Contains(errbuf.String(), "non-fast-forward") {
|
return nil, err
|
||||||
return nil, &git.ErrPushOutOfDate{
|
|
||||||
StdOut: outbuf.String(),
|
|
||||||
StdErr: errbuf.String(),
|
|
||||||
Err: err,
|
|
||||||
}
|
|
||||||
} else if strings.Contains(errbuf.String(), "! [remote rejected]") {
|
|
||||||
err := &git.ErrPushRejected{
|
|
||||||
StdOut: outbuf.String(),
|
|
||||||
StdErr: errbuf.String(),
|
|
||||||
Err: err,
|
|
||||||
}
|
|
||||||
err.GenerateMessage()
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return nil, fmt.Errorf("git push: %s", errbuf.String())
|
|
||||||
}
|
}
|
||||||
outbuf.Reset()
|
|
||||||
errbuf.Reset()
|
|
||||||
|
|
||||||
return &rpc.MergeBranchResponse{
|
return &rpc.MergeBranchResponse{
|
||||||
CommitId: mergeCommitID,
|
CommitId: mergeCommitID,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user