8

I'd like to be able to look at an assembly and quickly identify which branch it was built out of.

What's the best way to cofigure TFS Build to include the name of the current branch in the Build Number field (or a more appropriate field) of the assemblies it creates?

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Robert Levy
  • 28,747
  • 6
  • 62
  • 94
  • 2
    I would suggest using the [AssemblyConfigurationAttribute](http://msdn.microsoft.com/en-us/library/system.reflection.assemblyconfigurationattribute.aspx) as this is intended to be used for free-form textual build configuration information. I would also suggest moving to a pattern where you have a shared [VersionInfo](http://stackoverflow.com/questions/62353/what-are-the-best-practices-for-using-assembly-attributes) file between projects so that you can update this information in a single place. I know nothing of TFS, so can't help you with that. – Tim Lloyd Jan 27 '11 at 10:09
  • @chibacity The branch isn't a configuration. AssemblyInformationalVersionAttribute would seem to be a more appropriate place to store that information. It's a little strange that there isn't an assembly attribute explicitly for the branch. – mhenry1384 Dec 12 '12 at 18:44

2 Answers2

4

Have a look at the Version Task in http://msbuildtasks.tigris.org/

It will enable you to update the assemblyinfo.cs on build.

That still leaves the "how" to identify the branch...

On the risk of having a religious debate on version numbering you can dedicate one part of the major.minor.build.revision part to the branch. For example, major.minor just follows your 'outside/commercial' version number, build is the buildnumber form TFS and revision 1 indicates your main branch, revision 2 indicates a certain branch. You can also use on of the other assembly version attributes, like AssemblyInformationalAttribute to store an branch id in it.

I'm not complete up-to-date how you can detect the branch you're on but worst case that is just a matter of getting the current-directory and apply some logic to infer the branch-name. The output of a tf branch . command should also give you the branch name for a current workspace but you'll need a custom task to extract only the first line.

Not a plug-and-play answer but it might bring you in the right direction.

rene
  • 41,474
  • 78
  • 114
  • 152
  • Great starting point, thanks. I'm surprised this isn't a more commonly asked question with a simple solution though. – Robert Levy Jan 28 '11 at 14:47
  • I know this is an older answer; just commenting for update purposes. At least in TFS 2013 and TFS 2015, `tf branch .` does not seem to work. `tf branch` requires a source and target argument, and seems to create a branch with the target name from the source directory. [click for documentation](https://msdn.microsoft.com/en-us/library/d73s8b27.aspx) – Chaim Eliyah Feb 12 '16 at 23:46
0

The version is easy in terms of getting it and updating the various AssemblyInfo.cs files.

I'd organise your TFS structure in a similar way to the Subversion conventions, so have a trunk, tags and branches folder.

Through consistent naming you can make sure that your specific MSBuild file is always a set number of directories down from either the trunk of from a specific branch name.

Have a custom MSBuild task that works this out for you (by navigating up the TFS folder hierarchy), and feed this into your property that is used to update the AssemblyInformationalAttribute. You can use the FileUpdate task from http://msbuildtasks.tigris.org/ to perform a Regex replace on your AssemblyInfo.cs files.

Wim
  • 11,998
  • 1
  • 34
  • 57