Skip to content

Commit

Permalink
avoid using tmp remote for comparison to fork branch
Browse files Browse the repository at this point in the history
  • Loading branch information
changchaishi committed Jan 9, 2025
1 parent 957aed4 commit 04c2a5e
Showing 1 changed file with 20 additions and 25 deletions.
45 changes: 20 additions & 25 deletions services/repository/merge_upstream.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,12 @@ package repository
import (
"context"
"fmt"
"strconv"
"time"

git_model "code.gitea.io/gitea/models/git"
issue_model "code.gitea.io/gitea/models/issues"
repo_model "code.gitea.io/gitea/models/repo"
user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/log"
repo_module "code.gitea.io/gitea/modules/repository"
"code.gitea.io/gitea/modules/util"
"code.gitea.io/gitea/services/pull"
Expand Down Expand Up @@ -105,31 +102,29 @@ func GetUpstreamDivergingInfo(ctx context.Context, gitRepo *git.Repository, repo
return info, nil
}

// Add a temporary remote
tmpRemote := strconv.FormatInt(time.Now().UnixNano(), 10)
if err = gitRepo.AddRemote(tmpRemote, repo.BaseRepo.RepoPath(), false); err != nil {
log.Error("GetUpstreamDivergingInfo: AddRemote: %v", err)
}
defer func() {
if err := gitRepo.RemoveRemote(tmpRemote); err != nil {
log.Error("GetUpstreamDivergingInfo: RemoveRemote: %v", err)
}
}()

var remoteBranch string
_, remoteBranch, err = gitRepo.GetMergeBase(tmpRemote, baseBranch.CommitID, forkBranch.CommitID)
if err != nil {
log.Error("GetMergeBase: %v", err)
}

baseBranch.CommitID, err = git.GetFullCommitID(gitRepo.Ctx, gitRepo.Path, remoteBranch)
if err != nil {
baseBranch.CommitID = remoteBranch
}

diff, err := git.GetDivergingCommits(gitRepo.Ctx, gitRepo.Path, baseBranch.CommitID, forkBranch.CommitID)
if err != nil {
info.BaseIsNewer = baseBranch.UpdatedUnix > forkBranch.UpdatedUnix
if !info.BaseIsNewer {
var (
baseCommitID git.ObjectID
headCommit *git.Commit
)

gitRepo.GetCommit(forkBranch.CommitID)

if baseCommitID, err = gitRepo.ConvertToGitID(baseBranch.CommitID); err != nil {
return info, nil
}

if headCommit, err = gitRepo.GetCommit(forkBranch.CommitID); err != nil {
return info, nil
}

if isAncester, _ := headCommit.HasPreviousCommit(baseCommitID); !isAncester {
info.BaseIsNewer = true
}
}
return info, nil
}
info.CommitsBehind, info.CommitsAhead = diff.Behind, diff.Ahead
Expand Down

0 comments on commit 04c2a5e

Please sign in to comment.