Skip to content

Commit e82fea1

Browse files
committed
Use git_model.GetBranch instead of GetBranch of disk
1 parent 41b4ef8 commit e82fea1

File tree

7 files changed

+30
-24
lines changed

7 files changed

+30
-24
lines changed

models/git/branch.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,20 @@ func GetBranch(ctx context.Context, repoID int64, branchName string) (*Branch, e
169169
return &branch, nil
170170
}
171171

172+
func GetNonDeletedBranch(ctx context.Context, repoID int64, branchName string) (*Branch, error) {
173+
b, err := GetBranch(ctx, repoID, branchName)
174+
if err != nil {
175+
return nil, err
176+
}
177+
if b.IsDeleted {
178+
return nil, ErrBranchNotExist{
179+
RepoID: repoID,
180+
BranchName: branchName,
181+
}
182+
}
183+
return b, nil
184+
}
185+
172186
func GetBranches(ctx context.Context, repoID int64, branchNames []string) ([]*Branch, error) {
173187
branches := make([]*Branch, 0, len(branchNames))
174188
return branches, db.GetEngine(ctx).Where("repo_id=?", repoID).In("name", branchNames).Find(&branches)

routers/api/v1/repo/branch.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func GetBranch(ctx *context.APIContext) {
5858

5959
branchName := ctx.PathParam("*")
6060

61-
branch, err := ctx.Repo.GitRepo.GetBranch(branchName)
61+
branch, err := git_model.GetNonDeletedBranch(ctx, ctx.Repo.Repository.ID, branchName)
6262
if err != nil {
6363
if git.IsErrBranchNotExist(err) {
6464
ctx.NotFound(err)
@@ -68,7 +68,7 @@ func GetBranch(ctx *context.APIContext) {
6868
return
6969
}
7070

71-
c, err := branch.GetCommit()
71+
c, err := ctx.Repo.GitRepo.GetCommit(branch.CommitID)
7272
if err != nil {
7373
ctx.Error(http.StatusInternalServerError, "GetCommit", err)
7474
return
@@ -269,13 +269,13 @@ func CreateBranch(ctx *context.APIContext) {
269269
return
270270
}
271271

272-
branch, err := ctx.Repo.GitRepo.GetBranch(opt.BranchName)
272+
branch, err := git_model.GetNonDeletedBranch(ctx, ctx.Repo.Repository.ID, opt.BranchName)
273273
if err != nil {
274274
ctx.Error(http.StatusInternalServerError, "GetBranch", err)
275275
return
276276
}
277277

278-
commit, err := branch.GetCommit()
278+
commit, err := ctx.Repo.GitRepo.GetCommit(branch.CommitID)
279279
if err != nil {
280280
ctx.Error(http.StatusInternalServerError, "GetCommit", err)
281281
return

routers/web/repo/editor.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -655,7 +655,7 @@ func UploadFilePost(ctx *context.Context) {
655655
}
656656

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

services/convert/pull.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"context"
88
"fmt"
99

10+
git_model "code.gitea.io/gitea/models/git"
1011
issues_model "code.gitea.io/gitea/models/issues"
1112
"code.gitea.io/gitea/models/perm"
1213
access_model "code.gitea.io/gitea/models/perm/access"
@@ -23,8 +24,6 @@ import (
2324
// Optional - Merger
2425
func ToAPIPullRequest(ctx context.Context, pr *issues_model.PullRequest, doer *user_model.User) *api.PullRequest {
2526
var (
26-
baseBranch *git.Branch
27-
headBranch *git.Branch
2827
baseCommit *git.Commit
2928
err error
3029
)
@@ -136,14 +135,14 @@ func ToAPIPullRequest(ctx context.Context, pr *issues_model.PullRequest, doer *u
136135
}
137136
defer gitRepo.Close()
138137

139-
baseBranch, err = gitRepo.GetBranch(pr.BaseBranch)
138+
baseBranch, err := git_model.GetNonDeletedBranch(ctx, pr.BaseRepoID, pr.BaseBranch)
140139
if err != nil && !git.IsErrBranchNotExist(err) {
141140
log.Error("GetBranch[%s]: %v", pr.BaseBranch, err)
142141
return nil
143142
}
144143

145144
if err == nil {
146-
baseCommit, err = baseBranch.GetCommit()
145+
baseCommit, err = gitRepo.GetCommit(baseBranch.CommitID)
147146
if err != nil && !git.IsErrNotExist(err) {
148147
log.Error("GetCommit[%s]: %v", baseBranch.Name, err)
149148
return nil
@@ -189,7 +188,7 @@ func ToAPIPullRequest(ctx context.Context, pr *issues_model.PullRequest, doer *u
189188
}
190189
defer headGitRepo.Close()
191190

192-
headBranch, err = headGitRepo.GetBranch(pr.HeadBranch)
191+
headBranch, err := git_model.GetNonDeletedBranch(ctx, pr.HeadRepoID, pr.HeadBranch)
193192
if err != nil && !git.IsErrBranchNotExist(err) {
194193
log.Error("GetBranch[%s]: %v", pr.HeadBranch, err)
195194
return nil
@@ -212,7 +211,7 @@ func ToAPIPullRequest(ctx context.Context, pr *issues_model.PullRequest, doer *u
212211
endCommitID = headCommitID
213212
}
214213
} else {
215-
commit, err := headBranch.GetCommit()
214+
commit, err := headGitRepo.GetCommit(headBranch.CommitID)
216215
if err != nil && !git.IsErrNotExist(err) {
217216
log.Error("GetCommit[%s]: %v", headBranch.Name, err)
218217
return nil

services/repository/branch.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ import (
3636

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

services/repository/files/patch.go

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313
repo_model "code.gitea.io/gitea/models/repo"
1414
user_model "code.gitea.io/gitea/models/user"
1515
"code.gitea.io/gitea/modules/git"
16-
"code.gitea.io/gitea/modules/gitrepo"
1716
"code.gitea.io/gitea/modules/log"
1817
"code.gitea.io/gitea/modules/structs"
1918
asymkey_service "code.gitea.io/gitea/services/asymkey"
@@ -43,21 +42,15 @@ func (opts *ApplyDiffPatchOptions) Validate(ctx context.Context, repo *repo_mode
4342
opts.NewBranch = opts.OldBranch
4443
}
4544

46-
gitRepo, closer, err := gitrepo.RepositoryFromContextOrOpen(ctx, repo)
47-
if err != nil {
48-
return err
49-
}
50-
defer closer.Close()
51-
5245
// oldBranch must exist for this operation
53-
if _, err := gitRepo.GetBranch(opts.OldBranch); err != nil {
46+
if _, err := git_model.GetNonDeletedBranch(ctx, repo.ID, opts.OldBranch); err != nil {
5447
return err
5548
}
5649
// A NewBranch can be specified for the patch to be applied to.
5750
// Check to make sure the branch does not already exist, otherwise we can't proceed.
5851
// If we aren't branching to a new branch, make sure user can commit to the given branch
5952
if opts.NewBranch != opts.OldBranch {
60-
existingBranch, err := gitRepo.GetBranch(opts.NewBranch)
53+
existingBranch, err := git_model.GetNonDeletedBranch(ctx, repo.ID, opts.NewBranch)
6154
if existingBranch != nil {
6255
return git_model.ErrBranchAlreadyExists{
6356
BranchName: opts.NewBranch,

services/repository/files/update.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ func ChangeRepoFiles(ctx context.Context, repo *repo_model.Repository, doer *use
8686
defer closer.Close()
8787

8888
// oldBranch must exist for this operation
89-
if _, err := gitRepo.GetBranch(opts.OldBranch); err != nil && !repo.IsEmpty {
89+
if _, err := git_model.GetNonDeletedBranch(ctx, repo.ID, opts.OldBranch); err != nil && !repo.IsEmpty {
9090
return nil, err
9191
}
9292

@@ -124,7 +124,7 @@ func ChangeRepoFiles(ctx context.Context, repo *repo_model.Repository, doer *use
124124
// Check to make sure the branch does not already exist, otherwise we can't proceed.
125125
// If we aren't branching to a new branch, make sure user can commit to the given branch
126126
if opts.NewBranch != opts.OldBranch {
127-
existingBranch, err := gitRepo.GetBranch(opts.NewBranch)
127+
existingBranch, err := git_model.GetNonDeletedBranch(ctx, repo.ID, opts.NewBranch)
128128
if existingBranch != nil {
129129
return nil, git_model.ErrBranchAlreadyExists{
130130
BranchName: opts.NewBranch,

0 commit comments

Comments
 (0)