mirror of
https://github.com/go-gitea/gitea.git
synced 2026-01-21 00:25:04 +01:00
Use new MergeBase method
This commit is contained in:
parent
47f6f1dca9
commit
e5fe09301a
@ -18,32 +18,6 @@ import (
|
||||
"code.gitea.io/gitea/modules/git/gitcmd"
|
||||
)
|
||||
|
||||
// GetMergeBase checks and returns merge base of two branches and the reference used as base.
|
||||
func (repo *Repository) GetMergeBase(tmpRemote, base, head string) (string, string, error) {
|
||||
if tmpRemote == "" {
|
||||
tmpRemote = "origin"
|
||||
}
|
||||
|
||||
if tmpRemote != "origin" {
|
||||
tmpBaseName := RemotePrefix + tmpRemote + "/tmp_" + base
|
||||
// Fetch commit into a temporary branch in order to be able to handle commits and tags
|
||||
_, _, err := gitcmd.NewCommand("fetch", "--no-tags").
|
||||
AddDynamicArguments(tmpRemote).
|
||||
AddDashesAndList(base + ":" + tmpBaseName).
|
||||
WithDir(repo.Path).
|
||||
RunStdString(repo.Ctx)
|
||||
if err == nil {
|
||||
base = tmpBaseName
|
||||
}
|
||||
}
|
||||
|
||||
stdout, _, err := gitcmd.NewCommand("merge-base").
|
||||
AddDashesAndList(base, head).
|
||||
WithDir(repo.Path).
|
||||
RunStdString(repo.Ctx)
|
||||
return strings.TrimSpace(stdout), base, err
|
||||
}
|
||||
|
||||
type lineCountWriter struct {
|
||||
numLines int
|
||||
}
|
||||
|
||||
@ -12,23 +12,23 @@ import (
|
||||
)
|
||||
|
||||
// MergeBase checks and returns merge base of two commits.
|
||||
func MergeBase(ctx context.Context, repo Repository, commit1, commit2 string) (string, error) {
|
||||
func MergeBase(ctx context.Context, repo Repository, baseCommitID, headCommitID string) (string, error) {
|
||||
mergeBase, err := RunCmdString(ctx, repo, gitcmd.NewCommand("merge-base").
|
||||
AddDashesAndList(commit1, commit2))
|
||||
AddDashesAndList(baseCommitID, headCommitID))
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("get merge-base of %s and %s failed: %w", commit1, commit2, err)
|
||||
return "", fmt.Errorf("get merge-base of %s and %s failed: %w", baseCommitID, headCommitID, err)
|
||||
}
|
||||
return strings.TrimSpace(mergeBase), nil
|
||||
}
|
||||
|
||||
// MergeBaseFromRemote checks and returns merge base of two commits from different repositories.
|
||||
func MergeBaseFromRemote(ctx context.Context, repo, remoteRepo Repository, commit1, commit2 string) (string, error) {
|
||||
func MergeBaseFromRemote(ctx context.Context, baseRepo, headRepo Repository, baseCommitID, headCommitID string) (string, error) {
|
||||
// fetch head commit id into the current repository if the repositories are different
|
||||
if repo.RelativePath() != remoteRepo.RelativePath() {
|
||||
if err := FetchRemoteCommit(ctx, repo, remoteRepo, commit2); err != nil {
|
||||
if baseRepo.RelativePath() != headRepo.RelativePath() {
|
||||
if err := FetchRemoteCommit(ctx, baseRepo, headRepo, headCommitID); err != nil {
|
||||
return "", fmt.Errorf("FetchRemoteCommit: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
return MergeBase(ctx, repo, commit1, commit2)
|
||||
return MergeBase(ctx, baseRepo, baseCommitID, headCommitID)
|
||||
}
|
||||
|
||||
@ -736,7 +736,7 @@ func (g *GiteaLocalUploader) newPullRequest(ctx context.Context, pr *base.PullRe
|
||||
if pr.Base.Ref != "" && pr.Head.SHA != "" {
|
||||
// A PR against a tag base does not make sense - therefore pr.Base.Ref must be a branch
|
||||
// TODO: should we be checking for the refs/heads/ prefix on the pr.Base.Ref? (i.e. are these actually branches or refs)
|
||||
pr.Base.SHA, _, err = g.gitRepo.GetMergeBase("", git.BranchPrefix+pr.Base.Ref, pr.Head.SHA)
|
||||
pr.Base.SHA, err = gitrepo.MergeBase(ctx, g.repo, git.BranchPrefix+pr.Base.Ref, pr.Head.SHA)
|
||||
if err != nil {
|
||||
log.Error("Cannot determine the merge base for PR #%d in %s/%s. Error: %v", pr.Number, g.repoOwner, g.repoName, err)
|
||||
}
|
||||
|
||||
@ -510,14 +510,7 @@ func checkIfPRContentChanged(ctx context.Context, pr *issues_model.PullRequest,
|
||||
}
|
||||
defer cancel()
|
||||
|
||||
tmpRepo, err := git.OpenRepository(ctx, prCtx.tmpBasePath)
|
||||
if err != nil {
|
||||
return false, "", fmt.Errorf("OpenRepository: %w", err)
|
||||
}
|
||||
defer tmpRepo.Close()
|
||||
|
||||
// Find the merge-base
|
||||
mergeBase, _, err = tmpRepo.GetMergeBase("", "base", "tracking")
|
||||
mergeBase, err = gitrepo.MergeBaseFromRemote(ctx, pr.BaseRepo, pr.HeadRepo, pr.BaseBranch, pr.HeadBranch)
|
||||
if err != nil {
|
||||
return false, "", fmt.Errorf("GetMergeBase: %w", err)
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user