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

127 lines
3.1 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"
"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 {
*GitCommon
2022-01-02 01:34:33 +02:00
}
func NewSyncCommands(gitCommon *GitCommon) *SyncCommands {
2022-01-02 01:34:33 +02:00
return &SyncCommands{
GitCommon: gitCommon,
2022-01-02 01:34:33 +02:00
}
}
2020-09-29 12:03:39 +02:00
// Push pushes to a branch
type PushOpts struct {
2021-12-31 01:04:32 +02:00
Force bool
UpstreamRemote string
UpstreamBranch string
SetUpstream bool
}
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"
if opts.Force {
if self.version.IsOlderThan(2, 30, 0) {
cmdStr += " --force-with-lease"
} else {
cmdStr += " --force-with-lease --force-if-includes"
}
2020-09-29 12:03:39 +02:00
}
if opts.SetUpstream {
2021-10-20 13:21:16 +02:00
cmdStr += " --set-upstream"
}
if opts.UpstreamRemote != "" {
2022-01-02 01:34:33 +02:00
cmdStr += " " + self.cmd.Quote(opts.UpstreamRemote)
}
if opts.UpstreamBranch != "" {
if opts.UpstreamRemote == "" {
2022-01-02 01:34:33 +02:00
return nil, errors.New(self.Tr.MustSpecifyOriginError)
}
2022-01-02 01:34:33 +02:00
cmdStr += " " + self.cmd.Quote(opts.UpstreamBranch)
2020-09-29 12:03:39 +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
}
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.
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))
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))
return self.cmd.New(cmdStr).PromptOnCredentialRequest().WithMutex(self.syncMutex).Run()
2021-04-22 14:28:40 +02:00
}