Skip to content

Commit 0056fdb

Browse files
authored
Move git references checking to gitrepo packages to reduce expose of repository path (#33891)
1 parent f11ac6b commit 0056fdb

File tree

24 files changed

+79
-46
lines changed

24 files changed

+79
-46
lines changed

modules/git/repo_tag.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
package git
66

77
import (
8-
"context"
98
"fmt"
109
"io"
1110
"strings"
@@ -17,11 +16,6 @@ import (
1716
// TagPrefix tags prefix path on the repository
1817
const TagPrefix = "refs/tags/"
1918

20-
// IsTagExist returns true if given tag exists in the repository.
21-
func IsTagExist(ctx context.Context, repoPath, name string) bool {
22-
return IsReferenceExist(ctx, repoPath, TagPrefix+name)
23-
}
24-
2519
// CreateTag create one tag in the repository
2620
func (repo *Repository) CreateTag(name, revision string) error {
2721
_, _, err := NewCommand("tag").AddDashesAndList(name, revision).RunStdString(repo.Ctx, &RunOpts{Dir: repo.Path})

modules/gitrepo/branch.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,21 @@ func GetDefaultBranch(ctx context.Context, repo Repository) (string, error) {
4747
func GetWikiDefaultBranch(ctx context.Context, repo Repository) (string, error) {
4848
return git.GetDefaultBranch(ctx, wikiPath(repo))
4949
}
50+
51+
// IsReferenceExist returns true if given reference exists in the repository.
52+
func IsReferenceExist(ctx context.Context, repo Repository, name string) bool {
53+
return git.IsReferenceExist(ctx, repoPath(repo), name)
54+
}
55+
56+
func IsWikiReferenceExist(ctx context.Context, repo Repository, name string) bool {
57+
return git.IsReferenceExist(ctx, wikiPath(repo), name)
58+
}
59+
60+
// IsBranchExist returns true if given branch exists in the repository.
61+
func IsBranchExist(ctx context.Context, repo Repository, name string) bool {
62+
return IsReferenceExist(ctx, repo, git.BranchPrefix+name)
63+
}
64+
65+
func IsWikiBranchExist(ctx context.Context, repo Repository, name string) bool {
66+
return IsWikiReferenceExist(ctx, repo, git.BranchPrefix+name)
67+
}

modules/gitrepo/tag.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright 2025 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package gitrepo
5+
6+
import (
7+
"context"
8+
9+
"code.gitea.io/gitea/modules/git"
10+
)
11+
12+
// IsTagExist returns true if given tag exists in the repository.
13+
func IsTagExist(ctx context.Context, repo Repository, name string) bool {
14+
return IsReferenceExist(ctx, repo, git.TagPrefix+name)
15+
}

routers/api/v1/repo/branch.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ func CreateBranch(ctx *context.APIContext) {
227227
return
228228
}
229229
} else if len(opt.OldBranchName) > 0 { //nolint
230-
if ctx.Repo.GitRepo.IsBranchExist(opt.OldBranchName) { //nolint
230+
if gitrepo.IsBranchExist(ctx, ctx.Repo.Repository, opt.OldBranchName) { //nolint
231231
oldCommit, err = ctx.Repo.GitRepo.GetBranchCommit(opt.OldBranchName) //nolint
232232
if err != nil {
233233
ctx.APIErrorInternal(err)
@@ -1019,7 +1019,7 @@ func EditBranchProtection(ctx *context.APIContext) {
10191019
isPlainRule := !git_model.IsRuleNameSpecial(bpName)
10201020
var isBranchExist bool
10211021
if isPlainRule {
1022-
isBranchExist = git.IsBranchExist(ctx.Req.Context(), ctx.Repo.Repository.RepoPath(), bpName)
1022+
isBranchExist = gitrepo.IsBranchExist(ctx, ctx.Repo.Repository, bpName)
10231023
}
10241024

10251025
if isBranchExist {

routers/api/v1/repo/pull.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -742,7 +742,7 @@ func EditPullRequest(ctx *context.APIContext) {
742742

743743
// change pull target branch
744744
if !pr.HasMerged && len(form.Base) != 0 && form.Base != pr.BaseBranch {
745-
if !ctx.Repo.GitRepo.IsBranchExist(form.Base) {
745+
if !gitrepo.IsBranchExist(ctx, ctx.Repo.Repository, form.Base) {
746746
ctx.APIError(http.StatusNotFound, fmt.Errorf("new base '%s' not exist", form.Base))
747747
return
748748
}

routers/api/v1/repo/repo.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -734,7 +734,7 @@ func updateBasicProperties(ctx *context.APIContext, opts api.EditRepoOption) err
734734

735735
// Default branch only updated if changed and exist or the repository is empty
736736
updateRepoLicense := false
737-
if opts.DefaultBranch != nil && repo.DefaultBranch != *opts.DefaultBranch && (repo.IsEmpty || ctx.Repo.GitRepo.IsBranchExist(*opts.DefaultBranch)) {
737+
if opts.DefaultBranch != nil && repo.DefaultBranch != *opts.DefaultBranch && (repo.IsEmpty || gitrepo.IsBranchExist(ctx, ctx.Repo.Repository, *opts.DefaultBranch)) {
738738
if !repo.IsEmpty {
739739
if err := gitrepo.SetDefaultBranch(ctx, ctx.Repo.Repository, *opts.DefaultBranch); err != nil {
740740
ctx.APIErrorInternal(err)

routers/private/hook_pre_receive.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"code.gitea.io/gitea/models/unit"
1717
user_model "code.gitea.io/gitea/models/user"
1818
"code.gitea.io/gitea/modules/git"
19+
"code.gitea.io/gitea/modules/gitrepo"
1920
"code.gitea.io/gitea/modules/log"
2021
"code.gitea.io/gitea/modules/private"
2122
"code.gitea.io/gitea/modules/web"
@@ -447,13 +448,13 @@ func preReceiveFor(ctx *preReceiveContext, refFullName git.RefName) {
447448
baseBranchName := refFullName.ForBranchName()
448449

449450
baseBranchExist := false
450-
if ctx.Repo.GitRepo.IsBranchExist(baseBranchName) {
451+
if gitrepo.IsBranchExist(ctx, ctx.Repo.Repository, baseBranchName) {
451452
baseBranchExist = true
452453
}
453454

454455
if !baseBranchExist {
455456
for p, v := range baseBranchName {
456-
if v == '/' && ctx.Repo.GitRepo.IsBranchExist(baseBranchName[:p]) && p != len(baseBranchName)-1 {
457+
if v == '/' && gitrepo.IsBranchExist(ctx, ctx.Repo.Repository, baseBranchName[:p]) && p != len(baseBranchName)-1 {
457458
baseBranchExist = true
458459
break
459460
}

routers/web/repo/compare.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -301,8 +301,8 @@ func ParseCompareInfo(ctx *context.Context) *common.CompareInfo {
301301

302302
// Check if base branch is valid.
303303
baseIsCommit := ctx.Repo.GitRepo.IsCommitExist(ci.BaseBranch)
304-
baseIsBranch := ctx.Repo.GitRepo.IsBranchExist(ci.BaseBranch)
305-
baseIsTag := ctx.Repo.GitRepo.IsTagExist(ci.BaseBranch)
304+
baseIsBranch := gitrepo.IsBranchExist(ctx, ctx.Repo.Repository, ci.BaseBranch)
305+
baseIsTag := gitrepo.IsTagExist(ctx, ctx.Repo.Repository, ci.BaseBranch)
306306

307307
if !baseIsCommit && !baseIsBranch && !baseIsTag {
308308
// Check if baseBranch is short sha commit hash
@@ -504,8 +504,8 @@ func ParseCompareInfo(ctx *context.Context) *common.CompareInfo {
504504

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

routers/web/repo/issue_comment.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"code.gitea.io/gitea/models/renderhelper"
1414
user_model "code.gitea.io/gitea/models/user"
1515
"code.gitea.io/gitea/modules/git"
16+
"code.gitea.io/gitea/modules/gitrepo"
1617
"code.gitea.io/gitea/modules/log"
1718
"code.gitea.io/gitea/modules/markup/markdown"
1819
repo_module "code.gitea.io/gitea/modules/repository"
@@ -117,7 +118,7 @@ func NewComment(ctx *context.Context) {
117118
ctx.ServerError("Unable to load head repo", err)
118119
return
119120
}
120-
if ok := git.IsBranchExist(ctx, pull.HeadRepo.RepoPath(), pull.BaseBranch); !ok {
121+
if ok := gitrepo.IsBranchExist(ctx, pull.HeadRepo, pull.BaseBranch); !ok {
121122
// todo localize
122123
ctx.JSONError("The origin branch is delete, cannot reopen.")
123124
return

routers/web/repo/issue_view.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
user_model "code.gitea.io/gitea/models/user"
2525
"code.gitea.io/gitea/modules/emoji"
2626
"code.gitea.io/gitea/modules/git"
27+
"code.gitea.io/gitea/modules/gitrepo"
2728
"code.gitea.io/gitea/modules/log"
2829
"code.gitea.io/gitea/modules/markup"
2930
"code.gitea.io/gitea/modules/markup/markdown"
@@ -526,7 +527,7 @@ func preparePullViewDeleteBranch(ctx *context.Context, issue *issues_model.Issue
526527
pull := issue.PullRequest
527528
isPullBranchDeletable := canDelete &&
528529
pull.HeadRepo != nil &&
529-
git.IsBranchExist(ctx, pull.HeadRepo.RepoPath(), pull.HeadBranch) &&
530+
gitrepo.IsBranchExist(ctx, pull.HeadRepo, pull.HeadBranch) &&
530531
(!pull.HasMerged || ctx.Data["HeadBranchCommitID"] == ctx.Data["PullHeadCommitID"])
531532

532533
if isPullBranchDeletable && pull.HasMerged {

0 commit comments

Comments
 (0)