Skip to content

Commit e242a15

Browse files
committed
feat(hg): run command
1 parent d8e4a7b commit e242a15

File tree

1 file changed

+204
-2
lines changed

1 file changed

+204
-2
lines changed

libvcs/cmd/hg.py

Lines changed: 204 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,208 @@
1+
import enum
12
import pathlib
3+
from typing import Optional, Sequence, Union
4+
5+
from ..types import StrOrBytesPath, StrOrPath
6+
from .core import run
7+
8+
_CMD = Union[StrOrBytesPath, Sequence[StrOrBytesPath]]
9+
10+
11+
class HgColorType(enum.Enum):
12+
boolean = "boolean"
13+
always = "always"
14+
auto = "auto"
15+
never = "never"
16+
debug = "debug"
17+
18+
19+
class HgPagerType(enum.Enum):
20+
boolean = "boolean"
21+
always = "always"
22+
auto = "auto"
23+
never = "never"
224

325

426
class Hg:
5-
def __init__(self, dir: pathlib.Path):
6-
self.dir: pathlib.Path = dir
27+
def __init__(self, dir: StrOrPath):
28+
"""Lite, typed, pythonic wrapper for hg(1).
29+
30+
Parameters
31+
----------
32+
dir :
33+
Operates as PATH in the corresonding hg subcommand.
34+
35+
Examples
36+
--------
37+
>>> Hg(dir=tmp_path)
38+
<Hg dir=...>
39+
"""
40+
#: Directory to check out
41+
self.dir: pathlib.Path
42+
if isinstance(dir, pathlib.Path):
43+
self.dir = dir
44+
else:
45+
self.dir = pathlib.Path(dir)
46+
47+
def __repr__(self):
48+
return f"<Hg dir={self.dir}>"
49+
50+
def run(
51+
self,
52+
args: _CMD,
53+
config: Optional[str] = None,
54+
repository: Optional[str] = None,
55+
quiet: Optional[bool] = None,
56+
encoding: Optional[str] = None,
57+
encoding_mode: Optional[str] = None,
58+
verbose: Optional[bool] = None,
59+
traceback: Optional[bool] = None,
60+
debug: Optional[bool] = None,
61+
debugger: Optional[bool] = None,
62+
profile: Optional[bool] = None,
63+
version: Optional[bool] = None,
64+
hidden: Optional[bool] = None,
65+
time: Optional[bool] = None,
66+
pager: Optional[HgPagerType] = None,
67+
color: Optional[HgColorType] = None,
68+
**kwargs,
69+
):
70+
"""
71+
Passing None to a subcommand option, the flag won't be passed unless otherwise
72+
stated.
73+
74+
`hg help` and `hg help [cmd]`
75+
76+
Wraps hg's `Options <https://www.mercurial-scm.org/doc/hg.1.html>`_.
77+
78+
Parameters
79+
----------
80+
quiet : bool
81+
-q / --quiet
82+
repository : str
83+
``--repository REPO``
84+
cwd : :attr:`libvcs.cmd.types.StrOrBytesPath`, optional
85+
``--cwd DIR``, Defaults to :attr:`~.cwd`
86+
verbose : bool
87+
``-v / --verbose``
88+
non_interactive : bool
89+
``-y / --noninteractive``, defaults to True
90+
color : HgColorTypeLiteral
91+
``--color``
92+
debug : bool
93+
``--debug``
94+
debugger : bool
95+
``--debugger``
96+
encoding : str
97+
``--encoding ENCODE``
98+
encoding_mode : str
99+
``--encodingmode MODE``
100+
traceback : bool
101+
``--traceback``
102+
time : bool
103+
``--time``
104+
profile : bool
105+
``--profile``
106+
version : bool
107+
``--version``
108+
help : bool
109+
``-h / --help``
110+
hidden : bool
111+
``--hidden``
112+
pager : HgPagerType
113+
``--pager TYPE``
114+
config :
115+
``--config CONFIG [+]``, ``section.name=value``
116+
117+
Examples
118+
--------
119+
>>> hg = Hg(dir=tmp_path)
120+
>>> hg.run(['help']) # doctest: +NORMALIZE_WHITESPACE
121+
"Mercurial Distributed SCM..."
122+
"""
123+
124+
if isinstance(args, Sequence):
125+
cli_args = ["hg", *args]
126+
else:
127+
cli_args = ["hg", args]
128+
129+
if "cwd" not in kwargs:
130+
kwargs["cwd"] = self.dir
131+
132+
if repository is not None:
133+
cli_args.append(f"--repository {repository}")
134+
if config is not None:
135+
cli_args.append("--config {config}")
136+
if pager is not None:
137+
cli_args.append(f"--pager {pager}")
138+
if color is not None:
139+
cli_args.append(f"--color {color}")
140+
if verbose is True:
141+
cli_args.append("verbose")
142+
if quiet is True:
143+
cli_args.append("--quiet")
144+
if debug is True:
145+
cli_args.append("--debug")
146+
if debugger is True:
147+
cli_args.append("--debugger")
148+
if traceback is True:
149+
cli_args.append("--traceback")
150+
if time is True:
151+
cli_args.append("--time")
152+
if profile is True:
153+
cli_args.append("--profile")
154+
if version is True:
155+
cli_args.append("--version")
156+
if help is True:
157+
cli_args.append("--help")
158+
159+
return run(cmd=cli_args, **kwargs)
160+
161+
def clone(
162+
self,
163+
url: str,
164+
no_update: Optional[str] = None,
165+
update_rev: Optional[str] = None,
166+
rev: Optional[str] = None,
167+
branch: Optional[str] = None,
168+
ssh: Optional[str] = None,
169+
remote_cmd: Optional[str] = None,
170+
pull: Optional[bool] = None,
171+
stream: Optional[bool] = None,
172+
insecure: Optional[bool] = None,
173+
):
174+
"""Clone a working copy from a mercurial repo.
175+
176+
Wraps `hg clone <https://www.mercurial-scm.org/doc/hg.1.html#clone>`_.
177+
178+
Examples
179+
--------
180+
>>> hg = Hg(dir=tmp_path)
181+
>>> hg_remote_repo = create_hg_remote_repo()
182+
>>> hg.clone(url=f'file://{hg_remote_repo}')
183+
'updating to branch default...1 files updated, 0 files merged, ...'
184+
>>> hg.dir.exists()
185+
True
186+
"""
187+
required_flags: list[str] = [url, str(self.dir)]
188+
local_flags: list[str] = []
189+
190+
if ssh is not None:
191+
local_flags.append(f"--ssh {ssh}")
192+
if remote_cmd is not None:
193+
local_flags.append(f"--remotecmd {remote_cmd}")
194+
if rev is not None:
195+
local_flags.append(f"--rev {rev}")
196+
if branch is not None:
197+
local_flags.append(f"--branch {branch}")
198+
if no_update is True:
199+
local_flags.append("--noupdate")
200+
if pull is True:
201+
local_flags.append("--pull")
202+
if stream is True:
203+
local_flags.append("--stream")
204+
if insecure is True:
205+
local_flags.append("--insecure")
206+
return self.run(
207+
["clone", *local_flags, "--", *required_flags], check_returncode=False
208+
)

0 commit comments

Comments
 (0)