Skip to content
Snippets Groups Projects
Commit 15c56172 authored by Georges Racinet's avatar Georges Racinet
Browse files

servicer: expose context manager for working directories on self

This wraps things together by passing down the proper root of working
directories for the given repo (as prescribed in the Gitaly `Repository`
message.

The test is a simplified version of a service method creating changesets.
parent dc2defec
No related branches found
No related tags found
2 merge requests!151heptapod#743: making 0.36 the new stable,!145Pool of repository working directories
......@@ -4,6 +4,7 @@
# GNU General Public License version 2 or any later version.
#
# SPDX-License-Identifier: GPL-2.0-or-later
from contextlib import contextmanager
import gc
import logging
import os
......@@ -7,6 +8,7 @@
import gc
import logging
import os
from pathlib import Path
import time
import weakref
......@@ -17,6 +19,8 @@
ui as uimod,
)
from mercurial.repoview import _filteredrepotypes
from .workdir import working_directory
from .stub.shared_pb2 import (
Repository,
)
......@@ -253,3 +257,19 @@
os.mkdir(to_create.pop(), mode=0o755)
return temp_dir
@contextmanager
def working_dir(self, gl_repo: Repository, repo, context, changeset=None):
"""Provide a working directory updated to the given changeset.
The working directory is part from the pool of reusable working
directories and created if needed.
"""
tmp = Path(os.fsdecode(
self.temp_dir(gl_repo.storage_name, context, ensure=True)
))
rpath = normalize_rpath(gl_repo)
with working_directory(tmp / 'workdirs' / rpath, repo,
changeset=changeset) as wd:
yield wd
......@@ -182,3 +182,37 @@
with pytest.raises(AbortContext):
servicer.temp_dir('broken', context, ensure=True)
assert context.code == grpc.StatusCode.INTERNAL
def test_working_dir(tmpdir):
context = FakeContext()
servicer = HGitalyServicer(dict(default=as_bytes(tmpdir)))
storage_root = tmpdir.join('repos')
storage_root_bytes = pycompat.sysbytes(str(storage_root))
servicer = HGitalyServicer(dict(storname=storage_root_bytes))
# context is used for error raising only
context = FakeContext()
wrapper = LocalRepoWrapper.init(storage_root.join('awesome-proj.hg'))
gl_repo = Repository(storage_name='storname',
relative_path='awesome-proj.hg')
repo = servicer.load_repo(gl_repo, context)
with servicer.working_dir(gl_repo, repo, context) as wd:
wd_path = wd.path
wd_wrapper = LocalRepoWrapper.load(wd_path)
sha = wd_wrapper.commit_file('foo').hex()
wrapper.reload()
ctx = wrapper.repo[sha]
assert ctx.hex() == sha
with servicer.working_dir(gl_repo, repo, context, changeset=ctx) as wd:
assert wd.path == wd_path
wd_wrapper.reload()
sha2 = wd_wrapper.commit_file('nar').hex()
wrapper.reload()
ctx2 = wrapper.repo[sha2]
assert ctx2.hex() == sha2
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment