1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-12-05 23:18:28 +02:00

simplify pull logic

This commit is contained in:
Jesse Duffield
2021-10-20 22:21:16 +11:00
parent 5ee559b896
commit 6388af70ac
14 changed files with 163 additions and 224 deletions

View File

@@ -2,7 +2,6 @@ package commands
import (
"fmt"
"sync"
"github.com/go-errors/errors"
)
@@ -17,32 +16,33 @@ type PushOpts struct {
}
func (c *GitCommand) Push(opts PushOpts) error {
cmd := "git push"
cmdStr := "git push"
if c.GetConfigValue("push.followTags") != "false" {
cmd += " --follow-tags"
cmdStr += " --follow-tags"
}
if opts.Force {
cmd += " --force-with-lease"
cmdStr += " --force-with-lease"
}
if opts.SetUpstream {
cmd += " --set-upstream"
cmdStr += " --set-upstream"
}
if opts.UpstreamRemote != "" {
cmd += " " + c.OSCommand.Quote(opts.UpstreamRemote)
cmdStr += " " + c.OSCommand.Quote(opts.UpstreamRemote)
}
if opts.UpstreamBranch != "" {
if opts.UpstreamRemote == "" {
return errors.New(c.Tr.MustSpecifyOriginError)
}
cmd += " " + c.OSCommand.Quote(opts.UpstreamBranch)
cmdStr += " " + c.OSCommand.Quote(opts.UpstreamBranch)
}
return c.OSCommand.DetectUnamePass(cmd, opts.PromptUserForCredential)
cmdObj := c.NewCmdObjFromStr(cmdStr)
return c.OSCommand.DetectUnamePass(cmdObj, opts.PromptUserForCredential)
}
type FetchOptions struct {
@@ -53,16 +53,17 @@ type FetchOptions struct {
// Fetch fetch git repo
func (c *GitCommand) Fetch(opts FetchOptions) error {
command := "git fetch"
cmdStr := "git fetch"
if opts.RemoteName != "" {
command = fmt.Sprintf("%s %s", command, c.OSCommand.Quote(opts.RemoteName))
cmdStr = fmt.Sprintf("%s %s", cmdStr, c.OSCommand.Quote(opts.RemoteName))
}
if opts.BranchName != "" {
command = fmt.Sprintf("%s %s", command, c.OSCommand.Quote(opts.BranchName))
cmdStr = fmt.Sprintf("%s %s", cmdStr, c.OSCommand.Quote(opts.BranchName))
}
return c.OSCommand.DetectUnamePass(command, func(question string) string {
cmdObj := c.NewCmdObjFromStr(cmdStr)
return c.OSCommand.DetectUnamePass(cmdObj, func(question string) string {
if opts.PromptUserForCredential != nil {
return opts.PromptUserForCredential(question)
}
@@ -70,41 +71,45 @@ func (c *GitCommand) Fetch(opts FetchOptions) error {
})
}
type PullOptions struct {
PromptUserForCredential func(string) string
RemoteName string
BranchName string
FastForwardOnly bool
}
func (c *GitCommand) Pull(opts PullOptions) error {
if opts.PromptUserForCredential == nil {
return errors.New("PromptUserForCredential is required")
}
cmdStr := "git pull --no-edit"
if opts.FastForwardOnly {
cmdStr += " --ff-only"
}
if opts.RemoteName != "" {
cmdStr = fmt.Sprintf("%s %s", cmdStr, c.OSCommand.Quote(opts.RemoteName))
}
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.
cmdObj := c.NewCmdObjFromStr(cmdStr).AddEnvVars("GIT_SEQUENCE_EDITOR=:")
return c.OSCommand.DetectUnamePass(cmdObj, opts.PromptUserForCredential)
}
func (c *GitCommand) FastForward(branchName string, remoteName string, remoteBranchName string, promptUserForCredential func(string) string) error {
command := fmt.Sprintf("git fetch %s %s:%s", c.OSCommand.Quote(remoteName), c.OSCommand.Quote(remoteBranchName), c.OSCommand.Quote(branchName))
return c.OSCommand.DetectUnamePass(command, promptUserForCredential)
cmdStr := fmt.Sprintf("git fetch %s %s:%s", c.OSCommand.Quote(remoteName), c.OSCommand.Quote(remoteBranchName), c.OSCommand.Quote(branchName))
cmdObj := c.NewCmdObjFromStr(cmdStr)
return c.OSCommand.DetectUnamePass(cmdObj, promptUserForCredential)
}
func (c *GitCommand) FetchRemote(remoteName string, promptUserForCredential func(string) string) error {
command := fmt.Sprintf("git fetch %s", c.OSCommand.Quote(remoteName))
return c.OSCommand.DetectUnamePass(command, promptUserForCredential)
}
func (c *GitCommand) GetPullMode(mode string) string {
if mode != "auto" {
return mode
}
var isRebase bool
var isFf bool
var wg sync.WaitGroup
wg.Add(2)
go func() {
isRebase = c.GetConfigValue("pull.rebase") == "true"
wg.Done()
}()
go func() {
isFf = c.GetConfigValue("pull.ff") == "only"
wg.Done()
}()
wg.Wait()
if isRebase {
return "rebase"
} else if isFf {
return "ff-only"
} else {
return "merge"
}
cmdStr := fmt.Sprintf("git fetch %s", c.OSCommand.Quote(remoteName))
cmdObj := c.NewCmdObjFromStr(cmdStr)
return c.OSCommand.DetectUnamePass(cmdObj, promptUserForCredential)
}