Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
- migrate git describe command to new scm config
- add support for failing on missing submodules
- fix #279: expand errors when scm can be found upwards and relative_to wasnt used
- fix #577: introduce explicit scmversion node and short node

## v8.3.1

Expand Down
23 changes: 2 additions & 21 deletions src/setuptools_scm/_integration/dump_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,12 @@

from .. import _types as _t
from .._log import log as parent_log
from .._node_utils import _format_node_for_output
from .._version_cls import _version_as_tuple
from ..version import ScmVersion

log = parent_log.getChild("dump_version")


class _TemplateScmVersion:
"""Wrapper for ScmVersion that formats node for template output."""

def __init__(self, scm_version: ScmVersion) -> None:
self._scm_version = scm_version

def __getattr__(self, name: str) -> object:
# Delegate all attribute access to the wrapped ScmVersion
return getattr(self._scm_version, name)

@property
def node(self) -> str | None:
"""Return the node formatted for output."""
return _format_node_for_output(self._scm_version.node)


TEMPLATES = {
".py": """\
# file generated by setuptools-scm
Expand Down Expand Up @@ -64,7 +47,7 @@ def node(self) -> str | None:
__version__ = version = {version!r}
__version_tuple__ = version_tuple = {version_tuple!r}

__commit_id__ = commit_id = {scm_version.node!r}
__commit_id__ = commit_id = {scm_version.short_node!r}
""",
".txt": "{version}",
}
Expand Down Expand Up @@ -119,12 +102,10 @@ def write_version_to_path(
log.debug("dump %s into %s", version, target)
version_tuple = _version_as_tuple(version)
if scm_version is not None:
# Wrap ScmVersion to provide formatted node for templates
template_scm_version = _TemplateScmVersion(scm_version)
content = final_template.format(
version=version,
version_tuple=version_tuple,
scm_version=template_scm_version,
scm_version=scm_version,
)
else:
content = final_template.format(version=version, version_tuple=version_tuple)
Expand Down
5 changes: 5 additions & 0 deletions src/setuptools_scm/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,11 @@ def exact(self) -> bool:
"""returns true checked out exactly on a tag and no local changes apply"""
return self.distance == 0 and not self.dirty

@property
def short_node(self) -> str | None:
"""Return the node formatted for output."""
return _format_node_for_output(self.node)

def __repr__(self) -> str:
return (
f"<ScmVersion {self.tag} dist={self.distance} "
Expand Down
2 changes: 1 addition & 1 deletion testing/test_basic_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
template = """\
__version__ = version = {version!r}
__version_tuple__ = version_tuple = {version_tuple!r}
__sha__ = {scm_version.node!r}
__sha__ = {scm_version.short_node!r}
"""


Expand Down
4 changes: 2 additions & 2 deletions testing/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ def test_pretend_metadata_with_version(
major = {version_tuple[0]}
minor = {version_tuple[1]}
patch = {version_tuple[2]}
commit_hash = '{scm_version.node}'
commit_hash = '{scm_version.short_node}'
num_commit = {scm_version.distance}
""" # noqa: RUF027
# Use write_to with template to create version file
Expand Down Expand Up @@ -294,7 +294,7 @@ def test_pretend_metadata_with_scm_version(
# This is a template string, not an f-string - used by setuptools-scm templating
version_file_content = """
version = '{version}'
commit_hash = '{scm_version.node}'
commit_hash = '{scm_version.short_node}'
num_commit = {scm_version.distance}
""" # noqa: RUF027
version = wd.get_version(
Expand Down
Loading