the git commit id is a unique way to identify a certain instance of your software. Its especially helpful when lots of builds and versions are around (keeping your customer up-to-date with your development cycle).
I assume you use msysgit and added the bin directory to your PATH environment in such a way git and sed become available via the regular windows Commandline.
Straightforward thinking we want to process this kind of meta information by the “assembly” keyword, as we do for the application title, version and copyright.
First we have to layout a new class (global namespace)
[AttributeUsage(AttributeTargets.Assembly)]
public class AssemblyGitBuild : Attribute
{
public string gitBuild { get; private set; }
public AssemblyGitBuild(string txt) { gitBuild = txt; }
}
Add this to your Projects Prebuilding Events:
git rev-parse --short HEAD | sed -e 's/\(.*\)/[assembly: AssemblyGitBuild(\"\1\")]/' > $(ProjectDir)git_version.cs
Build it. Now you see a new file “git_version.cs” in your projects directory.
Add it to your project.
You can use the git id like that
String gitBuild = "unknown";
try
{
gitBuild = typeof(AssemblyGitBuild).Assembly.GetCustomAttributes(typeof(AssemblyGitBuild), false).Cast<AssemblyGitBuild>().First().gitBuild;
}
catch (Exception e)
{
Logger.Exception(e);
}
tstbVersion.Text = "Version " + Application.ProductVersion +" Build "+ gitBuild;