/
hg.py
72 lines (56 loc) · 1.89 KB
/
hg.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
"""Tool to manage a local hg (Mercurial) working copy from a repository.
.. todo::
The following is from pypa/pip (MIT license):
- [`HgSync.convert_pip_url`](libvcs.hg.convert_pip_url)
- [`HgSync.get_url`](libvcs.hg.HgSync.get_url)
- [`HgSync.get_revision`](libvcs.hg.HgSync.get_revision)
""" # E5
import logging
import pathlib
from typing import Any
from libvcs._internal.types import StrPath
from libvcs.cmd.hg import Hg
from .base import BaseSync
logger = logging.getLogger(__name__)
class HgSync(BaseSync):
"""Tool to manage a local hg (Mercurial) repository cloned from a remote one."""
bin_name = "hg"
schemes = ("hg", "hg+http", "hg+https", "hg+file")
cmd: Hg
def __init__(
self,
*,
url: str,
path: StrPath,
**kwargs: Any,
) -> None:
"""Local Mercurial repository.
Parameters
----------
url : str
URL in subversion repository
"""
super().__init__(url=url, path=path, **kwargs)
self.cmd = Hg(path=path, progress_callback=self.progress_callback)
def obtain(self, *args: Any, **kwargs: Any) -> None:
"""Clone and update a Mercurial repository to this location."""
self.cmd.clone(
no_update=True,
quiet=True,
url=self.url,
)
self.cmd.update(
quiet=True,
check_returncode=True,
)
def get_revision(self) -> str:
"""Get latest revision of this mercurial repository."""
return self.run(["parents", "--template={rev}"])
def update_repo(self, *args: Any, **kwargs: Any) -> None:
"""Pull changes from remote Mercurial repository into this one."""
if not pathlib.Path(self.path / ".hg").exists():
self.obtain()
self.update_repo()
else:
self.cmd.update()
self.cmd.pull(update=True)