-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathgit.go
More file actions
156 lines (124 loc) · 3.8 KB
/
git.go
File metadata and controls
156 lines (124 loc) · 3.8 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
package git
import (
"errors"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strings"
"syscall"
)
// ErrRemoteNotFound is returned when the remote cannot be found in git
var ErrRemoteNotFound = errors.New("Could not find remote matching app in 'git remote -v'")
// CreateRemote adds a git remote in the current directory.
func CreateRemote(host, remote, appID string) error {
cmd := exec.Command("git", "remote", "add", remote, RemoteURL(host, appID))
stderr, err := cmd.StderrPipe()
if err != nil {
return err
}
if err = cmd.Start(); err != nil {
return err
}
output, _ := ioutil.ReadAll(stderr)
fmt.Print(string(output))
return cmd.Wait()
}
// DeleteAppRemotes removes all git remotes corresponding to an app in the repository.
func DeleteAppRemotes(host, appID string) error {
names, err := remoteNamesFromAppID(host, appID)
if err != nil {
return err
}
for _, name := range names {
if err := DeleteRemote(name); err != nil {
return err
}
}
return nil
}
// DeleteRemote removes a remote from the repository
func DeleteRemote(name string) error {
_, err := exec.Command("git", "remote", "remove", name).Output()
return err
}
// remoteNamesFromAppID returns the git remote names for an app
func remoteNamesFromAppID(host, appID string) ([]string, error) {
out, err := exec.Command("git", "remote", "-v").Output()
if err != nil {
return []string{}, err
}
cmd := string(out)
remotes := []string{}
lines:
for _, line := range strings.Split(cmd, "\n") {
if strings.Contains(line, RemoteURL(host, appID)) {
name := strings.Split(line, "\t")[0]
// git remote -v can show duplicate remotes, so don't add a remote if it already has been added
for _, remote := range remotes {
if remote == name {
continue lines
}
}
remotes = append(remotes, name)
}
}
if len(remotes) == 0 {
return remotes, ErrRemoteNotFound
}
return remotes, nil
}
// DetectAppName detects if there is deis remote in git.
func DetectAppName(host string) (string, error) {
remote, err := findRemote(host)
// Don't return an error if remote can't be found, return directory name instead.
if err != nil {
dir, err := os.Getwd()
return strings.ToLower(filepath.Base(dir)), err
}
ss := strings.Split(remote, "/")
return strings.Split(ss[len(ss)-1], ".")[0], nil
}
func findRemote(host string) (string, error) {
out, err := exec.Command("git", "remote", "-v").Output()
if err != nil {
return "", err
}
cmd := string(out)
// Strip off any trailing :port number after the host name.
host = strings.Split(host, ":")[0]
builderHost := getBuilderHostname(host)
for _, line := range strings.Split(cmd, "\n") {
for _, remote := range strings.Split(line, " ") {
if strings.Contains(remote, host) || strings.Contains(remote, builderHost) {
return strings.Split(remote, "\t")[1], nil
}
}
}
return "", ErrRemoteNotFound
}
// RemoteURL returns the git URL of app.
func RemoteURL(host, appID string) string {
// Strip off any trailing :port number after the host name.
host = strings.Split(host, ":")[0]
return fmt.Sprintf("ssh://git@%s:2222/%s.git", getBuilderHostname(host), appID)
}
// getBuilderHostname derives the builder host name from the controller host name.
func getBuilderHostname(host string) string {
hostTokens := strings.Split(host, ".")
hostTokens[0] = fmt.Sprintf("%s-builder", hostTokens[0])
return strings.Join(hostTokens, ".")
}
// RemoteValue gets the url that a git remote is set to.
func RemoteValue(name string) (string, error) {
out, err := exec.Command("git", "remote", "get-url", name).Output()
if err != nil {
// get the return code of the program and see if it equals not found
if err.(*exec.ExitError).Sys().(syscall.WaitStatus).ExitStatus() == 128 {
return "", ErrRemoteNotFound
}
return "", err
}
return strings.Trim(string(out), "\n"), nil
}