3

I want to implement build id to about dialog of application that uses git. I know unix command to get a build id from git, but I have no idea how to grab it during build.

3 Answers 3

6

Probably easiest way to do it is to use pre-build events. Solution is to call git command, dump output to a text file, include that file as a resource and load resource in C# code.

Add prebuild.cmd to your project directory with the following content:

cd %1
git log -n 1 --format=format:"%%h" HEAD > %2

Go to your project properties, tab Build Events and enter following command as pre-build event command line:

"$(ProjectDir)prebuild.cmd" "$(ProjectDir)" "$(ProjectDir)revision.txt"

Create an empty revision.txt file in your project directory (or run a build once). Add it to your project and set Embedded Resource build action for it. It also makes sense to add this file to .gitignore because it is auto-generated.

In your C# project add a new utility class:

public static class GitRevisionProvider
{
    public static string GetHash()
    {
        using(var stream = Assembly.GetExecutingAssembly()
                                    .GetManifestResourceStream(
                                    "DEFAULT_NAMESPACE" + "." + "revision.txt"))
        using(var reader = new StreamReader(stream))
        {
            return reader.ReadLine();
        }
    }
}

Don't forget to replace DEFAULT_NAMESPACE with default namespace of your project (it is prepended to resource names and there is no generic way to get that, you'll have to hard-code it).

This solution assumes that path to git exists in %PATH% environment variable.

Sign up to request clarification or add additional context in comments.

3 Comments

I would mention that you need to have git in your PATH in case you are using git bash (which uses sh.exe to run) and that Assembly is in System.Reflection :) these 2 things I had to look up and fix, otherwise it's perfect
aha you mentioned the PATH I didn't notice
Works great! I'm using git describe --always --dirty --tags instead of git log...
0

if you are using Nant, you have to do set of tasks to get the git id

after this you can execute regular expression and get the ID and set it to property.

Comments

0

Based on max's answer here's an alternative solution that doesn't create a resource but directly creates the class file from the prebuild.cmd.

Add prebuild.cmd to your project directory with this content:

@echo off
cd %1
for /F "delims=" %%i in ('git describe --always --dirty --tags') do set git_revision=%%i
echo public static class Git> %2
echo {>> %2
echo     public static string GetRevision()>> %2
echo     {>> %2
echo         return "%git_revision%";>> %2
echo     }>> %2
echo }>> %2

Go to your project properties, tab Build Events and enter the following command as pre-build event command line:

"$(ProjectDir)prebuild.cmd" "$(ProjectDir)" "$(ProjectDir)Git.cs"

Create an empty Git.cs file in your project directory (or run a build once) and add (Existing Item...) it to your project. Also add Git.cs to .gitignore because it is auto-generated.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.