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 /services | |
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 'services')
-rw-r--r-- | services/agit/agit.go | 5 | ||||
-rw-r--r-- | services/automerge/automerge.go | 6 | ||||
-rw-r--r-- | services/context/api.go | 4 | ||||
-rw-r--r-- | services/context/repo.go | 6 | ||||
-rw-r--r-- | services/pull/commit_status.go | 5 | ||||
-rw-r--r-- | services/pull/protected_branch.go | 5 | ||||
-rw-r--r-- | services/pull/pull.go | 2 | ||||
-rw-r--r-- | services/pull/temp_repo.go | 3 | ||||
-rw-r--r-- | services/release/release.go | 2 | ||||
-rw-r--r-- | services/repository/branch.go | 8 | ||||
-rw-r--r-- | services/wiki/wiki.go | 2 |
11 files changed, 25 insertions, 23 deletions
diff --git a/services/agit/agit.go b/services/agit/agit.go index 7d2ccbd0c2..1e6ce93312 100644 --- a/services/agit/agit.go +++ b/services/agit/agit.go @@ -13,6 +13,7 @@ import ( repo_model "code.gitea.io/gitea/models/repo" 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/setting" @@ -56,10 +57,10 @@ func ProcReceive(ctx context.Context, repo *repo_model.Repository, gitRepo *git. baseBranchName := opts.RefFullNames[i].ForBranchName() currentTopicBranch := "" - if !gitRepo.IsBranchExist(baseBranchName) { + if !gitrepo.IsBranchExist(ctx, repo, baseBranchName) { // try match refs/for/<target-branch>/<topic-branch> for p, v := range baseBranchName { - if v == '/' && gitRepo.IsBranchExist(baseBranchName[:p]) && p != len(baseBranchName)-1 { + if v == '/' && gitrepo.IsBranchExist(ctx, repo, baseBranchName[:p]) && p != len(baseBranchName)-1 { currentTopicBranch = baseBranchName[p+1:] baseBranchName = baseBranchName[:p] break diff --git a/services/automerge/automerge.go b/services/automerge/automerge.go index bdb0493ae8..62d560ff94 100644 --- a/services/automerge/automerge.go +++ b/services/automerge/automerge.go @@ -248,13 +248,13 @@ func handlePullRequestAutoMerge(pullID int64, sha string) { switch pr.Flow { case issues_model.PullRequestFlowGithub: - headBranchExist := headGitRepo.IsBranchExist(pr.HeadBranch) - if pr.HeadRepo == nil || !headBranchExist { + headBranchExist := pr.HeadRepo != nil && gitrepo.IsBranchExist(ctx, pr.HeadRepo, 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 } case issues_model.PullRequestFlowAGit: - headBranchExist := git.IsReferenceExist(ctx, baseGitRepo.Path, pr.GetGitRefName()) + headBranchExist := gitrepo.IsReferenceExist(ctx, pr.BaseRepo, pr.GetGitRefName()) if !headBranchExist { log.Warn("Head branch of auto merge %-v does not exist [HeadRepoID: %d, Branch(Agit): %s]", pr, pr.HeadRepoID, pr.HeadBranch) return diff --git a/services/context/api.go b/services/context/api.go index 89280cac80..10fad419ba 100644 --- a/services/context/api.go +++ b/services/context/api.go @@ -304,14 +304,14 @@ func RepoRefForAPI(next http.Handler) http.Handler { refName, _, _ := getRefNameLegacy(ctx.Base, ctx.Repo, ctx.PathParam("*"), ctx.FormTrim("ref")) var err error - if ctx.Repo.GitRepo.IsBranchExist(refName) { + if gitrepo.IsBranchExist(ctx, ctx.Repo.Repository, refName) { ctx.Repo.Commit, err = ctx.Repo.GitRepo.GetBranchCommit(refName) if err != nil { ctx.APIErrorInternal(err) return } ctx.Repo.CommitID = ctx.Repo.Commit.ID.String() - } else if ctx.Repo.GitRepo.IsTagExist(refName) { + } else if gitrepo.IsTagExist(ctx, ctx.Repo.Repository, refName) { ctx.Repo.Commit, err = ctx.Repo.GitRepo.GetTagCommit(refName) if err != nil { ctx.APIErrorInternal(err) diff --git a/services/context/repo.go b/services/context/repo.go index e63e3e5317..6eccd1312a 100644 --- a/services/context/repo.go +++ b/services/context/repo.go @@ -814,7 +814,7 @@ func RepoRefByType(detectRefType git.RefType) func(*Context) { reqPath := ctx.PathParam("*") if reqPath == "" { refShortName = ctx.Repo.Repository.DefaultBranch - if !ctx.Repo.GitRepo.IsBranchExist(refShortName) { + if !gitrepo.IsBranchExist(ctx, ctx.Repo.Repository, refShortName) { brs, _, err := ctx.Repo.GitRepo.GetBranches(0, 1) if err == nil && len(brs) != 0 { refShortName = brs[0].Name @@ -854,7 +854,7 @@ func RepoRefByType(detectRefType git.RefType) func(*Context) { return } - if refType == git.RefTypeBranch && ctx.Repo.GitRepo.IsBranchExist(refShortName) { + if refType == git.RefTypeBranch && gitrepo.IsBranchExist(ctx, ctx.Repo.Repository, refShortName) { ctx.Repo.BranchName = refShortName ctx.Repo.RefFullName = git.RefNameFromBranch(refShortName) @@ -864,7 +864,7 @@ func RepoRefByType(detectRefType git.RefType) func(*Context) { return } ctx.Repo.CommitID = ctx.Repo.Commit.ID.String() - } else if refType == git.RefTypeTag && ctx.Repo.GitRepo.IsTagExist(refShortName) { + } else if refType == git.RefTypeTag && gitrepo.IsTagExist(ctx, ctx.Repo.Repository, refShortName) { ctx.Repo.RefFullName = git.RefNameFromTag(refShortName) ctx.Repo.Commit, err = ctx.Repo.GitRepo.GetTagCommit(refShortName) diff --git a/services/pull/commit_status.go b/services/pull/commit_status.go index aa1ad7cd66..0bfff21746 100644 --- a/services/pull/commit_status.go +++ b/services/pull/commit_status.go @@ -10,7 +10,6 @@ import ( "code.gitea.io/gitea/models/db" git_model "code.gitea.io/gitea/models/git" issues_model "code.gitea.io/gitea/models/issues" - "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/gitrepo" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/structs" @@ -131,10 +130,10 @@ func GetPullRequestCommitStatusState(ctx context.Context, pr *issues_model.PullR } defer closer.Close() - if pr.Flow == issues_model.PullRequestFlowGithub && !headGitRepo.IsBranchExist(pr.HeadBranch) { + if pr.Flow == issues_model.PullRequestFlowGithub && !gitrepo.IsBranchExist(ctx, pr.HeadRepo, pr.HeadBranch) { return "", errors.New("Head branch does not exist, can not merge") } - if pr.Flow == issues_model.PullRequestFlowAGit && !git.IsReferenceExist(ctx, headGitRepo.Path, pr.GetGitRefName()) { + if pr.Flow == issues_model.PullRequestFlowAGit && !gitrepo.IsReferenceExist(ctx, pr.HeadRepo, pr.GetGitRefName()) { return "", errors.New("Head branch does not exist, can not merge") } diff --git a/services/pull/protected_branch.go b/services/pull/protected_branch.go index 5f42629ddc..181bd32f44 100644 --- a/services/pull/protected_branch.go +++ b/services/pull/protected_branch.go @@ -8,7 +8,7 @@ import ( 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" ) func CreateOrUpdateProtectedBranch(ctx context.Context, repo *repo_model.Repository, @@ -22,7 +22,8 @@ func CreateOrUpdateProtectedBranch(ctx context.Context, repo *repo_model.Reposit isPlainRule := !git_model.IsRuleNameSpecial(protectBranch.RuleName) var isBranchExist bool if isPlainRule { - isBranchExist = git.IsBranchExist(ctx, repo.RepoPath(), protectBranch.RuleName) + // TODO: read the database directly to check if the branch exists + isBranchExist = gitrepo.IsBranchExist(ctx, repo, protectBranch.RuleName) } if isBranchExist { diff --git a/services/pull/pull.go b/services/pull/pull.go index 21f31e16cf..4641d4ac40 100644 --- a/services/pull/pull.go +++ b/services/pull/pull.go @@ -491,7 +491,7 @@ func AddTestPullRequestTask(opts TestPullRequestOptions) { for _, pr := range prs { divergence, err := GetDiverging(ctx, pr) if err != nil { - if git_model.IsErrBranchNotExist(err) && !git.IsBranchExist(ctx, pr.HeadRepo.RepoPath(), pr.HeadBranch) { + if git_model.IsErrBranchNotExist(err) && !gitrepo.IsBranchExist(ctx, pr.HeadRepo, pr.HeadBranch) { log.Warn("Cannot test PR %s/%d: head_branch %s no longer exists", pr.BaseRepo.Name, pr.IssueID, pr.HeadBranch) } else { log.Error("GetDiverging: %v", err) diff --git a/services/pull/temp_repo.go b/services/pull/temp_repo.go index 911db85585..3f33370798 100644 --- a/services/pull/temp_repo.go +++ b/services/pull/temp_repo.go @@ -15,6 +15,7 @@ import ( issues_model "code.gitea.io/gitea/models/issues" repo_model "code.gitea.io/gitea/models/repo" "code.gitea.io/gitea/modules/git" + "code.gitea.io/gitea/modules/gitrepo" "code.gitea.io/gitea/modules/log" repo_module "code.gitea.io/gitea/modules/repository" ) @@ -181,7 +182,7 @@ func createTemporaryRepoForPR(ctx context.Context, pr *issues_model.PullRequest) if err := git.NewCommand("fetch").AddArguments(fetchArgs...).AddDynamicArguments(remoteRepoName, headBranch+":"+trackingBranch). Run(ctx, prCtx.RunOpts()); err != nil { cancel() - if !git.IsBranchExist(ctx, pr.HeadRepo.RepoPath(), pr.HeadBranch) { + if !gitrepo.IsBranchExist(ctx, pr.HeadRepo, pr.HeadBranch) { return nil, nil, git_model.ErrBranchNotExist{ BranchName: pr.HeadBranch, } diff --git a/services/release/release.go b/services/release/release.go index d1dbb11ea1..0b8a74252a 100644 --- a/services/release/release.go +++ b/services/release/release.go @@ -77,7 +77,7 @@ func createTag(ctx context.Context, gitRepo *git.Repository, rel *repo_model.Rel var created bool // Only actual create when publish. if !rel.IsDraft { - if !gitRepo.IsTagExist(rel.TagName) { + if !gitrepo.IsTagExist(ctx, rel.Repo, rel.TagName) { if err := rel.LoadAttributes(ctx); err != nil { log.Error("LoadAttributes: %v", err) return false, err diff --git a/services/repository/branch.go b/services/repository/branch.go index d0d8851423..8804778bd5 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(to) { + if gitrepo.IsBranchExist(ctx, repo, to) { return "target_exist", nil } - if !gitRepo.IsBranchExist(from) { + if !gitrepo.IsBranchExist(ctx, repo, from) { return "from_not_exist", nil } @@ -618,12 +618,12 @@ func AddAllRepoBranchesToSyncQueue(ctx context.Context) error { return nil } -func SetRepoDefaultBranch(ctx context.Context, repo *repo_model.Repository, gitRepo *git.Repository, newBranchName string) error { +func SetRepoDefaultBranch(ctx context.Context, repo *repo_model.Repository, newBranchName string) error { if repo.DefaultBranch == newBranchName { return nil } - if !gitRepo.IsBranchExist(newBranchName) { + if !gitrepo.IsBranchExist(ctx, repo, newBranchName) { return git_model.ErrBranchNotExist{ BranchName: newBranchName, } diff --git a/services/wiki/wiki.go b/services/wiki/wiki.go index 66f4456185..413d416b8d 100644 --- a/services/wiki/wiki.go +++ b/services/wiki/wiki.go @@ -100,7 +100,7 @@ func updateWikiPage(ctx context.Context, doer *user_model.User, repo *repo_model return fmt.Errorf("InitWiki: %w", err) } - hasDefaultBranch := git.IsBranchExist(ctx, repo.WikiPath(), repo.DefaultWikiBranch) + hasDefaultBranch := gitrepo.IsWikiBranchExist(ctx, repo, repo.DefaultWikiBranch) basePath, err := repo_module.CreateTemporaryPath("update-wiki") if err != nil { |