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

112 lines
3.0 KiB
Go
Raw Normal View History

2020-09-29 12:03:39 +02:00
package commands
import (
"fmt"
"github.com/go-errors/errors"
2020-09-29 12:03:39 +02:00
)
// Push pushes to a branch
type PushOpts struct {
Force bool
UpstreamRemote string
UpstreamBranch string
SetUpstream bool
PromptUserForCredential func(string) string
}
func (c *GitCommand) Push(opts PushOpts) 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 == "" {
return 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-07 12:59:36 +02:00
cmdObj := c.NewCmdObj(cmdStr)
return c.DetectUnamePass(cmdObj, opts.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-07 12:59:36 +02:00
cmdObj := c.NewCmdObj(cmdStr)
return c.DetectUnamePass(cmdObj, func(question string) string {
2020-09-29 12:03:39 +02:00
if opts.PromptUserForCredential != nil {
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-07 12:59:36 +02:00
cmdObj := c.NewCmdObj(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-07 12:59:36 +02:00
cmdObj := c.NewCmdObj(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-07 12:59:36 +02:00
cmdObj := c.NewCmdObj(cmdStr)
return c.DetectUnamePass(cmdObj, promptUserForCredential)
2021-04-22 14:28:40 +02:00
}