Skip to content

Commit df7939a

Browse files
feat: configure version detection for vcs-versioning with tag prefix
- Add _own_version_of_vcs_versioning.py with hatchling code version source - Configure git_describe_command to match only 'vcs-versioning-*' tags - Guard setuptools import in _types.py with TYPE_CHECKING - Set up proper root/fallback_root paths relative to git repo - Add fallback_version for bootstrap before first tag - Update _own_version_helper.py docs for future setuptools-scm- prefix This allows vcs-versioning to: 1. Use its own version detection independent of setuptools-scm 2. Filter tags by 'vcs-versioning-' prefix 3. Bootstrap with fallback version until first proper tag is created 4. Work without setuptools as runtime dependency
1 parent 091feb8 commit df7939a

File tree

5 files changed

+105
-18
lines changed

5 files changed

+105
-18
lines changed

_own_version_helper.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
11
"""
2-
this module is a hack only in place to allow for setuptools
3-
to use the attribute for the versions
2+
Version helper for setuptools-scm package.
43
5-
it works only if the backend-path of the build-system section
6-
from pyproject.toml is respected
4+
This module allows setuptools-scm to use VCS metadata for its own version.
5+
It works only if the backend-path of the build-system section from
6+
pyproject.toml is respected.
7+
8+
Tag prefix configuration:
9+
- Currently: No prefix (for backward compatibility with existing tags)
10+
- Future: Will migrate to 'setuptools-scm-' prefix
711
"""
812

913
from __future__ import annotations
1014

1115
import logging
1216
import os
17+
import sys
1318

1419
from collections.abc import Callable
1520

@@ -57,6 +62,9 @@ def scm_version() -> str:
5762
else get_local_node_and_date
5863
)
5964

65+
# Note: tag_regex is currently NOT set to allow backward compatibility
66+
# with existing tags. To migrate to 'setuptools-scm-' prefix, uncomment:
67+
# tag_regex=r"^setuptools-scm-(?P<version>[vV]?\d+(?:\.\d+){0,2}[^\+]*)(?:\+.*)?$",
6068
return get_version(
6169
relative_to=__file__,
6270
parse=parse,
@@ -66,6 +74,7 @@ def scm_version() -> str:
6674

6775

6876
version: str
77+
print("__file__", __file__, file=sys.stderr)
6978

7079

7180
def __getattr__(name: str) -> str:
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
"""
2+
Version helper for vcs-versioning package.
3+
4+
This module allows vcs-versioning to use VCS metadata for its own version,
5+
with the tag prefix 'vcs-versioning-'.
6+
7+
Used by hatchling's code version source.
8+
"""
9+
10+
from __future__ import annotations
11+
12+
import logging
13+
import os
14+
15+
from collections.abc import Callable
16+
17+
from vcs_versioning import Configuration
18+
from vcs_versioning import _types as _t
19+
from vcs_versioning._backends import _git as git
20+
from vcs_versioning._backends import _hg as hg
21+
from vcs_versioning._fallbacks import fallback_version
22+
from vcs_versioning._fallbacks import parse_pkginfo
23+
from vcs_versioning._get_version_impl import get_version
24+
from vcs_versioning._version_schemes import ScmVersion
25+
from vcs_versioning._version_schemes import get_local_node_and_date
26+
from vcs_versioning._version_schemes import get_no_local_node
27+
from vcs_versioning._version_schemes import guess_next_dev_version
28+
29+
log = logging.getLogger("vcs_versioning")
30+
31+
# Try these parsers in order for vcs-versioning's own version
32+
try_parse: list[Callable[[_t.PathT, Configuration], ScmVersion | None]] = [
33+
parse_pkginfo,
34+
git.parse,
35+
hg.parse,
36+
git.parse_archival,
37+
hg.parse_archival,
38+
fallback_version, # Last resort: use fallback_version from config
39+
]
40+
41+
42+
def parse(root: str, config: Configuration) -> ScmVersion | None:
43+
for maybe_parse in try_parse:
44+
try:
45+
parsed = maybe_parse(root, config)
46+
except OSError as e:
47+
log.warning("parse with %s failed with: %s", maybe_parse, e)
48+
else:
49+
if parsed is not None:
50+
return parsed
51+
return None
52+
53+
54+
def _get_version() -> str:
55+
"""Get version from VCS with vcs-versioning- tag prefix."""
56+
# Use no-local-version if VCS_VERSIONING_NO_LOCAL is set (for CI uploads)
57+
local_scheme = (
58+
get_no_local_node
59+
if os.environ.get("VCS_VERSIONING_NO_LOCAL")
60+
else get_local_node_and_date
61+
)
62+
63+
# __file__ is nextgen/vcs-versioning/_own_version_helper.py
64+
# pyproject.toml is in nextgen/vcs-versioning/pyproject.toml
65+
pyproject_path = os.path.join(os.path.dirname(__file__), "pyproject.toml")
66+
67+
# root is the git repo root (../..)
68+
# fallback_root is the vcs-versioning package dir (.)
69+
# relative_to anchors to pyproject.toml
70+
# fallback_version is used when no vcs-versioning- tags exist yet
71+
return get_version(
72+
root="../..",
73+
fallback_root=".",
74+
relative_to=pyproject_path,
75+
parse=parse,
76+
version_scheme=guess_next_dev_version,
77+
local_scheme=local_scheme,
78+
tag_regex=r"^vcs-versioning-(?P<version>[vV]?\d+(?:\.\d+){0,2}[^\+]*)(?:\+.*)?$",
79+
git_describe_command="git describe --dirty --tags --long --match 'vcs-versioning-*'",
80+
fallback_version="0.1.0+pre.tag",
81+
)
82+
83+
84+
__version__: str = _get_version()

nextgen/vcs-versioning/pyproject.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
build-backend = "hatchling.build"
33
requires = [
44
"hatchling",
5+
"packaging>=20",
56
]
67

78
[project]
@@ -67,7 +68,9 @@ node-and-timestamp = "vcs_versioning._version_schemes:get_local_node_and_timesta
6768
"release-branch-semver" = "vcs_versioning._version_schemes:release_branch_semver_version"
6869

6970
[tool.hatch.version]
70-
path = "src/vcs_versioning/__about__.py"
71+
source = "code"
72+
path = "_own_version_of_vcs_versioning.py"
73+
search-paths = ["src"]
7174

7275
[tool.hatch.build.targets.wheel]
7376
packages = ["src/vcs_versioning"]

nextgen/vcs-versioning/src/vcs_versioning/_requirement_cls.py

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,8 @@
44

55
__all__ = ["Requirement", "extract_package_name"]
66

7-
try:
8-
from packaging.requirements import Requirement
9-
from packaging.utils import canonicalize_name
10-
except ImportError:
11-
from setuptools.extern.packaging.requirements import ( # type: ignore[import-not-found,no-redef]
12-
Requirement as Requirement,
13-
)
14-
from setuptools.extern.packaging.utils import ( # type: ignore[import-not-found,no-redef]
15-
canonicalize_name as canonicalize_name,
16-
)
17-
7+
from packaging.requirements import Requirement
8+
from packaging.utils import canonicalize_name
189

1910
log = logging.getLogger(__name__)
2011

nextgen/vcs-versioning/src/vcs_versioning/_types.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
from typing import TypeAlias
1010
from typing import Union
1111

12-
from setuptools import Distribution
13-
1412
if TYPE_CHECKING:
13+
from setuptools import Distribution
14+
1515
from . import _version_schemes as version
1616
from ._pyproject_reading import PyProjectData
1717
from ._toml import InvalidTomlError

0 commit comments

Comments
 (0)