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()) }