errors: add a function to extract exit status from error

Right now, we have some custom code to extract the exit status from an
appropriate error.  However, we'll need to use this in multiple places,
so let's refactor it out into a function.

We use the Go 1.13 errors package because we no longer test for Go 1.13
since our dependencies also use this package and there's no practical
way to avoid this dependency.
This commit is contained in:
brian m. carlson 2021-10-29 17:11:45 +00:00
parent a10748f18e
commit 44b8b6aba6
No known key found for this signature in database
GPG Key ID: 2D0C9BC12F82B3A1
2 changed files with 17 additions and 9 deletions

@ -1,9 +1,12 @@
package errors
import (
goerrors "errors"
"fmt"
"net/url"
"os/exec"
"strconv"
"syscall"
"time"
"github.com/pkg/errors"
@ -487,3 +490,14 @@ func parentOf(err error) error {
return nil
}
func ExitStatus(err error) int {
var eerr *exec.ExitError
if goerrors.As(err, &eerr) {
ws, ok := eerr.ProcessState.Sys().(syscall.WaitStatus)
if ok {
return ws.ExitStatus()
}
}
return -1
}

@ -14,13 +14,11 @@ import (
"io/ioutil"
"net/url"
"os"
"os/exec"
"path/filepath"
"regexp"
"strconv"
"strings"
"sync"
"syscall"
"time"
lfserrors "github.com/git-lfs/git-lfs/v3/errors"
@ -733,13 +731,9 @@ func GitAndRootDirs() (string, string, error) {
// If we got a fatal error, it's possible we're on a newer
// (2.24+) Git and we're not in a worktree, so fall back to just
// looking up the repo directory.
if e, ok := err.(*exec.ExitError); ok {
var ws syscall.WaitStatus
ws, ok = e.ProcessState.Sys().(syscall.WaitStatus)
if ok && ws.ExitStatus() == 128 {
absGitDir, err := GitDir()
return absGitDir, "", err
}
if lfserrors.ExitStatus(err) == 128 {
absGitDir, err := GitDir()
return absGitDir, "", err
}
return "", "", fmt.Errorf("failed to call git rev-parse --git-dir --show-toplevel: %q", buf.String())
}