Skip to content

Commit f5da7ed

Browse files
committed
Remvoe unnecessary GetBranch method
1 parent 570e4d9 commit f5da7ed

File tree

7 files changed

+40
-49
lines changed

7 files changed

+40
-49
lines changed

modules/git/repo_branch.go

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ func IsBranchExist(ctx context.Context, repoPath, name string) bool {
2525
return IsReferenceExist(ctx, repoPath, BranchPrefix+name)
2626
}
2727

28-
// GetHEADBranch returns corresponding branch of HEAD.
29-
func (repo *Repository) GetHEADBranch() (string, error) {
28+
// GetHEADBranchName returns corresponding branch of HEAD.
29+
func (repo *Repository) GetHEADBranchName() (string, error) {
3030
if repo == nil {
3131
return "", fmt.Errorf("nil repo")
3232
}
@@ -55,14 +55,6 @@ func GetDefaultBranch(ctx context.Context, repoPath string) (string, error) {
5555
return strings.TrimPrefix(stdout, BranchPrefix), nil
5656
}
5757

58-
// GetBranch returns a branch by it's name
59-
func (repo *Repository) GetBranch(branch string) (string, error) {
60-
if !repo.IsBranchExist(branch) {
61-
return "", ErrBranchNotExist{branch}
62-
}
63-
return branch, nil
64-
}
65-
6658
// DeleteBranchOptions Option(s) for delete branch
6759
type DeleteBranchOptions struct {
6860
Force bool

routers/api/v1/repo/commits.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,13 +180,13 @@ func GetAllCommits(ctx *context.APIContext) {
180180
var baseCommit *git.Commit
181181
if len(sha) == 0 {
182182
// no sha supplied - use default branch
183-
head, err := ctx.Repo.GitRepo.GetHEADBranch()
183+
headBranchName, err := ctx.Repo.GitRepo.GetHEADBranchName()
184184
if err != nil {
185185
ctx.APIErrorInternal(err)
186186
return
187187
}
188188

189-
baseCommit, err = ctx.Repo.GitRepo.GetBranchCommit(head)
189+
baseCommit, err = ctx.Repo.GitRepo.GetBranchCommit(headBranchName)
190190
if err != nil {
191191
ctx.APIErrorInternal(err)
192192
return

routers/web/repo/editor.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -668,7 +668,7 @@ func UploadFilePost(ctx *context.Context) {
668668
}
669669

670670
if oldBranchName != branchName {
671-
if _, err := ctx.Repo.GitRepo.GetBranch(branchName); err == nil {
671+
if exist, err := git_model.IsBranchExist(ctx, ctx.Repo.Repository.ID, branchName); err == nil && exist {
672672
ctx.Data["Err_NewBranchName"] = true
673673
ctx.RenderWithErr(ctx.Tr("repo.editor.branch_already_exists", branchName), tplUploadFile, &form)
674674
return
@@ -875,12 +875,11 @@ func GetUniquePatchBranchName(ctx *context.Context) string {
875875
prefix := ctx.Doer.LowerName + "-patch-"
876876
for i := 1; i <= 1000; i++ {
877877
branchName := fmt.Sprintf("%s%d", prefix, i)
878-
if _, err := ctx.Repo.GitRepo.GetBranch(branchName); err != nil {
879-
if git.IsErrBranchNotExist(err) {
880-
return branchName
881-
}
878+
if exist, err := git_model.IsBranchExist(ctx, ctx.Repo.Repository.ID, branchName); err != nil {
882879
log.Error("GetUniquePatchBranchName: %v", err)
883880
return ""
881+
} else if !exist {
882+
return branchName
884883
}
885884
}
886885
return ""

services/convert/pull.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -150,13 +150,13 @@ func ToAPIPullRequest(ctx context.Context, pr *issues_model.PullRequest, doer *u
150150
}
151151
defer gitRepo.Close()
152152

153-
baseBranch, err = gitRepo.GetBranch(pr.BaseBranch)
154-
if err != nil && !git.IsErrBranchNotExist(err) {
153+
exist, err := git_model.IsBranchExist(ctx, pr.BaseRepoID, pr.BaseBranch)
154+
if err != nil {
155155
log.Error("GetBranch[%s]: %v", pr.BaseBranch, err)
156156
return nil
157157
}
158158

159-
if err == nil {
159+
if exist {
160160
baseCommit, err = gitRepo.GetBranchCommit(pr.BaseBranch)
161161
if err != nil && !git.IsErrNotExist(err) {
162162
log.Error("GetCommit[%s]: %v", baseBranch, err)
@@ -196,8 +196,8 @@ func ToAPIPullRequest(ctx context.Context, pr *issues_model.PullRequest, doer *u
196196
}
197197
defer headGitRepo.Close()
198198

199-
headBranch, err = headGitRepo.GetBranch(pr.HeadBranch)
200-
if err != nil && !git.IsErrBranchNotExist(err) {
199+
exist, err = git_model.IsBranchExist(ctx, pr.HeadRepoID, pr.HeadBranch)
200+
if err != nil {
201201
log.Error("GetBranch[%s]: %v", pr.HeadBranch, err)
202202
return nil
203203
}
@@ -208,7 +208,7 @@ func ToAPIPullRequest(ctx context.Context, pr *issues_model.PullRequest, doer *u
208208
endCommitID string
209209
)
210210

211-
if git.IsErrBranchNotExist(err) {
211+
if !exist {
212212
headCommitID, err := headGitRepo.GetRefCommitID(apiPullRequest.Head.Ref)
213213
if err != nil && !git.IsErrNotExist(err) {
214214
log.Error("GetCommit[%s]: %v", pr.HeadBranch, err)
@@ -455,13 +455,13 @@ func ToAPIPullRequests(ctx context.Context, baseRepo *repo_model.Repository, prs
455455
defer headGitRepo.Close()
456456
}
457457

458-
headBranch, err := headGitRepo.GetBranch(pr.HeadBranch)
459-
if err != nil && !git.IsErrBranchNotExist(err) {
458+
exist, err := git_model.IsBranchExist(ctx, pr.HeadRepoID, pr.HeadBranch)
459+
if err != nil {
460460
log.Error("GetBranch[%s]: %v", pr.HeadBranch, err)
461461
return nil, err
462462
}
463463

464-
if git.IsErrBranchNotExist(err) {
464+
if !exist {
465465
headCommitID, err := headGitRepo.GetRefCommitID(apiPullRequest.Head.Ref)
466466
if err != nil && !git.IsErrNotExist(err) {
467467
log.Error("GetCommit[%s]: %v", pr.HeadBranch, err)
@@ -473,7 +473,7 @@ func ToAPIPullRequests(ctx context.Context, baseRepo *repo_model.Repository, prs
473473
} else {
474474
commit, err := headGitRepo.GetBranchCommit(pr.HeadBranch)
475475
if err != nil && !git.IsErrNotExist(err) {
476-
log.Error("GetCommit[%s]: %v", headBranch, err)
476+
log.Error("GetCommit[%s]: %v", pr.HeadBranch, err)
477477
return nil, err
478478
}
479479
if err == nil {

services/repository/files/patch.go

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
repo_model "code.gitea.io/gitea/models/repo"
1313
user_model "code.gitea.io/gitea/models/user"
1414
"code.gitea.io/gitea/modules/git"
15-
"code.gitea.io/gitea/modules/gitrepo"
1615
"code.gitea.io/gitea/modules/log"
1716
"code.gitea.io/gitea/modules/structs"
1817
"code.gitea.io/gitea/modules/util"
@@ -62,29 +61,26 @@ func (opts *ApplyDiffPatchOptions) Validate(ctx context.Context, repo *repo_mode
6261
opts.NewBranch = opts.OldBranch
6362
}
6463

65-
gitRepo, closer, err := gitrepo.RepositoryFromContextOrOpen(ctx, repo)
66-
if err != nil {
67-
return err
68-
}
69-
defer closer.Close()
70-
7164
// oldBranch must exist for this operation
72-
if _, err := gitRepo.GetBranch(opts.OldBranch); err != nil {
65+
if exist, err := git_model.IsBranchExist(ctx, repo.ID, opts.OldBranch); err != nil {
7366
return err
67+
} else if !exist {
68+
return git_model.ErrBranchNotExist{
69+
BranchName: opts.OldBranch,
70+
}
7471
}
7572
// A NewBranch can be specified for the patch to be applied to.
7673
// Check to make sure the branch does not already exist, otherwise we can't proceed.
7774
// If we aren't branching to a new branch, make sure user can commit to the given branch
7875
if opts.NewBranch != opts.OldBranch {
79-
existingBranch, err := gitRepo.GetBranch(opts.NewBranch)
80-
if existingBranch != "" {
76+
exist, err := git_model.IsBranchExist(ctx, repo.ID, opts.NewBranch)
77+
if err != nil {
78+
return err
79+
} else if exist {
8180
return git_model.ErrBranchAlreadyExists{
8281
BranchName: opts.NewBranch,
8382
}
8483
}
85-
if err != nil && !git.IsErrBranchNotExist(err) {
86-
return err
87-
}
8884
} else {
8985
protectedBranch, err := git_model.GetFirstMatchProtectedBranchRule(ctx, repo.ID, opts.OldBranch)
9086
if err != nil {

services/repository/files/update.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,12 @@ func ChangeRepoFiles(ctx context.Context, repo *repo_model.Repository, doer *use
107107
defer closer.Close()
108108

109109
// oldBranch must exist for this operation
110-
if _, err := gitRepo.GetBranch(opts.OldBranch); err != nil && !repo.IsEmpty {
110+
if exist, err := git_model.IsBranchExist(ctx, repo.ID, opts.OldBranch); err != nil {
111111
return nil, err
112+
} else if !exist && !repo.IsEmpty {
113+
return nil, git_model.ErrBranchNotExist{
114+
BranchName: opts.OldBranch,
115+
}
112116
}
113117

114118
var treePaths []string
@@ -145,15 +149,15 @@ func ChangeRepoFiles(ctx context.Context, repo *repo_model.Repository, doer *use
145149
// Check to make sure the branch does not already exist, otherwise we can't proceed.
146150
// If we aren't branching to a new branch, make sure user can commit to the given branch
147151
if opts.NewBranch != opts.OldBranch {
148-
existingBranch, err := gitRepo.GetBranch(opts.NewBranch)
149-
if existingBranch != "" {
152+
exist, err := git_model.IsBranchExist(ctx, repo.ID, opts.NewBranch)
153+
if err != nil {
154+
return nil, err
155+
}
156+
if exist {
150157
return nil, git_model.ErrBranchAlreadyExists{
151158
BranchName: opts.NewBranch,
152159
}
153160
}
154-
if err != nil && !git.IsErrBranchNotExist(err) {
155-
return nil, err
156-
}
157161
} else if err := VerifyBranchProtection(ctx, repo, doer, opts.OldBranch, treePaths); err != nil {
158162
return nil, err
159163
}

services/repository/migrate.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,12 +142,12 @@ func MigrateRepositoryGitData(ctx context.Context, u *user_model.User,
142142
if !repo.IsEmpty {
143143
if len(repo.DefaultBranch) == 0 {
144144
// Try to get HEAD branch and set it as default branch.
145-
headBranch, err := gitRepo.GetHEADBranch()
145+
headBranchName, err := gitRepo.GetHEADBranchName()
146146
if err != nil {
147147
return repo, fmt.Errorf("GetHEADBranch: %w", err)
148148
}
149-
if headBranch != "" {
150-
repo.DefaultBranch = headBranch
149+
if headBranchName != "" {
150+
repo.DefaultBranch = headBranchName
151151
}
152152
}
153153

0 commit comments

Comments
 (0)