-
-
Notifications
You must be signed in to change notification settings - Fork 107
/
sync.go
76 lines (67 loc) · 2.07 KB
/
sync.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package cmd
import (
"fmt"
"github.com/spf13/cobra"
"strings"
)
// syncCmd represents the sync command
var syncCmd = &cobra.Command{
Use: "sync",
Short: "Synchronizes local changes with changes on origin or specified branch",
Long: `sync
sync origin master
sync local-branch
`,
Run: func(cmd *cobra.Command, args []string) {
RunInTerminalWithColor("git", []string{"fetch"})
// if possibly squashed
if IsDiverged() {
RunInTerminalWithColor("git", []string{"status", "-sb", "--untracked-files=no"})
yes := AskConfirm("Force (destructive) push to origin/" + CurrentBranch() + "?")
if yes {
RunInTerminalWithColor("git", []string{"push", "--force-with-lease"})
}
return
}
if !CloudBranchExists() {
RunInTerminalWithColor("git", []string{"push", "--set-upstream", "origin", CurrentBranch()})
save("")
RunInTerminalWithColor("git", []string{"push"})
return
}
save("")
if !CloudBranchExists() {
RunInTerminalWithColor("git", []string{"push", "--set-upstream", "origin", CurrentBranch()})
}
if IsAheadOfCurrent() {
RunInTerminalWithColor("git", []string{"push"})
} else {
RunInTerminalWithColor("git", []string{"pull", "-r"})
if len(args) > 0 {
RunInTerminalWithColor("git", append([]string{"pull", "-r"}, args...))
}
RunInTerminalWithColor("git", []string{"push"})
}
// After syncing with current branch and user wants to sync with another branch
if CurrentBranch() == "master" && len(args) == 1 && strings.HasSuffix(args[0], "master") {
yes := AskConfirm("Squash & merge this branch into master?")
if yes {
RunInTerminalWithColor("git", []string{"merge", "--squash"})
return
}
fmt.Printf("Cancelling...")
//RunInTerminalWithColor("git", []string{"stash", "pop"}) deprecated switch stashing
return
}
if len(args) == 1 {
branch := args[0]
refreshOnBranch(branch)
}
},
//Args: cobra.MaximumNArgs(1),
}
func init() {
ShellCmd.AddCommand(syncCmd)
// syncCmd.PersistentFlags().String("foo", "", "A help for foo")
// syncCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}