1
1
mirror of https://github.com/go-gitea/gitea synced 2025-07-22 18:28:37 +00:00

Fix push message behavior (#33215) (#33317)

Backport #33215

Manually resolved "reqctx" conflict

---------

Co-authored-by: Chai-Shi <changchaishi@gmail.com>
This commit is contained in:
wxiaoguang
2025-01-19 10:48:28 +08:00
committed by GitHub
parent bf664c2e85
commit 8d9ea68f19
6 changed files with 139 additions and 135 deletions

View File

@@ -636,3 +636,61 @@ func SetRepoDefaultBranch(ctx context.Context, repo *repo_model.Repository, gitR
return nil
}
// BranchDivergingInfo contains the information about the divergence of a head branch to the base branch.
// This struct is also used in templates, so it needs to search for all references before changing it.
type BranchDivergingInfo struct {
BaseHasNewCommits bool
HeadCommitsBehind int
HeadCommitsAhead int
}
// GetBranchDivergingInfo returns the information about the divergence of a patch branch to the base branch.
func GetBranchDivergingInfo(ctx context.Context, baseRepo *repo_model.Repository, baseBranch string, headRepo *repo_model.Repository, headBranch string) (*BranchDivergingInfo, error) {
headGitBranch, err := git_model.GetBranch(ctx, headRepo.ID, headBranch)
if err != nil {
return nil, err
}
baseGitBranch, err := git_model.GetBranch(ctx, baseRepo.ID, baseBranch)
if err != nil {
return nil, err
}
info := &BranchDivergingInfo{}
if headGitBranch.CommitID == baseGitBranch.CommitID {
return info, nil
}
// if the fork repo has new commits, this call will fail because they are not in the base repo
// exit status 128 - fatal: Invalid symmetric difference expression aaaaaaaaaaaa...bbbbbbbbbbbb
// so at the moment, we first check the update time, then check whether the fork branch has base's head
diff, err := git.GetDivergingCommits(ctx, baseRepo.RepoPath(), baseGitBranch.CommitID, headGitBranch.CommitID)
if err != nil {
info.BaseHasNewCommits = baseGitBranch.UpdatedUnix > headGitBranch.UpdatedUnix
if headRepo.IsFork && info.BaseHasNewCommits {
return info, nil
}
// if the base's update time is before the fork, check whether the base's head is in the fork
headGitRepo, closer, err := gitrepo.RepositoryFromContextOrOpen(ctx, headRepo)
if err != nil {
return nil, err
}
defer closer.Close()
headCommit, err := headGitRepo.GetCommit(headGitBranch.CommitID)
if err != nil {
return nil, err
}
baseCommitID, err := git.NewIDFromString(baseGitBranch.CommitID)
if err != nil {
return nil, err
}
hasPreviousCommit, _ := headCommit.HasPreviousCommit(baseCommitID)
info.BaseHasNewCommits = !hasPreviousCommit
return info, nil
}
info.HeadCommitsBehind, info.HeadCommitsAhead = diff.Behind, diff.Ahead
return info, nil
}

View File

@@ -7,23 +7,15 @@ import (
"context"
"fmt"
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/gitrepo"
repo_module "code.gitea.io/gitea/modules/repository"
"code.gitea.io/gitea/modules/util"
"code.gitea.io/gitea/services/pull"
)
type UpstreamDivergingInfo struct {
BaseHasNewCommits bool
CommitsBehind int
CommitsAhead int
}
// MergeUpstream merges the base repository's default branch into the fork repository's current branch.
func MergeUpstream(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, branch string) (mergeStyle string, err error) {
if err = repo.MustNotBeArchived(); err != nil {
@@ -77,70 +69,18 @@ func MergeUpstream(ctx context.Context, doer *user_model.User, repo *repo_model.
}
// GetUpstreamDivergingInfo returns the information about the divergence between the fork repository's branch and the base repository's default branch.
func GetUpstreamDivergingInfo(ctx context.Context, repo *repo_model.Repository, branch string) (*UpstreamDivergingInfo, error) {
if !repo.IsFork {
func GetUpstreamDivergingInfo(ctx context.Context, forkRepo *repo_model.Repository, forkBranch string) (*BranchDivergingInfo, error) {
if !forkRepo.IsFork {
return nil, util.NewInvalidArgumentErrorf("repo is not a fork")
}
if repo.IsArchived {
if forkRepo.IsArchived {
return nil, util.NewInvalidArgumentErrorf("repo is archived")
}
if err := repo.GetBaseRepo(ctx); err != nil {
if err := forkRepo.GetBaseRepo(ctx); err != nil {
return nil, err
}
forkBranch, err := git_model.GetBranch(ctx, repo.ID, branch)
if err != nil {
return nil, err
}
baseBranch, err := git_model.GetBranch(ctx, repo.BaseRepo.ID, repo.BaseRepo.DefaultBranch)
if err != nil {
return nil, err
}
info := &UpstreamDivergingInfo{}
if forkBranch.CommitID == baseBranch.CommitID {
return info, nil
}
// if the fork repo has new commits, this call will fail because they are not in the base repo
// exit status 128 - fatal: Invalid symmetric difference expression aaaaaaaaaaaa...bbbbbbbbbbbb
// so at the moment, we first check the update time, then check whether the fork branch has base's head
diff, err := git.GetDivergingCommits(ctx, repo.BaseRepo.RepoPath(), baseBranch.CommitID, forkBranch.CommitID)
if err != nil {
info.BaseHasNewCommits = baseBranch.UpdatedUnix > forkBranch.UpdatedUnix
if info.BaseHasNewCommits {
return info, nil
}
// if the base's update time is before the fork, check whether the base's head is in the fork
baseGitRepo, baseGitRepoCloser, err := gitrepo.RepositoryFromContextOrOpen(ctx, repo.BaseRepo)
if err != nil {
return nil, err
}
defer baseGitRepoCloser.Close()
headGitRepo, headGitRepoCloser, err := gitrepo.RepositoryFromContextOrOpen(ctx, repo)
if err != nil {
return nil, err
}
defer headGitRepoCloser.Close()
baseCommitID, err := baseGitRepo.ConvertToGitID(baseBranch.CommitID)
if err != nil {
return nil, err
}
headCommit, err := headGitRepo.GetCommit(forkBranch.CommitID)
if err != nil {
return nil, err
}
hasPreviousCommit, _ := headCommit.HasPreviousCommit(baseCommitID)
info.BaseHasNewCommits = !hasPreviousCommit
return info, nil
}
info.CommitsBehind, info.CommitsAhead = diff.Behind, diff.Ahead
return info, nil
return GetBranchDivergingInfo(ctx, forkRepo.BaseRepo, forkRepo.BaseRepo.DefaultBranch, forkRepo, forkBranch)
}