diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2025-03-15 19:48:59 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2025-03-15 19:48:59 -0700 |
commit | 0056fdb94201f54fcbb51d741a68b04bf41213fc (patch) | |
tree | 5185b6ad1d8c8560e8cb06cb83a60d6c8fc35620 /routers | |
parent | f11ac6bf3cb45e01080b1ec5bd9cbdd1ee5cda92 (diff) | |
download | gitea-0056fdb94201f54fcbb51d741a68b04bf41213fc.tar.gz gitea-0056fdb94201f54fcbb51d741a68b04bf41213fc.zip |
Move git references checking to gitrepo packages to reduce expose of repository path (#33891)
Diffstat (limited to 'routers')
-rw-r--r-- | routers/api/v1/repo/branch.go | 4 | ||||
-rw-r--r-- | routers/api/v1/repo/pull.go | 2 | ||||
-rw-r--r-- | routers/api/v1/repo/repo.go | 2 | ||||
-rw-r--r-- | routers/private/hook_pre_receive.go | 5 | ||||
-rw-r--r-- | routers/web/repo/compare.go | 8 | ||||
-rw-r--r-- | routers/web/repo/issue_comment.go | 3 | ||||
-rw-r--r-- | routers/web/repo/issue_view.go | 3 | ||||
-rw-r--r-- | routers/web/repo/pull.go | 6 | ||||
-rw-r--r-- | routers/web/repo/release.go | 3 | ||||
-rw-r--r-- | routers/web/repo/setting/default_branch.go | 2 |
10 files changed, 21 insertions, 17 deletions
diff --git a/routers/api/v1/repo/branch.go b/routers/api/v1/repo/branch.go index f866f2048e..9c6e572fb4 100644 --- a/routers/api/v1/repo/branch.go +++ b/routers/api/v1/repo/branch.go @@ -227,7 +227,7 @@ func CreateBranch(ctx *context.APIContext) { return } } else if len(opt.OldBranchName) > 0 { //nolint - if ctx.Repo.GitRepo.IsBranchExist(opt.OldBranchName) { //nolint + if gitrepo.IsBranchExist(ctx, ctx.Repo.Repository, opt.OldBranchName) { //nolint oldCommit, err = ctx.Repo.GitRepo.GetBranchCommit(opt.OldBranchName) //nolint if err != nil { ctx.APIErrorInternal(err) @@ -1019,7 +1019,7 @@ func EditBranchProtection(ctx *context.APIContext) { isPlainRule := !git_model.IsRuleNameSpecial(bpName) var isBranchExist bool if isPlainRule { - isBranchExist = git.IsBranchExist(ctx.Req.Context(), ctx.Repo.Repository.RepoPath(), bpName) + isBranchExist = gitrepo.IsBranchExist(ctx, ctx.Repo.Repository, bpName) } if isBranchExist { diff --git a/routers/api/v1/repo/pull.go b/routers/api/v1/repo/pull.go index 2f32f01c4f..f5d0e37c65 100644 --- a/routers/api/v1/repo/pull.go +++ b/routers/api/v1/repo/pull.go @@ -742,7 +742,7 @@ func EditPullRequest(ctx *context.APIContext) { // change pull target branch if !pr.HasMerged && len(form.Base) != 0 && form.Base != pr.BaseBranch { - if !ctx.Repo.GitRepo.IsBranchExist(form.Base) { + if !gitrepo.IsBranchExist(ctx, ctx.Repo.Repository, form.Base) { ctx.APIError(http.StatusNotFound, fmt.Errorf("new base '%s' not exist", form.Base)) return } diff --git a/routers/api/v1/repo/repo.go b/routers/api/v1/repo/repo.go index 5ef510fd84..3d638cb05e 100644 --- a/routers/api/v1/repo/repo.go +++ b/routers/api/v1/repo/repo.go @@ -734,7 +734,7 @@ func updateBasicProperties(ctx *context.APIContext, opts api.EditRepoOption) err // Default branch only updated if changed and exist or the repository is empty updateRepoLicense := false - if opts.DefaultBranch != nil && repo.DefaultBranch != *opts.DefaultBranch && (repo.IsEmpty || ctx.Repo.GitRepo.IsBranchExist(*opts.DefaultBranch)) { + if opts.DefaultBranch != nil && repo.DefaultBranch != *opts.DefaultBranch && (repo.IsEmpty || gitrepo.IsBranchExist(ctx, ctx.Repo.Repository, *opts.DefaultBranch)) { if !repo.IsEmpty { if err := gitrepo.SetDefaultBranch(ctx, ctx.Repo.Repository, *opts.DefaultBranch); err != nil { ctx.APIErrorInternal(err) diff --git a/routers/private/hook_pre_receive.go b/routers/private/hook_pre_receive.go index c64033d9e2..ae23abc542 100644 --- a/routers/private/hook_pre_receive.go +++ b/routers/private/hook_pre_receive.go @@ -16,6 +16,7 @@ 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/log" "code.gitea.io/gitea/modules/private" "code.gitea.io/gitea/modules/web" @@ -447,13 +448,13 @@ func preReceiveFor(ctx *preReceiveContext, refFullName git.RefName) { baseBranchName := refFullName.ForBranchName() baseBranchExist := false - if ctx.Repo.GitRepo.IsBranchExist(baseBranchName) { + if gitrepo.IsBranchExist(ctx, ctx.Repo.Repository, baseBranchName) { baseBranchExist = true } if !baseBranchExist { for p, v := range baseBranchName { - if v == '/' && ctx.Repo.GitRepo.IsBranchExist(baseBranchName[:p]) && p != len(baseBranchName)-1 { + if v == '/' && gitrepo.IsBranchExist(ctx, ctx.Repo.Repository, baseBranchName[:p]) && p != len(baseBranchName)-1 { baseBranchExist = true break } diff --git a/routers/web/repo/compare.go b/routers/web/repo/compare.go index a01cd753bf..6cea95e387 100644 --- a/routers/web/repo/compare.go +++ b/routers/web/repo/compare.go @@ -301,8 +301,8 @@ func ParseCompareInfo(ctx *context.Context) *common.CompareInfo { // Check if base branch is valid. baseIsCommit := ctx.Repo.GitRepo.IsCommitExist(ci.BaseBranch) - baseIsBranch := ctx.Repo.GitRepo.IsBranchExist(ci.BaseBranch) - baseIsTag := ctx.Repo.GitRepo.IsTagExist(ci.BaseBranch) + baseIsBranch := gitrepo.IsBranchExist(ctx, ctx.Repo.Repository, ci.BaseBranch) + baseIsTag := gitrepo.IsTagExist(ctx, ctx.Repo.Repository, ci.BaseBranch) if !baseIsCommit && !baseIsBranch && !baseIsTag { // Check if baseBranch is short sha commit hash @@ -504,8 +504,8 @@ func ParseCompareInfo(ctx *context.Context) *common.CompareInfo { // Check if head branch is valid. headIsCommit := ci.HeadGitRepo.IsCommitExist(ci.HeadBranch) - headIsBranch := ci.HeadGitRepo.IsBranchExist(ci.HeadBranch) - headIsTag := ci.HeadGitRepo.IsTagExist(ci.HeadBranch) + headIsBranch := gitrepo.IsBranchExist(ctx, ci.HeadRepo, ci.HeadBranch) + headIsTag := gitrepo.IsTagExist(ctx, ci.HeadRepo, ci.HeadBranch) if !headIsCommit && !headIsBranch && !headIsTag { // Check if headBranch is short sha commit hash if headCommit, _ := ci.HeadGitRepo.GetCommit(ci.HeadBranch); headCommit != nil { diff --git a/routers/web/repo/issue_comment.go b/routers/web/repo/issue_comment.go index bc84950701..45463200f6 100644 --- a/routers/web/repo/issue_comment.go +++ b/routers/web/repo/issue_comment.go @@ -13,6 +13,7 @@ import ( "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" @@ -117,7 +118,7 @@ func NewComment(ctx *context.Context) { ctx.ServerError("Unable to load head repo", err) return } - if ok := git.IsBranchExist(ctx, pull.HeadRepo.RepoPath(), pull.BaseBranch); !ok { + if ok := gitrepo.IsBranchExist(ctx, pull.HeadRepo, pull.BaseBranch); !ok { // 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 37e1b27931..b312f1260a 100644 --- a/routers/web/repo/issue_view.go +++ b/routers/web/repo/issue_view.go @@ -24,6 +24,7 @@ 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" @@ -526,7 +527,7 @@ func preparePullViewDeleteBranch(ctx *context.Context, issue *issues_model.Issue pull := issue.PullRequest isPullBranchDeletable := canDelete && pull.HeadRepo != nil && - git.IsBranchExist(ctx, pull.HeadRepo.RepoPath(), pull.HeadBranch) && + gitrepo.IsBranchExist(ctx, pull.HeadRepo, pull.HeadBranch) && (!pull.HasMerged || ctx.Data["HeadBranchCommitID"] == ctx.Data["PullHeadCommitID"]) if isPullBranchDeletable && pull.HasMerged { diff --git a/routers/web/repo/pull.go b/routers/web/repo/pull.go index 9eb6917617..e12798f93d 100644 --- a/routers/web/repo/pull.go +++ b/routers/web/repo/pull.go @@ -347,7 +347,7 @@ func prepareViewPullInfo(ctx *context.Context, issue *issues_model.Issue) *git.C defer baseGitRepo.Close() } - if !baseGitRepo.IsBranchExist(pull.BaseBranch) { + if !gitrepo.IsBranchExist(ctx, pull.BaseRepo, pull.BaseBranch) { ctx.Data["BaseBranchNotExist"] = true ctx.Data["IsPullRequestBroken"] = true ctx.Data["BaseTarget"] = pull.BaseBranch @@ -404,9 +404,9 @@ func prepareViewPullInfo(ctx *context.Context, issue *issues_model.Issue) *git.C defer closer.Close() if pull.Flow == issues_model.PullRequestFlowGithub { - headBranchExist = headGitRepo.IsBranchExist(pull.HeadBranch) + headBranchExist = gitrepo.IsBranchExist(ctx, pull.HeadRepo, pull.HeadBranch) } else { - headBranchExist = git.IsReferenceExist(ctx, baseGitRepo.Path, pull.GetGitRefName()) + headBranchExist = gitrepo.IsReferenceExist(ctx, pull.BaseRepo, pull.GetGitRefName()) } if headBranchExist { diff --git a/routers/web/repo/release.go b/routers/web/repo/release.go index 2ad0bf7e3a..553bdbf6e5 100644 --- a/routers/web/repo/release.go +++ b/routers/web/repo/release.go @@ -17,6 +17,7 @@ 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" @@ -420,7 +421,7 @@ func NewReleasePost(ctx *context.Context) { return } - if !ctx.Repo.GitRepo.IsBranchExist(form.Target) { + if !gitrepo.IsBranchExist(ctx, ctx.Repo.Repository, form.Target) { ctx.RenderWithErr(ctx.Tr("form.target_branch_not_exist"), tplReleaseNew, &form) return } diff --git a/routers/web/repo/setting/default_branch.go b/routers/web/repo/setting/default_branch.go index e0822ba540..044c9e556a 100644 --- a/routers/web/repo/setting/default_branch.go +++ b/routers/web/repo/setting/default_branch.go @@ -34,7 +34,7 @@ func SetDefaultBranchPost(ctx *context.Context) { } branch := ctx.FormString("branch") - if err := repo_service.SetRepoDefaultBranch(ctx, ctx.Repo.Repository, ctx.Repo.GitRepo, branch); err != nil { + if err := repo_service.SetRepoDefaultBranch(ctx, ctx.Repo.Repository, branch); err != nil { switch { case git_model.IsErrBranchNotExist(err): ctx.Status(http.StatusNotFound) |