Skip to content
Snippets Groups Projects
Unverified Commit 0eab2faf authored by Karthik Nayak's avatar Karthik Nayak
Browse files

ref: Handle `git.ErrReferenceAmbiguous` in `FindBranch`

The `repo.GetReference` can throw a `git.ErrReferenceAmbiguous` error
when the provided reference is ambiguous. We shouldn't treat these
errors as internal errors as they would then be counted towards the
error budget. So catch these errors and treat them as
`NewInvalidArgument` errors.
parent 33b368ee
Tags
No related merge requests found
......@@ -26,6 +26,11 @@ func (s *server) FindBranch(ctx context.Context, req *gitalypb.FindBranchRequest
if errors.Is(err, git.ErrReferenceNotFound) {
return &gitalypb.FindBranchResponse{}, nil
}
if errors.Is(err, git.ErrReferenceAmbiguous) {
return nil, structerr.NewInvalidArgument("target reference is ambiguous: %w", err)
}
return nil, err
}
commit, err := repo.ReadCommit(ctx, git.Revision(branchRef.Target))
......
......@@ -87,7 +87,8 @@ func TestFailedFindBranchRequest(t *testing.T) {
ctx := testhelper.Context(t)
cfg, client := setupRefService(t)
repo, _ := gittest.CreateRepository(t, ctx, cfg)
repo, repoPath := gittest.CreateRepository(t, ctx, cfg)
gittest.WriteCommit(t, cfg, repoPath, gittest.WithBranch("branch"), gittest.WithMessage("branch"))
testCases := []struct {
desc string
......@@ -106,6 +107,12 @@ func TestFailedFindBranchRequest(t *testing.T) {
branchName: "",
expectedErr: status.Error(codes.InvalidArgument, "Branch name cannot be empty"),
},
{
desc: "ambiguous branch name",
repo: repo,
branchName: "b*",
expectedErr: structerr.NewInvalidArgument(`target reference is ambiguous: reference is ambiguous: conflicts with "refs/heads/branch"`),
},
}
for _, testCase := range testCases {
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment