forked from flynn/flynn
/
git.go
234 lines (196 loc) · 4.96 KB
/
git.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
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
package main
import (
"bufio"
"bytes"
"fmt"
"io/ioutil"
"os"
"os/exec"
"strings"
"syscall"
"github.com/flynn/flynn/Godeps/_workspace/src/github.com/flynn/go-docopt"
cfg "github.com/flynn/flynn/cli/config"
)
var gitRepo *bool
func inGitRepo() bool {
if gitRepo != nil {
return *gitRepo
}
b := exec.Command("git", "rev-parse", "--git-dir").Run() == nil
gitRepo = &b
return b
}
const gitURLSuffix = ".git"
func gitURL(conf *cfg.Cluster, app string) string {
prefix := gitHTTPURLPre(conf.GitURL)
if conf.SSHGit() {
prefix = gitSSHURLPre(conf.GitHost)
}
return prefix + app + gitURLSuffix
}
func gitSSHURLPre(gitHost string) string {
return "ssh://git@" + gitHost + "/"
}
func gitHTTPURLPre(url string) string {
return url + "/"
}
func mapOutput(out []byte, sep, term string) map[string]string {
m := make(map[string]string)
lines := strings.Split(string(out), term)
for _, line := range lines[:len(lines)-1] { // omit trailing ""
parts := strings.SplitN(line, sep, 2)
m[parts[0]] = parts[1]
}
return m
}
type remoteApp struct {
Cluster *cfg.Cluster
Name string
}
func gitRemoteNames() (results []string, err error) {
b, err := exec.Command("git", "remote").Output()
if err != nil {
return nil, err
}
s := bufio.NewScanner(bytes.NewBuffer(b))
s.Split(bufio.ScanWords)
for s.Scan() {
by := s.Bytes()
f := bytes.Fields(by)
results = append(results, string(f[0]))
}
if err = s.Err(); err != nil {
return nil, err
}
return
}
func gitRemotes() (map[string]remoteApp, error) {
b, err := exec.Command("git", "remote", "-v").Output()
if err != nil {
return nil, err
}
return parseGitRemoteOutput(b)
}
func appFromGitURL(remote string) *remoteApp {
for _, s := range config.Clusters {
if flagCluster != "" && s.Name != flagCluster {
continue
}
prefix := gitHTTPURLPre(s.GitURL)
if s.SSHGit() {
prefix = gitSSHURLPre(s.GitHost)
}
if strings.HasPrefix(remote, prefix) && strings.HasSuffix(remote, gitURLSuffix) {
return &remoteApp{s, remote[len(prefix) : len(remote)-len(gitURLSuffix)]}
}
}
return nil
}
func parseGitRemoteOutput(b []byte) (results map[string]remoteApp, err error) {
s := bufio.NewScanner(bytes.NewBuffer(b))
s.Split(bufio.ScanLines)
results = make(map[string]remoteApp)
for s.Scan() {
by := s.Bytes()
f := bytes.Fields(by)
if len(f) != 3 || string(f[2]) != "(push)" {
// this should have 3 tuples + be a push remote, skip it if not
continue
}
if app := appFromGitURL(string(f[1])); app != nil {
results[string(f[0])] = *app
}
}
if err = s.Err(); err != nil {
return nil, err
}
return
}
func remoteFromGitConfig() string {
b, err := exec.Command("git", "config", "flynn.remote").Output()
if err != nil {
return ""
}
return strings.TrimSpace(string(b))
}
type multipleRemotesError []string
func (remotes multipleRemotesError) Error() string {
return "error: Multiple apps listed in git remotes, please specify one with the global -a option to disambiguate.\n\nAvailable Flynn remotes:\n" + strings.Join(remotes, "\n")
}
func appFromGitRemote(remote string) (*remoteApp, error) {
if remote != "" {
b, err := exec.Command("git", "config", "remote."+remote+".url").Output()
if err != nil {
if isNotFound(err) {
wdir, _ := os.Getwd()
return nil, fmt.Errorf("could not find git remote "+remote+" in %s", wdir)
}
return nil, err
}
out := strings.TrimSpace(string(b))
app := appFromGitURL(out)
if app == nil {
return nil, fmt.Errorf("could not find app name in " + remote + " git remote")
}
return app, nil
}
// no remote specified, see if there is a single Flynn app remote
remotes, err := gitRemotes()
if err != nil {
return nil, nil // hide this error
}
if len(remotes) > 1 {
err := make(multipleRemotesError, 0, len(remotes))
for r := range remotes {
err = append(err, r)
}
return nil, err
}
for _, v := range remotes {
return &v, nil
}
return nil, fmt.Errorf("no apps in git remotes")
}
func isNotFound(err error) bool {
if ee, ok := err.(*exec.ExitError); ok {
if ws, ok := ee.ProcessState.Sys().(syscall.WaitStatus); ok {
return ws.ExitStatus() == 1
}
}
return false
}
func init() {
register("git-credentials", runGitCredentials, "usage: flynn git-credentials <operation>")
}
func runGitCredentials(args *docopt.Args) error {
if args.String["<operation>"] != "get" {
return nil
}
detailBytes, _ := ioutil.ReadAll(os.Stdin)
details := make(map[string]string)
for _, l := range bytes.Split(detailBytes, []byte("\n")) {
kv := bytes.SplitN(l, []byte("="), 2)
if len(kv) == 2 {
details[string(kv[0])] = string(kv[1])
}
}
if details["protocol"] != "https" {
return nil
}
if err := readConfig(); err != nil {
return nil
}
var cluster *cfg.Cluster
url := "https://" + details["host"]
for _, c := range config.Clusters {
if c.GitURL == url {
cluster = c
break
}
}
if cluster == nil {
return nil
}
fmt.Printf("protocol=https\nusername=user\nhost=%s\npassword=%s\n", details["host"], cluster.Key)
return nil
}