Skip to content

Commit 722bb6b

Browse files
mknyszekgopherbot
authored andcommitted
sweet: only print git command stderr on failure
Currently a bunch of git helpers route git's stderr to Sweet's stderr, but this just pollutes the output if everything worked fine. This change captures the output and dumps it only on error. Change-Id: I37e780e1deb98b44f13595fb8d53fd0568651de9 Reviewed-on: https://go-review.googlesource.com/c/benchmarks/+/613955 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Auto-Submit: Michael Knyszek <mknyszek@google.com> Reviewed-by: Michael Pratt <mpratt@google.com>
1 parent a745fca commit 722bb6b

File tree

1 file changed

+26
-13
lines changed

1 file changed

+26
-13
lines changed

sweet/harnesses/common.go

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
package harnesses
66

77
import (
8+
"bytes"
9+
"fmt"
810
"os"
911
"os/exec"
1012
"path/filepath"
@@ -17,37 +19,48 @@ func gitShallowClone(dir, url, ref string) error {
1719
// Git 2.46+ has a global --no-advice flag, but that's extremely recent as of this writing.
1820
cmd := exec.Command("git", "-c", "advice.detachedHead=false", "clone", "--depth", "1", "-b", ref, url, dir)
1921
log.TraceCommand(cmd, false)
20-
cmd.Stderr = os.Stderr
21-
_, err := cmd.Output()
22-
return err
22+
var buf bytes.Buffer
23+
cmd.Stderr = &buf
24+
if _, err := cmd.Output(); err != nil {
25+
return fmt.Errorf("git shallow clone: %v: stderr:\n%s", err, &buf)
26+
}
27+
return nil
2328
}
2429

2530
func gitRecursiveCloneToCommit(dir, url, branch, hash string) error {
2631
cloneCmd := exec.Command("git", "clone", "--recursive", "--shallow-submodules", "-b", branch, url, dir)
2732
log.TraceCommand(cloneCmd, false)
28-
cloneCmd.Stderr = os.Stderr
33+
var buf bytes.Buffer
34+
cloneCmd.Stderr = &buf
2935
if _, err := cloneCmd.Output(); err != nil {
30-
return err
36+
return fmt.Errorf("git recursive clone: %v: stderr:\n%s", err, &buf)
3137
}
38+
buf.Reset()
3239
checkoutCmd := exec.Command("git", "-C", dir, "-c", "advice.detachedHead=false", "checkout", hash)
3340
log.TraceCommand(checkoutCmd, false)
34-
checkoutCmd.Stderr = os.Stderr
35-
_, err := checkoutCmd.Output()
36-
return err
41+
checkoutCmd.Stderr = &buf
42+
if _, err := checkoutCmd.Output(); err != nil {
43+
return fmt.Errorf("git checkout: %v: stderr:\n%s", err, &buf)
44+
}
45+
return nil
3746
}
3847

3948
func gitCloneToCommit(dir, url, branch, hash string) error {
4049
cloneCmd := exec.Command("git", "clone", "-b", branch, url, dir)
4150
log.TraceCommand(cloneCmd, false)
42-
cloneCmd.Stderr = os.Stderr
51+
var buf bytes.Buffer
52+
cloneCmd.Stderr = &buf
4353
if _, err := cloneCmd.Output(); err != nil {
44-
return err
54+
return fmt.Errorf("git recursive clone: %v: stderr:\n%s", err, &buf)
4555
}
56+
buf.Reset()
4657
checkoutCmd := exec.Command("git", "-C", dir, "-c", "advice.detachedHead=false", "checkout", hash)
4758
log.TraceCommand(checkoutCmd, false)
48-
checkoutCmd.Stderr = os.Stderr
49-
_, err := checkoutCmd.Output()
50-
return err
59+
checkoutCmd.Stderr = &buf
60+
if _, err := checkoutCmd.Output(); err != nil {
61+
return fmt.Errorf("git checkout: %v: stderr:\n%s", err, &buf)
62+
}
63+
return nil
5164
}
5265

5366
func copyFile(dst, src string) error {

0 commit comments

Comments
 (0)