# HG changeset patch
# User Georges Racinet <georges.racinet@octobus.net>
# Date 1688081986 -7200
#      Fri Jun 30 01:39:46 2023 +0200
# Branch stable
# Node ID 5c477c3311dddacda5469935c1f0082d623f60be
# Parent  6b54aa74ebab589e34dd0eae27554650b6add37e
CommitService.FindCommit: errors for corner cases

As usual, the Rust reimplementation raises some questions, and we answer them.
The case of empty (missing) revision was spotted because Gitaly returns
the error about it even if the repo argument is missing.

diff --git a/hgitaly/service/commit.py b/hgitaly/service/commit.py
--- a/hgitaly/service/commit.py
+++ b/hgitaly/service/commit.py
@@ -486,8 +486,11 @@
     def FindCommit(self,
                    request: FindCommitRequest, context) -> FindCommitResponse:
         logger = LoggerAdapter(base_logger, context)
+        revision = request.revision
+        if not revision:
+            context.abort(StatusCode.INVALID_ARGUMENT, "empty revision")
+
         repo = self.load_repo(request.repository, context)
-        revision = request.revision
         ctx = gitlab_revision_changeset(repo, revision)
 
         if ctx is None:
diff --git a/hgitaly/service/tests/test_commit.py b/hgitaly/service/tests/test_commit.py
--- a/hgitaly/service/tests/test_commit.py
+++ b/hgitaly/service/tests/test_commit.py
@@ -275,6 +275,11 @@
     # null revision
     assert not find_commit(b'0' * 40).HasField('commit')
 
+    # no revision
+    with pytest.raises(grpc.RpcError) as exc_info:
+        find_commit(revision=b'')
+    assert exc_info.value.code() == grpc.StatusCode.INVALID_ARGUMENT
+
 
 def test_find_commits(commit_fixture_empty_repo):
     fixture = commit_fixture_empty_repo
diff --git a/tests_with_gitaly/test_commit.py b/tests_with_gitaly/test_commit.py
--- a/tests_with_gitaly/test_commit.py
+++ b/tests_with_gitaly/test_commit.py
@@ -232,6 +232,7 @@
     assert_compare(revision=b'refs/heads/branch/default')
     assert_compare(revision=b'topic/default/sampletop')
     assert_compare(revision=b'refs/heads/topic/default/sampletop')
+    assert_compare(revision=b'refs/heads')
     assert_compare(revision=b'start-tag')
     assert_compare(revision=b'refs/tags/start-tag')
     assert_compare(revision=b'refs/tags/unknown')
@@ -279,6 +280,16 @@
             rpc_helper.rpc('hg', revision=hg_sha_tag).commit.id.encode()
         ) == rpc_helper.rpc('git', revision=git_sha_tag).commit.id.encode()
 
+    # error cases
+    assert_compare_errors = rpc_helper.assert_compare_errors
+    assert_compare_errors()  # no revision
+    assert_compare_errors(repository=None,
+                          revision=b'HEAD',
+                          same_details=False)
+    assert_compare_errors(repository=None)  # and no revision
+    fixture.gitaly_repo.relative_path = 'unknown/repo'
+    assert_compare_errors(revision=b'HEAD', same_details=False)
+
 
 def test_compare_find_commits(gitaly_comparison):
     fixture = gitaly_comparison