diff --git a/git/api/commit.go b/git/api/commit.go index 76e493797..bf4f96d54 100644 --- a/git/api/commit.go +++ b/git/api/commit.go @@ -192,6 +192,9 @@ func (g *Git) listCommitSHAs( if cErr.IsExitCode(128) && cErr.IsAmbiguousArgErr() { return nil, errors.NotFound("reference %q is ambiguous", ref) } + if cErr.IsExitCode(128) && cErr.IsBadObject() { + return nil, errors.NotFound("commit not found") + } return nil, processGitErrorf(err, "failed to trigger rev-list command") } diff --git a/git/api/diff.go b/git/api/diff.go index 1508bf774..f66339ba0 100644 --- a/git/api/diff.go +++ b/git/api/diff.go @@ -156,6 +156,7 @@ func cutLinesFromFullFileDiff(w io.Writer, r io.Reader, startLine, endLine int) return scanner.Err() } +//nolint:gocognit func (g *Git) RawDiff( ctx context.Context, w io.Writer, @@ -235,11 +236,14 @@ again: _ = pipeWrite.CloseWithError(err) }() - if err = newCmd.Run(ctx, - command.WithDir(repoPath), - command.WithStdout(pipeWrite), - ); err != nil { + err = newCmd.Run(ctx, command.WithDir(repoPath), command.WithStdout(pipeWrite)) + if err != nil { err = processGitErrorf(err, "git diff failed between %q and %q", baseRef, headRef) + if cErr := command.AsError(err); cErr != nil { + if cErr.IsExitCode(128) && cErr.IsBadObject() { + err = errors.NotFound("commit not found") + } + } } }() diff --git a/git/command/error.go b/git/command/error.go index bb424af64..9d32c923d 100644 --- a/git/command/error.go +++ b/git/command/error.go @@ -57,6 +57,10 @@ func (e *Error) IsAmbiguousArgErr() bool { return strings.Contains(e.Error(), "ambiguous argument") } +func (e *Error) IsBadObject() bool { + return strings.Contains(e.Error(), "bad object") +} + func (e *Error) IsInvalidRefErr() bool { return strings.Contains(e.Error(), "not a valid ref") }