For some of you, this is a familiar scenario. A machine is running your code…and you have no idea what version it is. Perhaps you have just logged into a web server that no one has logged into in a while and you’re wondering how old those assemblies are. You’re thinking: “did we push last December’s release to this box?” It was a crazy release so, of course, no one remembers.

You may have a QA or production box that has had a few hotfixes deployed to it so some DLLs are at a later version than others. You may have a neglected failover web server on your hands. You may have a task server or middle tier server that share dependencies with your web server but you haven’t been keeping them all in sync and now one of them is causing problems. You may have a client that has some old version of your DLLs burned to a CD and is asking how much has changed since the CD was created. There’s no denying that it is hard to keep track of every DLL you compile and it is inevitable that you are going to run into one that leaves you wondering what version of the code it is.

This is a very common scenario and I’m going to show you a quick and easy way to fix it. What you need to do is embed the Git commit hash into the version number of each DLL before you compile. Simply run the following Powershell command from the root of your code repository:

$git_version = (&'C:Program Files (x86)Gitbingit.exe' rev-parse --short HEAD)
Get-ChildItem -recurse -include AssemblyInfo.cs | ForEach-Object {
     (Get-Content $_) -replace `
           '^s*[assembly: AssemblyFileVersion("(w*).(w*).(w*).([^"]*)")]', `
           "[assembly: AssemblyFileVersion(`"`$1.`$2.`$3.$git_version`")]" `
     | Out-File $_

This goes through all of your subfolders and opens any file called AssemblyInfo.cs and changes the last part of your version number to be the current Git commit. Compile your code again and browse to the DLL. Right click and hit Properties, then flip over to the Details tab.

details tab

There are a few ways to run this Powershell snippet. If you remove trailing backticks and line breaks you can paste it into a Powershell prompt as one long command. You could do it the old fashioned way and save it to a file and execute it every time you need to compile. I prefer not to need to remember to run this script since I always have a million other things on my mind on deployment night, so I logged into our build server and added it as a step to our build configuration to be run before the project compiles.

Hopefully this is a quick change to your build process but will provide benefits way out of proportion to how much effort it was. For any DLL, you will be able to use Git to view a diff on what code has been added or removed since it was created. If you have two DLLs at different versions you can see what the differences are. And if for some reason you can’t update that old DLL to the latest version but there is some bug you need to fix, you can branch from that revision, fix the bug and recompile without needing to worry about unrelated changes creeping in.

Topics: Tech Blog

Posts by Tag

See all

Subscribe Here!