summaryrefslogtreecommitdiffstats
path: root/pkg/commands/sync.go
diff options
context:
space:
mode:
authorJesse Duffield <jessedduffield@gmail.com>2020-09-29 20:03:39 +1000
committerJesse Duffield <jessedduffield@gmail.com>2020-09-29 20:48:49 +1000
commit72af7e41778bca93d82fa668641f515fba1d92bc (patch)
tree7e755e857be72205ee99641d5eb5d4556151ad8f /pkg/commands/sync.go
parent1767f91047a35318f6b1e469199c8a7f547f2afc (diff)
factor out code from git.go
Diffstat (limited to 'pkg/commands/sync.go')
-rw-r--r--pkg/commands/sync.go74
1 files changed, 74 insertions, 0 deletions
diff --git a/pkg/commands/sync.go b/pkg/commands/sync.go
new file mode 100644
index 000000000..1321af2d3
--- /dev/null
+++ b/pkg/commands/sync.go
@@ -0,0 +1,74 @@
+package commands
+
+import (
+ "fmt"
+ "strings"
+)
+
+// usingGpg tells us whether the user has gpg enabled so that we can know
+// whether we need to run a subprocess to allow them to enter their password
+func (c *GitCommand) usingGpg() bool {
+ overrideGpg := c.Config.GetUserConfig().GetBool("git.overrideGpg")
+ if overrideGpg {
+ return false
+ }
+
+ gpgsign, _ := c.getLocalGitConfig("commit.gpgsign")
+ if gpgsign == "" {
+ gpgsign, _ = c.getGlobalGitConfig("commit.gpgsign")
+ }
+ value := strings.ToLower(gpgsign)
+
+ return value == "true" || value == "1" || value == "yes" || value == "on"
+}
+
+// Push pushes to a branch
+func (c *GitCommand) Push(branchName string, force bool, upstream string, args string, promptUserForCredential func(string) string) error {
+ forceFlag := ""
+ if force {
+ forceFlag = "--force-with-lease"
+ }
+
+ setUpstreamArg := ""
+ if upstream != "" {
+ setUpstreamArg = "--set-upstream " + upstream
+ }
+
+ cmd := fmt.Sprintf("git push --follow-tags %s %s %s", forceFlag, setUpstreamArg, args)
+ return c.OSCommand.DetectUnamePass(cmd, promptUserForCredential)
+}
+
+type FetchOptions struct {
+ PromptUserForCredential func(string) string
+ RemoteName string
+ BranchName string
+}
+
+// Fetch fetch git repo
+func (c *GitCommand) Fetch(opts FetchOptions) error {
+ command := "git fetch"
+
+ if opts.RemoteName != "" {
+ command = fmt.Sprintf("%s %s", command, opts.RemoteName)
+ }
+ if opts.BranchName != "" {
+ command = fmt.Sprintf("%s %s", command, opts.BranchName)
+ }
+
+ return c.OSCommand.DetectUnamePass(command, func(question string) string {
+ if opts.PromptUserForCredential != nil {
+ return opts.PromptUserForCredential(question)
+ }
+ return "\n"
+ })
+}
+
+func (c *GitCommand) FastForward(branchName string, remoteName string, remoteBranchName string, promptUserForCredential func(string) string) error {
+ command := fmt.Sprintf("git fetch %s %s:%s", remoteName, remoteBranchName, branchName)
+ return c.OSCommand.DetectUnamePass(command, promptUserForCredential)
+}
+
+func (c *GitCommand) FetchRemote(remoteName string, promptUserForCredential func(string) string) error {
+ command := fmt.Sprintf("git fetch %s", remoteName)
+ return c.OSCommand.DetectUnamePass(command, promptUserForCredential)
+}