From f5e8d97c359e700d5dc9da98be3de2d83bda0689 Mon Sep 17 00:00:00 2001 From: Joris Roovers Date: Wed, 18 Jan 2023 12:30:39 +0100 Subject: Hatch vcs (#410) Use hatch-vcs for gitlint and gitlint-core versioning. Use a custom build hook in hatch_build.py to ensure the gitlint and gitlint-core versions always match. --- .github/workflows/checks.yml | 1 + gitlint-core/gitlint/__init__.py | 9 ++++++++- gitlint-core/pyproject.toml | 7 +++++-- hatch_build.py | 9 +++++++++ pyproject.toml | 12 ++++++------ 5 files changed, 29 insertions(+), 9 deletions(-) create mode 100644 hatch_build.py diff --git a/.github/workflows/checks.yml b/.github/workflows/checks.yml index 6bbf79c..d60332f 100644 --- a/.github/workflows/checks.yml +++ b/.github/workflows/checks.yml @@ -21,6 +21,7 @@ jobs: - uses: actions/checkout@v3.3.0 with: ref: ${{ github.event.pull_request.head.sha }} # Checkout pull request HEAD commit instead of merge commit + fetch-depth: 0 # checkout all history, needed for hatch versioning - name: Setup python uses: actions/setup-python@v4.5.0 diff --git a/gitlint-core/gitlint/__init__.py b/gitlint-core/gitlint/__init__.py index 959b7af..d08993f 100644 --- a/gitlint-core/gitlint/__init__.py +++ b/gitlint-core/gitlint/__init__.py @@ -1 +1,8 @@ -__version__ = "0.19.0.dev3" +import sys + +if sys.version_info >= (3, 8): + from importlib import metadata +else: + import importlib_metadata as metadata + +__version__ = metadata.version("gitlint-core") diff --git a/gitlint-core/pyproject.toml b/gitlint-core/pyproject.toml index 4f411c1..d6f2276 100644 --- a/gitlint-core/pyproject.toml +++ b/gitlint-core/pyproject.toml @@ -1,5 +1,5 @@ [build-system] -requires = ["hatchling"] +requires = ["hatchling", "hatch-vcs"] build-backend = "hatchling.build" [project] @@ -35,6 +35,7 @@ classifiers = [ dependencies = [ "arrow>=1", "Click>=8", + "importlib-metadata >= 1.0 ; python_version < \"3.8\"", "sh>=1.13.0 ; sys_platform != \"win32\"", ] @@ -54,12 +55,14 @@ Homepage = "https://jorisroovers.github.io/gitlint" Source = "https://github.com/jorisroovers/gitlint" [tool.hatch.version] -path = "gitlint/__init__.py" +source = "vcs" +raw-options = { root = ".." } [tool.hatch.build] include = [ "/gitlint", # ] + exclude = [ "/gitlint/tests", # ] diff --git a/hatch_build.py b/hatch_build.py new file mode 100644 index 0000000..c58a8e1 --- /dev/null +++ b/hatch_build.py @@ -0,0 +1,9 @@ +# hatch_build.py is executed by hatch at build-time and can contain custom build logic hooks +from hatchling.metadata.plugin.interface import MetadataHookInterface + + +class CustomMetadataHook(MetadataHookInterface): + """Custom metadata hook for hatch that ensures that gitlint and gitlint-core[trusted-deps] versions always match""" + + def update(self, metadata: dict) -> None: + metadata["dependencies"] = [f"gitlint-core[trusted-deps]=={metadata['version']}"] diff --git a/pyproject.toml b/pyproject.toml index 340d060..d5e82b7 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,10 +1,10 @@ [build-system] -requires = ["hatchling"] +requires = ["hatchling", "hatch-vcs"] build-backend = "hatchling.build" [project] name = "gitlint" -dynamic = ["version"] +dynamic = ["version", "dependencies"] description = "Git commit message linter written in python, checks your commit messages for style." readme = "README.md" @@ -33,7 +33,6 @@ classifiers = [ "Topic :: Software Development :: Quality Assurance", "Topic :: Software Development :: Testing", ] -dependencies = ["gitlint-core[trusted-deps]==0.19.0dev3"] [project.urls] Homepage = "https://jorisroovers.github.io/gitlint" @@ -42,7 +41,7 @@ Source = "https://github.com/jorisroovers/gitlint" Changelog = "https://github.com/jorisroovers/gitlint/blob/main/CHANGELOG.md" [tool.hatch.version] -path = "gitlint-core/gitlint/__init__.py" +source = "vcs" [tool.hatch.build] exclude = ["*"] @@ -96,8 +95,8 @@ format = "black --check --diff {args:.}" lint = "ruff {args:gitlint-core/gitlint qa}" autoformat = "black {args:.}" autofix = [ - "ruff --fix gitlint-core/gitlint qa", - "autoformat", # + "ruff --fix {args:gitlint-core/gitlint qa}", + "autoformat", # ] all = [ @@ -139,6 +138,7 @@ detached = true dependencies = [ "mkdocs==1.4.2", # ] + [tool.hatch.envs.docs.scripts] build = "mkdocs build --clean --strict" serve = "mkdocs serve" -- cgit v1.2.3