@@ -17,7 +17,6 @@ package git
1717import (
1818 "bytes"
1919 "fmt"
20- "io"
2120 "log"
2221 "os"
2322 "os/exec"
@@ -87,17 +86,16 @@ func ParseChangeInfo(googleapisDir string, hashes []string, gapicPkgs map[string
8786 var changes []* ChangeInfo
8887 for _ , hash := range hashes {
8988 // Get commit title and body
90- rawBody := bytes .NewBuffer (nil )
9189 c := execv .Command ("git" , "show" , "--pretty=format:%s~~%b" , "-s" , hash )
92- c .Stdout = rawBody
9390 c .Dir = googleapisDir
94- if err := c .Run (); err != nil {
91+ b , err := c .Output ()
92+ if err != nil {
9593 return nil , err
9694 }
9795
98- ss := strings .Split (rawBody . String ( ), "~~" )
96+ ss := strings .Split (string ( b ), "~~" )
9997 if len (ss ) != 2 {
100- return nil , fmt .Errorf ("expected two segments for commit, got %d: %q " , len (ss ), rawBody . String () )
98+ return nil , fmt .Errorf ("expected two segments for commit, got %d: %s " , len (ss ), b )
10199 }
102100 title , body := strings .TrimSpace (ss [0 ]), strings .TrimSpace (ss [1 ])
103101
@@ -154,46 +152,38 @@ func CommitsSinceHash(gitDir, hash string, inclusive bool) ([]string, error) {
154152 commitRange = fmt .Sprintf ("%s.." , hash )
155153 }
156154
157- out := bytes .NewBuffer (nil )
158155 c := execv .Command ("git" , "rev-list" , commitRange )
159- c .Stdout = out
160156 c .Dir = gitDir
161- if err := c .Run (); err != nil {
157+ b , err := c .Output ()
158+ if err != nil {
162159 return nil , err
163160 }
164- return strings .Split (strings .TrimSpace (out . String ( )), "\n " ), nil
161+ return strings .Split (strings .TrimSpace (string ( b )), "\n " ), nil
165162}
166163
167164// UpdateFilesSinceHash returns a listed of files updated since the provided
168165// hash for the given gitDir.
169166func UpdateFilesSinceHash (gitDir , hash string ) ([]string , error ) {
170- out := bytes .NewBuffer (nil )
171167 // The provided diff-filter flags restricts to files that have been:
172168 // - (A) Added
173169 // - (C) Copied
174170 // - (M) Modified
175171 // - (R) Renamed
176172 c := execv .Command ("git" , "diff-tree" , "--no-commit-id" , "--name-only" , "--diff-filter=ACMR" , "-r" , fmt .Sprintf ("%s..HEAD" , hash ))
177- c .Stdout = out
178173 c .Dir = gitDir
179- if err := c .Run (); err != nil {
174+ b , err := c .Output ()
175+ if err != nil {
180176 return nil , err
181177 }
182- return strings .Split (out . String ( ), "\n " ), nil
178+ return strings .Split (string ( b ), "\n " ), nil
183179}
184180
185181// HasChanges reports whether the given directory has uncommitted git changes.
186182func HasChanges (dir string ) (bool , error ) {
187- // Write command output to both os.Stderr and local, so that we can check
188- // whether there are modified files.
189- inmem := & bytes.Buffer {}
190- w := io .MultiWriter (os .Stderr , inmem )
191-
192183 c := execv .Command ("bash" , "-c" , "git status --short" )
193184 c .Dir = dir
194- c .Stdout = w
195- err := c .Run ()
196- return inmem .Len () > 0 , err
185+ b , err := c .Output ()
186+ return len (b ) > 0 , err
197187}
198188
199189// DeepClone clones a repository in the given directory.
@@ -249,12 +239,11 @@ func FileDiff(dir, filename string) (string, error) {
249239// filesChanged returns a list of files changed in a commit for the provdied
250240// hash in the given gitDir.
251241func filesChanged (gitDir , hash string ) ([]string , error ) {
252- out := bytes .NewBuffer (nil )
253242 c := execv .Command ("git" , "show" , "--pretty=format:" , "--name-only" , hash )
254- c .Stdout = out
255243 c .Dir = gitDir
256- if err := c .Run (); err != nil {
244+ b , err := c .Output ()
245+ if err != nil {
257246 return nil , err
258247 }
259- return strings .Split (out . String ( ), "\n " ), nil
248+ return strings .Split (string ( b ), "\n " ), nil
260249}
0 commit comments