Metadata-Version: 2.4
Name: setuptools-git-metadata
Version: 0.1.0.dev0
Summary: Bake Git provenance into setuptools distributions without editing package sources
Author-email: Nicholas Hairs <info+setuptools-git-metadata@nicholashairs.com>
License-Expression: MIT
Project-URL: GitHub, https://github.com/nhairs/setuptools-git-metadata
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Developers
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Programming Language :: Python :: 3.15
Classifier: Topic :: Software Development :: Build Tools
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: LICENCE
Requires-Dist: setuptools>=80
Requires-Dist: tomli>=2; python_version < "3.11"
Dynamic: license-file

# setuptools-git-metadata

Include your package's version, Git commit, branch and build timestamps in source distributions and wheels. Import those values through a committed, customisable `_version.py`.

The build tool requires Python 3.10.12+, 3.11.4+, or 3.12+, and setuptools 80 or later. Git is required when collecting commit information. The generated runtime loader supports Python 3.9 or later.

## Configure your project

Add the build requirement and enable metadata generation:

```toml
[build-system]
requires = ["setuptools>=80", "setuptools-git-metadata"]
build-backend = "setuptools.build_meta"

[project]
name = "example"
version = "1.2.3"

[tool.setuptools-git-metadata]
```

Metadata is included automatically in every package detected by setuptools. To select specific packages, add their import names:

```toml
[tool.setuptools-git-metadata]
packages = ["example", "example.utilities"]
```

The declared `project.version` determines the package version. Projects that customise setuptools' `build_py` or `sdist` commands must integrate the metadata commands with their overrides.

This development version is available from source. Build its wheel with `uv build --wheel`, then replace the `setuptools-git-metadata` requirement with `setuptools-git-metadata @ file:///ABSOLUTE_PATH/setuptools_git_metadata-0.1.0.dev0-py3-none-any.whl`.

## Add the runtime loader

Install the build tool in your development environment, then create a loader in each package where you want to import the version information:

```sh
python3 -m setuptools_git_metadata template --output src/example/_version.py
```

Commit the generated file. You can customise its display strings and add application helpers. The command refuses to overwrite an existing file. To print the template for comparison, run:

```sh
python3 -m setuptools_git_metadata template
```

Build your distributions using your usual setuptools build command. Each selected package receives `_git_metadata.json` automatically. Keep this generated file out of your development checkout.

Import version information from an installed package:

```python
from example._version import BUILD_INFO

print(BUILD_INFO.VERSION_INFO_SHORT)
print(BUILD_INFO.BUILD_GIT_HASH)
```

`BUILD_INFO` is a frozen `GitMetadata` instance. Its `VERSION_INFO_*` strings and `BUILD_DATETIME` are computed properties.

To load another package explicitly, use `GitMetadata.from_package("package_name")`.

Without generated metadata, string fields use `__metadata_missing__` and source/build timestamps are zero. Nullable fields, including unavailable branch and dirty state, remain `None`. This includes editable installs from a development checkout.

## Available metadata

| Runtime fields | Meaning |
| --- | --- |
| `PACKAGE_VERSION`, `BUILD_VERSION` | Declared project version |
| `BUILD_GIT_HASH`, `BUILD_GIT_HASH_SHORT` | Full Git commit hash and Git's unique abbreviation (at least eight characters) |
| `BUILD_GIT_BRANCH` | Branch name, or `None` when unavailable |
| `BUILD_GIT_DIRTY` | Whether selected source inputs have changed |
| `BUILD_GIT_COMMIT_TIMESTAMP` | Git commit time |
| `SOURCE_TIMESTAMP` | Original source metadata time |
| `SDIST_BUILD_TIMESTAMP` | Original source distribution build time |
| `WHEEL_BUILD_TIMESTAMP` | Wheel build time |
| `BUILD_TIMESTAMP`, `BUILD_DATETIME` | Current artifact build time |

`BUILD_INFO` also provides `VERSION_INFO_SHORT`, `VERSION_INFO`, `VERSION_INFO_LONG` and `VERSION_INFO_FULL` display strings.

Wheels built from a source distribution retain its version, commit, branch and source timestamps. Their wheel build time reflects the new build. Set `SOURCE_DATE_EPOCH` to a nonnegative integer to control the artifact build time.

A source tree without Git information or previously generated metadata produces unavailable Git fields. Invalid metadata and a version that conflicts with previously generated metadata cause the build to fail.

## Develop and test

```sh
uv sync --group dev
uvx tox -e lint
uvx tox
```

Tox covers CPython 3.10–3.15 and PyPy 3.10–3.12. GitHub Actions runs the suite on Ubuntu, macOS and Windows.
