diff --git a/routers/api/v1/repo/branch.go b/routers/api/v1/repo/branch.go index 65fac45aa11f2..a703ca6909516 100644 --- a/routers/api/v1/repo/branch.go +++ b/routers/api/v1/repo/branch.go @@ -225,7 +225,7 @@ func CreateBranch(ctx *context.APIContext) { return } } else if len(opt.OldBranchName) > 0 { //nolint:staticcheck // deprecated field - if gitrepo.IsBranchExist(ctx, ctx.Repo.Repository, opt.OldBranchName) { //nolint:staticcheck // deprecated field + if exist, _ := git_model.IsBranchExist(ctx, ctx.Repo.Repository.ID, opt.OldBranchName); exist { //nolint:staticcheck // deprecated field oldCommit, err = ctx.Repo.GitRepo.GetBranchCommit(opt.OldBranchName) //nolint:staticcheck // deprecated field if err != nil { ctx.APIErrorInternal(err) @@ -1011,7 +1011,11 @@ func EditBranchProtection(ctx *context.APIContext) { isPlainRule := !git_model.IsRuleNameSpecial(bpName) var isBranchExist bool if isPlainRule { - isBranchExist = gitrepo.IsBranchExist(ctx, ctx.Repo.Repository, bpName) + isBranchExist, err = git_model.IsBranchExist(ctx, ctx.Repo.Repository.ID, bpName) + if err != nil { + ctx.APIErrorInternal(err) + return + } } if isBranchExist { diff --git a/routers/api/v1/repo/pull.go b/routers/api/v1/repo/pull.go index ff6ddbce2d4bc..3c9254eee54b8 100644 --- a/routers/api/v1/repo/pull.go +++ b/routers/api/v1/repo/pull.go @@ -756,7 +756,12 @@ func EditPullRequest(ctx *context.APIContext) { // change pull target branch if !pr.HasMerged && len(form.Base) != 0 && form.Base != pr.BaseBranch { - if !gitrepo.IsBranchExist(ctx, ctx.Repo.Repository, form.Base) { + branchExist, err := git_model.IsBranchExist(ctx, ctx.Repo.Repository.ID, form.Base) + if err != nil { + ctx.APIErrorInternal(err) + return + } + if !branchExist { ctx.APIError(http.StatusNotFound, fmt.Errorf("new base '%s' not exist", form.Base)) return } diff --git a/routers/api/v1/utils/git.go b/routers/api/v1/utils/git.go index 1cfe01a639903..b872dcbd2cc77 100644 --- a/routers/api/v1/utils/git.go +++ b/routers/api/v1/utils/git.go @@ -6,6 +6,7 @@ package utils import ( "errors" + git_model "code.gitea.io/gitea/models/git" repo_model "code.gitea.io/gitea/models/repo" "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/gitrepo" @@ -27,7 +28,7 @@ func ResolveRefCommit(ctx reqctx.RequestContext, repo *repo_model.Repository, in return nil, err } refCommit := RefCommit{InputRef: inputRef} - if gitrepo.IsBranchExist(ctx, repo, inputRef) { + if exist, _ := git_model.IsBranchExist(ctx, repo.ID, inputRef); exist { refCommit.RefName = git.RefNameFromBranch(inputRef) } else if gitrepo.IsTagExist(ctx, repo, inputRef) { refCommit.RefName = git.RefNameFromTag(inputRef) diff --git a/routers/private/hook_pre_receive.go b/routers/private/hook_pre_receive.go index 99a0b450d701f..4f1fcce8ef7ba 100644 --- a/routers/private/hook_pre_receive.go +++ b/routers/private/hook_pre_receive.go @@ -18,7 +18,6 @@ import ( user_model "code.gitea.io/gitea/models/user" "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" "code.gitea.io/gitea/modules/private" "code.gitea.io/gitea/modules/web" @@ -449,12 +448,27 @@ func preReceiveFor(ctx *preReceiveContext, refFullName git.RefName) { baseBranchName := refFullName.ForBranchName() - baseBranchExist := gitrepo.IsBranchExist(ctx, ctx.Repo.Repository, baseBranchName) + baseBranchExist, err := git_model.IsBranchExist(ctx, ctx.Repo.Repository.ID, baseBranchName) + if err != nil { + ctx.JSON(http.StatusInternalServerError, private.Response{ + Err: err.Error(), + }) + return + } if !baseBranchExist { for p, v := range baseBranchName { - if v == '/' && gitrepo.IsBranchExist(ctx, ctx.Repo.Repository, baseBranchName[:p]) && p != len(baseBranchName)-1 { - baseBranchExist = true + if v != '/' || p == len(baseBranchName)-1 { + continue + } + baseBranchExist, err = git_model.IsBranchExist(ctx, ctx.Repo.Repository.ID, baseBranchName[:p]) + if err != nil { + ctx.JSON(http.StatusInternalServerError, private.Response{ + Err: err.Error(), + }) + return + } + if baseBranchExist { break } } diff --git a/routers/web/repo/activity.go b/routers/web/repo/activity.go index 8232f0cc04bc3..fcede1822aeca 100644 --- a/routers/web/repo/activity.go +++ b/routers/web/repo/activity.go @@ -8,7 +8,7 @@ import ( "time" activities_model "code.gitea.io/gitea/models/activities" - "code.gitea.io/gitea/models/git" + git_model "code.gitea.io/gitea/models/git" "code.gitea.io/gitea/models/unit" "code.gitea.io/gitea/modules/templates" "code.gitea.io/gitea/services/context" @@ -56,7 +56,7 @@ func Activity(ctx *context.Context) { canReadCode := ctx.Repo.CanRead(unit.TypeCode) if canReadCode { // GetActivityStats needs to read the default branch to get some information - branchExist, _ := git.IsBranchExist(ctx, ctx.Repo.Repository.ID, ctx.Repo.Repository.DefaultBranch) + branchExist, _ := git_model.IsBranchExist(ctx, ctx.Repo.Repository.ID, ctx.Repo.Repository.DefaultBranch) if !branchExist { ctx.Data["NotFoundPrompt"] = ctx.Tr("repo.branch.default_branch_not_exist", ctx.Repo.Repository.DefaultBranch) ctx.NotFound(nil) diff --git a/routers/web/repo/compare.go b/routers/web/repo/compare.go index 2e56f6934a05e..bc6276a33f42c 100644 --- a/routers/web/repo/compare.go +++ b/routers/web/repo/compare.go @@ -304,7 +304,7 @@ func ParseCompareInfo(ctx *context.Context) *common.CompareInfo { // Check if base branch is valid. baseIsCommit := ctx.Repo.GitRepo.IsCommitExist(ci.BaseBranch) - baseIsBranch := gitrepo.IsBranchExist(ctx, ctx.Repo.Repository, ci.BaseBranch) + baseIsBranch, _ := git_model.IsBranchExist(ctx, ctx.Repo.Repository.ID, ci.BaseBranch) baseIsTag := gitrepo.IsTagExist(ctx, ctx.Repo.Repository, ci.BaseBranch) if !baseIsCommit && !baseIsBranch && !baseIsTag { @@ -506,7 +506,7 @@ func ParseCompareInfo(ctx *context.Context) *common.CompareInfo { // Check if head branch is valid. headIsCommit := ci.HeadGitRepo.IsCommitExist(ci.HeadBranch) - headIsBranch := gitrepo.IsBranchExist(ctx, ci.HeadRepo, ci.HeadBranch) + headIsBranch, _ := git_model.IsBranchExist(ctx, ci.HeadRepo.ID, ci.HeadBranch) headIsTag := gitrepo.IsTagExist(ctx, ci.HeadRepo, ci.HeadBranch) if !headIsCommit && !headIsBranch && !headIsTag { // Check if headBranch is short sha commit hash diff --git a/routers/web/repo/issue_comment.go b/routers/web/repo/issue_comment.go index cb5b2d801952d..662b241ea3ddc 100644 --- a/routers/web/repo/issue_comment.go +++ b/routers/web/repo/issue_comment.go @@ -10,11 +10,11 @@ import ( "net/http" "strconv" + git_model "code.gitea.io/gitea/models/git" issues_model "code.gitea.io/gitea/models/issues" "code.gitea.io/gitea/models/renderhelper" user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/git" - "code.gitea.io/gitea/modules/gitrepo" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/markup/markdown" repo_module "code.gitea.io/gitea/modules/repository" @@ -119,7 +119,7 @@ func NewComment(ctx *context.Context) { ctx.ServerError("Unable to load head repo", err) return } - if ok := gitrepo.IsBranchExist(ctx, pull.HeadRepo, pull.BaseBranch); !ok { + if exist, _ := git_model.IsBranchExist(ctx, pull.HeadRepo.ID, pull.BaseBranch); !exist { // todo localize ctx.JSONError("The origin branch is delete, cannot reopen.") return diff --git a/routers/web/repo/issue_view.go b/routers/web/repo/issue_view.go index d9f6c33e3fbc5..49bb837ab9bb6 100644 --- a/routers/web/repo/issue_view.go +++ b/routers/web/repo/issue_view.go @@ -26,7 +26,6 @@ import ( user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/emoji" "code.gitea.io/gitea/modules/git" - "code.gitea.io/gitea/modules/gitrepo" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/markup" "code.gitea.io/gitea/modules/markup/markdown" @@ -563,8 +562,10 @@ func preparePullViewDeleteBranch(ctx *context.Context, issue *issues_model.Issue pull := issue.PullRequest isPullBranchDeletable := canDelete && pull.HeadRepo != nil && - gitrepo.IsBranchExist(ctx, pull.HeadRepo, pull.HeadBranch) && (!pull.HasMerged || ctx.Data["HeadBranchCommitID"] == ctx.Data["PullHeadCommitID"]) + if isPullBranchDeletable { + isPullBranchDeletable, _ = git_model.IsBranchExist(ctx, pull.HeadRepo.ID, pull.HeadBranch) + } if isPullBranchDeletable && pull.HasMerged { exist, err := issues_model.HasUnmergedPullRequestsByHeadInfo(ctx, pull.HeadRepoID, pull.HeadBranch) diff --git a/routers/web/repo/pull.go b/routers/web/repo/pull.go index c9ea836899ae8..d50d73872f283 100644 --- a/routers/web/repo/pull.go +++ b/routers/web/repo/pull.go @@ -348,7 +348,7 @@ func prepareViewPullInfo(ctx *context.Context, issue *issues_model.Issue) *pull_ defer baseGitRepo.Close() } - if !gitrepo.IsBranchExist(ctx, pull.BaseRepo, pull.BaseBranch) { + if exist, _ := git_model.IsBranchExist(ctx, pull.BaseRepo.ID, pull.BaseBranch); !exist { ctx.Data["BaseBranchNotExist"] = true ctx.Data["IsPullRequestBroken"] = true ctx.Data["BaseTarget"] = pull.BaseBranch @@ -405,7 +405,7 @@ func prepareViewPullInfo(ctx *context.Context, issue *issues_model.Issue) *pull_ defer closer.Close() if pull.Flow == issues_model.PullRequestFlowGithub { - headBranchExist = gitrepo.IsBranchExist(ctx, pull.HeadRepo, pull.HeadBranch) + headBranchExist, _ = git_model.IsBranchExist(ctx, pull.HeadRepo.ID, pull.HeadBranch) } else { headBranchExist = gitrepo.IsReferenceExist(ctx, pull.BaseRepo, pull.GetGitHeadRefName()) } diff --git a/routers/web/repo/release.go b/routers/web/repo/release.go index 36ea20c23e6f7..4ed9e0bdbde44 100644 --- a/routers/web/repo/release.go +++ b/routers/web/repo/release.go @@ -18,7 +18,6 @@ import ( "code.gitea.io/gitea/models/unit" user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/git" - "code.gitea.io/gitea/modules/gitrepo" "code.gitea.io/gitea/modules/markup/markdown" "code.gitea.io/gitea/modules/optional" "code.gitea.io/gitea/modules/setting" @@ -424,7 +423,7 @@ func NewReleasePost(ctx *context.Context) { return } - if !gitrepo.IsBranchExist(ctx, ctx.Repo.Repository, form.Target) { + if exist, _ := git_model.IsBranchExist(ctx, ctx.Repo.Repository.ID, form.Target); !exist { ctx.RenderWithErr(ctx.Tr("form.target_branch_not_exist"), tplReleaseNew, &form) return } diff --git a/services/agit/agit.go b/services/agit/agit.go index 4ed867b358323..0a461f24882d6 100644 --- a/services/agit/agit.go +++ b/services/agit/agit.go @@ -73,7 +73,7 @@ func ProcReceive(ctx context.Context, repo *repo_model.Repository, gitRepo *git. baseBranchName := opts.RefFullNames[i].ForBranchName() currentTopicBranch := "" - if !gitrepo.IsBranchExist(ctx, repo, baseBranchName) { + if exist, _ := git_model.IsBranchExist(ctx, repo.ID, baseBranchName); !exist { // try match refs/for// for p, v := range baseBranchName { if v == '/' && gitrepo.IsBranchExist(ctx, repo, baseBranchName[:p]) && p != len(baseBranchName)-1 { diff --git a/services/automerge/automerge.go b/services/automerge/automerge.go index a60883b4cc501..cf3207ccbc424 100644 --- a/services/automerge/automerge.go +++ b/services/automerge/automerge.go @@ -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" @@ -219,7 +220,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 diff --git a/services/pull/protected_branch.go b/services/pull/protected_branch.go index 181bd32f44357..68ae7bc2ed2ca 100644 --- a/services/pull/protected_branch.go +++ b/services/pull/protected_branch.go @@ -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 { diff --git a/services/pull/temp_repo.go b/services/pull/temp_repo.go index 8750b4288a429..4af1228a87844 100644 --- a/services/pull/temp_repo.go +++ b/services/pull/temp_repo.go @@ -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" ) @@ -180,7 +179,7 @@ func createTemporaryRepoForPR(ctx context.Context, pr *issues_model.PullRequest) if err := gitcmd.NewCommand("fetch").AddArguments(fetchArgs...).AddDynamicArguments(remoteRepoName, headBranch+":"+trackingBranch). Run(ctx, prCtx.RunOpts()); 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, } diff --git a/services/repository/branch.go b/services/repository/branch.go index df7202227aa70..2bcec7398ca1e 100644 --- a/services/repository/branch.go +++ b/services/repository/branch.go @@ -410,11 +410,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 } @@ -630,7 +630,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, }