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

125 lines
3.2 KiB
Go
Raw Normal View History

2020-09-29 12:03:39 +02:00
package commands
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
)
// Push pushes to a branch
type PushOpts struct {
2021-12-31 01:04:32 +02:00
Force bool
UpstreamRemote string
UpstreamBranch string
SetUpstream bool
}
2021-12-31 01:04:32 +02:00
func (c *GitCommand) PushCmdObj(opts PushOpts) (oscommands.ICmdObj, error) {
2021-10-20 13:21:16 +02:00
cmdStr := "git push"
if opts.Force {
2021-10-20 13:21:16 +02:00
cmdStr += " --force-with-lease"
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 != "" {
2021-10-20 13:21:16 +02:00
cmdStr += " " + c.OSCommand.Quote(opts.UpstreamRemote)
}
if opts.UpstreamBranch != "" {
if opts.UpstreamRemote == "" {
2021-12-31 01:04:32 +02:00
return nil, errors.New(c.Tr.MustSpecifyOriginError)
}
2021-10-20 13:21:16 +02:00
cmdStr += " " + c.OSCommand.Quote(opts.UpstreamBranch)
2020-09-29 12:03:39 +02:00
}
2021-12-29 05:33:38 +02:00
cmdObj := c.Cmd.New(cmdStr)
2021-12-31 01:04:32 +02:00
return cmdObj, nil
}
func (c *GitCommand) Push(opts PushOpts, promptUserForCredential func(string) string) error {
cmdObj, err := c.PushCmdObj(opts)
if err != nil {
return err
}
return c.DetectUnamePass(cmdObj, promptUserForCredential)
2020-09-29 12:03:39 +02:00
}
type FetchOptions struct {
PromptUserForCredential func(string) string
RemoteName string
BranchName string
}
// Fetch fetch git repo
func (c *GitCommand) 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 != "" {
2021-10-20 13:21:16 +02:00
cmdStr = fmt.Sprintf("%s %s", cmdStr, c.OSCommand.Quote(opts.RemoteName))
2020-09-29 12:03:39 +02:00
}
if opts.BranchName != "" {
2021-10-20 13:21:16 +02:00
cmdStr = fmt.Sprintf("%s %s", cmdStr, c.OSCommand.Quote(opts.BranchName))
2020-09-29 12:03:39 +02:00
}
2021-12-29 05:33:38 +02:00
cmdObj := c.Cmd.New(cmdStr)
2022-01-05 02:57:32 +02:00
userInitiated := opts.PromptUserForCredential != nil
if !userInitiated {
cmdObj.DontLog()
}
return c.DetectUnamePass(cmdObj, func(question string) string {
2022-01-05 02:57:32 +02:00
if userInitiated {
2020-09-29 12:03:39 +02:00
return opts.PromptUserForCredential(question)
}
return "\n"
})
}
2021-10-20 13:21:16 +02:00
type PullOptions struct {
PromptUserForCredential func(string) string
RemoteName string
BranchName string
FastForwardOnly bool
2020-09-29 12:03:39 +02:00
}
2021-10-20 13:21:16 +02:00
func (c *GitCommand) Pull(opts PullOptions) error {
if opts.PromptUserForCredential == nil {
return errors.New("PromptUserForCredential is required")
}
2021-04-22 14:28:40 +02:00
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 != "" {
cmdStr = fmt.Sprintf("%s %s", cmdStr, c.OSCommand.Quote(opts.RemoteName))
2021-04-22 14:28:40 +02:00
}
2021-10-20 13:21:16 +02:00
if opts.BranchName != "" {
cmdStr = fmt.Sprintf("%s %s", cmdStr, c.OSCommand.Quote(opts.BranchName))
}
// setting GIT_SEQUENCE_EDITOR to ':' as a way of skipping it, in case the user
// has 'pull.rebase = interactive' configured.
2021-12-29 05:33:38 +02:00
cmdObj := c.Cmd.New(cmdStr).AddEnvVars("GIT_SEQUENCE_EDITOR=:")
return c.DetectUnamePass(cmdObj, opts.PromptUserForCredential)
2021-10-20 13:21:16 +02:00
}
func (c *GitCommand) FastForward(branchName string, remoteName string, remoteBranchName string, promptUserForCredential func(string) string) error {
cmdStr := fmt.Sprintf("git fetch %s %s:%s", c.OSCommand.Quote(remoteName), c.OSCommand.Quote(remoteBranchName), c.OSCommand.Quote(branchName))
2021-12-29 05:33:38 +02:00
cmdObj := c.Cmd.New(cmdStr)
return c.DetectUnamePass(cmdObj, promptUserForCredential)
2021-10-20 13:21:16 +02:00
}
func (c *GitCommand) FetchRemote(remoteName string, promptUserForCredential func(string) string) error {
cmdStr := fmt.Sprintf("git fetch %s", c.OSCommand.Quote(remoteName))
2021-12-29 05:33:38 +02:00
cmdObj := c.Cmd.New(cmdStr)
return c.DetectUnamePass(cmdObj, promptUserForCredential)
2021-04-22 14:28:40 +02:00
}