Skip to main content

Extract version information from a Git repository and output as environment variables

Project description

git-version-utils

Extract version information from a Git repository and output as environment variables.

Installation

pip install git-version-utils

Usage

CLI

# Print all version info
git-version

# Output as environment variables (useful with `source` or `eval`)
git-version --property env

# Custom prefix for environment variables
git-version --prefix MYAPP --property env

# Custom tag pattern
git-version --tag-pattern "release-*" --property env

# Get a single property
git-version --property version
git-version --property version_major
git-version --property commit

# Specify repository path
git-version --repo /path/to/repo --property env

Python API

from git_version import GitVersion

gv = GitVersion(repo_path="/path/to/repo", tag_pattern="v[0-9]*")

print(gv.version)       # "1.2.3.42"
print(gv.version_major) # "1"
print(gv.version_minor) # "2"
print(gv.version_patch) # "3"
print(gv.build)         # "42"
print(gv.tag)           # "v1.2.3"
print(gv.branch)        # "main"
print(gv.short)         # "a1b2c3"
print(gv.full)          # "1.2.3.42-a1b2c3"
print(gv.commit)        # "a1b2c3d4e5f6..."

# Get all as environment variables
env_vars = gv.env(prefix="BUILD_VERSION")
for key, value in env_vars.items():
    print(f"{key}={value}")

Environment Variables Output

With default prefix BUILD_VERSION:

Variable Example Description
BUILD_VERSION 1.2.3.42 Full version: <semver>.<build>
BUILD_VERSION_MAJOR 1 Major version component
BUILD_VERSION_MINOR 2 Minor version component
BUILD_VERSION_PATCH 3 Patch version component
BUILD_VERSION_BUILD 42 Commits since last tag
BUILD_VERSION_TAG v1.2.3 Latest matching git tag
BUILD_VERSION_FULL 1.2.3.42-a1b2c3 Version with commit hash
BUILD_VERSION_EXTENDED 1.2.3.42-a1b2c3 Full if build>0, else version
BUILD_VERSION_SHORT a1b2c3 Short 6-char commit hash
BUILD_VERSION_COMMIT a1b2c3d4... Full 40-char commit hash
BUILD_VERSION_BRANCH main Current branch name
BUILD_VERSION_DEFAULT_BRANCH master Default branch from git config

CI/CD Integration

Shell (source)

source <(git-version --property env)
echo "$BUILD_VERSION"

CMake

execute_process(
    COMMAND git-version --property env
    OUTPUT_VARIABLE GIT_VERSION_ENV
    OUTPUT_STRIP_TRAILING_WHITESPACE
)

Docker

RUN pip install git-version-utils
RUN source <(git-version --property env) && echo "Building $BUILD_VERSION"

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_utils-0.1.0.0.tar.gz (7.5 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_utils-0.1.0.0-py3-none-any.whl (5.7 kB view details)

Uploaded Python 3

File details

Details for the file git_version_utils-0.1.0.0.tar.gz.

File metadata

  • Download URL: git_version_utils-0.1.0.0.tar.gz
  • Upload date:
  • Size: 7.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.13

File hashes

Hashes for git_version_utils-0.1.0.0.tar.gz
Algorithm Hash digest
SHA256 f130c7bbda8dea4af66ea5d60df688f5b01a2e864218fb7213e88ccbe690ba94
MD5 f6f24f19cb9db20036b9dd8526494f91
BLAKE2b-256 70c6f0d8684e167fba6f83c63cad6ab52c17434b088e8ab991c31d408a655f64

See more details on using hashes here.

File details

Details for the file git_version_utils-0.1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for git_version_utils-0.1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7943bdef2de5823ce12f2c87e3c7f1f31a1e32e63b5321818569a06849591f0b
MD5 ee2e3b7a90c4ee872567dbbce108cb95
BLAKE2b-256 9e9d3cb5832b4f9c47fc684e50b264648fd979ef27404d8d5a7a0b2704a1720f

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