Skip to content

Commit c46d935

Browse files
committedMar 23, 2025
Use a string represent a branch
1 parent 94e0214 commit c46d935

File tree

11 files changed

+25
-55
lines changed

11 files changed

+25
-55
lines changed
 

‎modules/git/repo_branch.go

+8-38
Original file line numberDiff line numberDiff line change
@@ -25,31 +25,22 @@ func IsBranchExist(ctx context.Context, repoPath, name string) bool {
2525
return IsReferenceExist(ctx, repoPath, BranchPrefix+name)
2626
}
2727

28-
// Branch represents a Git branch.
29-
type Branch struct {
30-
Name string
31-
Path string
32-
}
33-
3428
// GetHEADBranch returns corresponding branch of HEAD.
35-
func (repo *Repository) GetHEADBranch() (*Branch, error) {
29+
func (repo *Repository) GetHEADBranch() (string, error) {
3630
if repo == nil {
37-
return nil, fmt.Errorf("nil repo")
31+
return "", fmt.Errorf("nil repo")
3832
}
3933
stdout, _, err := NewCommand("symbolic-ref", "HEAD").RunStdString(repo.Ctx, &RunOpts{Dir: repo.Path})
4034
if err != nil {
41-
return nil, err
35+
return "", err
4236
}
4337
stdout = strings.TrimSpace(stdout)
4438

4539
if !strings.HasPrefix(stdout, BranchPrefix) {
46-
return nil, fmt.Errorf("invalid HEAD branch: %v", stdout)
40+
return "", fmt.Errorf("invalid HEAD branch: %v", stdout)
4741
}
4842

49-
return &Branch{
50-
Name: stdout[len(BranchPrefix):],
51-
Path: stdout,
52-
}, nil
43+
return stdout[len(BranchPrefix):], nil
5344
}
5445

5546
func GetDefaultBranch(ctx context.Context, repoPath string) (string, error) {
@@ -65,32 +56,11 @@ func GetDefaultBranch(ctx context.Context, repoPath string) (string, error) {
6556
}
6657

6758
// GetBranch returns a branch by it's name
68-
func (repo *Repository) GetBranch(branch string) (*Branch, error) {
59+
func (repo *Repository) GetBranch(branch string) (string, error) {
6960
if !repo.IsBranchExist(branch) {
70-
return nil, ErrBranchNotExist{branch}
71-
}
72-
return &Branch{
73-
Path: repo.Path,
74-
Name: branch,
75-
}, nil
76-
}
77-
78-
// GetBranches returns a slice of *git.Branch
79-
func (repo *Repository) GetBranches(skip, limit int) ([]*Branch, int, error) {
80-
brs, countAll, err := repo.GetBranchNames(skip, limit)
81-
if err != nil {
82-
return nil, 0, err
61+
return "", ErrBranchNotExist{branch}
8362
}
84-
85-
branches := make([]*Branch, len(brs))
86-
for i := range brs {
87-
branches[i] = &Branch{
88-
Path: repo.Path,
89-
Name: brs[i],
90-
}
91-
}
92-
93-
return branches, countAll, nil
63+
return branch, nil
9464
}
9565

9666
// DeleteBranchOptions Option(s) for delete branch

‎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/commits.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ func GetAllCommits(ctx *context.APIContext) {
186186
return
187187
}
188188

189-
baseCommit, err = ctx.Repo.GitRepo.GetBranchCommit(head.Name)
189+
baseCommit, err = ctx.Repo.GitRepo.GetBranchCommit(head)
190190
if err != nil {
191191
ctx.APIErrorInternal(err)
192192
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
@@ -815,9 +815,9 @@ func RepoRefByType(detectRefType git.RefType) func(*Context) {
815815
if reqPath == "" {
816816
refShortName = ctx.Repo.Repository.DefaultBranch
817817
if !gitrepo.IsBranchExist(ctx, ctx.Repo.Repository, refShortName) {
818-
brs, _, err := ctx.Repo.GitRepo.GetBranches(0, 1)
818+
brs, _, err := ctx.Repo.GitRepo.GetBranchNames(0, 1)
819819
if err == nil && len(brs) != 0 {
820-
refShortName = brs[0].Name
820+
refShortName = brs[0]
821821
} else if len(brs) == 0 {
822822
log.Error("No branches in non-empty repository %s", ctx.Repo.GitRepo.Path)
823823
} else {

‎services/convert/pull.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ import (
2828
// Optional - Merger
2929
func ToAPIPullRequest(ctx context.Context, pr *issues_model.PullRequest, doer *user_model.User) *api.PullRequest {
3030
var (
31-
baseBranch *git.Branch
32-
headBranch *git.Branch
31+
baseBranch string
32+
headBranch string
3333
baseCommit *git.Commit
3434
err error
3535
)
@@ -159,7 +159,7 @@ func ToAPIPullRequest(ctx context.Context, pr *issues_model.PullRequest, doer *u
159159
if err == nil {
160160
baseCommit, err = gitRepo.GetBranchCommit(pr.BaseBranch)
161161
if err != nil && !git.IsErrNotExist(err) {
162-
log.Error("GetCommit[%s]: %v", baseBranch.Name, err)
162+
log.Error("GetCommit[%s]: %v", baseBranch, err)
163163
return nil
164164
}
165165

@@ -221,7 +221,7 @@ func ToAPIPullRequest(ctx context.Context, pr *issues_model.PullRequest, doer *u
221221
} else {
222222
commit, err := headGitRepo.GetBranchCommit(pr.HeadBranch)
223223
if err != nil && !git.IsErrNotExist(err) {
224-
log.Error("GetCommit[%s]: %v", headBranch.Name, err)
224+
log.Error("GetCommit[%s]: %v", headBranch, err)
225225
return nil
226226
}
227227
if err == nil {
@@ -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.Name, err)
476+
log.Error("GetCommit[%s]: %v", headBranch, err)
477477
return nil, err
478478
}
479479
if err == nil {

‎services/mirror/mirror_pull.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,7 @@ func runSync(ctx context.Context, m *repo_model.Mirror) ([]*mirrorSyncResult, bo
419419
}
420420

421421
for _, branch := range branches {
422-
cache.Remove(m.Repo.GetCommitsCountCacheKey(branch.Name, true))
422+
cache.Remove(m.Repo.GetCommitsCountCacheKey(branch, true))
423423
}
424424

425425
m.UpdatedUnix = timeutil.TimeStampNow()

‎services/pull/pull.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -763,7 +763,7 @@ func CloseRepoBranchesPulls(ctx context.Context, doer *user_model.User, repo *re
763763

764764
var errs []error
765765
for _, branch := range branches {
766-
prs, err := issues_model.GetUnmergedPullRequestsByHeadInfo(ctx, repo.ID, branch.Name)
766+
prs, err := issues_model.GetUnmergedPullRequestsByHeadInfo(ctx, repo.ID, branch)
767767
if err != nil {
768768
return err
769769
}

‎services/repository/files/patch.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ func (opts *ApplyDiffPatchOptions) Validate(ctx context.Context, repo *repo_mode
7777
// If we aren't branching to a new branch, make sure user can commit to the given branch
7878
if opts.NewBranch != opts.OldBranch {
7979
existingBranch, err := gitRepo.GetBranch(opts.NewBranch)
80-
if existingBranch != nil {
80+
if existingBranch != "" {
8181
return git_model.ErrBranchAlreadyExists{
8282
BranchName: opts.NewBranch,
8383
}

‎services/repository/files/update.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ func ChangeRepoFiles(ctx context.Context, repo *repo_model.Repository, doer *use
146146
// If we aren't branching to a new branch, make sure user can commit to the given branch
147147
if opts.NewBranch != opts.OldBranch {
148148
existingBranch, err := gitRepo.GetBranch(opts.NewBranch)
149-
if existingBranch != nil {
149+
if existingBranch != "" {
150150
return nil, git_model.ErrBranchAlreadyExists{
151151
BranchName: opts.NewBranch,
152152
}

‎services/repository/migrate.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,8 @@ func MigrateRepositoryGitData(ctx context.Context, u *user_model.User,
145145
if err != nil {
146146
return repo, fmt.Errorf("GetHEADBranch: %w", err)
147147
}
148-
if headBranch != nil {
149-
repo.DefaultBranch = headBranch.Name
148+
if headBranch != "" {
149+
repo.DefaultBranch = headBranch
150150
}
151151
}
152152

0 commit comments

Comments
 (0)
Failed to load comments.