0
0
Fork 0
mirror of https://github.com/go-gitea/gitea synced 2024-12-04 20:15:02 +01:00

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 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) { func GetBranches(ctx context.Context, repoID int64, branchNames []string) ([]*Branch, error) {
branches := make([]*Branch, 0, len(branchNames)) branches := make([]*Branch, 0, len(branchNames))
return branches, db.GetEngine(ctx).Where("repo_id=?", repoID).In("name", branchNames).Find(&branches) 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("*") 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 err != nil {
if git.IsErrBranchNotExist(err) { if git.IsErrBranchNotExist(err) {
ctx.NotFound(err) ctx.NotFound(err)
@ -68,7 +68,7 @@ func GetBranch(ctx *context.APIContext) {
return return
} }
c, err := branch.GetCommit() c, err := ctx.Repo.GitRepo.GetCommit(branch.CommitID)
if err != nil { if err != nil {
ctx.Error(http.StatusInternalServerError, "GetCommit", err) ctx.Error(http.StatusInternalServerError, "GetCommit", err)
return return
@ -269,13 +269,13 @@ func CreateBranch(ctx *context.APIContext) {
return return
} }
branch, err := ctx.Repo.GitRepo.GetBranch(opt.BranchName) branch, err := git_model.GetNonDeletedBranch(ctx, ctx.Repo.Repository.ID, opt.BranchName)
if err != nil { if err != nil {
ctx.Error(http.StatusInternalServerError, "GetBranch", err) ctx.Error(http.StatusInternalServerError, "GetBranch", err)
return return
} }
commit, err := branch.GetCommit() commit, err := ctx.Repo.GitRepo.GetCommit(branch.CommitID)
if err != nil { if err != nil {
ctx.Error(http.StatusInternalServerError, "GetCommit", err) ctx.Error(http.StatusInternalServerError, "GetCommit", err)
return return

View file

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

View file

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

View file

@ -36,7 +36,7 @@ import (
// CreateNewBranch creates a new repository branch // 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) { 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 { if err != nil {
return err return err
} }

View file

@ -13,7 +13,6 @@ import (
repo_model "code.gitea.io/gitea/models/repo" repo_model "code.gitea.io/gitea/models/repo"
user_model "code.gitea.io/gitea/models/user" user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/gitrepo"
"code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/modules/structs"
asymkey_service "code.gitea.io/gitea/services/asymkey" 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 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 // 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 return err
} }
// A NewBranch can be specified for the patch to be applied to. // 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. // 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 we aren't branching to a new branch, make sure user can commit to the given branch
if opts.NewBranch != opts.OldBranch { if opts.NewBranch != opts.OldBranch {
existingBranch, err := gitRepo.GetBranch(opts.NewBranch) existingBranch, err := git_model.GetNonDeletedBranch(ctx, repo.ID, opts.NewBranch)
if existingBranch != nil { if existingBranch != nil {
return git_model.ErrBranchAlreadyExists{ return git_model.ErrBranchAlreadyExists{
BranchName: opts.NewBranch, BranchName: opts.NewBranch,

View file

@ -86,7 +86,7 @@ func ChangeRepoFiles(ctx context.Context, repo *repo_model.Repository, doer *use
defer closer.Close() defer closer.Close()
// oldBranch must exist for this operation // 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 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. // 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 we aren't branching to a new branch, make sure user can commit to the given branch
if opts.NewBranch != opts.OldBranch { if opts.NewBranch != opts.OldBranch {
existingBranch, err := gitRepo.GetBranch(opts.NewBranch) existingBranch, err := git_model.GetNonDeletedBranch(ctx, repo.ID, opts.NewBranch)
if existingBranch != nil { if existingBranch != nil {
return nil, git_model.ErrBranchAlreadyExists{ return nil, git_model.ErrBranchAlreadyExists{
BranchName: opts.NewBranch, BranchName: opts.NewBranch,