1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2024-12-14 11:23:09 +02:00
lazygit/pkg/commands/git_commands/branch.go

175 lines
5.7 KiB
Go
Raw Normal View History

2022-01-08 05:00:36 +02:00
package git_commands
2020-09-29 12:03:39 +02:00
import (
"fmt"
"strings"
"github.com/jesseduffield/lazygit/pkg/commands/oscommands"
"github.com/jesseduffield/lazygit/pkg/utils"
)
2022-01-02 01:34:33 +02:00
type BranchCommands struct {
*GitCommon
2022-01-02 01:34:33 +02:00
}
func NewBranchCommands(gitCommon *GitCommon) *BranchCommands {
2022-01-02 01:34:33 +02:00
return &BranchCommands{
GitCommon: gitCommon,
2022-01-02 01:34:33 +02:00
}
}
// New creates a new branch
func (self *BranchCommands) New(name string, base string) error {
return self.cmd.New(fmt.Sprintf("git checkout -b %s %s", self.cmd.Quote(name), self.cmd.Quote(base))).Run()
2020-09-29 12:03:39 +02:00
}
// CurrentBranchInfo get the current branch information.
func (self *BranchCommands) CurrentBranchInfo() (BranchInfo, error) {
2022-01-02 01:34:33 +02:00
branchName, err := self.cmd.New("git symbolic-ref --short HEAD").DontLog().RunWithOutput()
2020-09-29 12:03:39 +02:00
if err == nil && branchName != "HEAD\n" {
trimmedBranchName := strings.TrimSpace(branchName)
return BranchInfo{
RefName: trimmedBranchName,
DisplayName: trimmedBranchName,
DetachedHead: false,
}, nil
2020-09-29 12:03:39 +02:00
}
output, err := self.cmd.New(`git branch --points-at=HEAD --format="%(HEAD)%00%(objectname)%00%(refname)"`).DontLog().RunWithOutput()
2020-09-29 12:03:39 +02:00
if err != nil {
return BranchInfo{}, err
2020-09-29 12:03:39 +02:00
}
for _, line := range utils.SplitLines(output) {
split := strings.Split(strings.TrimRight(line, "\r\n"), "\x00")
if len(split) == 3 && split[0] == "*" {
sha := split[1]
displayName := split[2]
return BranchInfo{
RefName: sha,
DisplayName: displayName,
DetachedHead: true,
}, nil
2020-09-29 12:03:39 +02:00
}
}
return BranchInfo{
RefName: "HEAD",
DisplayName: "HEAD",
DetachedHead: true,
}, nil
2020-09-29 12:03:39 +02:00
}
2022-01-02 01:34:33 +02:00
// Delete delete branch
func (self *BranchCommands) Delete(branch string, force bool) error {
2020-09-29 12:03:39 +02:00
command := "git branch -d"
if force {
command = "git branch -D"
}
2022-01-02 01:34:33 +02:00
return self.cmd.New(fmt.Sprintf("%s %s", command, self.cmd.Quote(branch))).Run()
2020-09-29 12:03:39 +02:00
}
// Checkout checks out a branch (or commit), with --force if you set the force arg to true
type CheckoutOptions struct {
Force bool
EnvVars []string
}
2022-01-02 01:34:33 +02:00
func (self *BranchCommands) Checkout(branch string, options CheckoutOptions) error {
2020-09-29 12:03:39 +02:00
forceArg := ""
if options.Force {
2021-04-10 09:31:23 +02:00
forceArg = " --force"
2020-09-29 12:03:39 +02:00
}
2021-12-07 12:59:36 +02:00
2022-01-02 01:34:33 +02:00
return self.cmd.New(fmt.Sprintf("git checkout%s %s", forceArg, self.cmd.Quote(branch))).
2021-12-07 12:59:36 +02:00
// prevents git from prompting us for input which would freeze the program
// TODO: see if this is actually needed here
AddEnvVars("GIT_TERMINAL_PROMPT=0").
2021-12-29 05:33:38 +02:00
AddEnvVars(options.EnvVars...).
Run()
2020-09-29 12:03:39 +02:00
}
2022-01-02 01:34:33 +02:00
// GetGraph gets the color-formatted graph of the log for the given branch
2020-09-29 12:03:39 +02:00
// Currently it limits the result to 100 commits, but when we get async stuff
// working we can do lazy loading
2022-01-02 01:34:33 +02:00
func (self *BranchCommands) GetGraph(branchName string) (string, error) {
return self.GetGraphCmdObj(branchName).DontLog().RunWithOutput()
2020-09-29 12:03:39 +02:00
}
2022-01-02 01:34:33 +02:00
func (self *BranchCommands) GetGraphCmdObj(branchName string) oscommands.ICmdObj {
branchLogCmdTemplate := self.UserConfig.Git.BranchLogCmd
2020-09-29 12:03:39 +02:00
templateValues := map[string]string{
2022-01-02 01:34:33 +02:00
"branchName": self.cmd.Quote(branchName),
2020-09-29 12:03:39 +02:00
}
2022-01-02 01:34:33 +02:00
return self.cmd.New(utils.ResolvePlaceholderString(branchLogCmdTemplate, templateValues)).DontLog()
2020-09-29 12:03:39 +02:00
}
func (self *BranchCommands) SetCurrentBranchUpstream(remoteName string, remoteBranchName string) error {
return self.cmd.New(fmt.Sprintf("git branch --set-upstream-to=%s/%s", self.cmd.Quote(remoteName), self.cmd.Quote(remoteBranchName))).Run()
2020-09-29 12:03:39 +02:00
}
2022-01-02 01:34:33 +02:00
func (self *BranchCommands) SetUpstream(remoteName string, remoteBranchName string, branchName string) error {
return self.cmd.New(fmt.Sprintf("git branch --set-upstream-to=%s/%s %s", self.cmd.Quote(remoteName), self.cmd.Quote(remoteBranchName), self.cmd.Quote(branchName))).Run()
2020-09-29 12:03:39 +02:00
}
func (self *BranchCommands) UnsetUpstream(branchName string) error {
return self.cmd.New(fmt.Sprintf("git branch --unset-upstream %s", self.cmd.Quote(branchName))).Run()
}
2020-09-29 12:03:39 +02:00
2022-01-02 01:34:33 +02:00
func (self *BranchCommands) GetCurrentBranchUpstreamDifferenceCount() (string, string) {
return self.GetCommitDifferences("HEAD", "HEAD@{u}")
}
func (self *BranchCommands) GetUpstreamDifferenceCount(branchName string) (string, string) {
return self.GetCommitDifferences(branchName, branchName+"@{u}")
2020-09-29 12:03:39 +02:00
}
// GetCommitDifferences checks how many pushables/pullables there are for the
// current branch
2022-01-02 01:34:33 +02:00
func (self *BranchCommands) GetCommitDifferences(from, to string) (string, string) {
2020-09-29 12:03:39 +02:00
command := "git rev-list %s..%s --count"
2022-01-02 01:34:33 +02:00
pushableCount, err := self.cmd.New(fmt.Sprintf(command, to, from)).DontLog().RunWithOutput()
2020-09-29 12:03:39 +02:00
if err != nil {
return "?", "?"
}
2022-01-02 01:34:33 +02:00
pullableCount, err := self.cmd.New(fmt.Sprintf(command, from, to)).DontLog().RunWithOutput()
2020-09-29 12:03:39 +02:00
if err != nil {
return "?", "?"
}
return strings.TrimSpace(pushableCount), strings.TrimSpace(pullableCount)
}
2022-01-02 01:34:33 +02:00
func (self *BranchCommands) IsHeadDetached() bool {
err := self.cmd.New("git symbolic-ref -q HEAD").DontLog().Run()
return err != nil
}
func (self *BranchCommands) Rename(oldName string, newName string) error {
return self.cmd.New(fmt.Sprintf("git branch --move %s %s", self.cmd.Quote(oldName), self.cmd.Quote(newName))).Run()
}
func (self *BranchCommands) GetRawBranches() (string, error) {
2022-03-26 14:55:44 +02:00
return self.cmd.New(`git for-each-ref --sort=-committerdate --format="%(HEAD)%00%(refname:short)%00%(upstream:short)%00%(upstream:track)" refs/heads`).DontLog().RunWithOutput()
2022-01-02 01:34:33 +02:00
}
2020-09-29 12:03:39 +02:00
type MergeOpts struct {
FastForwardOnly bool
}
2022-01-02 01:34:33 +02:00
func (self *BranchCommands) Merge(branchName string, opts MergeOpts) error {
2021-12-30 08:19:01 +02:00
mergeArg := ""
2022-01-02 01:34:33 +02:00
if self.UserConfig.Git.Merging.Args != "" {
mergeArg = " " + self.UserConfig.Git.Merging.Args
2021-12-30 08:19:01 +02:00
}
2020-09-29 12:03:39 +02:00
2022-01-02 01:34:33 +02:00
command := fmt.Sprintf("git merge --no-edit%s %s", mergeArg, self.cmd.Quote(branchName))
2020-09-29 12:03:39 +02:00
if opts.FastForwardOnly {
command = fmt.Sprintf("%s --ff-only", command)
}
2022-01-02 01:34:33 +02:00
return self.cmd.New(command).Run()
}
2022-01-08 04:22:29 +02:00
func (self *BranchCommands) AllBranchesLogCmdObj() oscommands.ICmdObj {
return self.cmd.New(self.UserConfig.Git.AllBranchesLogCmd).DontLog()
}