Skip to main content

Make a version from git repo/tree status for files of interest

Project description

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

git_version_stamp-0.4.1.tar.gz (5.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

git_version_stamp-0.4.1-py3-none-any.whl (5.8 kB view details)

Uploaded Python 3

File details

Details for the file git_version_stamp-0.4.1.tar.gz.

File metadata

  • Download URL: git_version_stamp-0.4.1.tar.gz
  • Upload date:
  • Size: 5.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.6.10

File hashes

Hashes for git_version_stamp-0.4.1.tar.gz
Algorithm Hash digest
SHA256 df50fd648a0614474e6e9aa17b6a9cb16ed968a7932762a01d1044cb6872744e
MD5 14056dd3944912dc8039814e8db1fa8e
BLAKE2b-256 318e2083ea80bf97666249f120cdd77eeaa0a2c914a6bc3f26d0f7cb59844694

See more details on using hashes here.

File details

Details for the file git_version_stamp-0.4.1-py3-none-any.whl.

File metadata

File hashes

Hashes for git_version_stamp-0.4.1-py3-none-any.whl
Algorithm Hash digest
SHA256 2c3939e018fb2aee62c64984afefcfa221f766891fb1a19da77b63e07d5586bf
MD5 f640341a69110d802851ea852a208c8c
BLAKE2b-256 d2b5b72fd97fff7ef2034ee0df268101db64b934c4fb43ef6ff3399453e550af

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page