Skip to content

Commit c27d87a

Browse files
authored
Refactor Branch struct in package modules/git (#33980)
The `Branch` struct in `modules/git` package is unnecessary. We can just use a `string` to represent a branch
1 parent 285950a commit c27d87a

File tree

16 files changed

+169
-147
lines changed

16 files changed

+169
-147
lines changed

models/fixtures/branch.yml

+108
Original file line numberDiff line numberDiff line change
@@ -93,3 +93,111 @@
9393
is_deleted: false
9494
deleted_by_id: 0
9595
deleted_unix: 0
96+
97+
-
98+
id: 16
99+
repo_id: 16
100+
name: 'master'
101+
commit_id: '69554a64c1e6030f051e5c3f94bfbd773cd6a324'
102+
commit_message: 'not signed commit'
103+
commit_time: 1502042309
104+
pusher_id: 2
105+
is_deleted: false
106+
deleted_by_id: 0
107+
deleted_unix: 0
108+
109+
-
110+
id: 17
111+
repo_id: 16
112+
name: 'not-signed'
113+
commit_id: '69554a64c1e6030f051e5c3f94bfbd773cd6a324'
114+
commit_message: 'not signed commit'
115+
commit_time: 1502042309
116+
pusher_id: 2
117+
is_deleted: false
118+
deleted_by_id: 0
119+
deleted_unix: 0
120+
121+
-
122+
id: 18
123+
repo_id: 16
124+
name: 'good-sign-not-yet-validated'
125+
commit_id: '27566bd5738fc8b4e3fef3c5e72cce608537bd95'
126+
commit_message: 'good signed commit (with not yet validated email)'
127+
commit_time: 1502042234
128+
pusher_id: 2
129+
is_deleted: false
130+
deleted_by_id: 0
131+
deleted_unix: 0
132+
133+
-
134+
id: 19
135+
repo_id: 16
136+
name: 'good-sign'
137+
commit_id: 'f27c2b2b03dcab38beaf89b0ab4ff61f6de63441'
138+
commit_message: 'good signed commit'
139+
commit_time: 1502042101
140+
pusher_id: 2
141+
is_deleted: false
142+
deleted_by_id: 0
143+
deleted_unix: 0
144+
145+
-
146+
id: 20
147+
repo_id: 1
148+
name: 'feature/1'
149+
commit_id: '65f1bf27bc3bf70f64657658635e66094edbcb4d'
150+
commit_message: 'Initial commit'
151+
commit_time: 1489950479
152+
pusher_id: 2
153+
is_deleted: false
154+
deleted_by_id: 0
155+
deleted_unix: 0
156+
157+
-
158+
id: 21
159+
repo_id: 49
160+
name: 'master'
161+
commit_id: 'aacbdfe9e1c4b47f60abe81849045fa4e96f1d75'
162+
commit_message: "Add 'test/test.txt'"
163+
commit_time: 1572535577
164+
pusher_id: 2
165+
is_deleted: false
166+
deleted_by_id: 0
167+
deleted_unix: 0
168+
169+
-
170+
id: 22
171+
repo_id: 1
172+
name: 'develop'
173+
commit_id: '65f1bf27bc3bf70f64657658635e66094edbcb4d'
174+
commit_message: "Initial commit"
175+
commit_time: 1489927679
176+
pusher_id: 1
177+
is_deleted: false
178+
deleted_by_id: 0
179+
deleted_unix: 0
180+
181+
-
182+
id: 23
183+
repo_id: 3
184+
name: 'master'
185+
commit_id: '2a47ca4b614a9f5a43abbd5ad851a54a616ffee6'
186+
commit_message: "init project"
187+
commit_time: 1497448461
188+
pusher_id: 1
189+
is_deleted: false
190+
deleted_by_id: 0
191+
deleted_unix: 0
192+
193+
-
194+
id: 24
195+
repo_id: 3
196+
name: 'test_branch'
197+
commit_id: 'd22b4d4daa5be07329fcef6ed458f00cf3392da0'
198+
commit_message: "test commit"
199+
commit_time: 1602935385
200+
pusher_id: 1
201+
is_deleted: false
202+
deleted_by_id: 0
203+
deleted_unix: 0

modules/git/repo_branch.go

-67
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ package git
77
import (
88
"context"
99
"errors"
10-
"fmt"
1110
"strings"
1211
)
1312

@@ -25,36 +24,6 @@ func IsBranchExist(ctx context.Context, repoPath, name string) bool {
2524
return IsReferenceExist(ctx, repoPath, BranchPrefix+name)
2625
}
2726

28-
// Branch represents a Git branch.
29-
type Branch struct {
30-
Name string
31-
Path string
32-
33-
gitRepo *Repository
34-
}
35-
36-
// GetHEADBranch returns corresponding branch of HEAD.
37-
func (repo *Repository) GetHEADBranch() (*Branch, error) {
38-
if repo == nil {
39-
return nil, errors.New("nil repo")
40-
}
41-
stdout, _, err := NewCommand("symbolic-ref", "HEAD").RunStdString(repo.Ctx, &RunOpts{Dir: repo.Path})
42-
if err != nil {
43-
return nil, err
44-
}
45-
stdout = strings.TrimSpace(stdout)
46-
47-
if !strings.HasPrefix(stdout, BranchPrefix) {
48-
return nil, fmt.Errorf("invalid HEAD branch: %v", stdout)
49-
}
50-
51-
return &Branch{
52-
Name: stdout[len(BranchPrefix):],
53-
Path: stdout,
54-
gitRepo: repo,
55-
}, nil
56-
}
57-
5827
func GetDefaultBranch(ctx context.Context, repoPath string) (string, error) {
5928
stdout, _, err := NewCommand("symbolic-ref", "HEAD").RunStdString(ctx, &RunOpts{Dir: repoPath})
6029
if err != nil {
@@ -67,37 +36,6 @@ func GetDefaultBranch(ctx context.Context, repoPath string) (string, error) {
6736
return strings.TrimPrefix(stdout, BranchPrefix), nil
6837
}
6938

70-
// GetBranch returns a branch by it's name
71-
func (repo *Repository) GetBranch(branch string) (*Branch, error) {
72-
if !repo.IsBranchExist(branch) {
73-
return nil, ErrBranchNotExist{branch}
74-
}
75-
return &Branch{
76-
Path: repo.Path,
77-
Name: branch,
78-
gitRepo: repo,
79-
}, nil
80-
}
81-
82-
// GetBranches returns a slice of *git.Branch
83-
func (repo *Repository) GetBranches(skip, limit int) ([]*Branch, int, error) {
84-
brs, countAll, err := repo.GetBranchNames(skip, limit)
85-
if err != nil {
86-
return nil, 0, err
87-
}
88-
89-
branches := make([]*Branch, len(brs))
90-
for i := range brs {
91-
branches[i] = &Branch{
92-
Path: repo.Path,
93-
Name: brs[i],
94-
gitRepo: repo,
95-
}
96-
}
97-
98-
return branches, countAll, nil
99-
}
100-
10139
// DeleteBranchOptions Option(s) for delete branch
10240
type DeleteBranchOptions struct {
10341
Force bool
@@ -147,11 +85,6 @@ func (repo *Repository) RemoveRemote(name string) error {
14785
return err
14886
}
14987

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-
15588
// RenameBranch rename a branch
15689
func (repo *Repository) RenameBranch(from, to string) error {
15790
_, _, err := NewCommand("branch", "-m").AddDynamicArguments(from, to).RunStdString(repo.Ctx, &RunOpts{Dir: repo.Path})

modules/gitrepo/branch.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@ import (
1111

1212
// GetBranchesByPath returns a branch by its path
1313
// if limit = 0 it will not limit
14-
func GetBranchesByPath(ctx context.Context, repo Repository, skip, limit int) ([]*git.Branch, int, error) {
14+
func GetBranchesByPath(ctx context.Context, repo Repository, skip, limit int) ([]string, int, error) {
1515
gitRepo, err := OpenRepository(ctx, repo)
1616
if err != nil {
1717
return nil, 0, err
1818
}
1919
defer gitRepo.Close()
2020

21-
return gitRepo.GetBranches(skip, limit)
21+
return gitRepo.GetBranchNames(skip, limit)
2222
}
2323

2424
func GetBranchCommitID(ctx context.Context, repo Repository, branch string) (string, error) {

routers/api/v1/repo/branch.go

+10-17
Original file line numberDiff line numberDiff line change
@@ -59,17 +59,16 @@ func GetBranch(ctx *context.APIContext) {
5959

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

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

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

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

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

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

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

routers/api/v1/repo/commits.go

+1-7
Original file line numberDiff line numberDiff line change
@@ -179,13 +179,7 @@ func GetAllCommits(ctx *context.APIContext) {
179179
var baseCommit *git.Commit
180180
if len(sha) == 0 {
181181
// no sha supplied - use default branch
182-
head, err := ctx.Repo.GitRepo.GetHEADBranch()
183-
if err != nil {
184-
ctx.APIErrorInternal(err)
185-
return
186-
}
187-
188-
baseCommit, err = ctx.Repo.GitRepo.GetBranchCommit(head.Name)
182+
baseCommit, err = ctx.Repo.GitRepo.GetBranchCommit(ctx.Repo.Repository.DefaultBranch)
189183
if err != nil {
190184
ctx.APIErrorInternal(err)
191185
return

routers/web/repo/editor.go

+4-5
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 ""

routers/web/repo/view_home.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ func handleRepoEmptyOrBroken(ctx *context.Context) {
269269
} else if reallyEmpty {
270270
showEmpty = true // the repo is really empty
271271
updateContextRepoEmptyAndStatus(ctx, true, repo_model.RepositoryReady)
272-
} else if branches, _, _ := ctx.Repo.GitRepo.GetBranches(0, 1); len(branches) == 0 {
272+
} else if branches, _, _ := ctx.Repo.GitRepo.GetBranchNames(0, 1); len(branches) == 0 {
273273
showEmpty = true // it is not really empty, but there is no branch
274274
// at the moment, other repo units like "actions" are not able to handle such case,
275275
// so we just mark the repo as empty to prevent from displaying these units.

services/context/repo.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -817,9 +817,9 @@ func RepoRefByType(detectRefType git.RefType) func(*Context) {
817817
if reqPath == "" {
818818
refShortName = ctx.Repo.Repository.DefaultBranch
819819
if !gitrepo.IsBranchExist(ctx, ctx.Repo.Repository, refShortName) {
820-
brs, _, err := ctx.Repo.GitRepo.GetBranches(0, 1)
820+
brs, _, err := ctx.Repo.GitRepo.GetBranchNames(0, 1)
821821
if err == nil && len(brs) != 0 {
822-
refShortName = brs[0].Name
822+
refShortName = brs[0]
823823
} else if len(brs) == 0 {
824824
log.Error("No branches in non-empty repository %s", ctx.Repo.GitRepo.Path)
825825
} else {

0 commit comments

Comments
 (0)