@@ -3,7 +3,6 @@ package git
33import (
44 "errors"
55 "fmt"
6- "io/ioutil"
76 "os"
87 "os/exec"
98 "path/filepath"
@@ -14,23 +13,25 @@ import (
1413// ErrRemoteNotFound is returned when the remote cannot be found in git
1514var ErrRemoteNotFound = errors .New ("Could not find remote matching app in 'git remote -v'" )
1615
16+ func gitError (err * exec.ExitError , cmd []string ) error {
17+ msg := fmt .Sprintf ("Error when running '%s'\n " , strings .Join (cmd , " " ))
18+ out := string (err .Stderr )
19+ if out != "" {
20+ msg += strings .TrimSpace (out )
21+ }
22+
23+ return errors .New (msg )
24+ }
25+
1726// CreateRemote adds a git remote in the current directory.
1827func CreateRemote (host , remote , appID string ) error {
19- cmd := exec .Command ("git" , "remote" , "add" , remote , RemoteURL (host , appID ))
20- stderr , err := cmd .StderrPipe ()
21-
28+ cmd := []string {"git" , "remote" , "add" , remote , RemoteURL (host , appID )}
29+ _ , err := exec .Command (cmd [0 ], cmd [1 :]... ).Output ()
2230 if err != nil {
23- return err
24- }
25-
26- if err = cmd .Start (); err != nil {
27- return err
31+ return gitError (err .(* exec.ExitError ), cmd )
2832 }
2933
30- output , _ := ioutil .ReadAll (stderr )
31- fmt .Print (string (output ))
32-
33- return cmd .Wait ()
34+ return nil
3435}
3536
3637// DeleteAppRemotes removes all git remotes corresponding to an app in the repository.
@@ -52,23 +53,28 @@ func DeleteAppRemotes(host, appID string) error {
5253
5354// DeleteRemote removes a remote from the repository
5455func DeleteRemote (name string ) error {
55- _ , err := exec .Command ("git" , "remote" , "remove" , name ).Output ()
56- return err
56+ cmd := []string {"git" , "remote" , "remove" , name }
57+ _ , err := exec .Command (cmd [0 ], cmd [1 :]... ).Output ()
58+ if err != nil {
59+ return gitError (err .(* exec.ExitError ), cmd )
60+ }
61+
62+ return nil
5763}
5864
5965// remoteNamesFromAppID returns the git remote names for an app
6066func remoteNamesFromAppID (host , appID string ) ([]string , error ) {
61- out , err := exec .Command ("git" , "remote" , "-v" ).Output ()
67+ cmd := []string {"git" , "remote" , "-v" }
68+ out , err := exec .Command (cmd [0 ], cmd [1 :]... ).Output ()
6269
6370 if err != nil {
64- return []string {}, err
71+ return []string {}, gitError ( err .( * exec. ExitError ), cmd )
6572 }
6673
67- cmd := string (out )
6874 remotes := []string {}
6975
7076lines:
71- for _ , line := range strings .Split (cmd , "\n " ) {
77+ for _ , line := range strings .Split (string ( out ) , "\n " ) {
7278 if strings .Contains (line , RemoteURL (host , appID )) {
7379 name := strings .Split (line , "\t " )[0 ]
7480 // git remote -v can show duplicate remotes, so don't add a remote if it already has been added
@@ -103,19 +109,17 @@ func DetectAppName(host string) (string, error) {
103109}
104110
105111func findRemote (host string ) (string , error ) {
106- out , err := exec . Command ( "git" , "remote" , "-v" ). Output ()
107-
112+ cmd := [] string { "git" , "remote" , "-v" }
113+ out , err := exec . Command ( cmd [ 0 ], cmd [ 1 :] ... ). Output ()
108114 if err != nil {
109- return "" , err
115+ return "" , gitError ( err .( * exec. ExitError ), cmd )
110116 }
111117
112- cmd := string (out )
113-
114118 // Strip off any trailing :port number after the host name.
115119 host = strings .Split (host , ":" )[0 ]
116120 builderHost := getBuilderHostname (host )
117121
118- for _ , line := range strings .Split (cmd , "\n " ) {
122+ for _ , line := range strings .Split (string ( out ) , "\n " ) {
119123 for _ , remote := range strings .Split (line , " " ) {
120124 if strings .Contains (remote , host ) || strings .Contains (remote , builderHost ) {
121125 return strings .Split (remote , "\t " )[1 ], nil
@@ -142,14 +146,15 @@ func getBuilderHostname(host string) string {
142146
143147// RemoteValue gets the url that a git remote is set to.
144148func RemoteValue (name string ) (string , error ) {
145- out , err := exec .Command ("git" , "remote" , "get-url" , name ).Output ()
149+ cmd := []string {"git" , "remote" , "get-url" , name }
150+ out , err := exec .Command (cmd [0 ], cmd [1 :]... ).Output ()
146151
147152 if err != nil {
148153 // get the return code of the program and see if it equals not found
149154 if err .(* exec.ExitError ).Sys ().(syscall.WaitStatus ).ExitStatus () == 128 {
150155 return "" , ErrRemoteNotFound
151156 }
152- return "" , err
157+ return "" , gitError ( err .( * exec. ExitError ), cmd )
153158 }
154159
155160 return strings .Trim (string (out ), "\n " ), nil
0 commit comments