How do I maintain the version number of my bash script which is git source controlled?
bash, git, version
Solution
As far as I can tell, what you want is impossible. In order to have the version number be committed into the version control software, you'd have to edit the version number, commit, then tag; not the other way around. You can however have the process a little more streamlined.
We can do this by writing a post-commit hook that'll read your script's version number and if it has changed from last time, write out a new tag. In order to do this, cd into `.git/hooks` from your project directory, create a file called `post-commit` (or move `post-commit.sample`) and make it executable. Then edit it so it looks something like this:
#!/bin/bash
NEWEST_TAG=$(git describe --abbrev=0 --tags)
SCRIPT_VERSION=$(grep "^version=" myscript | awk -F= '{print $2}')
if [ x$NEWEST_TAG != x$SCRIPT_VERSION ]; then
git tag -a $SCRIPT_VERSION -m "version $SCRIPT_VERSION"
fi
Next time you bump your script's version number and commit your changes, you'll see a new tag added to your latest commit.
Problem
I've just finished the first working version of a more complex bash script, and I'm wrapping my head around on how to maintain the scripts version. Why do I need this? Following GNU Coding Standards For Commandline Interfaces I've added a version option which among a license and copyright header shows the current version. Still, I don't know how to keep the version 'up-to-date'. My idea so far is to use git tags for major | minor | patch releases, and somehow replace a variable contained in the script. So, if I have a tag named `1.1.0`, then ``` $ myscript --version ``` Should output something like: ``` myscript 1.1.0 ``` The script contains a shell variable for this: ``` version=1.1.0 ``` Still, I don't now how to keep the version in sync with the latest tag?