From 44b8b6aba6a5cb8bdfa5c15e1d5bfc3b5676193a Mon Sep 17 00:00:00 2001 From: "brian m. carlson" Date: Fri, 29 Oct 2021 17:11:45 +0000 Subject: [PATCH] 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. --- errors/types.go | 14 ++++++++++++++ git/git.go | 12 +++--------- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/errors/types.go b/errors/types.go index 71005d92..d175e9d8 100644 --- a/errors/types.go +++ b/errors/types.go @@ -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 +} diff --git a/git/git.go b/git/git.go index f9c1273d..28e62878 100644 --- a/git/git.go +++ b/git/git.go @@ -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()) }