mirror of
https://github.com/go-gitea/gitea.git
synced 2025-11-10 11:47:56 -05: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 {
|
||||
|
||||
Reference in New Issue
Block a user