Skip to content
This repository has been archived by the owner on Nov 1, 2022. It is now read-only.

Commit

Permalink
Give full output of command on git errors
Browse files Browse the repository at this point in the history
As a side effect, the tracing of commands also prints the full output
(instead of separating stderr and stdout), which I think is more useful.
  • Loading branch information
2opremio committed May 16, 2019
1 parent 416e842 commit 9f0ad02
Showing 1 changed file with 16 additions and 23 deletions.
39 changes: 16 additions & 23 deletions git/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,14 @@ package git
import (
"bufio"
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"strings"

"context"

"github.com/pkg/errors"
)

Expand Down Expand Up @@ -303,7 +301,7 @@ func changed(ctx context.Context, workingDir, ref string, subPaths []string) ([]
}

// traceGitCommand returns a log line that can be useful when debugging and developing git activity
func traceGitCommand(args []string, config gitCmdConfig, stdout string, stderr string) string {
func traceGitCommand(args []string, config gitCmdConfig, stdOutAndStdErr string) string {
for _, exemptedCommand := range exemptedTraceCommands {
if exemptedCommand == args[0] {
return ""
Expand All @@ -318,14 +316,12 @@ func traceGitCommand(args []string, config gitCmdConfig, stdout string, stderr s
}

command := `git ` + strings.Join(args, " ")
out := prepare(stdout)
err := prepare(stderr)
out := prepare(stdOutAndStdErr)

return fmt.Sprintf(
"TRACE: command=%q out=%q err=%q dir=%q env=%q",
"TRACE: command=%q out=%q dir=%q env=%q",
command,
out,
err,
config.dir,
strings.Join(config.env, ","),
)
Expand All @@ -339,30 +335,27 @@ func execGitCmd(ctx context.Context, args []string, config gitCmdConfig) error {
c.Dir = config.dir
}
c.Env = append(env(), config.env...)
c.Stdout = ioutil.Discard
if config.out != nil {
c.Stdout = config.out
}
errOut := &bytes.Buffer{}
c.Stderr = errOut

traceStdout := &bytes.Buffer{}
traceStderr := &bytes.Buffer{}
if trace {
c.Stdout = io.MultiWriter(c.Stdout, traceStdout)
c.Stderr = io.MultiWriter(c.Stderr, traceStderr)
stdOutAndStdErr := &bytes.Buffer{}
c.Stdout = stdOutAndStdErr
c.Stderr = stdOutAndStdErr
if config.out != nil {
c.Stdout = io.MultiWriter(c.Stdout, config.out)
}

err := c.Run()
if err != nil {
msg := findErrorMessage(errOut)
if msg != "" {
err = errors.New(msg)
if len(stdOutAndStdErr.Bytes()) > 0 {
err = errors.New(stdOutAndStdErr.String())
msg := findErrorMessage(stdOutAndStdErr)
if msg != "" {
err = fmt.Errorf("%s, full output: %s", msg, err.Error())
}
}
}

if trace {
if traceCommand := traceGitCommand(args, config, traceStdout.String(), traceStderr.String()); traceCommand != "" {
if traceCommand := traceGitCommand(args, config, stdOutAndStdErr.String()); traceCommand != "" {
println(traceCommand)
}
}
Expand Down

0 comments on commit 9f0ad02

Please sign in to comment.