Skip to content

Commit 94e0214

Browse files
committed
Remove repo field in branch struct of git module
1 parent 0fee4f1 commit 94e0214

File tree

4 files changed

+31
-43
lines changed

4 files changed

+31
-43
lines changed

models/git/branch.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,18 @@ func GetBranch(ctx context.Context, repoID int64, branchName string) (*Branch, e
173173
return &branch, nil
174174
}
175175

176+
// IsBranchExist returns true if the branch exists in the repository.
177+
func IsBranchExist(ctx context.Context, repoID int64, branchName string) (bool, error) {
178+
var branch Branch
179+
has, err := db.GetEngine(ctx).Where("repo_id=?", repoID).And("name=?", branchName).Get(&branch)
180+
if err != nil {
181+
return false, err
182+
} else if !has {
183+
return false, nil
184+
}
185+
return !branch.IsDeleted, nil
186+
}
187+
176188
func GetBranches(ctx context.Context, repoID int64, branchNames []string, includeDeleted bool) ([]*Branch, error) {
177189
branches := make([]*Branch, 0, len(branchNames))
178190

modules/git/repo_branch.go

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@ func IsBranchExist(ctx context.Context, repoPath, name string) bool {
2929
type Branch struct {
3030
Name string
3131
Path string
32-
33-
gitRepo *Repository
3432
}
3533

3634
// GetHEADBranch returns corresponding branch of HEAD.
@@ -49,9 +47,8 @@ func (repo *Repository) GetHEADBranch() (*Branch, error) {
4947
}
5048

5149
return &Branch{
52-
Name: stdout[len(BranchPrefix):],
53-
Path: stdout,
54-
gitRepo: repo,
50+
Name: stdout[len(BranchPrefix):],
51+
Path: stdout,
5552
}, nil
5653
}
5754

@@ -73,9 +70,8 @@ func (repo *Repository) GetBranch(branch string) (*Branch, error) {
7370
return nil, ErrBranchNotExist{branch}
7471
}
7572
return &Branch{
76-
Path: repo.Path,
77-
Name: branch,
78-
gitRepo: repo,
73+
Path: repo.Path,
74+
Name: branch,
7975
}, nil
8076
}
8177

@@ -89,9 +85,8 @@ func (repo *Repository) GetBranches(skip, limit int) ([]*Branch, int, error) {
8985
branches := make([]*Branch, len(brs))
9086
for i := range brs {
9187
branches[i] = &Branch{
92-
Path: repo.Path,
93-
Name: brs[i],
94-
gitRepo: repo,
88+
Path: repo.Path,
89+
Name: brs[i],
9590
}
9691
}
9792

@@ -147,11 +142,6 @@ func (repo *Repository) RemoveRemote(name string) error {
147142
return err
148143
}
149144

150-
// GetCommit returns the head commit of a branch
151-
func (branch *Branch) GetCommit() (*Commit, error) {
152-
return branch.gitRepo.GetBranchCommit(branch.Name)
153-
}
154-
155145
// RenameBranch rename a branch
156146
func (repo *Repository) RenameBranch(from, to string) error {
157147
_, _, err := NewCommand("branch", "-m").AddDynamicArguments(from, to).RunStdString(repo.Ctx, &RunOpts{Dir: repo.Path})

routers/api/v1/repo/branch.go

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -60,17 +60,16 @@ func GetBranch(ctx *context.APIContext) {
6060

6161
branchName := ctx.PathParam("*")
6262

63-
branch, err := ctx.Repo.GitRepo.GetBranch(branchName)
63+
exist, err := git_model.IsBranchExist(ctx, ctx.Repo.Repository.ID, branchName)
6464
if err != nil {
65-
if git.IsErrBranchNotExist(err) {
66-
ctx.APIErrorNotFound(err)
67-
} else {
68-
ctx.APIErrorInternal(err)
69-
}
65+
ctx.APIErrorInternal(err)
66+
return
67+
} else if !exist {
68+
ctx.APIErrorNotFound(err)
7069
return
7170
}
7271

73-
c, err := branch.GetCommit()
72+
c, err := ctx.Repo.GitRepo.GetBranchCommit(branchName)
7473
if err != nil {
7574
ctx.APIErrorInternal(err)
7675
return
@@ -82,7 +81,7 @@ func GetBranch(ctx *context.APIContext) {
8281
return
8382
}
8483

85-
br, err := convert.ToBranch(ctx, ctx.Repo.Repository, branch.Name, c, branchProtection, ctx.Doer, ctx.Repo.IsAdmin())
84+
br, err := convert.ToBranch(ctx, ctx.Repo.Repository, branchName, c, branchProtection, ctx.Doer, ctx.Repo.IsAdmin())
8685
if err != nil {
8786
ctx.APIErrorInternal(err)
8887
return
@@ -261,25 +260,19 @@ func CreateBranch(ctx *context.APIContext) {
261260
return
262261
}
263262

264-
branch, err := ctx.Repo.GitRepo.GetBranch(opt.BranchName)
265-
if err != nil {
266-
ctx.APIErrorInternal(err)
267-
return
268-
}
269-
270-
commit, err := branch.GetCommit()
263+
commit, err := ctx.Repo.GitRepo.GetBranchCommit(opt.BranchName)
271264
if err != nil {
272265
ctx.APIErrorInternal(err)
273266
return
274267
}
275268

276-
branchProtection, err := git_model.GetFirstMatchProtectedBranchRule(ctx, ctx.Repo.Repository.ID, branch.Name)
269+
branchProtection, err := git_model.GetFirstMatchProtectedBranchRule(ctx, ctx.Repo.Repository.ID, opt.BranchName)
277270
if err != nil {
278271
ctx.APIErrorInternal(err)
279272
return
280273
}
281274

282-
br, err := convert.ToBranch(ctx, ctx.Repo.Repository, branch.Name, commit, branchProtection, ctx.Doer, ctx.Repo.IsAdmin())
275+
br, err := convert.ToBranch(ctx, ctx.Repo.Repository, opt.BranchName, commit, branchProtection, ctx.Doer, ctx.Repo.IsAdmin())
283276
if err != nil {
284277
ctx.APIErrorInternal(err)
285278
return

services/convert/pull.go

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ func ToAPIPullRequest(ctx context.Context, pr *issues_model.PullRequest, doer *u
157157
}
158158

159159
if err == nil {
160-
baseCommit, err = baseBranch.GetCommit()
160+
baseCommit, err = gitRepo.GetBranchCommit(pr.BaseBranch)
161161
if err != nil && !git.IsErrNotExist(err) {
162162
log.Error("GetCommit[%s]: %v", baseBranch.Name, err)
163163
return nil
@@ -169,13 +169,6 @@ func ToAPIPullRequest(ctx context.Context, pr *issues_model.PullRequest, doer *u
169169
}
170170

171171
if pr.Flow == issues_model.PullRequestFlowAGit {
172-
gitRepo, err := gitrepo.OpenRepository(ctx, pr.BaseRepo)
173-
if err != nil {
174-
log.Error("OpenRepository[%s]: %v", pr.GetGitRefName(), err)
175-
return nil
176-
}
177-
defer gitRepo.Close()
178-
179172
apiPullRequest.Head.Sha, err = gitRepo.GetRefCommitID(pr.GetGitRefName())
180173
if err != nil {
181174
log.Error("GetRefCommitID[%s]: %v", pr.GetGitRefName(), err)
@@ -226,7 +219,7 @@ func ToAPIPullRequest(ctx context.Context, pr *issues_model.PullRequest, doer *u
226219
endCommitID = headCommitID
227220
}
228221
} else {
229-
commit, err := headBranch.GetCommit()
222+
commit, err := headGitRepo.GetBranchCommit(pr.HeadBranch)
230223
if err != nil && !git.IsErrNotExist(err) {
231224
log.Error("GetCommit[%s]: %v", headBranch.Name, err)
232225
return nil
@@ -478,7 +471,7 @@ func ToAPIPullRequests(ctx context.Context, baseRepo *repo_model.Repository, prs
478471
apiPullRequest.Head.Sha = headCommitID
479472
}
480473
} else {
481-
commit, err := headBranch.GetCommit()
474+
commit, err := headGitRepo.GetBranchCommit(pr.HeadBranch)
482475
if err != nil && !git.IsErrNotExist(err) {
483476
log.Error("GetCommit[%s]: %v", headBranch.Name, err)
484477
return nil, err

0 commit comments

Comments
 (0)