From f25af74b44a02aacebe509fc7ecaeb83ffac01f2 Mon Sep 17 00:00:00 2001 From: Daisuke Taniwaki Date: Thu, 13 Jun 2019 13:18:09 +0900 Subject: [PATCH] Show better error message for unexisting commit --- pkg/ghost/git/file.go | 11 +++-------- pkg/ghost/git/validation.go | 7 ++++++- pkg/util/exec.go | 10 ++++++++++ 3 files changed, 19 insertions(+), 9 deletions(-) diff --git a/pkg/ghost/git/file.go b/pkg/ghost/git/file.go index 9ebd3d0..d7d7357 100644 --- a/pkg/ghost/git/file.go +++ b/pkg/ghost/git/file.go @@ -18,7 +18,6 @@ import ( "fmt" "os" "os/exec" - "syscall" "github.com/pfnet-research/git-ghost/pkg/util" "github.com/pfnet-research/git-ghost/pkg/util/errors" @@ -94,13 +93,9 @@ func AppendNonIndexedDiffFiles(dir, filepath string, nonIndexedFilepaths []strin cmd.Stdout = f ggerr := util.JustRunCmd(cmd) if ggerr != nil { - if exiterr, ok := ggerr.Cause().(*exec.ExitError); ok { - if status, ok := exiterr.Sys().(syscall.WaitStatus); ok { - // exit 1 is valid for git diff - if status.ExitStatus() == 1 { - continue - } - } + if util.GetExitCode(ggerr.Cause()) == 1 { + // exit 1 is valid for git diff + continue } errs = multierror.Append(errs, ggerr) } diff --git a/pkg/ghost/git/validation.go b/pkg/ghost/git/validation.go index ee040fd..c9a0942 100644 --- a/pkg/ghost/git/validation.go +++ b/pkg/ghost/git/validation.go @@ -30,7 +30,12 @@ func ValidateGit() errors.GitGhostError { // ValidateCommittish check committish is valid on dir func ValidateCommittish(dir, committish string) errors.GitGhostError { - return util.JustRunCmd( + output, err := util.JustOutputCmd( exec.Command("git", "-C", dir, "cat-file", "-e", committish), ) + if err != nil && util.GetExitCode(err.Cause()) == 1 && len(output) == 0 { + // exit 1 is for unexisting committish. + return errors.Errorf("%s does not exist", committish) + } + return err } diff --git a/pkg/util/exec.go b/pkg/util/exec.go index 817acab..399fe20 100644 --- a/pkg/util/exec.go +++ b/pkg/util/exec.go @@ -19,6 +19,7 @@ import ( "os" "os/exec" "strings" + "syscall" "github.com/pfnet-research/git-ghost/pkg/util/errors" @@ -62,3 +63,12 @@ func JustRunCmd(cmd *exec.Cmd) errors.GitGhostError { } return nil } + +func GetExitCode(err error) int { + if exiterr, ok := err.(*exec.ExitError); ok { + if status, ok := exiterr.Sys().(syscall.WaitStatus); ok { + return status.ExitStatus() + } + } + return -1 +}