Skip to main content

No project description provided

Project description

Git Substitute

PyPI tests

This repository contains a basic tool to generate files with Git information through templates. A typical application could be embedding Git version info into source code right before compilation.

For many modern platforms, this is not relevant. In Python you can get version info automatically already through setuptools or poetry backends and in C++ you could configure CMake to do the same. But some more old-fashioned platforms, like TwinCAT PLC, don't have such options and then a tool like this could come in handy.

Help

usage: git_substitute [-h] [--output OUTPUT | --stdout] [--exact-only] [--repo REPO] [--verbose] [--quiet] [--version] template

Generate files with information from Git, based on templates.

positional arguments:
  template              Path to the template file. If this filename ends with `_template`, `_Template` or `Template`, and no other options are supplied, the output will be a similar file without this suffix.

options:
  -h, --help            show this help message and exit
  --output OUTPUT, -o OUTPUT
                        Output is saved to this file
  --stdout              Echo result to the terminal instead
  --exact-only          Without this flag, the repository is searched a level higher, with this flag only the specified directory is considered
  --repo REPO, -r REPO  Set explicit root to Git repository (by default, start searching for current directory)
  --verbose, -v         Print information to the terminal
  --quiet, -q           Print logs to stderr instead of stdout
  --version, -V         show program's version number and exit

Installing

The recommended system-wide install is through pipx: pipx install git-substitute

You can also install it regularly through pip (in your venv): pip install git-substitute.

Or from a local clone, run poetry install or pip install . [-e]. You could also install directly from git with pip install https://github.com/RobertoRoos/git-substitute.

How to use

Commands

Run as either git_substitute ... or python -m git_substitute ....

Typical usage: git_substitute current_version_template.cs. This will copy the template file, substitute Git variables and commands and create a new file with a similar name, but without _template in the name.

See git_substitute --help for all options.

Templates

Use {{...}} in template file for placeholders. There are types of content: variables and direct Git function calls.

Variables are in uppercase with underscores, and start with GIT_. E.g.: {{GIT_TAG}}.

Instead of predefined variables you could use direct calls to Git. These are lowercase and start with git .... E.g. {{git refname --dirty}}.
Note that the interface through GitPython is used, instead of opening a shell directly.

Variables

In case you are not sure what information to embed, use GIT_DESCRIPTION_DIRTY as it is very concise and clear.

Placeholder Description Example
GIT_HASH Full hash of the last commit 4cc498b3c37375d8d9138fdab553ced012cafc7a
GIT_HASH_SHORT 8-char hash of the last commit 4cc498b3
GIT_DATE Datetime of the last commit 17-12-2024 12:47:10
GIT_NOW The current date and time, not a git command at all 19-12-2024 16:20:35
GIT_TAG Most relevant tag (result of git tag) v1.0.0
GIT_VERSION Guaranteed 3-digit 1.2.3 like-string, based on most relevant tag 1.0.0
GIT_BRANCH Current branch name master
GIT_DESCRIPTION Most relevant tag + number of commits since then + last commit (result of git describe --tags --always) v0.0.1-1-g4cc498b
GIT_DESCRIPTION_DIRTY Same as GIT_DESCRIPTION, except it also adds the --dirty argument to mark if there were uncommitted changes v0.0.1-1-g4cc498b-dirty
GIT_DIRTY 1 if there are uncommited chances, otherwise 0 0

To use literal placeholder symbols in your template, escape them with a backslash: \{\{ and \}\}.

Example

// File: src/version_template.cpp

#include <string>

// Specificy the version for this library
const std::string version = "{{GIT_DESCRIPTION_DIRTY}}";

Then in your build script you may have a step like:

git_substitute src/version_template.cpp

Which will create src/version.cpp and can be compiled into your application.

How to develop

Install with development dependencies with poetry install --with dev.

Poetry Dynamic Versioning is used for automatic version detection.

How to publish

The package is published to PyPi, at https://pypi.org/project/git-substitute/.

Publishing is now done manually, through poetry build and poetry publish.

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_substitute-0.1.1.tar.gz (5.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_substitute-0.1.1-py3-none-any.whl (6.6 kB view details)

Uploaded Python 3

File details

Details for the file git_substitute-0.1.1.tar.gz.

File metadata

  • Download URL: git_substitute-0.1.1.tar.gz
  • Upload date:
  • Size: 5.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.5 CPython/3.12.7 Windows/10

File hashes

Hashes for git_substitute-0.1.1.tar.gz
Algorithm Hash digest
SHA256 d36ac629f95868e34cc7f0c8d2a0fdb1a8679a05effdc4760424eb412dfa1b10
MD5 a96561b5b4792149125d7fe1c59b746a
BLAKE2b-256 381e86aa08ee50502e5c9d1744684f5c8d9f8c6bd1b704494e895496a2dcbc69

See more details on using hashes here.

File details

Details for the file git_substitute-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: git_substitute-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 6.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.5 CPython/3.12.7 Windows/10

File hashes

Hashes for git_substitute-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 7284330839a3cafda673f1c386508b260c93ce71e16b912789c9d91bd8e0114c
MD5 aec982eab16cbdb29ead94402fd95b3c
BLAKE2b-256 8a0269d4502569faca379a935a84e2f80fade487e6c3acc0fcbba6d8f2eaeabd

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