Last Updated: February 25, 2016
· dongli

Sync application version with Git tag automatically!

When developing an application, we need to track the version of it. In my work, I set a string in the code, and user can inquire it through some command option as following:

<command> -v

In the same time, the codes should be version controlled by a version control software. I prefer Git. In Git, there is a subcommand that let you tag the commit, so it is perfect to set version using it to show the development progress:


But sometimes I made a tag but forgot to update the version, so it would be great that the tag and the version can be synchronized. I searched around and wrote a pre-push (do not forget to make it executable!) hook to do the dirty job for me:



# check the latest tag
LATEST_TAG=$(git tag -l | tail -n 1)

# check the version number in code
LATEST_VERSION=$(awk -F '"' '/String version/ { print $2; }' $CODE)

# compare
if [[ $LATEST_TAG != $LATEST_VERSION ]]; then
    echo "[Warning]: The latest tag ($LATEST_TAG) does not equal" \
        "the latest version number ($LATEST_VERSION) in code!"
    echo "[Notice]: Be CLEAR-HEADED! Use tag (0) or version number (1)?"
    read -p '[0/1] > ' ans
    if [[ $ans == 0 ]]; then
        echo "[Notice]: Change the version number in code."
        awk -F '"' -v tag=$LATEST_TAG '/String version/ {
                sub($2, tag);
            } !/String version/ {
            }' $CODE > tmp
        mv tmp $CODE
        git add $CODE
        echo "[Notice]: Building installer"
        cd $DIR
        cd $REPO_DIR
        for file in $(ls $DIR/payload); do
            git add $DIR/payload/$file
        git add $DIR/codemate.installer
        git commit -m "Update version number to $LATEST_TAG"
        git tag -f $LATEST_TAG
    elif [[ $ans == 1 ]]; then
        echo "[Notice]: Retag to the new commit."
        git tag $LATEST_VERSION
        echo "[Error]: Unknown input \"$ans\", so quit pushing!"
        exit 1
    echo "[Notice]: Tag-version consistency is checked."

In short, I just compare the latest tag with the version number in the code, if they are different, then update the version number and add the changes in the code and commit, and finally retag.

To avoid silly mistake, I add a conditional block to let human choose which one should be used (tag or version number).