Click here to Skip to main content
15,881,455 members
Articles / Programming Languages / C#

TFS 2010 SDK: Smart Merge - Programmatically Create Your Own Merge Tool

Rate me:
Please Sign up or sign in to vote.
4.36/5 (6 votes)
1 Nov 2011CPOL3 min read 45.5K   8   12
TFS 2010 SDK: Smart Merge - Programmatically create your own Merge tool

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

image

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

C#
 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

C#
 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.

C#
 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. Let's see how we can achieve this using the TFS API.

C#
 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 namespaceMicrosoft.TeamFoundation.VersionControl.Client’ => http://msdn.microsoft.com/en-us/library/bb138934(v=VS.100).aspx

C#
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 Programmatically

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
C#
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)


Written By
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)!

Comments and Discussions

 
QuestionWhat's wrong with source code Pin
Sapan Patibandha3-Apr-18 2:07
Sapan Patibandha3-Apr-18 2:07 
QuestionSourceCode? Pin
cSonic24-Sep-15 1:45
cSonic24-Sep-15 1:45 
Questionfutures roadmap ? Pin
kiquenet.com28-Nov-14 0:55
professionalkiquenet.com28-Nov-14 0:55 
GeneralVery useful tool Pin
-=SerP=-25-Feb-14 6:11
-=SerP=-25-Feb-14 6:11 
QuestionCode Pin
Amit Deserwani14-Nov-13 0:26
Amit Deserwani14-Nov-13 0:26 
QuestionNice work! Pin
Brian Welsh19-Jul-12 3:53
Brian Welsh19-Jul-12 3:53 
QuestionMy Vote of 5 Pin
vk1237723-Apr-12 5:19
vk1237723-Apr-12 5:19 
QuestionThanks for Sharing this. Pin
Member 21821568-Nov-11 6:02
Member 21821568-Nov-11 6:02 
AnswerRe: Thanks for Sharing this. Pin
Tarun_Arora8-Nov-11 6:43
Tarun_Arora8-Nov-11 6:43 
QuestionMy Vote of 5 Pin
RaviRanjanKr5-Nov-11 4:50
professionalRaviRanjanKr5-Nov-11 4:50 
AnswerRe: My Vote of 5 Pin
Tarun_Arora5-Nov-11 10:04
Tarun_Arora5-Nov-11 10:04 
QuestionGreat Work Pin
Abinash Bishoyi3-Nov-11 6:30
Abinash Bishoyi3-Nov-11 6:30 

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

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