0

Is there any way to generate some C# constant which values is the name of the current branch?

My goal is to generate something like this:

public static Constants {
    public const string MyProductVersionName = "release/V0.2";
}

(release/V0.2 is the name of the current branch).

My question applies both to TFS 2013 and/or GIT, as I'm evaluating both source control services.

I look at Tf Command-Line Utility Commands to see if I can tweak a pre build event, but I didn't find how to get the current branch name.

Using git, I found in another answer on SO this command line:

git rev-parse --abbrev-ref HEAD

This works fine.

In both case, what would be the correct way to generate such file?

Community
  • 1
  • 1
Steve B
  • 34,941
  • 18
  • 92
  • 155

1 Answers1

1

I've never had the misfortune to have to use TFS, so can't help there.

With git, you have identified how to get the current branch name. The trick then would be to use that to replace a character string in your file as a pre-build event. I'd suggest using powershell for this as it can offer far simpler-to-use search/replace features than cmd.

Change your file to something like:

public static Constants 
{
    public const string MyProductVersionName = "[BRANCH_VALUE]";
}

Then see How to capture output in a variable rather than a logfile? for details of capturing the output of git to a powershell variable. Then How can you find and replace text in a file using the Windows command-line environment? for details of search and replace.

The idea then would be to replace [BRANCH_VALUE] with the git branch in a pre-compile event.

Community
  • 1
  • 1
David Arno
  • 40,354
  • 15
  • 79
  • 124
  • 1
    I suspect I could also use a T4 template that rely on endte... but postbuild script will be far more easier to create. Just waiting for a TFS solution :). `tf.exe branches . /t` give the information, but with a high effort of parsing. – Steve B Oct 22 '13 at 07:37