mirror of
https://github.com/go-gitea/gitea
synced 2025-12-06 21:08:25 +00:00
Use git model to detect whether branch exist instead of gitrepo method (#35459)
This commit is contained in:
@@ -6,6 +6,7 @@ package agit
|
||||
import (
|
||||
"context"
|
||||
"encoding/base64"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
@@ -32,6 +33,34 @@ func parseAgitPushOptionValue(s string) string {
|
||||
return s
|
||||
}
|
||||
|
||||
func GetAgitBranchInfo(ctx context.Context, repoID int64, baseBranchName string) (string, string, error) {
|
||||
baseBranchExist, err := git_model.IsBranchExist(ctx, repoID, baseBranchName)
|
||||
if err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
if baseBranchExist {
|
||||
return baseBranchName, "", nil
|
||||
}
|
||||
|
||||
// try match <target-branch>/<topic-branch>
|
||||
// refs/for have been trimmed to get baseBranchName
|
||||
for p, v := range baseBranchName {
|
||||
if v != '/' {
|
||||
continue
|
||||
}
|
||||
|
||||
baseBranchExist, err := git_model.IsBranchExist(ctx, repoID, baseBranchName[:p])
|
||||
if err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
if baseBranchExist {
|
||||
return baseBranchName[:p], baseBranchName[p+1:], nil
|
||||
}
|
||||
}
|
||||
|
||||
return "", "", util.NewNotExistErrorf("base branch does not exist")
|
||||
}
|
||||
|
||||
// ProcReceive handle proc receive work
|
||||
func ProcReceive(ctx context.Context, repo *repo_model.Repository, gitRepo *git.Repository, opts *private.HookOptions) ([]private.HookProcReceiveRefResult, error) {
|
||||
results := make([]private.HookProcReceiveRefResult, 0, len(opts.OldCommitIDs))
|
||||
@@ -70,17 +99,19 @@ func ProcReceive(ctx context.Context, repo *repo_model.Repository, gitRepo *git.
|
||||
continue
|
||||
}
|
||||
|
||||
baseBranchName := opts.RefFullNames[i].ForBranchName()
|
||||
currentTopicBranch := ""
|
||||
if !gitrepo.IsBranchExist(ctx, repo, baseBranchName) {
|
||||
// try match refs/for/<target-branch>/<topic-branch>
|
||||
for p, v := range baseBranchName {
|
||||
if v == '/' && gitrepo.IsBranchExist(ctx, repo, baseBranchName[:p]) && p != len(baseBranchName)-1 {
|
||||
currentTopicBranch = baseBranchName[p+1:]
|
||||
baseBranchName = baseBranchName[:p]
|
||||
break
|
||||
}
|
||||
baseBranchName, currentTopicBranch, err := GetAgitBranchInfo(ctx, repo.ID, opts.RefFullNames[i].ForBranchName())
|
||||
if err != nil {
|
||||
if !errors.Is(err, util.ErrNotExist) {
|
||||
return nil, fmt.Errorf("failed to get branch information. Error: %w", err)
|
||||
}
|
||||
// If branch does not exist, we can continue
|
||||
results = append(results, private.HookProcReceiveRefResult{
|
||||
OriginalRef: opts.RefFullNames[i],
|
||||
OldOID: opts.OldCommitIDs[i],
|
||||
NewOID: opts.NewCommitIDs[i],
|
||||
Err: "base-branch does not exist",
|
||||
})
|
||||
continue
|
||||
}
|
||||
|
||||
if len(topicBranch) == 0 && len(currentTopicBranch) == 0 {
|
||||
|
||||
@@ -6,11 +6,56 @@ package agit
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"code.gitea.io/gitea/models/unittest"
|
||||
"code.gitea.io/gitea/modules/util"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
unittest.MainTest(m)
|
||||
}
|
||||
|
||||
func TestParseAgitPushOptionValue(t *testing.T) {
|
||||
assert.Equal(t, "a", parseAgitPushOptionValue("a"))
|
||||
assert.Equal(t, "a", parseAgitPushOptionValue("{base64}YQ=="))
|
||||
assert.Equal(t, "{base64}invalid value", parseAgitPushOptionValue("{base64}invalid value"))
|
||||
}
|
||||
|
||||
func TestGetAgitBranchInfo(t *testing.T) {
|
||||
assert.NoError(t, unittest.PrepareTestDatabase())
|
||||
|
||||
_, _, err := GetAgitBranchInfo(t.Context(), 1, "non-exist-basebranch")
|
||||
assert.ErrorIs(t, err, util.ErrNotExist)
|
||||
|
||||
baseBranch, currentTopicBranch, err := GetAgitBranchInfo(t.Context(), 1, "master")
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "master", baseBranch)
|
||||
assert.Empty(t, currentTopicBranch)
|
||||
|
||||
baseBranch, currentTopicBranch, err = GetAgitBranchInfo(t.Context(), 1, "master/topicbranch")
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "master", baseBranch)
|
||||
assert.Equal(t, "topicbranch", currentTopicBranch)
|
||||
|
||||
baseBranch, currentTopicBranch, err = GetAgitBranchInfo(t.Context(), 1, "master/")
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "master", baseBranch)
|
||||
assert.Empty(t, currentTopicBranch)
|
||||
|
||||
_, _, err = GetAgitBranchInfo(t.Context(), 1, "/")
|
||||
assert.ErrorIs(t, err, util.ErrNotExist)
|
||||
|
||||
_, _, err = GetAgitBranchInfo(t.Context(), 1, "//")
|
||||
assert.ErrorIs(t, err, util.ErrNotExist)
|
||||
|
||||
baseBranch, currentTopicBranch, err = GetAgitBranchInfo(t.Context(), 1, "master/topicbranch/")
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "master", baseBranch)
|
||||
assert.Equal(t, "topicbranch/", currentTopicBranch)
|
||||
|
||||
baseBranch, currentTopicBranch, err = GetAgitBranchInfo(t.Context(), 1, "master/topicbranch/1")
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "master", baseBranch)
|
||||
assert.Equal(t, "topicbranch/1", currentTopicBranch)
|
||||
}
|
||||
|
||||
@@ -11,6 +11,7 @@ import (
|
||||
"strings"
|
||||
|
||||
"code.gitea.io/gitea/models/db"
|
||||
git_model "code.gitea.io/gitea/models/git"
|
||||
issues_model "code.gitea.io/gitea/models/issues"
|
||||
access_model "code.gitea.io/gitea/models/perm/access"
|
||||
pull_model "code.gitea.io/gitea/models/pull"
|
||||
@@ -207,7 +208,10 @@ func handlePullRequestAutoMerge(pullID int64, sha string) {
|
||||
|
||||
switch pr.Flow {
|
||||
case issues_model.PullRequestFlowGithub:
|
||||
headBranchExist := pr.HeadRepo != nil && gitrepo.IsBranchExist(ctx, pr.HeadRepo, pr.HeadBranch)
|
||||
headBranchExist := pr.HeadRepo != nil
|
||||
if headBranchExist {
|
||||
headBranchExist, _ = git_model.IsBranchExist(ctx, pr.HeadRepo.ID, pr.HeadBranch)
|
||||
}
|
||||
if !headBranchExist {
|
||||
log.Warn("Head branch of auto merge %-v does not exist [HeadRepoID: %d, Branch: %s]", pr, pr.HeadRepoID, pr.HeadBranch)
|
||||
return
|
||||
|
||||
@@ -96,8 +96,12 @@ func GetPullRequestCommitStatusState(ctx context.Context, pr *issues_model.PullR
|
||||
}
|
||||
defer closer.Close()
|
||||
|
||||
if pr.Flow == issues_model.PullRequestFlowGithub && !gitrepo.IsBranchExist(ctx, pr.HeadRepo, pr.HeadBranch) {
|
||||
return "", errors.New("Head branch does not exist, can not merge")
|
||||
if pr.Flow == issues_model.PullRequestFlowGithub {
|
||||
if exist, err := git_model.IsBranchExist(ctx, pr.HeadRepo.ID, pr.HeadBranch); err != nil {
|
||||
return "", errors.Wrap(err, "IsBranchExist")
|
||||
} else if !exist {
|
||||
return "", errors.New("Head branch does not exist, can not merge")
|
||||
}
|
||||
}
|
||||
if pr.Flow == issues_model.PullRequestFlowAGit && !gitrepo.IsReferenceExist(ctx, pr.HeadRepo, pr.GetGitHeadRefName()) {
|
||||
return "", errors.New("Head branch does not exist, can not merge")
|
||||
|
||||
@@ -8,7 +8,6 @@ import (
|
||||
|
||||
git_model "code.gitea.io/gitea/models/git"
|
||||
repo_model "code.gitea.io/gitea/models/repo"
|
||||
"code.gitea.io/gitea/modules/gitrepo"
|
||||
)
|
||||
|
||||
func CreateOrUpdateProtectedBranch(ctx context.Context, repo *repo_model.Repository,
|
||||
@@ -22,8 +21,7 @@ func CreateOrUpdateProtectedBranch(ctx context.Context, repo *repo_model.Reposit
|
||||
isPlainRule := !git_model.IsRuleNameSpecial(protectBranch.RuleName)
|
||||
var isBranchExist bool
|
||||
if isPlainRule {
|
||||
// TODO: read the database directly to check if the branch exists
|
||||
isBranchExist = gitrepo.IsBranchExist(ctx, repo, protectBranch.RuleName)
|
||||
isBranchExist, _ = git_model.IsBranchExist(ctx, repo.ID, protectBranch.RuleName)
|
||||
}
|
||||
|
||||
if isBranchExist {
|
||||
|
||||
@@ -16,7 +16,6 @@ import (
|
||||
repo_model "code.gitea.io/gitea/models/repo"
|
||||
"code.gitea.io/gitea/modules/git"
|
||||
"code.gitea.io/gitea/modules/git/gitcmd"
|
||||
"code.gitea.io/gitea/modules/gitrepo"
|
||||
"code.gitea.io/gitea/modules/log"
|
||||
repo_module "code.gitea.io/gitea/modules/repository"
|
||||
)
|
||||
@@ -182,7 +181,7 @@ func createTemporaryRepoForPR(ctx context.Context, pr *issues_model.PullRequest)
|
||||
if err := prCtx.PrepareGitCmd(gitcmd.NewCommand("fetch").AddArguments(fetchArgs...).AddDynamicArguments(remoteRepoName, headBranch+":"+trackingBranch)).
|
||||
Run(ctx); err != nil {
|
||||
cancel()
|
||||
if !gitrepo.IsBranchExist(ctx, pr.HeadRepo, pr.HeadBranch) {
|
||||
if exist, _ := git_model.IsBranchExist(ctx, pr.HeadRepo.ID, pr.HeadBranch); !exist {
|
||||
return nil, nil, git_model.ErrBranchNotExist{
|
||||
BranchName: pr.HeadBranch,
|
||||
}
|
||||
|
||||
@@ -409,11 +409,11 @@ func RenameBranch(ctx context.Context, repo *repo_model.Repository, doer *user_m
|
||||
return "target_exist", nil
|
||||
}
|
||||
|
||||
if gitrepo.IsBranchExist(ctx, repo, to) {
|
||||
if exist, _ := git_model.IsBranchExist(ctx, repo.ID, to); exist {
|
||||
return "target_exist", nil
|
||||
}
|
||||
|
||||
if !gitrepo.IsBranchExist(ctx, repo, from) {
|
||||
if exist, _ := git_model.IsBranchExist(ctx, repo.ID, from); !exist {
|
||||
return "from_not_exist", nil
|
||||
}
|
||||
|
||||
@@ -624,7 +624,7 @@ func SetRepoDefaultBranch(ctx context.Context, repo *repo_model.Repository, newB
|
||||
return nil
|
||||
}
|
||||
|
||||
if !gitrepo.IsBranchExist(ctx, repo, newBranchName) {
|
||||
if exist, _ := git_model.IsBranchExist(ctx, repo.ID, newBranchName); !exist {
|
||||
return git_model.ErrBranchNotExist{
|
||||
BranchName: newBranchName,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user