DEV Community

Miro
Miro

Posted on

6 2

pre-commit autoincrement for c# projects

I was trying to figure out how to have autoincrement for c# projects that would work in the background, that would locally and that will not change on every build allowing you to build an app with the version stored in source control. And the result is this pre-commit script. It’s not perfect but it works for me. To use, put the file into .git\hooks\ folder in your project.

#!/bin/sh
#
# Update AssemblyFileVersion in every AssemblyInfo.cs if the files in solution
# change with the build part of the version number to be year & day of the year
# and the revision part with to be 1 & hour & minute.
# For example v1.0 commited at 15/06/2017 at 03:47 will become 1.0.17166.10347
#
# Known issues: does not work with subfolder changes
list_files() {
for f in $(git diff --staged --name-only)
do
dir=$(dirname $f)
find $dir -name "AssemblyInfo.cs"
done
}
build=$(date +%y%j)
rev=1$(date +%H%M)
for f in $(list_files | sort | uniq)
do
sed -i -e 's/AssemblyFileVersion(\"\([0-9]\+\)\.*\([0-9]\+\)*\.*\([0-9]\+\)*\.*\([0-9]\+\)*")/AssemblyFileVersion("\1\.\2\.'$build'\.'$rev'")/' $f
git add $f
done
view raw pre-commit hosted with ❤ by GitHub

Top comments (2)

Collapse
 
jessekphillips profile image
Jesse Phillips

I've been trying to take an approach which provides some dev control and increasing version per commit.

Unlike yours the version is injected by the build system.

Collapse
 
milolav profile image
Miro

I'd say that versioning through build system is the correct way. Mine was pretty much just a simple hack that works offline and without any dependencies.

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay