2016-11-03 17:16:01 -05:00
|
|
|
// Copyright 2015 The Gogs Authors. All rights reserved.
|
2018-11-27 16:52:20 -05:00
|
|
|
// Copyright 2018 The Gitea Authors. All rights reserved.
|
2022-11-27 13:20:29 -05:00
|
|
|
// SPDX-License-Identifier: MIT
|
2016-11-03 17:16:01 -05:00
|
|
|
|
|
|
|
package git
|
|
|
|
|
|
|
|
import (
|
2021-11-30 15:06:32 -05:00
|
|
|
"context"
|
2022-07-15 19:10:02 -05:00
|
|
|
"errors"
|
2016-11-03 17:16:01 -05:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2016-12-22 04:30:52 -05:00
|
|
|
// BranchPrefix base dir of the branch information file store on git
|
|
|
|
const BranchPrefix = "refs/heads/"
|
2016-11-03 17:16:01 -05:00
|
|
|
|
2021-07-28 04:42:56 -05:00
|
|
|
// AGit Flow
|
|
|
|
|
2022-01-10 04:32:37 -05:00
|
|
|
// PullRequestPrefix special ref to create a pull request: refs/for/<targe-branch>/<topic-branch>
|
2021-07-28 04:42:56 -05:00
|
|
|
// or refs/for/<targe-branch> -o topic='<topic-branch>'
|
|
|
|
const PullRequestPrefix = "refs/for/"
|
|
|
|
|
|
|
|
// TODO: /refs/for-review for suggest change interface
|
|
|
|
|
2016-11-03 17:16:01 -05:00
|
|
|
// IsReferenceExist returns true if given reference exists in the repository.
|
2021-11-30 15:06:32 -05:00
|
|
|
func IsReferenceExist(ctx context.Context, repoPath, name string) bool {
|
2022-10-23 09:44:45 -05:00
|
|
|
_, _, err := NewCommand(ctx, "show-ref", "--verify").AddDashesAndList(name).RunStdString(&RunOpts{Dir: repoPath})
|
2016-11-03 17:16:01 -05:00
|
|
|
return err == nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsBranchExist returns true if given branch exists in the repository.
|
2021-11-30 15:06:32 -05:00
|
|
|
func IsBranchExist(ctx context.Context, repoPath, name string) bool {
|
|
|
|
return IsReferenceExist(ctx, repoPath, BranchPrefix+name)
|
2016-11-03 17:16:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Branch represents a Git branch.
|
|
|
|
type Branch struct {
|
|
|
|
Name string
|
|
|
|
Path string
|
2019-04-19 07:17:27 -05:00
|
|
|
|
|
|
|
gitRepo *Repository
|
2016-11-03 17:16:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetHEADBranch returns corresponding branch of HEAD.
|
|
|
|
func (repo *Repository) GetHEADBranch() (*Branch, error) {
|
2020-02-26 01:32:22 -05:00
|
|
|
if repo == nil {
|
|
|
|
return nil, fmt.Errorf("nil repo")
|
|
|
|
}
|
2022-03-31 21:55:30 -05:00
|
|
|
stdout, _, err := NewCommand(repo.Ctx, "symbolic-ref", "HEAD").RunStdString(&RunOpts{Dir: repo.Path})
|
2016-11-03 17:16:01 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
stdout = strings.TrimSpace(stdout)
|
|
|
|
|
2016-12-22 04:30:52 -05:00
|
|
|
if !strings.HasPrefix(stdout, BranchPrefix) {
|
2016-11-03 17:16:01 -05:00
|
|
|
return nil, fmt.Errorf("invalid HEAD branch: %v", stdout)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Branch{
|
2019-04-19 07:17:27 -05:00
|
|
|
Name: stdout[len(BranchPrefix):],
|
|
|
|
Path: stdout,
|
|
|
|
gitRepo: repo,
|
2016-11-03 17:16:01 -05:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetDefaultBranch sets default branch of repository.
|
|
|
|
func (repo *Repository) SetDefaultBranch(name string) error {
|
2022-10-23 09:44:45 -05:00
|
|
|
_, _, err := NewCommand(repo.Ctx, "symbolic-ref", "HEAD").AddDynamicArguments(BranchPrefix + name).RunStdString(&RunOpts{Dir: repo.Path})
|
2016-11-03 17:16:01 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-09-24 23:09:23 -05:00
|
|
|
// GetDefaultBranch gets default branch of repository.
|
|
|
|
func (repo *Repository) GetDefaultBranch() (string, error) {
|
2022-03-31 21:55:30 -05:00
|
|
|
stdout, _, err := NewCommand(repo.Ctx, "symbolic-ref", "HEAD").RunStdString(&RunOpts{Dir: repo.Path})
|
2022-07-15 19:10:02 -05:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
stdout = strings.TrimSpace(stdout)
|
|
|
|
if !strings.HasPrefix(stdout, BranchPrefix) {
|
|
|
|
return "", errors.New("the HEAD is not a branch: " + stdout)
|
|
|
|
}
|
|
|
|
return strings.TrimPrefix(stdout, BranchPrefix), nil
|
2020-09-24 23:09:23 -05:00
|
|
|
}
|
|
|
|
|
2019-04-19 07:17:27 -05:00
|
|
|
// 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
|
2021-02-03 14:06:13 -05:00
|
|
|
// if limit = 0 it will not limit
|
2022-01-19 18:26:57 -05:00
|
|
|
func GetBranchesByPath(ctx context.Context, path string, skip, limit int) ([]*Branch, int, error) {
|
2022-03-29 14:13:41 -05:00
|
|
|
gitRepo, err := OpenRepository(ctx, path)
|
2018-11-27 16:52:20 -05:00
|
|
|
if err != nil {
|
2021-02-03 14:06:13 -05:00
|
|
|
return nil, 0, err
|
2016-11-03 17:16:01 -05:00
|
|
|
}
|
2019-11-13 02:01:19 -05:00
|
|
|
defer gitRepo.Close()
|
2019-04-19 07:17:27 -05:00
|
|
|
|
2021-12-08 14:08:16 -05:00
|
|
|
return gitRepo.GetBranches(skip, limit)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetBranches returns a slice of *git.Branch
|
|
|
|
func (repo *Repository) GetBranches(skip, limit int) ([]*Branch, int, error) {
|
|
|
|
brs, countAll, err := repo.GetBranchNames(skip, limit)
|
2019-04-19 07:17:27 -05:00
|
|
|
if err != nil {
|
2021-02-03 14:06:13 -05:00
|
|
|
return nil, 0, err
|
2018-11-27 16:52:20 -05:00
|
|
|
}
|
|
|
|
|
2019-04-19 07:17:27 -05:00
|
|
|
branches := make([]*Branch, len(brs))
|
|
|
|
for i := range brs {
|
|
|
|
branches[i] = &Branch{
|
2021-12-08 14:08:16 -05:00
|
|
|
Path: repo.Path,
|
2019-04-19 07:17:27 -05:00
|
|
|
Name: brs[i],
|
2021-12-08 14:08:16 -05:00
|
|
|
gitRepo: repo,
|
2019-04-19 07:17:27 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-03 14:06:13 -05:00
|
|
|
return branches, countAll, nil
|
2016-11-03 17:16:01 -05:00
|
|
|
}
|
|
|
|
|
2016-12-22 04:30:52 -05:00
|
|
|
// DeleteBranchOptions Option(s) for delete branch
|
2016-11-03 17:16:01 -05:00
|
|
|
type DeleteBranchOptions struct {
|
|
|
|
Force bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteBranch delete a branch by name on repository.
|
|
|
|
func (repo *Repository) DeleteBranch(name string, opts DeleteBranchOptions) error {
|
2022-02-06 14:01:47 -05:00
|
|
|
cmd := NewCommand(repo.Ctx, "branch")
|
2016-11-03 17:16:01 -05:00
|
|
|
|
|
|
|
if opts.Force {
|
2017-08-03 08:48:36 -05:00
|
|
|
cmd.AddArguments("-D")
|
|
|
|
} else {
|
|
|
|
cmd.AddArguments("-d")
|
2016-11-03 17:16:01 -05:00
|
|
|
}
|
|
|
|
|
2022-10-23 09:44:45 -05:00
|
|
|
cmd.AddDashesAndList(name)
|
2022-03-31 21:55:30 -05:00
|
|
|
_, _, err := cmd.RunStdString(&RunOpts{Dir: repo.Path})
|
2016-11-03 17:16:01 -05:00
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-02-05 09:43:28 -05:00
|
|
|
// CreateBranch create a new branch
|
2019-05-11 10:29:17 -05:00
|
|
|
func (repo *Repository) CreateBranch(branch, oldbranchOrCommit string) error {
|
2022-02-06 14:01:47 -05:00
|
|
|
cmd := NewCommand(repo.Ctx, "branch")
|
2022-10-23 09:44:45 -05:00
|
|
|
cmd.AddDashesAndList(branch, oldbranchOrCommit)
|
2017-02-05 09:43:28 -05:00
|
|
|
|
2022-03-31 21:55:30 -05:00
|
|
|
_, _, err := cmd.RunStdString(&RunOpts{Dir: repo.Path})
|
2017-02-05 09:43:28 -05:00
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-11-03 17:16:01 -05:00
|
|
|
// AddRemote adds a new remote to repository.
|
|
|
|
func (repo *Repository) AddRemote(name, url string, fetch bool) error {
|
2022-02-06 14:01:47 -05:00
|
|
|
cmd := NewCommand(repo.Ctx, "remote", "add")
|
2016-11-03 17:16:01 -05:00
|
|
|
if fetch {
|
|
|
|
cmd.AddArguments("-f")
|
|
|
|
}
|
2022-10-23 09:44:45 -05:00
|
|
|
cmd.AddDynamicArguments(name, url)
|
2016-11-03 17:16:01 -05:00
|
|
|
|
2022-03-31 21:55:30 -05:00
|
|
|
_, _, err := cmd.RunStdString(&RunOpts{Dir: repo.Path})
|
2016-11-03 17:16:01 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveRemote removes a remote from repository.
|
|
|
|
func (repo *Repository) RemoveRemote(name string) error {
|
2022-10-23 09:44:45 -05:00
|
|
|
_, _, err := NewCommand(repo.Ctx, "remote", "rm").AddDynamicArguments(name).RunStdString(&RunOpts{Dir: repo.Path})
|
2016-11-03 17:16:01 -05:00
|
|
|
return err
|
|
|
|
}
|
2019-04-19 07:17:27 -05:00
|
|
|
|
|
|
|
// GetCommit returns the head commit of a branch
|
|
|
|
func (branch *Branch) GetCommit() (*Commit, error) {
|
|
|
|
return branch.gitRepo.GetBranchCommit(branch.Name)
|
|
|
|
}
|
2021-10-08 12:03:04 -05:00
|
|
|
|
|
|
|
// RenameBranch rename a branch
|
|
|
|
func (repo *Repository) RenameBranch(from, to string) error {
|
2022-10-23 09:44:45 -05:00
|
|
|
_, _, err := NewCommand(repo.Ctx, "branch", "-m").AddDynamicArguments(from, to).RunStdString(&RunOpts{Dir: repo.Path})
|
2021-10-08 12:03:04 -05:00
|
|
|
return err
|
|
|
|
}
|