Skip to content

Commit cd34ffe

Browse files
committed
Use git model to detect whether branch exist instead of gitrepo method
1 parent f04b9aa commit cd34ffe

File tree

15 files changed

+55
-30
lines changed

15 files changed

+55
-30
lines changed

routers/api/v1/repo/branch.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ func CreateBranch(ctx *context.APIContext) {
225225
return
226226
}
227227
} else if len(opt.OldBranchName) > 0 { //nolint:staticcheck // deprecated field
228-
if gitrepo.IsBranchExist(ctx, ctx.Repo.Repository, opt.OldBranchName) { //nolint:staticcheck // deprecated field
228+
if exist, _ := git_model.IsBranchExist(ctx, ctx.Repo.Repository.ID, opt.OldBranchName); exist { //nolint:staticcheck // deprecated field
229229
oldCommit, err = ctx.Repo.GitRepo.GetBranchCommit(opt.OldBranchName) //nolint:staticcheck // deprecated field
230230
if err != nil {
231231
ctx.APIErrorInternal(err)
@@ -1011,7 +1011,11 @@ func EditBranchProtection(ctx *context.APIContext) {
10111011
isPlainRule := !git_model.IsRuleNameSpecial(bpName)
10121012
var isBranchExist bool
10131013
if isPlainRule {
1014-
isBranchExist = gitrepo.IsBranchExist(ctx, ctx.Repo.Repository, bpName)
1014+
isBranchExist, err = git_model.IsBranchExist(ctx, ctx.Repo.Repository.ID, bpName)
1015+
if err != nil {
1016+
ctx.APIErrorInternal(err)
1017+
return
1018+
}
10151019
}
10161020

10171021
if isBranchExist {

routers/api/v1/repo/pull.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -756,7 +756,12 @@ func EditPullRequest(ctx *context.APIContext) {
756756

757757
// change pull target branch
758758
if !pr.HasMerged && len(form.Base) != 0 && form.Base != pr.BaseBranch {
759-
if !gitrepo.IsBranchExist(ctx, ctx.Repo.Repository, form.Base) {
759+
branchExist, err := git_model.IsBranchExist(ctx, ctx.Repo.Repository.ID, form.Base)
760+
if err != nil {
761+
ctx.APIErrorInternal(err)
762+
return
763+
}
764+
if !branchExist {
760765
ctx.APIError(http.StatusNotFound, fmt.Errorf("new base '%s' not exist", form.Base))
761766
return
762767
}

routers/api/v1/utils/git.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package utils
66
import (
77
"errors"
88

9+
git_model "code.gitea.io/gitea/models/git"
910
repo_model "code.gitea.io/gitea/models/repo"
1011
"code.gitea.io/gitea/modules/git"
1112
"code.gitea.io/gitea/modules/gitrepo"
@@ -27,7 +28,7 @@ func ResolveRefCommit(ctx reqctx.RequestContext, repo *repo_model.Repository, in
2728
return nil, err
2829
}
2930
refCommit := RefCommit{InputRef: inputRef}
30-
if gitrepo.IsBranchExist(ctx, repo, inputRef) {
31+
if exist, _ := git_model.IsBranchExist(ctx, repo.ID, inputRef); exist {
3132
refCommit.RefName = git.RefNameFromBranch(inputRef)
3233
} else if gitrepo.IsTagExist(ctx, repo, inputRef) {
3334
refCommit.RefName = git.RefNameFromTag(inputRef)

routers/private/hook_pre_receive.go

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import (
1717
"code.gitea.io/gitea/models/unit"
1818
user_model "code.gitea.io/gitea/models/user"
1919
"code.gitea.io/gitea/modules/git"
20-
"code.gitea.io/gitea/modules/gitrepo"
2120
"code.gitea.io/gitea/modules/log"
2221
"code.gitea.io/gitea/modules/private"
2322
"code.gitea.io/gitea/modules/web"
@@ -448,12 +447,27 @@ func preReceiveFor(ctx *preReceiveContext, refFullName git.RefName) {
448447

449448
baseBranchName := refFullName.ForBranchName()
450449

451-
baseBranchExist := gitrepo.IsBranchExist(ctx, ctx.Repo.Repository, baseBranchName)
450+
baseBranchExist, err := git_model.IsBranchExist(ctx, ctx.Repo.Repository.ID, baseBranchName)
451+
if err != nil {
452+
ctx.JSON(http.StatusInternalServerError, private.Response{
453+
Err: err.Error(),
454+
})
455+
return
456+
}
452457

453458
if !baseBranchExist {
454459
for p, v := range baseBranchName {
455-
if v == '/' && gitrepo.IsBranchExist(ctx, ctx.Repo.Repository, baseBranchName[:p]) && p != len(baseBranchName)-1 {
456-
baseBranchExist = true
460+
if v != '/' || p == len(baseBranchName)-1 {
461+
continue
462+
}
463+
baseBranchExist, err = git_model.IsBranchExist(ctx, ctx.Repo.Repository.ID, baseBranchName[:p])
464+
if err != nil {
465+
ctx.JSON(http.StatusInternalServerError, private.Response{
466+
Err: err.Error(),
467+
})
468+
return
469+
}
470+
if baseBranchExist {
457471
break
458472
}
459473
}

routers/web/repo/activity.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"time"
99

1010
activities_model "code.gitea.io/gitea/models/activities"
11-
"code.gitea.io/gitea/models/git"
11+
git_model "code.gitea.io/gitea/models/git"
1212
"code.gitea.io/gitea/models/unit"
1313
"code.gitea.io/gitea/modules/templates"
1414
"code.gitea.io/gitea/services/context"
@@ -56,7 +56,7 @@ func Activity(ctx *context.Context) {
5656
canReadCode := ctx.Repo.CanRead(unit.TypeCode)
5757
if canReadCode {
5858
// GetActivityStats needs to read the default branch to get some information
59-
branchExist, _ := git.IsBranchExist(ctx, ctx.Repo.Repository.ID, ctx.Repo.Repository.DefaultBranch)
59+
branchExist, _ := git_model.IsBranchExist(ctx, ctx.Repo.Repository.ID, ctx.Repo.Repository.DefaultBranch)
6060
if !branchExist {
6161
ctx.Data["NotFoundPrompt"] = ctx.Tr("repo.branch.default_branch_not_exist", ctx.Repo.Repository.DefaultBranch)
6262
ctx.NotFound(nil)

routers/web/repo/compare.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ func ParseCompareInfo(ctx *context.Context) *common.CompareInfo {
303303

304304
// Check if base branch is valid.
305305
baseIsCommit := ctx.Repo.GitRepo.IsCommitExist(ci.BaseBranch)
306-
baseIsBranch := gitrepo.IsBranchExist(ctx, ctx.Repo.Repository, ci.BaseBranch)
306+
baseIsBranch, _ := git_model.IsBranchExist(ctx, ctx.Repo.Repository.ID, ci.BaseBranch)
307307
baseIsTag := gitrepo.IsTagExist(ctx, ctx.Repo.Repository, ci.BaseBranch)
308308

309309
if !baseIsCommit && !baseIsBranch && !baseIsTag {
@@ -505,7 +505,7 @@ func ParseCompareInfo(ctx *context.Context) *common.CompareInfo {
505505

506506
// Check if head branch is valid.
507507
headIsCommit := ci.HeadGitRepo.IsCommitExist(ci.HeadBranch)
508-
headIsBranch := gitrepo.IsBranchExist(ctx, ci.HeadRepo, ci.HeadBranch)
508+
headIsBranch, _ := git_model.IsBranchExist(ctx, ci.HeadRepo.ID, ci.HeadBranch)
509509
headIsTag := gitrepo.IsTagExist(ctx, ci.HeadRepo, ci.HeadBranch)
510510
if !headIsCommit && !headIsBranch && !headIsTag {
511511
// Check if headBranch is short sha commit hash

routers/web/repo/issue_comment.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ import (
1010
"net/http"
1111
"strconv"
1212

13+
git_model "code.gitea.io/gitea/models/git"
1314
issues_model "code.gitea.io/gitea/models/issues"
1415
"code.gitea.io/gitea/models/renderhelper"
1516
user_model "code.gitea.io/gitea/models/user"
1617
"code.gitea.io/gitea/modules/git"
17-
"code.gitea.io/gitea/modules/gitrepo"
1818
"code.gitea.io/gitea/modules/log"
1919
"code.gitea.io/gitea/modules/markup/markdown"
2020
repo_module "code.gitea.io/gitea/modules/repository"
@@ -119,7 +119,7 @@ func NewComment(ctx *context.Context) {
119119
ctx.ServerError("Unable to load head repo", err)
120120
return
121121
}
122-
if ok := gitrepo.IsBranchExist(ctx, pull.HeadRepo, pull.BaseBranch); !ok {
122+
if exist, _ := git_model.IsBranchExist(ctx, pull.HeadRepo.ID, pull.BaseBranch); !exist {
123123
// todo localize
124124
ctx.JSONError("The origin branch is delete, cannot reopen.")
125125
return

routers/web/repo/issue_view.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import (
2525
user_model "code.gitea.io/gitea/models/user"
2626
"code.gitea.io/gitea/modules/emoji"
2727
"code.gitea.io/gitea/modules/git"
28-
"code.gitea.io/gitea/modules/gitrepo"
2928
"code.gitea.io/gitea/modules/log"
3029
"code.gitea.io/gitea/modules/markup"
3130
"code.gitea.io/gitea/modules/markup/markdown"
@@ -562,8 +561,10 @@ func preparePullViewDeleteBranch(ctx *context.Context, issue *issues_model.Issue
562561
pull := issue.PullRequest
563562
isPullBranchDeletable := canDelete &&
564563
pull.HeadRepo != nil &&
565-
gitrepo.IsBranchExist(ctx, pull.HeadRepo, pull.HeadBranch) &&
566564
(!pull.HasMerged || ctx.Data["HeadBranchCommitID"] == ctx.Data["PullHeadCommitID"])
565+
if isPullBranchDeletable {
566+
isPullBranchDeletable, _ = git_model.IsBranchExist(ctx, pull.HeadRepo.ID, pull.HeadBranch)
567+
}
567568

568569
if isPullBranchDeletable && pull.HasMerged {
569570
exist, err := issues_model.HasUnmergedPullRequestsByHeadInfo(ctx, pull.HeadRepoID, pull.HeadBranch)

routers/web/repo/pull.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ func prepareViewPullInfo(ctx *context.Context, issue *issues_model.Issue) *pull_
348348
defer baseGitRepo.Close()
349349
}
350350

351-
if !gitrepo.IsBranchExist(ctx, pull.BaseRepo, pull.BaseBranch) {
351+
if exist, _ := git_model.IsBranchExist(ctx, pull.BaseRepo.ID, pull.BaseBranch); !exist {
352352
ctx.Data["BaseBranchNotExist"] = true
353353
ctx.Data["IsPullRequestBroken"] = true
354354
ctx.Data["BaseTarget"] = pull.BaseBranch
@@ -405,7 +405,7 @@ func prepareViewPullInfo(ctx *context.Context, issue *issues_model.Issue) *pull_
405405
defer closer.Close()
406406

407407
if pull.Flow == issues_model.PullRequestFlowGithub {
408-
headBranchExist = gitrepo.IsBranchExist(ctx, pull.HeadRepo, pull.HeadBranch)
408+
headBranchExist, _ = git_model.IsBranchExist(ctx, pull.HeadRepo.ID, pull.HeadBranch)
409409
} else {
410410
headBranchExist = gitrepo.IsReferenceExist(ctx, pull.BaseRepo, pull.GetGitHeadRefName())
411411
}

routers/web/repo/release.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import (
1818
"code.gitea.io/gitea/models/unit"
1919
user_model "code.gitea.io/gitea/models/user"
2020
"code.gitea.io/gitea/modules/git"
21-
"code.gitea.io/gitea/modules/gitrepo"
2221
"code.gitea.io/gitea/modules/markup/markdown"
2322
"code.gitea.io/gitea/modules/optional"
2423
"code.gitea.io/gitea/modules/setting"
@@ -424,7 +423,7 @@ func NewReleasePost(ctx *context.Context) {
424423
return
425424
}
426425

427-
if !gitrepo.IsBranchExist(ctx, ctx.Repo.Repository, form.Target) {
426+
if exist, _ := git_model.IsBranchExist(ctx, ctx.Repo.Repository.ID, form.Target); exist {
428427
ctx.RenderWithErr(ctx.Tr("form.target_branch_not_exist"), tplReleaseNew, &form)
429428
return
430429
}

0 commit comments

Comments
 (0)