-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathui.go
More file actions
266 lines (245 loc) · 6.84 KB
/
Copy pathui.go
File metadata and controls
266 lines (245 loc) · 6.84 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
package main
import (
"bufio"
"bytes"
"fmt"
"os"
"os/exec"
"strings"
)
var reader = bufio.NewReader(os.Stdin)
func pickSuggestion(suggestions []string) string {
fmt.Println()
fmt.Println(" suggestions:")
fmt.Println()
for i, s := range suggestions {
fmt.Printf(" %d %s\n", i+1, subjectLine(s))
if preview := bodyPreview(s); preview != "" {
fmt.Printf(" %s\n", colorMuted(preview))
}
}
fmt.Println()
fmt.Printf(" [")
for i := range suggestions {
if i > 0 {
fmt.Printf("/")
}
fmt.Printf("%d", i+1)
}
fmt.Printf("] pick, [e] edit, [q] quit › ")
for {
input := readLine()
switch input {
case "q", "quit", "exit":
return ""
case "e", "edit":
return editMessage(suggestions[0])
}
for i := range suggestions {
if input == fmt.Sprintf("%d", i+1) {
return suggestions[i]
}
}
fmt.Printf(" enter ")
for i := range suggestions {
if i > 0 {
fmt.Printf("/")
}
fmt.Printf("%d", i+1)
}
fmt.Printf(", e, or q › ")
}
}
func editMessage(suggestion string) string {
fmt.Printf("\n edit message (enter to keep, ctrl+c to abort):\n")
fmt.Printf(" > %s\n", suggestion)
fmt.Printf(" > ")
input := readLine()
if input == "" {
return suggestion
}
cleaned := sanitizeMessage(input)
if cleaned == "" {
fmt.Println(" empty message, using original.")
return suggestion
}
return cleaned
}
func askPush(platform string) {
proj := loadProjectConfig()
if platform == "" {
platform = proj.effectivePrimary()
}
if platform == "" {
platform = "github"
}
branch := getCurrentBranch()
if branch == "" || branch == "HEAD" {
return
}
remote := remoteForPlatform(platform)
if !remoteExists(remote) {
fmt.Println()
c := loadConfig()
_, repoName := getRepoOwnerAndName()
if repoName == "" {
cwd, _ := os.Getwd()
if idx := strings.LastIndex(cwd, "/"); idx >= 0 {
repoName = cwd[idx+1:]
}
}
detectedURL, _ := buildOriginURL(platform, repoName, c)
if detectedURL == "" {
detectedURL, _ = fetchRepoCloneURL(platform, repoName, c)
}
if detectedURL != "" {
fmt.Printf(" remote '%s' not configured.\n\n", remote)
fmt.Printf(" detected: %s\n\n", detectedURL)
fmt.Println(" 1 connect to this repo")
fmt.Println(" 2 enter URL manually")
fmt.Println(" 3 skip")
fmt.Println()
fmt.Printf(" [1/2/3] pick › ")
switch strings.TrimSpace(readLine()) {
case "1":
if err := gitAddRemote(remote, detectedURL); err != nil {
exec.Command("git", "remote", "set-url", remote, detectedURL).Run()
}
fmt.Printf(" %s remote '%s' configured\n\n", colorGreen("✓"), remote)
case "2":
fmt.Printf(" remote URL › ")
url := strings.TrimSpace(readLine())
if url == "" {
return
}
if err := gitAddRemote(remote, url); err != nil {
fmt.Printf(" %s could not add remote: %v\n\n", colorRed("✗"), err)
return
}
fmt.Printf(" %s remote '%s' added\n\n", colorGreen("✓"), remote)
default:
fmt.Println(" skipped.")
return
}
} else {
fmt.Printf(" remote '%s' not configured.\n\n", remote)
fmt.Printf(" could not detect repo URL for %s — enter manually or skip.\n\n", platform)
fmt.Println(" 1 enter URL manually")
fmt.Println(" 2 skip")
fmt.Println()
fmt.Printf(" [1/2] pick › ")
if strings.TrimSpace(readLine()) != "1" {
fmt.Println(" skipped.")
return
}
fmt.Printf(" remote URL › ")
url := strings.TrimSpace(readLine())
if url == "" {
return
}
if err := gitAddRemote(remote, url); err != nil {
fmt.Printf(" %s could not add remote: %v\n\n", colorRed("✗"), err)
return
}
fmt.Printf(" %s remote '%s' added\n\n", colorGreen("✓"), remote)
}
}
fmt.Printf("\n push to %s/%s? [Y/n] › ", remote, branch)
for {
input := readLine()
switch input {
case "y", "yes", "":
fmt.Printf(" pushing...")
authHeader := authHeaderForPlatformName(platform)
var err error
if !hasUpstream(branch) {
err = runPushUpstreamWithAuth(remote, branch, authHeader)
} else {
err = runPushWithAuth(remote, branch, authHeader)
}
if err != nil {
fmt.Println()
r := detectAndRecover(err.Error())
if r != nil {
if offerRecovery(r) {
fmt.Printf(" retrying push...")
if !hasUpstream(branch) {
err = runPushUpstreamWithAuth(remote, branch, authHeader)
} else {
err = runPushWithAuth(remote, branch, authHeader)
}
if err != nil {
fmt.Printf("\n %s push failed: %s\n", colorRed("✗"), err)
} else {
fmt.Printf("\n %s pushed to %s/%s\n", colorGreen("✓"), remote, branch)
}
}
return
}
proj := loadProjectConfig()
mirrors := proj.mirrors
errMsg := err.Error()
isPlatformError := strings.Contains(errMsg, "403") || strings.Contains(errMsg, "permission") || strings.Contains(errMsg, "not allowed") || strings.Contains(errMsg, "authentication") || strings.Contains(errMsg, "not found") || strings.Contains(errMsg, "repository")
if len(mirrors) > 0 && isPlatformError {
fmt.Printf(" %s push to %s failed.\n\n", colorRed("✗"), remote)
fmt.Println(" 1 push to mirror instead")
fmt.Println(" 2 skip")
fmt.Println()
fmt.Printf(" [1/2] pick › ")
if strings.TrimSpace(readLine()) == "1" {
var availableMirrors []string
for _, m := range mirrors {
if remoteExists(platformRemoteName(m)) {
availableMirrors = append(availableMirrors, m)
}
}
if len(availableMirrors) == 0 {
fmt.Println(" no mirror remotes configured. run 'commitdog init' to add one.")
return
}
pushed := false
for _, m := range availableMirrors {
mirrorRemote := platformRemoteName(m)
mirrorAuth := authHeaderForPlatformName(m)
fmt.Printf(" trying %s...", m)
if merr := runPushWithAuth(mirrorRemote, branch, mirrorAuth); merr != nil {
fmt.Printf("\n %s push to %s failed: %s\n", colorRed("✗"), m, merr)
continue
}
fmt.Printf("\n %s pushed to %s/%s\n", colorGreen("✓"), mirrorRemote, branch)
pushed = true
break
}
if !pushed {
fmt.Printf("\n %s all platforms unreachable. check your network and tokens.\n\n", colorRed("✗"))
}
}
} else {
fmt.Printf(" %s push failed: %s\n", colorRed("✗"), err)
}
} else {
fmt.Printf("\n %s pushed to %s/%s\n", colorGreen("✓"), remote, branch)
}
return
case "n", "no":
fmt.Println(" skipped.")
return
default:
fmt.Printf(" Y or n › ")
}
}
}
func hasUpstream(branch string) bool {
cmd := exec.Command("git", "rev-parse", "--abbrev-ref", "--symbolic-full-name", branch+"@{upstream}")
var out bytes.Buffer
cmd.Stdout = &out
cmd.Stderr = nil
return cmd.Run() == nil
}
func readLine() string {
line, err := reader.ReadString('\n')
if err != nil {
return ""
}
return strings.TrimSpace(line)
}