Skip to content

Commit

Permalink
Fixed some output in visualizing
Browse files Browse the repository at this point in the history
  • Loading branch information
coordt committed Jan 22, 2024
1 parent 0bbd814 commit 406f97a
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 14 deletions.
2 changes: 1 addition & 1 deletion bumpversion/scm.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class SCMInfo:

tool: Optional[Type["SourceCodeManager"]] = None
commit_sha: Optional[str] = None
distance_to_latest_tag: Optional[int] = None
distance_to_latest_tag: int = 0
current_version: Optional[str] = None
branch_name: Optional[str] = None
short_branch_name: Optional[str] = None
Expand Down
30 changes: 21 additions & 9 deletions bumpversion/utils.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
"""General utilities."""
import datetime
import string
from collections import ChainMap
from dataclasses import asdict
from typing import TYPE_CHECKING, Any, List, Optional, Tuple

if TYPE_CHECKING: # pragma: no-coverage
from bumpversion.config import Config
from bumpversion.scm import SCMInfo
from bumpversion.version_part import Version


Expand Down Expand Up @@ -51,19 +53,29 @@ def labels_for_format(serialize_format: str) -> List[str]:
return [item[1] for item in string.Formatter().parse(serialize_format) if item[1]]


def get_context(
config: "Config", current_version: Optional["Version"] = None, new_version: Optional["Version"] = None
) -> ChainMap:
"""Return the context for rendering messages and tags."""
import datetime
def base_context(scm_info: Optional["SCMInfo"] = None) -> ChainMap:
"""The default context for rendering messages and tags."""
from bumpversion.scm import SCMInfo # Including this here to avoid circular imports

scm = asdict(scm_info) if scm_info else asdict(SCMInfo())

ctx = ChainMap(
{"current_version": config.current_version},
{"now": datetime.datetime.now(), "utcnow": datetime.datetime.utcnow()},
return ChainMap(
{
"now": datetime.datetime.now(),
"utcnow": datetime.datetime.now(datetime.timezone.utc),
},
prefixed_environ(),
asdict(config.scm_info),
scm,
{c: c for c in ("#", ";")},
)


def get_context(
config: "Config", current_version: Optional["Version"] = None, new_version: Optional["Version"] = None
) -> ChainMap:
"""Return the context for rendering messages and tags."""
ctx = base_context(config.scm_info)
ctx.new_child({"current_version": config.current_version})
if current_version:
ctx = ctx.new_child({f"current_{part}": current_version[part].value for part in current_version})
if new_version:
Expand Down
23 changes: 19 additions & 4 deletions bumpversion/visualize.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from bumpversion.config import Config
from bumpversion.exceptions import BumpVersionError
from bumpversion.ui import print_info
from bumpversion.utils import get_context
from bumpversion.utils import base_context, get_context

BOX_CHARS = {
"ascii": ["+", "+", "+", "+", "+", "+", "+", "+", "-", "|", "+"],
Expand Down Expand Up @@ -92,10 +92,25 @@ def labeled_line(label: str, border: Border, fit_length: Optional[int] = None) -
return f" {label} {border.line * (fit_length - len(label))}{border.line} "


def filter_version_parts(config: Config) -> list[str]:
"""
Return the version parts that are in the configuration.
Args:
config: The configuration to check against
Returns:
The version parts that are in the configuration
"""
version_parts = [part for part in config.version_config.order if not part.startswith("$")]
default_context = base_context(config.scm_info)
return [part for part in version_parts if part not in default_context]


def visualize(config: Config, version_str: str, box_style: str = "light") -> None:
"""Output a visualization of the bump-my-version bump process."""
version = config.version_config.parse(version_str)
version_parts = config.version_config.order
version_parts = filter_version_parts(config)
num_parts = len(version_parts)

box_style = box_style if box_style in BOX_CHARS else "light"
Expand All @@ -111,8 +126,8 @@ def visualize(config: Config, version_str: str, box_style: str = "light") -> Non
try:
next_version = get_next_version(version, config, part, None)
next_version_str = config.version_config.serialize(next_version, get_context(config))
except (BumpVersionError, ValueError):
next_version_str = "invalid"
except (BumpVersionError, ValueError) as e:
next_version_str = f"invalid: {e}"

has_next = i < num_parts - 1
has_previous = i > 0
Expand Down

0 comments on commit 406f97a

Please sign in to comment.