2016-11-03 23:16:01 +01:00
|
|
|
// Copyright 2015 The Gogs Authors. All rights reserved.
|
2018-11-27 22:52:20 +01:00
|
|
|
// Copyright 2018 The Gitea Authors. All rights reserved.
|
2016-11-03 23:16:01 +01:00
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package git
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
2018-11-27 22:52:20 +01:00
|
|
|
|
|
|
|
"gopkg.in/src-d/go-git.v4/plumbing"
|
2016-11-03 23:16:01 +01:00
|
|
|
)
|
|
|
|
|
2016-12-22 10:30:52 +01:00
|
|
|
// BranchPrefix base dir of the branch information file store on git
|
|
|
|
const BranchPrefix = "refs/heads/"
|
2016-11-03 23:16:01 +01:00
|
|
|
|
|
|
|
// IsReferenceExist returns true if given reference exists in the repository.
|
|
|
|
func IsReferenceExist(repoPath, name string) bool {
|
|
|
|
_, err := NewCommand("show-ref", "--verify", name).RunInDir(repoPath)
|
|
|
|
return err == nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsBranchExist returns true if given branch exists in the repository.
|
|
|
|
func IsBranchExist(repoPath, name string) bool {
|
2016-12-22 10:30:52 +01:00
|
|
|
return IsReferenceExist(repoPath, BranchPrefix+name)
|
2016-11-03 23:16:01 +01:00
|
|
|
}
|
|
|
|
|
2016-12-22 10:30:52 +01:00
|
|
|
// IsBranchExist returns true if given branch exists in current repository.
|
2016-11-03 23:16:01 +01:00
|
|
|
func (repo *Repository) IsBranchExist(name string) bool {
|
2019-04-19 14:17:27 +02:00
|
|
|
_, err := repo.gogitRepo.Reference(plumbing.ReferenceName(BranchPrefix+name), true)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
2016-11-03 23:16:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Branch represents a Git branch.
|
|
|
|
type Branch struct {
|
|
|
|
Name string
|
|
|
|
Path string
|
2019-04-19 14:17:27 +02:00
|
|
|
|
|
|
|
gitRepo *Repository
|
2016-11-03 23:16:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetHEADBranch returns corresponding branch of HEAD.
|
|
|
|
func (repo *Repository) GetHEADBranch() (*Branch, error) {
|
|
|
|
stdout, err := NewCommand("symbolic-ref", "HEAD").RunInDir(repo.Path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
stdout = strings.TrimSpace(stdout)
|
|
|
|
|
2016-12-22 10:30:52 +01:00
|
|
|
if !strings.HasPrefix(stdout, BranchPrefix) {
|
2016-11-03 23:16:01 +01:00
|
|
|
return nil, fmt.Errorf("invalid HEAD branch: %v", stdout)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Branch{
|
2019-04-19 14:17:27 +02:00
|
|
|
Name: stdout[len(BranchPrefix):],
|
|
|
|
Path: stdout,
|
|
|
|
gitRepo: repo,
|
2016-11-03 23:16:01 +01:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetDefaultBranch sets default branch of repository.
|
|
|
|
func (repo *Repository) SetDefaultBranch(name string) error {
|
2016-12-22 10:30:52 +01:00
|
|
|
_, err := NewCommand("symbolic-ref", "HEAD", BranchPrefix+name).RunInDir(repo.Path)
|
2016-11-03 23:16:01 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetBranches returns all branches of the repository.
|
|
|
|
func (repo *Repository) GetBranches() ([]string, error) {
|
2019-04-19 14:17:27 +02:00
|
|
|
var branchNames []string
|
|
|
|
|
|
|
|
branches, err := repo.gogitRepo.Branches()
|
2016-11-03 23:16:01 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-04-19 14:17:27 +02:00
|
|
|
branches.ForEach(func(branch *plumbing.Reference) error {
|
|
|
|
branchNames = append(branchNames, strings.TrimPrefix(branch.Name().String(), BranchPrefix))
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
// TODO: Sort?
|
|
|
|
|
|
|
|
return branchNames, 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
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetBranchesByPath returns a branch by it's path
|
|
|
|
func GetBranchesByPath(path string) ([]*Branch, error) {
|
|
|
|
gitRepo, err := OpenRepository(path)
|
2018-11-27 22:52:20 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2016-11-03 23:16:01 +01:00
|
|
|
}
|
2019-04-19 14:17:27 +02:00
|
|
|
|
|
|
|
brs, err := gitRepo.GetBranches()
|
|
|
|
if err != nil {
|
2018-11-27 22:52:20 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-04-19 14:17:27 +02:00
|
|
|
branches := make([]*Branch, len(brs))
|
|
|
|
for i := range brs {
|
|
|
|
branches[i] = &Branch{
|
|
|
|
Path: path,
|
|
|
|
Name: brs[i],
|
|
|
|
gitRepo: gitRepo,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-03 23:16:01 +01:00
|
|
|
return branches, nil
|
|
|
|
}
|
|
|
|
|
2016-12-22 10:30:52 +01:00
|
|
|
// DeleteBranchOptions Option(s) for delete branch
|
2016-11-03 23:16:01 +01:00
|
|
|
type DeleteBranchOptions struct {
|
|
|
|
Force bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteBranch delete a branch by name on repository.
|
|
|
|
func (repo *Repository) DeleteBranch(name string, opts DeleteBranchOptions) error {
|
2017-08-03 15:48:36 +02:00
|
|
|
cmd := NewCommand("branch")
|
2016-11-03 23:16:01 +01:00
|
|
|
|
|
|
|
if opts.Force {
|
2017-08-03 15:48:36 +02:00
|
|
|
cmd.AddArguments("-D")
|
|
|
|
} else {
|
|
|
|
cmd.AddArguments("-d")
|
2016-11-03 23:16:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
cmd.AddArguments(name)
|
|
|
|
_, err := cmd.RunInDir(repo.Path)
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-02-05 15:43:28 +01:00
|
|
|
// CreateBranch create a new branch
|
|
|
|
func (repo *Repository) CreateBranch(branch, newBranch string) error {
|
|
|
|
cmd := NewCommand("branch")
|
|
|
|
cmd.AddArguments(branch, newBranch)
|
|
|
|
|
|
|
|
_, err := cmd.RunInDir(repo.Path)
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-11-03 23:16:01 +01:00
|
|
|
// AddRemote adds a new remote to repository.
|
|
|
|
func (repo *Repository) AddRemote(name, url string, fetch bool) error {
|
|
|
|
cmd := NewCommand("remote", "add")
|
|
|
|
if fetch {
|
|
|
|
cmd.AddArguments("-f")
|
|
|
|
}
|
|
|
|
cmd.AddArguments(name, url)
|
|
|
|
|
|
|
|
_, err := cmd.RunInDir(repo.Path)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveRemote removes a remote from repository.
|
|
|
|
func (repo *Repository) RemoveRemote(name string) error {
|
|
|
|
_, err := NewCommand("remote", "remove", name).RunInDir(repo.Path)
|
|
|
|
return err
|
|
|
|
}
|
2019-04-19 14:17:27 +02:00
|
|
|
|
|
|
|
// GetCommit returns the head commit of a branch
|
|
|
|
func (branch *Branch) GetCommit() (*Commit, error) {
|
|
|
|
return branch.gitRepo.GetBranchCommit(branch.Name)
|
|
|
|
}
|