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

110 lines
2.8 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 (
"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) {
if opts.UpstreamBranch != "" && opts.UpstreamRemote == "" {
return nil, errors.New(self.Tr.MustSpecifyOriginError)
2020-09-29 12:03:39 +02:00
}
cmdArgs := NewGitCmd("push").
ArgIf(opts.Force, "--force-with-lease").
ArgIf(opts.SetUpstream, "--set-upstream").
ArgIf(opts.UpstreamRemote != "", opts.UpstreamRemote).
ArgIf(opts.UpstreamBranch != "", opts.UpstreamBranch).
ToArgv()
2020-09-29 12:03:39 +02:00
cmdObj := self.cmd.New(cmdArgs).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
2020-09-29 12:03:39 +02:00
}
// Fetch fetch git repo
func (self *SyncCommands) FetchCmdObj(opts FetchOptions) oscommands.ICmdObj {
cmdArgs := NewGitCmd("fetch").
ArgIf(self.UserConfig.Git.FetchAll, "--all").
ToArgv()
2020-09-29 12:03:39 +02:00
cmdObj := self.cmd.New(cmdArgs)
2022-01-02 01:34:33 +02:00
if opts.Background {
cmdObj.DontLog().FailOnCredentialRequest()
} else {
cmdObj.PromptOnCredentialRequest()
2022-01-05 02:57:32 +02:00
}
return cmdObj.WithMutex(self.syncMutex)
}
func (self *SyncCommands) Fetch(opts FetchOptions) error {
cmdObj := self.FetchCmdObj(opts)
return cmdObj.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 {
cmdArgs := NewGitCmd("pull").
Arg("--no-edit").
ArgIf(opts.FastForwardOnly, "--ff-only").
ArgIf(opts.RemoteName != "", opts.RemoteName).
ArgIf(opts.BranchName != "", opts.BranchName).
ToArgv()
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(cmdArgs).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 {
cmdArgs := NewGitCmd("fetch").
Arg(remoteName).
Arg(remoteBranchName + ":" + branchName).
ToArgv()
return self.cmd.New(cmdArgs).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 {
cmdArgs := NewGitCmd("fetch").
Arg(remoteName).
ToArgv()
return self.cmd.New(cmdArgs).PromptOnCredentialRequest().WithMutex(self.syncMutex).Run()
2021-04-22 14:28:40 +02:00
}