11package main
22
33import (
4- "bytes"
54 "errors"
65 "fmt"
7- "io"
86 "io/ioutil"
97 "os"
108 "os/exec"
@@ -18,34 +16,23 @@ func createBranch(name string) (string, error) {
1816 if err != nil {
1917 return "" , err
2018 }
21- var buf bytes.Buffer
22- err = gitCommand (& buf , "checkout" , "-b" , name )
23- if err != nil {
24- return "" , errors .New (strings .TrimSpace (buf .String ()))
25- }
2619
27- return strings . TrimSpace ( buf . String ()), nil
20+ return git ( "checkout" , "-b" , name )
2821}
2922
3023func hasStagedFiles () error {
31- var buf bytes.Buffer
32- err := gitCommand (& buf , "diff" , "--cached" , "--name-only" )
24+ msg , err := git ("diff" , "--cached" , "--name-only" )
3325 if err != nil {
3426 return err
3527 }
36- if strings . TrimSpace ( buf . String ()) == "" {
28+ if msg == "" {
3729 return errors .New ("There is no file to commit, please execute the `git add` command to add the commit file." )
3830 }
3931 return nil
4032}
4133
4234func currentBranch () (string , error ) {
43- var buf bytes.Buffer
44- err := gitCommand (& buf , "symbolic-ref" , "--short" , "HEAD" )
45- if err != nil {
46- return "" , errors .New (strings .TrimSpace (buf .String ()))
47- }
48- return strings .TrimSpace (buf .String ()), nil
35+ return git ("symbolic-ref" , "--short" , "HEAD" )
4936}
5037
5138func push () (string , error ) {
@@ -58,12 +45,12 @@ func push() (string, error) {
5845 if err != nil {
5946 return "" , err
6047 }
61- var buf bytes. Buffer
62- err = gitCommand ( & buf , "push" , "origin" , branch )
63- if err ! = nil {
64- return "" , errors . New ( strings . TrimSpace ( buf . String ()) )
48+
49+ msg , err := git ( "push" , "origin" , branch )
50+ if err = = nil {
51+ msg = fmt . Sprintf ( "Push to origin/%s success. \n \n %s" , branch , msg )
6552 }
66- return strings . TrimSpace ( buf . String ()), nil
53+ return msg , err
6754}
6855
6956func commitMessageCheck (f string ) error {
@@ -104,10 +91,9 @@ func commit(msg commitMsg) error {
10491 return err
10592 }
10693
107- var errBuf bytes.Buffer
108- err = gitCommand (& errBuf , "commit" , "-F" , f .Name ())
94+ _ , err = git ("commit" , "-F" , f .Name ())
10995 if err != nil {
110- return errors . New ( strings . TrimSpace ( errBuf . String ()))
96+ return err
11197 }
11298
11399 return nil
@@ -125,13 +111,12 @@ func gitAuthor() (string, string, error) {
125111 name := "Undefined"
126112 email := "Undefined"
127113
128- var buf bytes.Buffer
129- err := gitCommand (& buf , "var" , "GIT_AUTHOR_IDENT" )
114+ msg , err := git ("var" , "GIT_AUTHOR_IDENT" )
130115 if err != nil {
131116 return "" , "" , err
132117 }
133118
134- authorInfo := strings .Fields (buf . String () )
119+ authorInfo := strings .Fields (msg )
135120 if len (authorInfo ) > 1 && authorInfo [0 ] != "" {
136121 name = authorInfo [0 ]
137122 }
@@ -142,30 +127,25 @@ func gitAuthor() (string, string, error) {
142127}
143128
144129func repoCheck () error {
145- var buf bytes.Buffer
146- err := gitCommand (& buf , "rev-parse" , "--show-toplevel" )
147- if err != nil {
148- return errors .New (strings .TrimSpace (buf .String ()))
149- }
150- return nil
130+ _ , err := git ("rev-parse" , "--show-toplevel" )
131+ return err
151132}
152133
153- func gitCommand ( out io. Writer , cmds ... string ) error {
134+ func git ( cmds ... string ) ( string , error ) {
154135 var cmd * exec.Cmd
155- switch runtime .GOOS {
156- case "windows" :
136+ if runtime .GOOS == "windows" {
157137 cmd = exec .Command ("git.exe" , cmds ... )
158- case "linux" , "darwin" :
138+ } else {
159139 cmd = exec .Command ("git" , cmds ... )
160- default :
161- return fmt .Errorf ("unsupported platform" )
162140 }
163141
164- cmd .Stdin = os .Stdin
165- if out != nil {
166- cmd .Stdout = out
167- cmd .Stderr = out
142+ bs , err := cmd .CombinedOutput ()
143+ if err != nil {
144+ if bs != nil {
145+ return "" , errors .New (strings .TrimSpace (string (bs )))
146+ }
147+ return "" , err
168148 }
169149
170- return cmd . Run ()
150+ return strings . TrimSpace ( string ( bs )), nil
171151}
0 commit comments