2022-01-08 05:00:36 +02:00
|
|
|
package git_commands
|
2020-09-29 12:03:39 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2021-10-19 13:41:19 +02:00
|
|
|
|
|
|
|
"github.com/go-errors/errors"
|
2021-12-31 01:04:32 +02:00
|
|
|
"github.com/jesseduffield/lazygit/pkg/commands/oscommands"
|
2020-09-29 12:03:39 +02:00
|
|
|
)
|
|
|
|
|
2022-01-02 01:34:33 +02:00
|
|
|
type SyncCommands struct {
|
2022-01-18 12:26:21 +02:00
|
|
|
*GitCommon
|
2022-01-02 01:34:33 +02:00
|
|
|
}
|
|
|
|
|
2022-01-18 12:26:21 +02:00
|
|
|
func NewSyncCommands(gitCommon *GitCommon) *SyncCommands {
|
2022-01-02 01:34:33 +02:00
|
|
|
return &SyncCommands{
|
2022-01-18 12:26:21 +02:00
|
|
|
GitCommon: gitCommon,
|
2022-01-02 01:34:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-29 12:03:39 +02:00
|
|
|
// Push pushes to a branch
|
2021-10-19 13:41:19 +02:00
|
|
|
type PushOpts struct {
|
2021-12-31 01:04:32 +02:00
|
|
|
Force bool
|
|
|
|
UpstreamRemote string
|
|
|
|
UpstreamBranch string
|
|
|
|
SetUpstream bool
|
2021-10-19 13:41:19 +02:00
|
|
|
}
|
|
|
|
|
2022-01-02 01:34:33 +02:00
|
|
|
func (self *SyncCommands) PushCmdObj(opts PushOpts) (oscommands.ICmdObj, error) {
|
2021-10-20 13:21:16 +02:00
|
|
|
cmdStr := "git push"
|
2021-10-19 13:41:19 +02:00
|
|
|
|
|
|
|
if opts.Force {
|
2021-10-20 13:21:16 +02:00
|
|
|
cmdStr += " --force-with-lease"
|
2020-09-29 12:03:39 +02:00
|
|
|
}
|
|
|
|
|
2021-10-19 13:41:19 +02:00
|
|
|
if opts.SetUpstream {
|
2021-10-20 13:21:16 +02:00
|
|
|
cmdStr += " --set-upstream"
|
2021-10-19 13:41:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if opts.UpstreamRemote != "" {
|
2022-01-02 01:34:33 +02:00
|
|
|
cmdStr += " " + self.cmd.Quote(opts.UpstreamRemote)
|
2021-10-19 13:41:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if opts.UpstreamBranch != "" {
|
|
|
|
if opts.UpstreamRemote == "" {
|
2022-01-02 01:34:33 +02:00
|
|
|
return nil, errors.New(self.Tr.MustSpecifyOriginError)
|
2021-10-19 13:41:19 +02:00
|
|
|
}
|
2022-01-02 01:34:33 +02:00
|
|
|
cmdStr += " " + self.cmd.Quote(opts.UpstreamBranch)
|
2020-09-29 12:03:39 +02:00
|
|
|
}
|
|
|
|
|
2022-01-16 05:46:53 +02:00
|
|
|
cmdObj := self.cmd.New(cmdStr).PromptOnCredentialRequest().WithMutex(self.syncMutex)
|
2021-12-31 01:04:32 +02:00
|
|
|
return cmdObj, nil
|
|
|
|
}
|
|
|
|
|
2022-01-02 01:34:33 +02:00
|
|
|
func (self *SyncCommands) Push(opts PushOpts) error {
|
|
|
|
cmdObj, err := self.PushCmdObj(opts)
|
2021-12-31 01:04:32 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-01-02 01:34:33 +02:00
|
|
|
return cmdObj.Run()
|
2020-09-29 12:03:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type FetchOptions struct {
|
2022-01-02 01:34:33 +02:00
|
|
|
Background bool
|
|
|
|
RemoteName string
|
|
|
|
BranchName string
|
2020-09-29 12:03:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch fetch git repo
|
2022-01-02 01:34:33 +02:00
|
|
|
func (self *SyncCommands) Fetch(opts FetchOptions) error {
|
2021-10-20 13:21:16 +02:00
|
|
|
cmdStr := "git fetch"
|
2020-09-29 12:03:39 +02:00
|
|
|
|
|
|
|
if opts.RemoteName != "" {
|
2022-01-02 01:34:33 +02:00
|
|
|
cmdStr = fmt.Sprintf("%s %s", cmdStr, self.cmd.Quote(opts.RemoteName))
|
2020-09-29 12:03:39 +02:00
|
|
|
}
|
|
|
|
if opts.BranchName != "" {
|
2022-01-02 01:34:33 +02:00
|
|
|
cmdStr = fmt.Sprintf("%s %s", cmdStr, self.cmd.Quote(opts.BranchName))
|
2020-09-29 12:03:39 +02:00
|
|
|
}
|
|
|
|
|
2022-01-02 01:34:33 +02:00
|
|
|
cmdObj := self.cmd.New(cmdStr)
|
|
|
|
if opts.Background {
|
|
|
|
cmdObj.DontLog().FailOnCredentialRequest()
|
|
|
|
} else {
|
|
|
|
cmdObj.PromptOnCredentialRequest()
|
2022-01-05 02:57:32 +02:00
|
|
|
}
|
2022-01-16 05:46:53 +02:00
|
|
|
return cmdObj.WithMutex(self.syncMutex).Run()
|
2020-09-29 12:03:39 +02:00
|
|
|
}
|
|
|
|
|
2021-10-20 13:21:16 +02:00
|
|
|
type PullOptions struct {
|
2022-01-02 01:34:33 +02:00
|
|
|
RemoteName string
|
|
|
|
BranchName string
|
|
|
|
FastForwardOnly bool
|
2020-09-29 12:03:39 +02:00
|
|
|
}
|
|
|
|
|
2022-01-02 01:34:33 +02:00
|
|
|
func (self *SyncCommands) Pull(opts PullOptions) error {
|
2021-10-20 13:21:16 +02:00
|
|
|
cmdStr := "git pull --no-edit"
|
|
|
|
|
|
|
|
if opts.FastForwardOnly {
|
|
|
|
cmdStr += " --ff-only"
|
2021-04-22 14:28:40 +02:00
|
|
|
}
|
|
|
|
|
2021-10-20 13:21:16 +02:00
|
|
|
if opts.RemoteName != "" {
|
2022-01-02 01:34:33 +02:00
|
|
|
cmdStr = fmt.Sprintf("%s %s", cmdStr, self.cmd.Quote(opts.RemoteName))
|
2021-04-22 14:28:40 +02:00
|
|
|
}
|
2021-10-20 13:21:16 +02:00
|
|
|
if opts.BranchName != "" {
|
2022-01-02 01:34:33 +02:00
|
|
|
cmdStr = fmt.Sprintf("%s %s", cmdStr, self.cmd.Quote(opts.BranchName))
|
2021-10-20 13:21:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// setting GIT_SEQUENCE_EDITOR to ':' as a way of skipping it, in case the user
|
|
|
|
// has 'pull.rebase = interactive' configured.
|
2022-01-16 05:46:53 +02:00
|
|
|
return self.cmd.New(cmdStr).AddEnvVars("GIT_SEQUENCE_EDITOR=:").PromptOnCredentialRequest().WithMutex(self.syncMutex).Run()
|
2021-10-20 13:21:16 +02:00
|
|
|
}
|
|
|
|
|
2022-01-02 01:34:33 +02:00
|
|
|
func (self *SyncCommands) FastForward(branchName string, remoteName string, remoteBranchName string) error {
|
|
|
|
cmdStr := fmt.Sprintf("git fetch %s %s:%s", self.cmd.Quote(remoteName), self.cmd.Quote(remoteBranchName), self.cmd.Quote(branchName))
|
2022-01-16 05:46:53 +02:00
|
|
|
return self.cmd.New(cmdStr).PromptOnCredentialRequest().WithMutex(self.syncMutex).Run()
|
2021-10-20 13:21:16 +02:00
|
|
|
}
|
|
|
|
|
2022-01-02 01:34:33 +02:00
|
|
|
func (self *SyncCommands) FetchRemote(remoteName string) error {
|
|
|
|
cmdStr := fmt.Sprintf("git fetch %s", self.cmd.Quote(remoteName))
|
2022-01-16 05:46:53 +02:00
|
|
|
return self.cmd.New(cmdStr).PromptOnCredentialRequest().WithMutex(self.syncMutex).Run()
|
2021-04-22 14:28:40 +02:00
|
|
|
}
|