2022-01-08 05:00:36 +02:00
|
|
|
package git_commands
|
2020-09-29 12:03:39 +02:00
|
|
|
|
|
|
|
import (
|
2021-10-19 13:41:19 +02:00
|
|
|
"github.com/go-errors/errors"
|
2023-07-09 03:32:27 +02:00
|
|
|
"github.com/jesseduffield/gocui"
|
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
|
|
|
}
|
|
|
|
|
2023-07-09 13:09:52 +02:00
|
|
|
func (self *SyncCommands) PushCmdObj(task gocui.Task, opts PushOpts) (oscommands.ICmdObj, error) {
|
2023-05-19 12:18:02 +02:00
|
|
|
if opts.UpstreamBranch != "" && opts.UpstreamRemote == "" {
|
|
|
|
return nil, errors.New(self.Tr.MustSpecifyOriginError)
|
2020-09-29 12:03:39 +02:00
|
|
|
}
|
|
|
|
|
2023-05-21 09:00:29 +02:00
|
|
|
cmdArgs := NewGitCmd("push").
|
2023-05-19 12:18:02 +02:00
|
|
|
ArgIf(opts.Force, "--force-with-lease").
|
|
|
|
ArgIf(opts.SetUpstream, "--set-upstream").
|
2023-05-21 09:00:29 +02:00
|
|
|
ArgIf(opts.UpstreamRemote != "", opts.UpstreamRemote).
|
|
|
|
ArgIf(opts.UpstreamBranch != "", opts.UpstreamBranch).
|
|
|
|
ToArgv()
|
2020-09-29 12:03:39 +02:00
|
|
|
|
2023-07-09 03:32:27 +02:00
|
|
|
cmdObj := self.cmd.New(cmdArgs).PromptOnCredentialRequest(task).WithMutex(self.syncMutex)
|
2021-12-31 01:04:32 +02:00
|
|
|
return cmdObj, nil
|
|
|
|
}
|
|
|
|
|
2023-07-09 13:09:52 +02:00
|
|
|
func (self *SyncCommands) Push(task gocui.Task, opts PushOpts) error {
|
2023-07-09 03:32:27 +02:00
|
|
|
cmdObj, err := self.PushCmdObj(task, 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
|
|
|
}
|
|
|
|
|
2023-07-09 13:09:52 +02:00
|
|
|
func (self *SyncCommands) FetchCmdObj(task gocui.Task) oscommands.ICmdObj {
|
2023-03-01 10:09:35 +02:00
|
|
|
cmdArgs := NewGitCmd("fetch").
|
|
|
|
ArgIf(self.UserConfig.Git.FetchAll, "--all").
|
|
|
|
ToArgv()
|
2020-09-29 12:03:39 +02:00
|
|
|
|
2023-05-21 09:00:29 +02:00
|
|
|
cmdObj := self.cmd.New(cmdArgs)
|
2023-07-09 03:32:27 +02:00
|
|
|
cmdObj.PromptOnCredentialRequest(task)
|
2023-07-09 13:09:52 +02:00
|
|
|
return cmdObj
|
2023-05-30 18:54:32 +02:00
|
|
|
}
|
|
|
|
|
2023-07-09 13:09:52 +02:00
|
|
|
func (self *SyncCommands) Fetch(task gocui.Task) error {
|
|
|
|
return self.FetchCmdObj(task).Run()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *SyncCommands) FetchBackgroundCmdObj() oscommands.ICmdObj {
|
2023-07-09 03:32:27 +02:00
|
|
|
cmdArgs := NewGitCmd("fetch").
|
|
|
|
ArgIf(self.UserConfig.Git.FetchAll, "--all").
|
|
|
|
ToArgv()
|
|
|
|
|
|
|
|
cmdObj := self.cmd.New(cmdArgs)
|
|
|
|
cmdObj.DontLog().FailOnCredentialRequest()
|
2023-07-09 13:09:52 +02:00
|
|
|
cmdObj.WithMutex(self.syncMutex)
|
|
|
|
return cmdObj
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *SyncCommands) FetchBackground() error {
|
|
|
|
return self.FetchBackgroundCmdObj().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
|
|
|
}
|
|
|
|
|
2023-07-09 13:09:52 +02:00
|
|
|
func (self *SyncCommands) Pull(task gocui.Task, opts PullOptions) error {
|
2023-05-21 09:00:29 +02:00
|
|
|
cmdArgs := NewGitCmd("pull").
|
2023-05-19 12:18:02 +02:00
|
|
|
Arg("--no-edit").
|
|
|
|
ArgIf(opts.FastForwardOnly, "--ff-only").
|
2023-05-21 09:00:29 +02:00
|
|
|
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.
|
2023-07-09 03:32:27 +02:00
|
|
|
return self.cmd.New(cmdArgs).AddEnvVars("GIT_SEQUENCE_EDITOR=:").PromptOnCredentialRequest(task).WithMutex(self.syncMutex).Run()
|
2021-10-20 13:21:16 +02:00
|
|
|
}
|
|
|
|
|
2023-07-09 13:09:52 +02:00
|
|
|
func (self *SyncCommands) FastForward(task gocui.Task, branchName string, remoteName string, remoteBranchName string) error {
|
2023-05-21 09:00:29 +02:00
|
|
|
cmdArgs := NewGitCmd("fetch").
|
|
|
|
Arg(remoteName).
|
|
|
|
Arg(remoteBranchName + ":" + branchName).
|
|
|
|
ToArgv()
|
2023-05-19 12:18:02 +02:00
|
|
|
|
2023-07-09 03:32:27 +02:00
|
|
|
return self.cmd.New(cmdArgs).PromptOnCredentialRequest(task).WithMutex(self.syncMutex).Run()
|
2021-10-20 13:21:16 +02:00
|
|
|
}
|
|
|
|
|
2023-07-09 13:09:52 +02:00
|
|
|
func (self *SyncCommands) FetchRemote(task gocui.Task, remoteName string) error {
|
2023-05-21 09:00:29 +02:00
|
|
|
cmdArgs := NewGitCmd("fetch").
|
|
|
|
Arg(remoteName).
|
|
|
|
ToArgv()
|
2023-05-19 12:18:02 +02:00
|
|
|
|
2023-07-09 03:32:27 +02:00
|
|
|
return self.cmd.New(cmdArgs).PromptOnCredentialRequest(task).WithMutex(self.syncMutex).Run()
|
2021-04-22 14:28:40 +02:00
|
|
|
}
|