mirror of
https://github.com/go-gitea/gitea.git
synced 2024-11-24 04:38:09 +08:00
ce8de35334
* Add options to git.Clone to make it more capable * Begin the process of removing the local copy and tidy up * Remove Wiki LocalCopy Checkouts * Remove the last LocalRepo helpers * Remove WithTemporaryFile * Enable push-hooks for these routes * Ensure tests cope with hooks Signed-off-by: Andrew Thornton <art27@cantab.net> * Remove Repository.LocalCopyPath() * Move temporary repo to use the standard temporary path * Fix the tests Signed-off-by: Andrew Thornton <art27@cantab.net> * Remove LocalWikiPath * Fix missing remove Signed-off-by: Andrew Thornton <art27@cantab.net> * Use AppURL for Oauth user link (#6894) * Use AppURL for Oauth user link Fix #6843 * Update oauth.go * Update oauth.go * internal/ssh: ignore env command totally (#6825) * ssh: ignore env command totally * Remove commented code Needed fix described in issue #6889 * Escape the commit message on issues update and title in telegram hook (#6901) * update sdk to latest (#6903) * improve description of branch protection (fix #6886) (#6906) The branch protection description text were not quite accurate. * Fix logging documentation (#6904) * ENABLE_MACARON_REDIRECT should be REDIRECT_MACARON_LOG * Allow DISABLE_ROUTER_LOG to be set in the [log] section * [skip ci] Updated translations via Crowdin * Move sdk structs to modules/structs (#6905) * move sdk structs to moduels/structs * fix tests * fix fmt * fix swagger * fix vendor
179 lines
4.2 KiB
Go
179 lines
4.2 KiB
Go
// Copyright 2015 The Gogs Authors. All rights reserved.
|
|
// Copyright 2018 The Gitea Authors. All rights reserved.
|
|
// 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"
|
|
|
|
"gopkg.in/src-d/go-git.v4/plumbing"
|
|
)
|
|
|
|
// BranchPrefix base dir of the branch information file store on git
|
|
const BranchPrefix = "refs/heads/"
|
|
|
|
// 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 {
|
|
return IsReferenceExist(repoPath, BranchPrefix+name)
|
|
}
|
|
|
|
// IsBranchExist returns true if given branch exists in current repository.
|
|
func (repo *Repository) IsBranchExist(name string) bool {
|
|
_, err := repo.gogitRepo.Reference(plumbing.ReferenceName(BranchPrefix+name), true)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
return true
|
|
}
|
|
|
|
// 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) {
|
|
stdout, err := NewCommand("symbolic-ref", "HEAD").RunInDir(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
|
|
}
|
|
|
|
// SetDefaultBranch sets default branch of repository.
|
|
func (repo *Repository) SetDefaultBranch(name string) error {
|
|
_, err := NewCommand("symbolic-ref", "HEAD", BranchPrefix+name).RunInDir(repo.Path)
|
|
return err
|
|
}
|
|
|
|
// GetBranches returns all branches of the repository.
|
|
func (repo *Repository) GetBranches() ([]string, error) {
|
|
var branchNames []string
|
|
|
|
branches, err := repo.gogitRepo.Branches()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
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)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
brs, err := gitRepo.GetBranches()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
branches := make([]*Branch, len(brs))
|
|
for i := range brs {
|
|
branches[i] = &Branch{
|
|
Path: path,
|
|
Name: brs[i],
|
|
gitRepo: gitRepo,
|
|
}
|
|
}
|
|
|
|
return branches, nil
|
|
}
|
|
|
|
// DeleteBranchOptions Option(s) for delete branch
|
|
type DeleteBranchOptions struct {
|
|
Force bool
|
|
}
|
|
|
|
// DeleteBranch delete a branch by name on repository.
|
|
func (repo *Repository) DeleteBranch(name string, opts DeleteBranchOptions) error {
|
|
cmd := NewCommand("branch")
|
|
|
|
if opts.Force {
|
|
cmd.AddArguments("-D")
|
|
} else {
|
|
cmd.AddArguments("-d")
|
|
}
|
|
|
|
cmd.AddArguments(name)
|
|
_, err := cmd.RunInDir(repo.Path)
|
|
|
|
return err
|
|
}
|
|
|
|
// CreateBranch create a new branch
|
|
func (repo *Repository) CreateBranch(branch, oldbranchOrCommit string) error {
|
|
cmd := NewCommand("branch")
|
|
cmd.AddArguments("--", branch, oldbranchOrCommit)
|
|
|
|
_, err := cmd.RunInDir(repo.Path)
|
|
|
|
return err
|
|
}
|
|
|
|
// 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
|
|
}
|
|
|
|
// GetCommit returns the head commit of a branch
|
|
func (branch *Branch) GetCommit() (*Commit, error) {
|
|
return branch.gitRepo.GetBranchCommit(branch.Name)
|
|
}
|