feat: [CODE-3061]: make git functions return not found if commit is not found (#3304)

* linter fix
* make git functions return not found if commit is not found
pull/3616/head
Marko Gaćeša 2025-01-21 16:15:41 +00:00 committed by Harness
parent 9290cf559e
commit 40dd6a19ed
3 changed files with 15 additions and 4 deletions

View File

@ -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")
}

View File

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

View File

@ -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")
}