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
7 changes: 5 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "git-version-utils"
version = "0.0.0"
dynamic = ["version"]
description = "Extract version information from a Git repository and output as environment variables"
readme = "README.md"
authors = [
Expand All @@ -22,4 +22,7 @@ Repository = "https://github.com/synacker/git_version_utils"
git-version = "git_version.cli:main"

[tool.setuptools.packages.find]
where = ["src"]
where = ["src"]

[tool.setuptools.dynamic]
version = { attr = "git_version._version.__version__" }
41 changes: 32 additions & 9 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,47 @@
Run `python tools/write_version.py` before building to generate
src/git_version/_version.py with the correct version.

The version in pyproject.toml is a fallback placeholder (0.0.0).
The version in pyproject.toml is declared as dynamic and resolved
from git_version._version.__version__ at build time.
"""

import os
import re
from pathlib import Path

from setuptools import setup


def get_version() -> str:
"""Get the package version from pyproject.toml."""
pyproject = os.path.join(os.path.dirname(__file__), "pyproject.toml")
if not os.path.exists(pyproject):
return "0.0.0"
with open(pyproject, "r") as f:
content = f.read()
match = re.search(r'^version\s*=\s*"([^"]+)"', content, re.MULTILINE)
return match.group(1) if match else "0.0.0"
"""Get the package version from the generated _version.py or from git.

Priority:
1. src/git_version/_version.py (generated by tools/write_version.py)
2. GIT_VERSION_UTILS_VERSION environment variable
3. pyproject.toml fallback (0.0.0)
"""
# Try _version.py first (generated by tools/write_version.py)
version_py = Path(__file__).parent / "src" / "git_version" / "_version.py"
if version_py.exists():
ns: dict[str, str] = {}
exec(version_py.read_text(), ns)
if "__version__" in ns:
return ns["__version__"]

# Try environment variable
env_version = os.environ.get("GIT_VERSION_UTILS_VERSION")
if env_version:
return env_version

# Fallback to pyproject.toml
pyproject = Path(__file__).parent / "pyproject.toml"
if pyproject.exists():
content = pyproject.read_text()
match = re.search(r'^version\s*=\s*"([^"]+)"', content, re.MULTILINE)
if match:
return match.group(1)

return "0.0.0"


if __name__ == "__main__":
Expand Down
Loading