-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsync.go
More file actions
158 lines (143 loc) · 3.87 KB
/
Copy pathsync.go
File metadata and controls
158 lines (143 loc) · 3.87 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package main
import (
"bytes"
"fmt"
"os"
"os/exec"
"strings"
)
func runSync() {
if err := verifyGitRepo(); err != nil {
fatal("not a git repository.")
}
if isDirtyWorkingTree() {
fmt.Println()
fmt.Printf(" you have unstaged changes. stash them first? [Y/n] › ")
input := strings.ToLower(strings.TrimSpace(readLine()))
if input == "n" || input == "no" {
fmt.Println(" aborted.")
fmt.Println()
return
}
if err := exec.Command("git", "stash", "push", "-m", "commitdog-sync-stash").Run(); err != nil {
fatal("stash failed: %v", err)
}
fmt.Println(" ✓ stashed. will pop after sync.")
defer func() {
fmt.Printf(" popping stash...")
if err := exec.Command("git", "stash", "pop").Run(); err != nil {
fmt.Printf("\n could not pop stash — run 'git stash pop' manually\n")
} else {
fmt.Println(" done")
}
}()
}
remotes := getRemotes()
if len(remotes) == 0 {
fatal("no remote configured. run 'commitdog init' first.")
}
branch := getCurrentBranch()
if branch == "" || branch == "HEAD" {
fatal("not on a branch.")
}
remote := remotes[0]
fmt.Println()
fmt.Printf(" syncing %s/%s\n", remote, branch)
fmt.Println()
fmt.Printf(" fetching...")
if err := gitFetch(remote); err != nil {
fmt.Println()
r := detectAndRecover(err.Error())
if r != nil && offerRecovery(r) {
return
}
fatal("fetch failed: %v", err)
}
fmt.Println(" done")
fmt.Printf(" pulling (rebase)...")
pulled, err := gitPullRebase(remote, branch)
if err != nil {
fmt.Println()
r := detectAndRecover(err.Error())
if r != nil {
if offerRecovery(r) {
return
}
fatal("sync failed: %s — %s", r.message, r.hint)
}
if isNothingToPull(err) {
fmt.Println(" already up to date")
} else {
fatal("pull failed: %v", err)
}
} else {
if strings.Contains(pulled, "Already up to date") || strings.Contains(pulled, "up to date") {
fmt.Println(" already up to date")
} else {
fmt.Println(" done")
}
}
fmt.Printf(" pushing...")
authHeader := currentAuthHeader()
var pushErr error
if !hasUpstream(branch) {
pushErr = runPushUpstreamWithAuth(remote, branch, authHeader)
} else {
pushErr = runPushWithAuth(remote, branch, authHeader)
}
if pushErr != nil {
fmt.Println()
if isNothingToPush(pushErr) {
fmt.Println(" nothing to push")
} else {
r := detectAndRecover(pushErr.Error())
if r != nil {
if offerRecovery(r) {
return
}
fatal("push failed: %s — %s", r.message, r.hint)
}
fatal("push failed: %v", pushErr)
}
} else {
fmt.Println(" done")
}
fmt.Printf("\n %s %s/%s is in sync\n\n", colorGreen("✓"), remote, branch)
}
func gitFetch(remote string) error {
if !isSafeGitRef(remote) {
return fmt.Errorf("invalid remote name")
}
cmd := exec.Command("git", "fetch", remote)
cmd.Env = append(os.Environ(), "GIT_PAGER=cat", "GIT_TERMINAL_PROMPT=0")
var stderr bytes.Buffer
cmd.Stderr = &stderr
if err := cmd.Run(); err != nil {
return fmt.Errorf("%s", strings.TrimSpace(stderr.String()))
}
return nil
}
func gitPullRebase(remote, branch string) (string, error) {
if !isSafeGitRef(remote) || !isSafeGitRef(branch) {
return "", fmt.Errorf("invalid remote or branch name")
}
cmd := exec.Command("git", "pull", "--rebase", remote, branch)
cmd.Env = append(os.Environ(), "GIT_PAGER=cat", "GIT_TERMINAL_PROMPT=0")
var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
if err := cmd.Run(); err != nil {
return "", fmt.Errorf("%s", strings.TrimSpace(stderr.String()))
}
return stdout.String() + stderr.String(), nil
}
func isNothingToPush(err error) bool {
msg := strings.ToLower(err.Error())
return strings.Contains(msg, "everything up-to-date") ||
strings.Contains(msg, "nothing to push")
}
func isNothingToPull(err error) bool {
msg := strings.ToLower(err.Error())
return strings.Contains(msg, "already up to date") ||
strings.Contains(msg, "up-to-date")
}