diff --git a/git/api/commit.go b/git/api/commit.go index de2e02e66..76e493797 100644 --- a/git/api/commit.go +++ b/git/api/commit.go @@ -188,8 +188,10 @@ func (g *Git) listCommitSHAs( } output := &bytes.Buffer{} err := cmd.Run(ctx, command.WithDir(repoPath), command.WithStdout(output)) - if err != nil { - // TODO: handle error in case they don't have a common merge base! + if cErr := command.AsError(err); cErr != nil { + if cErr.IsExitCode(128) && cErr.IsAmbiguousArgErr() { + return nil, errors.NotFound("reference %q is ambiguous", ref) + } return nil, processGitErrorf(err, "failed to trigger rev-list command") } @@ -563,7 +565,7 @@ func (g *Git) GetFullCommitID( output := &bytes.Buffer{} err := cmd.Run(ctx, command.WithDir(repoPath), command.WithStdout(output)) if err != nil { - if strings.Contains(err.Error(), "exit status 128") { + if command.AsError(err).IsExitCode(128) { return sha.None, errors.NotFound("commit not found %s", shortID) } return sha.None, err diff --git a/git/api/ref.go b/git/api/ref.go index 8d0a0f015..a3a6f25f2 100644 --- a/git/api/ref.go +++ b/git/api/ref.go @@ -236,8 +236,8 @@ func (g *Git) GetRef( ) output := &bytes.Buffer{} err := cmd.Run(ctx, command.WithDir(repoPath), command.WithStdout(output)) - if err != nil { - if command.AsError(err).IsExitCode(128) && strings.Contains(err.Error(), "not a valid ref") { + if cErr := command.AsError(err); cErr != nil { + if cErr.IsExitCode(128) && cErr.IsInvalidRefErr() { return sha.None, errors.NotFound("reference %q not found", ref) } return sha.None, err diff --git a/git/api/rev.go b/git/api/rev.go index 4c2ac9810..3510446ce 100644 --- a/git/api/rev.go +++ b/git/api/rev.go @@ -18,7 +18,6 @@ import ( "bytes" "context" "fmt" - "strings" "github.com/harness/gitness/errors" "github.com/harness/gitness/git/command" @@ -33,7 +32,7 @@ func (g *Git) ResolveRev(ctx context.Context, output := &bytes.Buffer{} err := cmd.Run(ctx, command.WithDir(repoPath), command.WithStdout(output)) if err != nil { - if strings.Contains(err.Error(), "ambiguous argument") { + if command.AsError(err).IsAmbiguousArgErr() { return sha.None, errors.InvalidArgument("could not resolve git revision: %s", rev) } return sha.None, fmt.Errorf("failed to resolve git revision: %w", err) diff --git a/git/command/error.go b/git/command/error.go index 44aa0db00..bb424af64 100644 --- a/git/command/error.go +++ b/git/command/error.go @@ -18,6 +18,7 @@ import ( "errors" "fmt" "os/exec" + "strings" ) var ( @@ -52,6 +53,14 @@ func (e *Error) IsExitCode(code int) bool { return e.ExitCode() == code } +func (e *Error) IsAmbiguousArgErr() bool { + return strings.Contains(e.Error(), "ambiguous argument") +} + +func (e *Error) IsInvalidRefErr() bool { + return strings.Contains(e.Error(), "not a valid ref") +} + func (e *Error) Error() string { if len(e.StdErr) != 0 { return fmt.Sprintf("%s: %s", e.Err.Error(), e.StdErr) diff --git a/git/hook/refupdate.go b/git/hook/refupdate.go index ad6082c0f..65dd07f3a 100644 --- a/git/hook/refupdate.go +++ b/git/hook/refupdate.go @@ -290,8 +290,8 @@ func (u *RefUpdater) getRef(ctx context.Context) (sha.SHA, error) { ) output := &bytes.Buffer{} err := cmd.Run(ctx, command.WithDir(u.repoPath), command.WithStdout(output)) - if err != nil { - if command.AsError(err).IsExitCode(128) && strings.Contains(err.Error(), "not a valid ref") { + if cErr := command.AsError(err); cErr != nil { + if cErr.IsExitCode(128) && cErr.IsInvalidRefErr() { return sha.None, errors.NotFound("reference %q not found", u.ref) } return sha.None, err diff --git a/git/push_remote.go b/git/push_remote.go index 3dd72063f..23eabfc1f 100644 --- a/git/push_remote.go +++ b/git/push_remote.go @@ -48,14 +48,15 @@ func (s *Service) PushRemote(ctx context.Context, params *PushRemoteParams) erro } repoPath := getFullPathForRepo(s.reposRoot, params.RepoUID) - if ok, err := s.git.HasBranches(ctx, repoPath); ok { - if err != nil { - return errors.Internal(err, "push to repo failed") - } + isEmpty, err := s.git.HasBranches(ctx, repoPath) + if err != nil { + return errors.Internal(err, "push to repo failed") + } + if isEmpty { return errors.InvalidArgument("cannot push empty repo") } - err := s.git.Push(ctx, repoPath, api.PushOptions{ + err = s.git.Push(ctx, repoPath, api.PushOptions{ Remote: params.RemoteURL, Force: false, Env: nil, diff --git a/git/sharedrepo/sharedrepo.go b/git/sharedrepo/sharedrepo.go index 8b3d22252..333e4413c 100644 --- a/git/sharedrepo/sharedrepo.go +++ b/git/sharedrepo/sharedrepo.go @@ -241,7 +241,7 @@ func (r *SharedRepo) GetTreeSHA( command.WithStdout(stdout), ) if err != nil { - if strings.Contains(err.Error(), "ambiguous argument") { + if command.AsError(err).IsAmbiguousArgErr() { return sha.None, errors.NotFound("could not resolve git revision %q", rev) } return sha.None, fmt.Errorf("failed to get tree sha: %w", err)