Commit c44b71ca authored by Raphaël Gomès's avatar Raphaël Gomès
Browse files

Implement `old_revision` parameter for `WriteRef` (#53) WIP

parent 2a1f33305231
Pipeline #20088 failed with stages
in 1 minute and 4 seconds
......@@ -8,6 +8,7 @@ from grpc import StatusCode
import logging
import tempfile
from hgitaly.git import NULL_COMMIT_ID
from mercurial import (
archival,
scmutil,
......@@ -23,7 +24,7 @@ from hgext3rd.heptapod.keep_around import (
from ..errors import (
invalid_argument,
not_implemented,
not_implemented, internal_error,
)
from ..branch import (
iter_gitlab_branches,
......@@ -164,6 +165,27 @@ class RepositoryServiceServicer(RepositoryServiceServicer, HGitalyServicer):
repo = self.load_repo(request.repository, context)
try:
expected_old_rev = request.old_revision
old_rev = gitlab_revision_changeset(repo, ref)
if old_rev is not None:
old_rev = old_rev.hex()
NULL_COMMIT_ID_BYTES = NULL_COMMIT_ID.encode('ascii')
is_null_rev = expected_old_rev == NULL_COMMIT_ID_BYTES
# FIXME probably not correct
expected_no_old_rev = (is_null_rev or len(expected_old_rev) == 0)
if expected_no_old_rev:
if old_rev is not None and old_rev != NULL_COMMIT_ID_BYTES:
msg = "Expected old rev to not exist, got %r" % old_rev
return invalid_argument(context, WriteRefResponse, msg)
elif expected_old_rev != old_rev:
return invalid_argument(
context,
WriteRefResponse,
"Expected old rev %r, got %r" % (expected_old_rev, old_rev)
)
special_ref_name = parse_special_ref(ref)
if special_ref_name is not None:
target_sha = gitlab_revision_changeset(repo, target)
......
......@@ -28,6 +28,8 @@ from hgitaly.stub.repository_service_pb2 import (
)
from hgitaly.stub.repository_service_pb2_grpc import RepositoryServiceStub
from hgitaly.git import NULL_COMMIT_ID
def test_repository_exists(grpc_channel, server_repos_root):
repo_stub = RepositoryServiceStub(grpc_channel)
......@@ -130,6 +132,54 @@ def test_write_special_refs_exceptions(
revision=b'76ac23fe' * 5))
def test_write_ref_with_old_value(grpc_channel, server_repos_root):
repo_stub = RepositoryServiceStub(grpc_channel)
wrapper, grpc_repo = make_empty_repo(server_repos_root)
change = wrapper.commit_file('foo')
change2 = wrapper.commit_file('foo2')
repo_stub.WriteRef(WriteRefRequest(
repository=grpc_repo,
ref=b'refs/merge-requests/12/head',
revision=change.hex())
)
# Pass the right old_revision
repo_stub.WriteRef(WriteRefRequest(
repository=grpc_repo,
ref=b'refs/merge-requests/12/head',
revision=change2.hex(),
old_revision=change.hex())
)
# Check that the old rev does not exist
repo_stub.WriteRef(WriteRefRequest(
repository=grpc_repo,
ref=b'refs/merge-requests/10/head',
revision=change2.hex(),
old_revision=NULL_COMMIT_ID.encode('ascii'))
)
with pytest.raises(grpc.RpcError) as exc_info:
# Pass the wrong old_revision
repo_stub.WriteRef(WriteRefRequest(
repository=grpc_repo,
ref=b'refs/merge-requests/42/head',
revision=change.hex(),
old_revision=change.hex())
)
assert exc_info.value.code() == grpc.StatusCode.INVALID_ARGUMENT
assert "Expected old rev %r" % change.hex() in exc_info.value.details()
with pytest.raises(grpc.RpcError) as exc_info:
# Wrongly expect old revision to not exist
repo_stub.WriteRef(WriteRefRequest(
repository=grpc_repo,
ref=b'refs/merge-requests/10/head',
revision=change.hex(),
old_revision=NULL_COMMIT_ID.encode('ascii'))
)
assert exc_info.value.code() == grpc.StatusCode.INVALID_ARGUMENT
assert "Expected old rev to not exist" in exc_info.value.details()
@contextmanager
def get_archive_tarfile(stub, grpc_repo, commit_id, path=b''):
with BytesIO() as fobj:
......
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