Skip to content

Commit

Permalink
internal/vcs: show stderr when git exits with an error
Browse files Browse the repository at this point in the history
For example, rather than the unhelpful error

    running "git" ["-c" "log.showsignature=false" "log" "-1" "--format=%H:%ct"]: exit status 128

we would now show

    running "git" ["-c" "log.showsignature=false" "log" "-1" "--format=%H:%ct"]: exit status 128: fatal: your current branch 'master' does not have any commits yet

which is arguably quite long, but at least it's useful to understand
why git is failing.

Signed-off-by: Daniel Martí <[email protected]>
Change-Id: Ia7e40cb59fce41b3ec096bb95977cdeb11cea01b
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1198173
TryBot-Result: CUEcueckoo <[email protected]>
Unity-Result: CUE porcuepine <[email protected]>
Reviewed-by: Matthew Sackman <[email protected]>
  • Loading branch information
mvdan committed Jul 22, 2024
1 parent 243da87 commit d0724a0
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion internal/vcs/vcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package vcs

import (
"bytes"
"context"
"fmt"
"os"
Expand Down Expand Up @@ -115,7 +116,10 @@ func runCmd(ctx context.Context, dir string, cmdName string, args ...string) (st
cmd.Dir = dir

out, err := cmd.Output()
if err != nil {
if exitErr, ok := err.(*exec.ExitError); ok {
// git's stderr often ends with a newline, which is unnecessary.
return "", fmt.Errorf("running %q %q: %v: %s", cmdName, args, err, bytes.TrimSpace(exitErr.Stderr))
} else if err != nil {
return "", fmt.Errorf("running %q %q: %v", cmdName, args, err)
}
return string(out), nil
Expand Down

0 comments on commit d0724a0

Please sign in to comment.