Report a git-derived version from source checkouts
--version no longer shows the 0.0.0.dev0 placeholder when run from a checkout; it is derived lazily from git describe. Release binaries keep the version stamped from the tag and never invoke git. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
19a5373796
commit
926c0f0470
5 files changed
+173
-5
No files matched your search
+13
-2
@@ -14,7 +14,6 @@ import sys
|
||||
from collections.abc import Sequence
|
||||
from pathlib import Path
|
||||
|
||||
from envbao import __version__
|
||||
from envbao.client import BaoClient
|
||||
from envbao.config import Config, load_config, resolve_config_path
|
||||
from envbao.errors import EXIT_FAILURE, EXIT_OK, EnvBaoError, PermissionPolicyError
|
||||
@@ -22,6 +21,7 @@ from envbao.logging_setup import add_file_logging, setup_stderr_logging
|
||||
from envbao.render import SHELLS, detect_shell, render
|
||||
from envbao.resolve import resolve, validate_queries
|
||||
from envbao.security import check_config_dir
|
||||
from envbao.version import get_version
|
||||
|
||||
log = logging.getLogger("envbao")
|
||||
|
||||
@@ -46,6 +46,17 @@ path = "myapp/prod"
|
||||
"""
|
||||
|
||||
|
||||
class _VersionAction(argparse.Action):
|
||||
"""``--version`` that resolves the version lazily (git is never run otherwise)."""
|
||||
|
||||
def __init__(self, option_strings: Sequence[str], dest: str, **kwargs: object) -> None:
|
||||
super().__init__(option_strings, dest, nargs=0, help="show version and exit")
|
||||
|
||||
def __call__(self, parser: argparse.ArgumentParser, *args: object, **kwargs: object) -> None:
|
||||
sys.stdout.write(f"envbao {get_version()}\n")
|
||||
parser.exit()
|
||||
|
||||
|
||||
def build_parser() -> argparse.ArgumentParser:
|
||||
"""Create the argument parser."""
|
||||
parser = argparse.ArgumentParser(
|
||||
@@ -74,7 +85,7 @@ def build_parser() -> argparse.ArgumentParser:
|
||||
help="create ~/.envbao with safe permissions and a template config.toml",
|
||||
)
|
||||
parser.add_argument("-v", "--verbose", action="count", default=0, help="log more to stderr")
|
||||
parser.add_argument("--version", action="version", version=f"envbao {__version__}")
|
||||
parser.add_argument("--version", action=_VersionAction)
|
||||
return parser
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,66 @@
|
||||
"""Version reporting.
|
||||
|
||||
Release binaries carry the version stamped into ``_version.py`` from the git tag. When
|
||||
running from a source checkout (stamp is the placeholder) the version is derived from
|
||||
``git describe`` so a development build never masquerades as a release, e.g.
|
||||
``0.1.0.dev3+g19a5373`` (3 commits after ``v0.1.0``), with ``.dirty`` appended if the
|
||||
working tree has uncommitted changes.
|
||||
|
||||
git is only consulted lazily (``--version``), never during normal shell start-up.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import re
|
||||
import shutil
|
||||
import subprocess # noqa: S404
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
from envbao._version import __version__ as STAMPED
|
||||
|
||||
PLACEHOLDER = "0.0.0.dev0"
|
||||
_DESCRIBE_RE = re.compile(r"^v(?P<tag>.+?)-(?P<n>\d+)-g(?P<sha>[0-9a-f]+)(?P<dirty>-dirty)?$")
|
||||
|
||||
|
||||
def format_describe(described: str) -> str | None:
|
||||
"""Convert ``git describe --long --dirty`` output to a PEP 440 style version.
|
||||
|
||||
``v0.1.0-0-gabc1234`` -> ``0.1.0``;
|
||||
``v0.1.0-3-gabc1234-dirty`` -> ``0.1.0.dev3+gabc1234.dirty``.
|
||||
Returns None if the text is not from a ``v*`` tag.
|
||||
"""
|
||||
match = _DESCRIBE_RE.match(described.strip())
|
||||
if not match:
|
||||
return None
|
||||
tag, count, sha, dirty = match["tag"], int(match["n"]), match["sha"], bool(match["dirty"])
|
||||
if count == 0 and not dirty:
|
||||
return tag
|
||||
local = f"g{sha}.dirty" if dirty else f"g{sha}"
|
||||
return f"{tag}.dev{count}+{local}"
|
||||
|
||||
|
||||
def _git_version(root: Path) -> str | None:
|
||||
git = shutil.which("git")
|
||||
if not git or not (root / ".git").exists():
|
||||
return None
|
||||
try:
|
||||
proc = subprocess.run( # noqa: S603
|
||||
[git, "describe", "--tags", "--long", "--dirty", "--match", "v[0-9]*"],
|
||||
cwd=root,
|
||||
capture_output=True,
|
||||
text=True,
|
||||
timeout=5,
|
||||
check=False,
|
||||
)
|
||||
except (OSError, subprocess.SubprocessError):
|
||||
return None
|
||||
return format_describe(proc.stdout) if proc.returncode == 0 else None
|
||||
|
||||
|
||||
def get_version() -> str:
|
||||
"""Return the stamped release version, or a git-derived one for source checkouts."""
|
||||
if STAMPED != PLACEHOLDER or getattr(sys, "frozen", False):
|
||||
return STAMPED
|
||||
root = Path(__file__).resolve().parents[2]
|
||||
return _git_version(root) or STAMPED
|
||||
Reference in new issue
Block a user