Skip to content

Commit 560999d

Browse files
committed
Improved Mercurial support
1 parent 6ccfa7d commit 560999d

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

bumpversion/scm.py

+17-5
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from dataclasses import dataclass
77
from pathlib import Path
88
from tempfile import NamedTemporaryFile
9-
from typing import TYPE_CHECKING, ChainMap, List, Optional, Type, Union
9+
from typing import TYPE_CHECKING, List, MutableMapping, Optional, Type, Union
1010

1111
if TYPE_CHECKING: # pragma: no-coverage
1212
from bumpversion.config import Config
@@ -89,7 +89,7 @@ def commit_to_scm(
8989
cls,
9090
files: List[Union[str, Path]],
9191
config: "Config",
92-
context: ChainMap,
92+
context: MutableMapping,
9393
extra_args: Optional[List[str]] = None,
9494
dry_run: bool = False,
9595
) -> None:
@@ -130,7 +130,7 @@ def commit_to_scm(
130130
)
131131

132132
@classmethod
133-
def tag_in_scm(cls, config: "Config", context: ChainMap, dry_run: bool = False) -> None:
133+
def tag_in_scm(cls, config: "Config", context: MutableMapping, dry_run: bool = False) -> None:
134134
"""Tag the current commit in the source code management system."""
135135
sign_tags = config.sign_tags
136136
tag_name = config.tag_name.format(**context)
@@ -165,7 +165,7 @@ def assert_nondirty(cls) -> None:
165165

166166
if lines:
167167
joined_lines = b"\n".join(lines).decode()
168-
raise DirtyWorkingDirectoryError(f"Git working directory is not clean:\n{joined_lines}")
168+
raise DirtyWorkingDirectoryError(f"Git working directory is not clean:\n\n{joined_lines}")
169169

170170
@classmethod
171171
def latest_tag_info(cls, tag_pattern: str) -> SCMInfo:
@@ -245,7 +245,19 @@ class Mercurial(SourceCodeManager):
245245
@classmethod
246246
def latest_tag_info(cls, tag_pattern: str) -> SCMInfo:
247247
"""Return information about the latest tag."""
248-
return SCMInfo(tool=cls)
248+
current_version = None
249+
re_pattern = tag_pattern.replace("*", ".*")
250+
result = subprocess.run(
251+
["hg", "log", "-r", f"tag('re:{re_pattern}')", "--template", "{latesttag}\n"],
252+
text=True,
253+
check=True,
254+
capture_output=True,
255+
)
256+
result.check_returncode()
257+
if result.stdout:
258+
current_version = result.stdout.splitlines(keepends=False)[0].lstrip("v")
259+
is_dirty = len(subprocess.check_output(["hg", "status", "-mard"])) != 0
260+
return SCMInfo(tool=cls, current_version=current_version, dirty=is_dirty)
249261

250262
@classmethod
251263
def assert_nondirty(cls) -> None:

0 commit comments

Comments
 (0)