Use git_model.GetBranch instead of GetBranch of disk

This commit is contained in:
Lunny Xiao 2024-11-06 11:48:11 -08:00
parent 41b4ef825d
commit e82fea12d5
No known key found for this signature in database
GPG Key ID: C3B7C91B632F738A
7 changed files with 30 additions and 24 deletions

View File

@ -169,6 +169,20 @@ func GetBranch(ctx context.Context, repoID int64, branchName string) (*Branch, e
return &branch, nil
}
func GetNonDeletedBranch(ctx context.Context, repoID int64, branchName string) (*Branch, error) {
b, err := GetBranch(ctx, repoID, branchName)
if err != nil {
return nil, err
}
if b.IsDeleted {
return nil, ErrBranchNotExist{
RepoID: repoID,
BranchName: branchName,
}
}
return b, nil
}
func GetBranches(ctx context.Context, repoID int64, branchNames []string) ([]*Branch, error) {
branches := make([]*Branch, 0, len(branchNames))
return branches, db.GetEngine(ctx).Where("repo_id=?", repoID).In("name", branchNames).Find(&branches)

View File

@ -58,7 +58,7 @@ func GetBranch(ctx *context.APIContext) {
branchName := ctx.PathParam("*")
branch, err := ctx.Repo.GitRepo.GetBranch(branchName)
branch, err := git_model.GetNonDeletedBranch(ctx, ctx.Repo.Repository.ID, branchName)
if err != nil {
if git.IsErrBranchNotExist(err) {
ctx.NotFound(err)
@ -68,7 +68,7 @@ func GetBranch(ctx *context.APIContext) {
return
}
c, err := branch.GetCommit()
c, err := ctx.Repo.GitRepo.GetCommit(branch.CommitID)
if err != nil {
ctx.Error(http.StatusInternalServerError, "GetCommit", err)
return
@ -269,13 +269,13 @@ func CreateBranch(ctx *context.APIContext) {
return
}
branch, err := ctx.Repo.GitRepo.GetBranch(opt.BranchName)
branch, err := git_model.GetNonDeletedBranch(ctx, ctx.Repo.Repository.ID, opt.BranchName)
if err != nil {
ctx.Error(http.StatusInternalServerError, "GetBranch", err)
return
}
commit, err := branch.GetCommit()
commit, err := ctx.Repo.GitRepo.GetCommit(branch.CommitID)
if err != nil {
ctx.Error(http.StatusInternalServerError, "GetCommit", err)
return

View File

@ -655,7 +655,7 @@ func UploadFilePost(ctx *context.Context) {
}
if oldBranchName != branchName {
if _, err := ctx.Repo.GitRepo.GetBranch(branchName); err == nil {
if _, err := git_model.GetNonDeletedBranch(ctx, ctx.Repo.Repository.ID, branchName); err == nil {
ctx.Data["Err_NewBranchName"] = true
ctx.RenderWithErr(ctx.Tr("repo.editor.branch_already_exists", branchName), tplUploadFile, &form)
return
@ -853,7 +853,7 @@ func GetUniquePatchBranchName(ctx *context.Context) string {
prefix := ctx.Doer.LowerName + "-patch-"
for i := 1; i <= 1000; i++ {
branchName := fmt.Sprintf("%s%d", prefix, i)
if _, err := ctx.Repo.GitRepo.GetBranch(branchName); err != nil {
if _, err := git_model.GetNonDeletedBranch(ctx, ctx.Repo.Repository.ID, branchName); err != nil {
if git.IsErrBranchNotExist(err) {
return branchName
}

View File

@ -7,6 +7,7 @@ import (
"context"
"fmt"
git_model "code.gitea.io/gitea/models/git"
issues_model "code.gitea.io/gitea/models/issues"
"code.gitea.io/gitea/models/perm"
access_model "code.gitea.io/gitea/models/perm/access"
@ -23,8 +24,6 @@ import (
// Optional - Merger
func ToAPIPullRequest(ctx context.Context, pr *issues_model.PullRequest, doer *user_model.User) *api.PullRequest {
var (
baseBranch *git.Branch
headBranch *git.Branch
baseCommit *git.Commit
err error
)
@ -136,14 +135,14 @@ func ToAPIPullRequest(ctx context.Context, pr *issues_model.PullRequest, doer *u
}
defer gitRepo.Close()
baseBranch, err = gitRepo.GetBranch(pr.BaseBranch)
baseBranch, err := git_model.GetNonDeletedBranch(ctx, pr.BaseRepoID, pr.BaseBranch)
if err != nil && !git.IsErrBranchNotExist(err) {
log.Error("GetBranch[%s]: %v", pr.BaseBranch, err)
return nil
}
if err == nil {
baseCommit, err = baseBranch.GetCommit()
baseCommit, err = gitRepo.GetCommit(baseBranch.CommitID)
if err != nil && !git.IsErrNotExist(err) {
log.Error("GetCommit[%s]: %v", baseBranch.Name, err)
return nil
@ -189,7 +188,7 @@ func ToAPIPullRequest(ctx context.Context, pr *issues_model.PullRequest, doer *u
}
defer headGitRepo.Close()
headBranch, err = headGitRepo.GetBranch(pr.HeadBranch)
headBranch, err := git_model.GetNonDeletedBranch(ctx, pr.HeadRepoID, pr.HeadBranch)
if err != nil && !git.IsErrBranchNotExist(err) {
log.Error("GetBranch[%s]: %v", pr.HeadBranch, err)
return nil
@ -212,7 +211,7 @@ func ToAPIPullRequest(ctx context.Context, pr *issues_model.PullRequest, doer *u
endCommitID = headCommitID
}
} else {
commit, err := headBranch.GetCommit()
commit, err := headGitRepo.GetCommit(headBranch.CommitID)
if err != nil && !git.IsErrNotExist(err) {
log.Error("GetCommit[%s]: %v", headBranch.Name, err)
return nil

View File

@ -36,7 +36,7 @@ import (
// CreateNewBranch creates a new repository branch
func CreateNewBranch(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, gitRepo *git.Repository, oldBranchName, branchName string) (err error) {
branch, err := git_model.GetBranch(ctx, repo.ID, oldBranchName)
branch, err := git_model.GetNonDeletedBranch(ctx, repo.ID, oldBranchName)
if err != nil {
return err
}

View File

@ -13,7 +13,6 @@ 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/structs"
asymkey_service "code.gitea.io/gitea/services/asymkey"
@ -43,21 +42,15 @@ func (opts *ApplyDiffPatchOptions) Validate(ctx context.Context, repo *repo_mode
opts.NewBranch = opts.OldBranch
}
gitRepo, closer, err := gitrepo.RepositoryFromContextOrOpen(ctx, repo)
if err != nil {
return err
}
defer closer.Close()
// oldBranch must exist for this operation
if _, err := gitRepo.GetBranch(opts.OldBranch); err != nil {
if _, err := git_model.GetNonDeletedBranch(ctx, repo.ID, opts.OldBranch); err != nil {
return err
}
// A NewBranch can be specified for the patch to be applied to.
// Check to make sure the branch does not already exist, otherwise we can't proceed.
// If we aren't branching to a new branch, make sure user can commit to the given branch
if opts.NewBranch != opts.OldBranch {
existingBranch, err := gitRepo.GetBranch(opts.NewBranch)
existingBranch, err := git_model.GetNonDeletedBranch(ctx, repo.ID, opts.NewBranch)
if existingBranch != nil {
return git_model.ErrBranchAlreadyExists{
BranchName: opts.NewBranch,

View File

@ -86,7 +86,7 @@ func ChangeRepoFiles(ctx context.Context, repo *repo_model.Repository, doer *use
defer closer.Close()
// oldBranch must exist for this operation
if _, err := gitRepo.GetBranch(opts.OldBranch); err != nil && !repo.IsEmpty {
if _, err := git_model.GetNonDeletedBranch(ctx, repo.ID, opts.OldBranch); err != nil && !repo.IsEmpty {
return nil, err
}
@ -124,7 +124,7 @@ func ChangeRepoFiles(ctx context.Context, repo *repo_model.Repository, doer *use
// Check to make sure the branch does not already exist, otherwise we can't proceed.
// If we aren't branching to a new branch, make sure user can commit to the given branch
if opts.NewBranch != opts.OldBranch {
existingBranch, err := gitRepo.GetBranch(opts.NewBranch)
existingBranch, err := git_model.GetNonDeletedBranch(ctx, repo.ID, opts.NewBranch)
if existingBranch != nil {
return nil, git_model.ErrBranchAlreadyExists{
BranchName: opts.NewBranch,