mirror of
https://gitcode.com/gitea/gitea.git
synced 2025-06-03 02:33:01 +08:00
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
This commit is contained in:
@ -7,7 +7,6 @@ package git
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
@ -25,36 +24,6 @@ func IsBranchExist(ctx context.Context, repoPath, name string) bool {
|
||||
return IsReferenceExist(ctx, repoPath, BranchPrefix+name)
|
||||
}
|
||||
|
||||
// Branch represents a Git branch.
|
||||
type Branch struct {
|
||||
Name string
|
||||
Path string
|
||||
|
||||
gitRepo *Repository
|
||||
}
|
||||
|
||||
// GetHEADBranch returns corresponding branch of HEAD.
|
||||
func (repo *Repository) GetHEADBranch() (*Branch, error) {
|
||||
if repo == nil {
|
||||
return nil, errors.New("nil repo")
|
||||
}
|
||||
stdout, _, err := NewCommand("symbolic-ref", "HEAD").RunStdString(repo.Ctx, &RunOpts{Dir: repo.Path})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
stdout = strings.TrimSpace(stdout)
|
||||
|
||||
if !strings.HasPrefix(stdout, BranchPrefix) {
|
||||
return nil, fmt.Errorf("invalid HEAD branch: %v", stdout)
|
||||
}
|
||||
|
||||
return &Branch{
|
||||
Name: stdout[len(BranchPrefix):],
|
||||
Path: stdout,
|
||||
gitRepo: repo,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func GetDefaultBranch(ctx context.Context, repoPath string) (string, error) {
|
||||
stdout, _, err := NewCommand("symbolic-ref", "HEAD").RunStdString(ctx, &RunOpts{Dir: repoPath})
|
||||
if err != nil {
|
||||
@ -67,37 +36,6 @@ func GetDefaultBranch(ctx context.Context, repoPath string) (string, error) {
|
||||
return strings.TrimPrefix(stdout, BranchPrefix), nil
|
||||
}
|
||||
|
||||
// GetBranch returns a branch by it's name
|
||||
func (repo *Repository) GetBranch(branch string) (*Branch, error) {
|
||||
if !repo.IsBranchExist(branch) {
|
||||
return nil, ErrBranchNotExist{branch}
|
||||
}
|
||||
return &Branch{
|
||||
Path: repo.Path,
|
||||
Name: branch,
|
||||
gitRepo: repo,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// GetBranches returns a slice of *git.Branch
|
||||
func (repo *Repository) GetBranches(skip, limit int) ([]*Branch, int, error) {
|
||||
brs, countAll, err := repo.GetBranchNames(skip, limit)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
branches := make([]*Branch, len(brs))
|
||||
for i := range brs {
|
||||
branches[i] = &Branch{
|
||||
Path: repo.Path,
|
||||
Name: brs[i],
|
||||
gitRepo: repo,
|
||||
}
|
||||
}
|
||||
|
||||
return branches, countAll, nil
|
||||
}
|
||||
|
||||
// DeleteBranchOptions Option(s) for delete branch
|
||||
type DeleteBranchOptions struct {
|
||||
Force bool
|
||||
@ -147,11 +85,6 @@ func (repo *Repository) RemoveRemote(name string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
// GetCommit returns the head commit of a branch
|
||||
func (branch *Branch) GetCommit() (*Commit, error) {
|
||||
return branch.gitRepo.GetBranchCommit(branch.Name)
|
||||
}
|
||||
|
||||
// RenameBranch rename a branch
|
||||
func (repo *Repository) RenameBranch(from, to string) error {
|
||||
_, _, err := NewCommand("branch", "-m").AddDynamicArguments(from, to).RunStdString(repo.Ctx, &RunOpts{Dir: repo.Path})
|
||||
|
Reference in New Issue
Block a user