You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
135 lines
3.3 KiB
Go
135 lines
3.3 KiB
Go
8 years ago
|
// Copyright 2015 The Gogs Authors. All rights reserved.
|
||
6 years ago
|
// Copyright 2018 The Gitea Authors. All rights reserved.
|
||
8 years ago
|
// 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"
|
||
6 years ago
|
|
||
|
"gopkg.in/src-d/go-git.v4"
|
||
|
"gopkg.in/src-d/go-git.v4/plumbing"
|
||
8 years ago
|
)
|
||
|
|
||
8 years ago
|
// BranchPrefix base dir of the branch information file store on git
|
||
|
const BranchPrefix = "refs/heads/"
|
||
8 years ago
|
|
||
|
// 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 {
|
||
8 years ago
|
return IsReferenceExist(repoPath, BranchPrefix+name)
|
||
8 years ago
|
}
|
||
|
|
||
8 years ago
|
// IsBranchExist returns true if given branch exists in current repository.
|
||
8 years ago
|
func (repo *Repository) IsBranchExist(name string) bool {
|
||
|
return IsBranchExist(repo.Path, name)
|
||
|
}
|
||
|
|
||
|
// Branch represents a Git branch.
|
||
|
type Branch struct {
|
||
|
Name string
|
||
|
Path string
|
||
|
}
|
||
|
|
||
|
// 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)
|
||
|
|
||
8 years ago
|
if !strings.HasPrefix(stdout, BranchPrefix) {
|
||
8 years ago
|
return nil, fmt.Errorf("invalid HEAD branch: %v", stdout)
|
||
|
}
|
||
|
|
||
|
return &Branch{
|
||
8 years ago
|
Name: stdout[len(BranchPrefix):],
|
||
8 years ago
|
Path: stdout,
|
||
|
}, nil
|
||
|
}
|
||
|
|
||
|
// SetDefaultBranch sets default branch of repository.
|
||
|
func (repo *Repository) SetDefaultBranch(name string) error {
|
||
8 years ago
|
_, err := NewCommand("symbolic-ref", "HEAD", BranchPrefix+name).RunInDir(repo.Path)
|
||
8 years ago
|
return err
|
||
|
}
|
||
|
|
||
|
// GetBranches returns all branches of the repository.
|
||
|
func (repo *Repository) GetBranches() ([]string, error) {
|
||
6 years ago
|
r, err := git.PlainOpen(repo.Path)
|
||
8 years ago
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
6 years ago
|
branchIter, err := r.Branches()
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
8 years ago
|
}
|
||
6 years ago
|
branches := make([]string, 0)
|
||
|
if err = branchIter.ForEach(func(branch *plumbing.Reference) error {
|
||
|
branches = append(branches, branch.Name().Short())
|
||
|
return nil
|
||
|
}); err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
8 years ago
|
return branches, nil
|
||
|
}
|
||
|
|
||
8 years ago
|
// DeleteBranchOptions Option(s) for delete branch
|
||
8 years ago
|
type DeleteBranchOptions struct {
|
||
|
Force bool
|
||
|
}
|
||
|
|
||
|
// DeleteBranch delete a branch by name on repository.
|
||
|
func (repo *Repository) DeleteBranch(name string, opts DeleteBranchOptions) error {
|
||
7 years ago
|
cmd := NewCommand("branch")
|
||
8 years ago
|
|
||
|
if opts.Force {
|
||
7 years ago
|
cmd.AddArguments("-D")
|
||
|
} else {
|
||
|
cmd.AddArguments("-d")
|
||
8 years ago
|
}
|
||
|
|
||
|
cmd.AddArguments(name)
|
||
|
_, err := cmd.RunInDir(repo.Path)
|
||
|
|
||
|
return err
|
||
|
}
|
||
|
|
||
8 years ago
|
// 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
|
||
|
}
|
||
|
|
||
8 years ago
|
// 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
|
||
|
}
|