## Overview:

This is a very basic problem: I would like to know the Git checkout version of my Python code when running the code.

The standard way to achieve this is to use clean/smudge filters. However, the standard examples are quite basic and use a mixture of Perl/Ruby/Shell scripts.

Here is one script that can be added to a Python project, and which will work both as a clean and smudge filter:

https://gist.github.com/pkrusche/7369262

This script will transform stdin to expand some keywords with git version/author/date information. You can specify the option --clean to remove this information before commit.

## Setup:

• Copy versioning.py into your git repository.

• Add a version.py file with this content:

• Add this file to the repository:
• Run:

Note that the path to versioning.py must be relative to the root of your repository.

You update the file version like this (this can be a post-checkout hook, too – see below):

This should give you the following updated version.py file:

## Installing Hooks

Now, I might forget updating the version file every time I change something. The way to do this in git is to add hooks. I added two hooks to do this, a post-checkout hook and a post-commit hook. I used the same file (the files will be run in the root directory of the repository):

Now, every time I commit or checkout, my version number will be updated automatically.

Category: code Tags: python git