Skip to main content

git-version-stamp

This small utility (written in Python) creates a simple version code based on the status of a git repo/tree for files of interest, mostly for use in build scripts to embed into build output and/or name build artifacts.

You should also consider these more established tools:

The main differences with this one:

  • In general it's much less developed than any of those
  • It doesn't integrate with setuptools, it just spits out a version string
  • It uses timestamp-oriented versioning (rather than last-tag-plus-change-count) for untagged builds, which is more useful for apps or firmware images but less appropriate for libraries
  • It lets you pick a subset of files in the repo via include and exclude lists, and the version is based on the status of those files

If you actually use this, maybe let me (egnor@ofb.net) know so I'm a bit more motivated to make it a proper project with docs and tests and stuff? PRs welcome in any case.

Usage

Install this package:

  • pip install git-version-stamp (or use any pip-compatible installer)
  • OR just copy git_version_stamp.py (it has no dependencies)

Invoke the utility from inside a git working tree:

  • git-version-stamp . if installed with pip or similar
  • OR python -m git_version_stamp .
  • OR ./git_version_stamp.py . if you copied the file
  • OR import git_version_stamp and use the API described below

By default, it prints a version stamp to stdout reflecting the (sub)tree state:

  • YYYYMMDD-<tag> if the tree is synced to a tagged version with no changes
  • YYYYMMDD.HHMMSS-git-<hash> if the tree is synced to an untagged commit
  • YYYYMMDD.HHMMSS-mod-<user>@<host> if the tree has been modified locally

The timestamp will be the committer time of the relevant commit, or the latest local file modification time.

Command line arguments:

  • list files/subtrees to scan (files in .gitignore will be skipped)
  • --exclude <dir/file> ... (default: none) - ignore these files/subtrees
  • --wrap <format> (default: text) - instead of plain text, use:
    • text - plain text (default)
    • json or c_string - JSON quoted string (also valid C/C++)
    • cpp_flag - -DGIT_VERSION_STAMP=... for C/C++ compiler
    • cpp_macro - #define GIT_VERSION_STAMP "..." for C/C++
    • cpp_symbol - extern char GIT_VERSION_STAMP[] = "..." for C/C++
    • arduino_cli_flag - --build-property=compiler.cpp.extra_flags=-D... for Arduino CLI
    • shell - quoted for shell scripts (or append _shell to any above)
  • --debug - enable debug logging

API

After importing the git_version_stamp in Python, you can invoke git_version_stamp.get(include=["."], exclude=[]) to get a Stamp object representing the repo state.

The Stamp object has these properties/methods:

  • stamp.tag - name of the most recent tag ("" if untagged)
  • stamp.tag_commit - SHA-1 of the commit that was tagged ("" if untagged)
  • stamp.tag_time - Unix time of the commit that was tagged (0 if untagged)
  • stamp.commit - SHA-1 of the most recent commit since the tag ("" if none)
  • stamp.commit_time - Unix time of that commit (0 if none)
  • stamp.local_mod_time - Unix time of the most recent local edit (0 if none)
  • str(stamp) - the version string as printed by the CLI
  • stamp.wrap(format="text") - a re-encoded string as output by --wrap

Metadata

Release files for git-version-stamp 0.4.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for git-version-stamp 0.4.1
File Size Uploaded Provenance Actions
git_version_stamp-0.4.1.tar.gz 5.2 kB Without provenance Details for git_version_stamp-0.4.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for git-version-stamp 0.4.1
File Interpreter ABI Platform Actions
git_version_stamp-0.4.1-py3-none-any.whl Python 3 none any Details for git_version_stamp-0.4.1-py3-none-any.whl

Total release size: 11.0 kB

Release files / git_version_stamp-0.4.1.tar.gz

Download URL git_version_stamp-0.4.1.tar.gz
Size 5.2 kB
Tags Source
SHA-256 checksum
How to use checksums
df50fd648a0614474e6e9aa17b6a9cb16ed968a7932762a01d1044cb6872744e
BLAKE2b-256 checksum
How to use checksums
318e2083ea80bf97666249f120cdd77eeaa0a2c914a6bc3f26d0f7cb59844694
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.6.10

Release files / git_version_stamp-0.4.1-py3-none-any.whl

Download URL git_version_stamp-0.4.1-py3-none-any.whl
Size 5.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
2c3939e018fb2aee62c64984afefcfa221f766891fb1a19da77b63e07d5586bf
BLAKE2b-256 checksum
How to use checksums
d2b5b72fd97fff7ef2034ee0df268101db64b934c4fb43ef6ff3399453e550af
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.6.10

Security for git-version-stamp 0.4.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page