Skip to content

Commit 1778615

Browse files
authored
Merge branch 'main' into private-reusable-workflow
2 parents 7e71bea + 5454fda commit 1778615

File tree

18 files changed

+165
-54
lines changed

18 files changed

+165
-54
lines changed

models/fixtures/branch.yml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,3 +225,27 @@
225225
is_deleted: false
226226
deleted_by_id: 0
227227
deleted_unix: 0
228+
229+
-
230+
id: 27
231+
repo_id: 1
232+
name: 'DefaultBranch'
233+
commit_id: '90c1019714259b24fb81711d4416ac0f18667dfa'
234+
commit_message: 'add license'
235+
commit_time: 1709345946
236+
pusher_id: 1
237+
is_deleted: false
238+
deleted_by_id: 0
239+
deleted_unix: 0
240+
241+
-
242+
id: 28
243+
repo_id: 1
244+
name: 'sub-home-md-img-check'
245+
commit_id: '4649299398e4d39a5c09eb4f534df6f1e1eb87cc'
246+
commit_message: "Test how READMEs render images when found in a subfolder"
247+
commit_time: 1678403550
248+
pusher_id: 1
249+
is_deleted: false
250+
deleted_by_id: 0
251+
deleted_unix: 0

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: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"time"
1414

1515
activities_model "code.gitea.io/gitea/models/activities"
16+
git_model "code.gitea.io/gitea/models/git"
1617
issues_model "code.gitea.io/gitea/models/issues"
1718
access_model "code.gitea.io/gitea/models/perm/access"
1819
pull_model "code.gitea.io/gitea/models/pull"
@@ -755,7 +756,12 @@ func EditPullRequest(ctx *context.APIContext) {
755756

756757
// change pull target branch
757758
if !pr.HasMerged && len(form.Base) != 0 && form.Base != pr.BaseBranch {
758-
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 {
759765
ctx.APIError(http.StatusNotFound, fmt.Errorf("new base '%s' not exist", form.Base))
760766
return
761767
}

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: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ import (
2121
"code.gitea.io/gitea/modules/gitrepo"
2222
"code.gitea.io/gitea/modules/log"
2323
"code.gitea.io/gitea/modules/private"
24+
"code.gitea.io/gitea/modules/util"
2425
"code.gitea.io/gitea/modules/web"
26+
"code.gitea.io/gitea/services/agit"
2527
gitea_context "code.gitea.io/gitea/services/context"
2628
pull_service "code.gitea.io/gitea/services/pull"
2729
)
@@ -452,25 +454,18 @@ func preReceiveFor(ctx *preReceiveContext, refFullName git.RefName) {
452454
return
453455
}
454456

455-
baseBranchName := refFullName.ForBranchName()
456-
457-
baseBranchExist := gitrepo.IsBranchExist(ctx, ctx.Repo.Repository, baseBranchName)
458-
459-
if !baseBranchExist {
460-
for p, v := range baseBranchName {
461-
if v == '/' && gitrepo.IsBranchExist(ctx, ctx.Repo.Repository, baseBranchName[:p]) && p != len(baseBranchName)-1 {
462-
baseBranchExist = true
463-
break
464-
}
457+
_, _, err := agit.GetAgitBranchInfo(ctx, ctx.Repo.Repository.ID, refFullName.ForBranchName())
458+
if err != nil {
459+
if !errors.Is(err, util.ErrNotExist) {
460+
ctx.JSON(http.StatusForbidden, private.Response{
461+
UserMsg: fmt.Sprintf("Unexpected ref: %s", refFullName),
462+
})
463+
} else {
464+
ctx.JSON(http.StatusInternalServerError, private.Response{
465+
Err: err.Error(),
466+
})
465467
}
466468
}
467-
468-
if !baseBranchExist {
469-
ctx.JSON(http.StatusForbidden, private.Response{
470-
UserMsg: fmt.Sprintf("Unexpected ref: %s", refFullName),
471-
})
472-
return
473-
}
474469
}
475470

476471
func generateGitEnv(opts *private.HookOptions) (env []string) {

routers/web/repo/compare.go

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

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

312312
if !baseIsCommit && !baseIsBranch && !baseIsTag {
@@ -508,7 +508,7 @@ func ParseCompareInfo(ctx *context.Context) *common.CompareInfo {
508508

509509
// Check if head branch is valid.
510510
headIsCommit := ci.HeadGitRepo.IsCommitExist(ci.HeadBranch)
511-
headIsBranch := gitrepo.IsBranchExist(ctx, ci.HeadRepo, ci.HeadBranch)
511+
headIsBranch, _ := git_model.IsBranchExist(ctx, ci.HeadRepo.ID, ci.HeadBranch)
512512
headIsTag := gitrepo.IsTagExist(ctx, ci.HeadRepo, ci.HeadBranch)
513513
if !headIsCommit && !headIsBranch && !headIsTag {
514514
// 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
@@ -11,11 +11,11 @@ import (
1111
"strconv"
1212
"strings"
1313

14+
git_model "code.gitea.io/gitea/models/git"
1415
issues_model "code.gitea.io/gitea/models/issues"
1516
"code.gitea.io/gitea/models/renderhelper"
1617
user_model "code.gitea.io/gitea/models/user"
1718
"code.gitea.io/gitea/modules/git"
18-
"code.gitea.io/gitea/modules/gitrepo"
1919
"code.gitea.io/gitea/modules/htmlutil"
2020
"code.gitea.io/gitea/modules/log"
2121
"code.gitea.io/gitea/modules/markup/markdown"
@@ -121,7 +121,7 @@ func NewComment(ctx *context.Context) {
121121
ctx.ServerError("Unable to load head repo", err)
122122
return
123123
}
124-
if ok := gitrepo.IsBranchExist(ctx, pull.HeadRepo, pull.BaseBranch); !ok {
124+
if exist, _ := git_model.IsBranchExist(ctx, pull.HeadRepo.ID, pull.BaseBranch); !exist {
125125
// todo localize
126126
ctx.JSONError("The origin branch is delete, cannot reopen.")
127127
return

routers/web/repo/issue_view.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import (
2626
user_model "code.gitea.io/gitea/models/user"
2727
"code.gitea.io/gitea/modules/emoji"
2828
"code.gitea.io/gitea/modules/git"
29-
"code.gitea.io/gitea/modules/gitrepo"
3029
"code.gitea.io/gitea/modules/log"
3130
"code.gitea.io/gitea/modules/markup"
3231
"code.gitea.io/gitea/modules/markup/markdown"
@@ -566,8 +565,10 @@ func preparePullViewDeleteBranch(ctx *context.Context, issue *issues_model.Issue
566565
pull := issue.PullRequest
567566
isPullBranchDeletable := canDelete &&
568567
pull.HeadRepo != nil &&
569-
gitrepo.IsBranchExist(ctx, pull.HeadRepo, pull.HeadBranch) &&
570568
(!pull.HasMerged || ctx.Data["HeadBranchCommitID"] == ctx.Data["PullHeadCommitID"])
569+
if isPullBranchDeletable {
570+
isPullBranchDeletable, _ = git_model.IsBranchExist(ctx, pull.HeadRepo.ID, pull.HeadBranch)
571+
}
571572

572573
if isPullBranchDeletable && pull.HasMerged {
573574
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
@@ -358,7 +358,7 @@ func prepareViewPullInfo(ctx *context.Context, issue *issues_model.Issue) *pull_
358358
defer baseGitRepo.Close()
359359
}
360360

361-
if !gitrepo.IsBranchExist(ctx, pull.BaseRepo, pull.BaseBranch) {
361+
if exist, _ := git_model.IsBranchExist(ctx, pull.BaseRepo.ID, pull.BaseBranch); !exist {
362362
ctx.Data["BaseBranchNotExist"] = true
363363
ctx.Data["IsPullRequestBroken"] = true
364364
ctx.Data["BaseTarget"] = pull.BaseBranch
@@ -415,7 +415,7 @@ func prepareViewPullInfo(ctx *context.Context, issue *issues_model.Issue) *pull_
415415
defer closer.Close()
416416

417417
if pull.Flow == issues_model.PullRequestFlowGithub {
418-
headBranchExist = gitrepo.IsBranchExist(ctx, pull.HeadRepo, pull.HeadBranch)
418+
headBranchExist, _ = git_model.IsBranchExist(ctx, pull.HeadRepo.ID, pull.HeadBranch)
419419
} else {
420420
headBranchExist = gitrepo.IsReferenceExist(ctx, pull.BaseRepo, pull.GetGitHeadRefName())
421421
}

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)