summaryrefslogtreecommitdiffstats
path: root/pkg/commands/sync.go
diff options
context:
space:
mode:
authorJesse Duffield <jessedduffield@gmail.com>2021-10-20 22:21:16 +1100
committerJesse Duffield <jessedduffield@gmail.com>2021-10-22 21:33:17 +1100
commit6388af70acda91bf1bdc9cba9f38dc810f49f9ca (patch)
tree1901eb73ccf719c04b99b933a0cc2182e6dd3a0c /pkg/commands/sync.go
parent5ee559b89660cb850040ce41fee242514a09ba8b (diff)
simplify pull logic
Diffstat (limited to 'pkg/commands/sync.go')
-rw-r--r--pkg/commands/sync.go91
1 files changed, 48 insertions, 43 deletions
diff --git a/pkg/commands/sync.go b/pkg/commands/sync.go
index 4fc7d73bb..7051f2236 100644
--- a/pkg/commands/sync.go
+++ b/pkg/commands/sync.go
@@ -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 {
})
}
-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)
+type PullOptions struct {
+ PromptUserForCredential func(string) string
+ RemoteName string
+ BranchName string
+ FastForwardOnly bool
}
-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) Pull(opts PullOptions) error {
+ if opts.PromptUserForCredential == nil {
+ return errors.New("PromptUserForCredential is required")
+ }
-func (c *GitCommand) GetPullMode(mode string) string {
- if mode != "auto" {
- return mode
+ cmdStr := "git pull --no-edit"
+
+ if opts.FastForwardOnly {
+ cmdStr += " --ff-only"
}
- 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"
+ 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 {
+ 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 {
+ cmdStr := fmt.Sprintf("git fetch %s", c.OSCommand.Quote(remoteName))
+ cmdObj := c.NewCmdObjFromStr(cmdStr)
+ return c.OSCommand.DetectUnamePass(cmdObj, promptUserForCredential)
}