forked from flynn/flynn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgit.go
163 lines (136 loc) · 3.36 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
package main
import (
"bufio"
"bytes"
"errors"
"fmt"
"os"
"os/exec"
"strings"
"syscall"
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 gitURLSuf = ".git"
func gitURLPre(gitHost string) string {
return "ssh://git@" + gitHost + "/"
}
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 strings.HasPrefix(remote, gitURLPre(s.GitHost)) && strings.HasSuffix(remote, gitURLSuf) {
return &remoteApp{s, remote[len(gitURLPre(s.GitHost)) : len(remote)-len(gitURLSuf)]}
}
}
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))
}
var errMultipleFlynnRemotes = errors.New("multiple apps in git remotes")
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 {
return nil, errMultipleFlynnRemotes
}
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
}