I needed a way to auto-tag my release when I’m modifying the AssemblyInfo.cs file in my C# application.

I created a pre-commit, post-commit and simple version.sh file to do so:

#! /bin/bash
basedir="$(dirname "$0")"
. $basedir/version.sh
if [ "$version" != "" ]; then
    git tag -a "v$version" -m "`git log -1 --format=%s`"
    echo "Created a new tag, v$version"    
fi
#! /bin/bash
basedir="$(dirname "$0")"
. $basedir/version.sh
if [ "$version" != "" ]; then
    sed -i "1s/.*/version: ${version}.{build}/" $TOP_LVL/appveyor.yml
    git add $TOP_LVL/appveyor.yml
    echo "Modified AppVeyor config version, v$version"    
fi
#!/bin/bash
export ASSEMBLY_FILE='SoundSwitch/Properties/AssemblyInfo.cs'
export TOP_LVL="$(git rev-parse --show-toplevel)"
export version=`git diff HEAD^..HEAD -- $TOP_LVL/$ASSEMBLY_FILE | \
grep "assembly: AssemblyVersion" | sed -s 's/[^0-9\.]//g'| sed 's/.$//' | tail -1`

The version.sh contain which file need to be monitored. When this file is changed and part of the commit, the new version will be extracted. (please be sure you only have one

[assembly: AssemblyVersion("3.4.2.*")]

in your Assembly file). The version need to be specified as shown (X.X.X.X) where the last X is the build version (that I let Visual Studio manage and don’t consider relevant for a tag).

pre-commit

Check for version change, if it’s the case, change also the version the appveyor.yml file and add it to the commit.

post-commit

If the version change, add a new tag using the commit message and the version extracted from the AssemblyInfo.cs file.

 

Credits to entreprehero for the image