I wondered if there was a way to automatically update the patch version of SemVer when a commit is made. I tried to make it using the commit hook in python.
- Create a
version.py
file with a variable to store the version:
1 | # version.py |
- Add a pre-commit hook script that updates the version in the
version.py
file:
1 | # .git/hooks/pre-commit |
This script gets the current version number from the version.py
file,
increments the patch number, updates the version in version.py
, and stages the
file for commit.
- Make the pre-commit hook script executable:
1 | chmod +x .git/hooks/pre-commit |
Now, when you make a commit, the pre-commit hook script will automatically
update the version in version.py
. You can use the __version__
variable in
your Python code to refer to the current version number. For example:
1 | # main.py |
This will output MyApp version 0.1.1
if the latest commit incremented the
patch number.