Commit 00f38176 authored by Sushil Khanchi's avatar Sushil Khanchi 🐨
Browse files

RepositoryService: implement FindMergeBase RPC

parent eb8506ce133b
......@@ -76,8 +76,28 @@
def FindMergeBase(self,
request: FindMergeBaseRequest,
context) -> FindMergeBaseResponse:
return not_implemented(context, FindMergeBaseResponse,
issue=26)
logger.debug("FindMergeBase request=%r", request)
repo = self.load_repo(request.repository, context)
if len(request.revisions) < 2:
# require at least 2 revisions
return invalid_argument(
context, FindMergeBaseResponse,
'FindMergeBase: at least 2 revisions are required'
)
ctxs = []
for rev in request.revisions:
ctx = gitlab_revision_changeset(repo, rev)
if ctx is None:
return FindMergeBaseResponse()
ctxs.append(ctx)
# As per Gitaly, In future we may support this method for more
# than two revisions, but for now we don't.
ctx_from, ctx_to = ctxs[0], ctxs[1]
gca = repo.revs(
b"ancestor(%d, %d)" % (ctx_from.rev(), ctx_to.rev())
).first()
base = repo[gca].hex() if gca is not None else ''
return FindMergeBaseResponse(base=base)
def RepositoryExists(self,
request: RepositoryExistsRequest,
......
......@@ -21,9 +21,10 @@
from hgitaly.tests.common import make_empty_repo
from hgitaly.stub.repository_service_pb2 import (
FindMergeBaseRequest,
GetArchiveRequest,
HasLocalBranchesRequest,
RepositoryExistsRequest,
WriteRefRequest,
)
from hgitaly.stub.repository_service_pb2_grpc import RepositoryServiceStub
......@@ -24,9 +25,13 @@
GetArchiveRequest,
HasLocalBranchesRequest,
RepositoryExistsRequest,
WriteRefRequest,
)
from hgitaly.stub.repository_service_pb2_grpc import RepositoryServiceStub
from mercurial import (
node as node_mod,
pycompat,
)
def test_repository_exists(grpc_channel, server_repos_root):
......@@ -225,3 +230,52 @@
assert extract_dir.join('archive-dir', 'foo').read() == large_content
del large_content
def test_find_merge_base(grpc_channel, server_repos_root):
repo_stub = RepositoryServiceStub(grpc_channel)
wrapper, grpc_repo = make_empty_repo(server_repos_root)
# repo structure:
#
# o 2 add animal (branch/stable)
# |
# | 1 add bar
# |/
# |
# o 0 add foo
#
ctx0 = wrapper.write_commit('foo')
sha0 = ctx0.hex()
sha1 = wrapper.write_commit('bar').hex()
sha2 = wrapper.write_commit('animal', branch='stable', parent=ctx0).hex()
# commting new root, to test no gca case
sha3 = wrapper.commit_file('tut', branch='other',
parent=node_mod.nullid).hex()
def do_rpc(revisions):
request = FindMergeBaseRequest(
repository=grpc_repo,
revisions=revisions,
)
response = repo_stub.FindMergeBase(request)
return pycompat.sysbytes(response.base)
# Actual test
assert do_rpc([sha1, sha2]) == sha0
assert do_rpc([b'branch/stable', sha1]) == sha0
# when no merge base (gca) found
assert do_rpc([sha0, sha3]) == b''
# test with invalid_argument, as it requires minimum 2 revisions
with pytest.raises(grpc.RpcError) as exc_info:
do_rpc([sha0])
assert exc_info.value.code() == grpc.StatusCode.INVALID_ARGUMENT
assert (
exc_info.value.details()
==
'FindMergeBase: at least 2 revisions are required'
)
sha_not_exists = b'deadnode' * 5
assert do_rpc([sha0, sha_not_exists]) == b''
Markdown is supported
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