diff options
author | James Fargher <jfargher@gitlab.com> | 2022-10-28 03:22:15 +0300 |
---|---|---|
committer | James Fargher <jfargher@gitlab.com> | 2022-10-28 04:31:59 +0300 |
commit | ac30fd8062d0d43e7ec6aaf3bf17d3aa776ec009 (patch) | |
tree | 1b7578773f1a7f1626e7dbc0d0b9709607f094fe | |
parent | 0d64f864c4c46a8c40e1cbb3a13347fa654bc1ec (diff) |
gitaly-git2go: Extract CommitNotFoundError
We are looking to expose the fact that a commit was not found at the RPC
level. In order to do this we need to distinguish the error in git2go by
creating a new error type over gob.
-rw-r--r-- | cmd/gitaly-git2go/cherry_pick_test.go | 6 | ||||
-rw-r--r-- | cmd/gitaly-git2go/revert_test.go | 4 | ||||
-rw-r--r-- | cmd/gitaly-git2go/util.go | 6 | ||||
-rw-r--r-- | internal/git2go/serialization.go | 12 |
4 files changed, 23 insertions, 5 deletions
diff --git a/cmd/gitaly-git2go/cherry_pick_test.go b/cmd/gitaly-git2go/cherry_pick_test.go index 82f11cd11..93308da28 100644 --- a/cmd/gitaly-git2go/cherry_pick_test.go +++ b/cmd/gitaly-git2go/cherry_pick_test.go @@ -133,14 +133,16 @@ func TestCherryPick(t *testing.T) { }, { desc: "fails on nonexistent ours commit", - expectedErrMsg: "cherry-pick: ours commit lookup: lookup commit \"nonexistent\": revspec 'nonexistent' not found", + expectedErrAs: &git2go.CommitNotFoundError{}, + expectedErrMsg: "cherry-pick: ours commit lookup: commit not found: \"nonexistent\"", }, { desc: "fails on nonexistent cherry-pick commit", ours: []gittest.TreeEntry{ {Path: "file", Content: "fooqux", Mode: "100644"}, }, - expectedErrMsg: "cherry-pick: commit lookup: lookup commit \"nonexistent\": revspec 'nonexistent' not found", + expectedErrAs: &git2go.CommitNotFoundError{}, + expectedErrMsg: "cherry-pick: commit lookup: commit not found: \"nonexistent\"", }, } for _, tc := range testcases { diff --git a/cmd/gitaly-git2go/revert_test.go b/cmd/gitaly-git2go/revert_test.go index 0c96988ee..b4f3fbe8c 100644 --- a/cmd/gitaly-git2go/revert_test.go +++ b/cmd/gitaly-git2go/revert_test.go @@ -155,7 +155,7 @@ func TestRevert_trees(t *testing.T) { return "nonexistent", revertOid.String() }, - expectedErr: "revert: ours commit lookup: lookup commit \"nonexistent\": revspec 'nonexistent' not found", + expectedErr: "revert: ours commit lookup: commit not found: \"nonexistent\"", }, { desc: "nonexistent revert fails", @@ -166,7 +166,7 @@ func TestRevert_trees(t *testing.T) { return oursOid.String(), "nonexistent" }, - expectedErr: "revert: revert commit lookup: lookup commit \"nonexistent\": revspec 'nonexistent' not found", + expectedErr: "revert: revert commit lookup: commit not found: \"nonexistent\"", }, } for _, tc := range testcases { diff --git a/cmd/gitaly-git2go/util.go b/cmd/gitaly-git2go/util.go index a6ecd2a33..187ce0708 100644 --- a/cmd/gitaly-git2go/util.go +++ b/cmd/gitaly-git2go/util.go @@ -6,11 +6,15 @@ import ( "fmt" git "github.com/libgit2/git2go/v34" + "gitlab.com/gitlab-org/gitaly/v15/internal/git2go" ) func lookupCommit(repo *git.Repository, ref string) (*git.Commit, error) { object, err := repo.RevparseSingle(ref) - if err != nil { + switch { + case git.IsErrorCode(err, git.ErrorCodeNotFound): + return nil, git2go.CommitNotFoundError{Revision: ref} + case err != nil: return nil, fmt.Errorf("lookup commit %q: %w", ref, err) } diff --git a/internal/git2go/serialization.go b/internal/git2go/serialization.go index b4967ca25..0f4949c5f 100644 --- a/internal/git2go/serialization.go +++ b/internal/git2go/serialization.go @@ -3,6 +3,7 @@ package git2go import ( "encoding/gob" "errors" + "fmt" "reflect" ) @@ -29,6 +30,7 @@ var registeredTypes = map[reflect.Type]struct{}{ reflect.TypeOf(EmptyError{}): {}, reflect.TypeOf(IndexError("")): {}, reflect.TypeOf(ConflictError{}): {}, + reflect.TypeOf(CommitNotFoundError{}): {}, } // Result is the serialized result. @@ -78,6 +80,16 @@ func (err EmptyError) Error() string { return "could not apply because the result was empty" } +// CommitNotFoundError indicates that the given commit rev could not be found. +type CommitNotFoundError struct { + // Revision used to lookup the commit + Revision string +} + +func (err CommitNotFoundError) Error() string { + return fmt.Sprintf("commit not found: %q", err.Revision) +} + // SerializableError returns an error that is Gob serializable. // Registered types are serialized directly. Unregistered types // are transformed in to an opaque error using their error message. |