diff --git a/models/git/branch.go b/models/git/branch.go index ba1ada5517..c71ff64fd1 100644 --- a/models/git/branch.go +++ b/models/git/branch.go @@ -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) diff --git a/routers/api/v1/repo/branch.go b/routers/api/v1/repo/branch.go index bb16858c81..c9736443bd 100644 --- a/routers/api/v1/repo/branch.go +++ b/routers/api/v1/repo/branch.go @@ -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 diff --git a/routers/web/repo/editor.go b/routers/web/repo/editor.go index 9396115b0d..34e06b6ed4 100644 --- a/routers/web/repo/editor.go +++ b/routers/web/repo/editor.go @@ -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 } diff --git a/services/convert/pull.go b/services/convert/pull.go index 4ec24a8276..c2ed918c70 100644 --- a/services/convert/pull.go +++ b/services/convert/pull.go @@ -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 diff --git a/services/repository/branch.go b/services/repository/branch.go index 600ba96e92..b771721b8f 100644 --- a/services/repository/branch.go +++ b/services/repository/branch.go @@ -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 } diff --git a/services/repository/files/patch.go b/services/repository/files/patch.go index ab0e7ffd36..f5d36dda1c 100644 --- a/services/repository/files/patch.go +++ b/services/repository/files/patch.go @@ -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, diff --git a/services/repository/files/update.go b/services/repository/files/update.go index b1b64bacd9..33b1af8e8a 100644 --- a/services/repository/files/update.go +++ b/services/repository/files/update.go @@ -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,