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:
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.
Copy versioning.py into your git repository.
Add a version.py file with this content:
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
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.