Click here to Skip to main content
Click here to Skip to main content

Tagged as

TFS 2010 SDK: Smart Merge - Programmatically Create your own Merge Tool

, 1 Nov 2011
Rate this:
Please Sign up or sign in to vote.
Technorati Tags: Team Foundation Server 2010,TFS SDK,TFS API,TFS Merge Programmatically,TFS Work Items Programmatically,TFS Administration Console,ALM   Download a working Demo: Smart Merge using TFS API The information available in the Merge window in Team Foundation Server 2010 is very importan

The information available in the Merge window in Team Foundation Server 2010 is very important in the decision making during the merging process. However, at present the merge window shows very limited information, more than often you are interested to know the work item, files modified, code reviewer notes, policies overridden, etc associated with the change set. Our friends at Microsoft are working hard to change the game again with vNext, but because at present the merge window is a modal window you have to cancel the merge process and go back one after the other to check the additional information you need. If you can relate to what i am saying, you will enjoy this blog post! I will show you how to programmatically create your own merging window using the TFS 2010 API.

A few screen shots of the WPF TFS 2010 API – Custom Merging Application that we will be creating programmatically,

image

image_thumb12_thumb.png

Excited??? Let’s start coding…

1. Get All Team Project Collections for the TFS Server

You can read more on connecting to TFS programmatically on my blog post => How to connect to TFS Programmatically

   1:  public static ReadOnlyCollection<CatalogNode> GetAllTeamProjectCollections()
   2:  {
   3:              TfsConfigurationServer configurationServer =
   4:                      TfsConfigurationServerFactory.
   5:                           GetConfigurationServer(new Uri("http://xxx:8080/tfs/"));
   6:   
   7:              CatalogNode catalogNode = configurationServer.CatalogNode;
   8:              return catalogNode.QueryChildren(new Guid[] 
   9:                      { CatalogResourceTypes.ProjectCollection },
  10:                                 false, CatalogQueryOptions.None);
  11:   }

2. Get All Team Projects for the selected Team Project Collection

You can read more on connecting to TFS programmatically on my blog post => How to connect to TFS Programmatically

   1:  public static ReadOnlyCollection<CatalogNode> GetTeamProjects(string instanceId)
   2:  {
   3:              ReadOnlyCollection<CatalogNode> teamProjects = null;
   4:   
   5:              TfsConfigurationServer configurationServer =
   6:                      TfsConfigurationServerFactory.GetConfigurationServer(new Uri("http://xxx:8080/tfs/"));
   7:   
   8:              CatalogNode catalogNode = configurationServer.CatalogNode;
   9:              var teamProjectCollections = catalogNode.QueryChildren(new Guid[] {CatalogResourceTypes.ProjectCollection },
  10:                  false, CatalogQueryOptions.None);
  11:   
  12:              foreach (var teamProjectCollection in teamProjectCollections)
  13:              {
  14:                  if (string.Compare(teamProjectCollection.Resource.Properties["InstanceId"], instanceId, true) == 0)
  15:                  {
  16:                      teamProjects = teamProjectCollection.QueryChildren(new Guid[] { CatalogResourceTypes.TeamProject }, false,
  17:                                                  CatalogQueryOptions.None);
  18:                  }
  19:              }
  20:   
  21:           return teamProjects;
  22:  }

3. Get All Branches with in a Team Project programmatically

I will be passing the name of the Team Project for which i want to retrieve all the branches. When consuming the ‘Version Control Service’ you have the method QueryRootBranchObjects, you need to pass the recursion type => none, one, full. Full implies you are interested in all branches under that root branch.

   1:  public static List<BranchObject> GetParentBranch(string projectName)
   2:  {
   3:              var branches = new List<BranchObject>();
   4:   
   5:              var tfs = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new Uri("http://<ServerName>:8080/tfs/<teamProjectName>"));
   6:              var versionControl = tfs.GetService<VersionControlServer>();
   7:   
   8:              var allBranches = versionControl.QueryRootBranchObjects(RecursionType.Full);
   9:   
  10:              foreach (var branchObject in allBranches)
  11:              {
  12:                  if (branchObject.Properties.RootItem.Item.ToUpper().Contains(projectName.ToUpper()))
  13:                  {
  14:                      branches.Add(branchObject);
  15:                  }
  16:              }
  17:   
  18:              return branches;
  19:  }

4. Get All Branches associated to the Parent Branch Programmatically

Now that we have the parent branch, it is important to retrieve all child branches of that parent branch. Lets see how we can achieve this using the TFS API.

   1:  public static List<ItemIdentifier> GetChildBranch(string parentBranch)
   2:  {
   3:              var branches = new List<ItemIdentifier>();
   4:   
   5:              var tfs = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new Uri("http://<ServerName>:8080/tfs/<CollectionName>"));
   6:              var versionControl = tfs.GetService<VersionControlServer>();
   7:   
   8:              var i = new ItemIdentifier(parentBranch);
   9:              var allBranches =
  10:                  versionControl.QueryBranchObjects(i, RecursionType.None);
  11:   
  12:              foreach (var branchObject in allBranches)
  13:              {
  14:                  foreach (var childBranche in branchObject.ChildBranches)
  15:                  {
  16:                      branches.Add(childBranche);
  17:                  }
  18:              }
  19:   
  20:              return branches;
  21:  }

5. Get Merge candidates between two branches Programmatically

Now that we have the parent and the child branch that we are interested to perform a merge between we will use the method ‘GetMergeCandidates’ in the namespace ‘Microsoft.TeamFoundation.VersionControl.Client’ => http://msdn.microsoft.com/en-us/library/bb138934(v=VS.100).aspx

   1:  public static MergeCandidate[] GetMergeCandidates(string fromBranch, string toBranch)
   2:  {
   3:              var tfs = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new Uri("http://<ServerName>:8080/tfs/<CollectionName>"));
   4:              var versionControl = tfs.GetService<VersionControlServer>();
   5:   
   6:              return versionControl.GetMergeCandidates(fromBranch, toBranch, RecursionType.Full);
   7:  }

6. Get changeset details Programatically

Now that we have the changeset id that we are interested in, we can get details of the changeset. The Changeset object contains the properties => http://msdn.microsoft.com/en-us/library/microsoft.teamfoundation.versioncontrol.client.changeset.aspx

  • Changes: Gets or sets an array of Change objects that comprise this changeset.
  • CheckinNote: Gets or sets the check-in note of the changeset.
  • Comment: Gets or sets the comment of the changeset.
  • PolicyOverride: Gets or sets the policy override information of this changeset.
  • WorkItems: Gets an array of work items that are associated with this changeset.
   1:  public static Changeset GetChangeSetDetails(int changeSetId)

   2:   {
   3:              var tfs = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new Uri("http://<ServerName>:8080/tfs/<CollectionName>"));

   4:              var versionControl = tfs.GetService<VersionControlServer>();
   5:   

   6:              return versionControl.GetChangeset(changeSetId);
   7:   }

7. Possibilities

In future posts i will try and extend this idea to explore further possibilities, but few features that i am sure will further help during the merge decision making process would be,

  • View changed files
  • Compare modified file with current/previous version
  • Merge Preview
  • Last Merge date

Any other features that you can think of?

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Tarun_Arora
Software Developer (Senior) Avanade
United Kingdom United Kingdom
Solution Developer - C# .NET, ALM
 
Tarun Arora is a Microsoft Certified professional developer for Enterprise Applications. He has over 5 years of experience developing 'Energy Trading & Risk Management' solutions using Microsoft Technologies. Tarun has great passion for technology and travel (not necessarily in the same order)!
Follow on   Twitter

Comments and Discussions

 
GeneralVery useful tool Pinmember-=SerP=-25-Feb-14 6:11 
QuestionCode PinmemberAmit Deserwani14-Nov-13 0:26 
QuestionNice work! PinmemberBrian Welsh19-Jul-12 3:53 
QuestionMy Vote of 5 Pinmembervk1237723-Apr-12 5:19 
QuestionThanks for Sharing this. PinmemberMember 21821568-Nov-11 6:02 
AnswerRe: Thanks for Sharing this. PinmemberTarun_Arora8-Nov-11 6:43 
QuestionMy Vote of 5 PinmemberRaviRanjankr5-Nov-11 4:50 
AnswerRe: My Vote of 5 PinmemberTarun_Arora5-Nov-11 10:04 
QuestionGreat Work PinmemberAbinash Bishoyi3-Nov-11 6:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.140827.1 | Last Updated 1 Nov 2011
Article Copyright 2011 by Tarun_Arora
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid