Geeks With Blogs

News




View Tarun Arora's profile on LinkedIn

profile for Tarun Arora at Stack Overflow, Q&A for professional and enthusiast programmers

Tarun Arora - Visual Studio ALM MVP ALM, Agile, Automation, Performance Testing, Software QA, Cloud, ...

 

Download a working Demo: Smart Merge using TFS API

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,

imageimage

image_thumb12

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?

Share this post:
Digg This
Posted on Sunday, June 26, 2011 4:26 PM TFS2010 , TFS API | Back to top


Comments on this post: TFS 2010 SDK: Smart Merge - Programmatically Create your own Merge Tool

# re: TFS 2010 SDK: Smart Merge - Programmatically Create your own Merge Tool
Requesting Gravatar...
Wow! now this is really impressive.
Left by Param on Jul 09, 2011 10:33 AM

# re: TFS 2010 SDK: Smart Merge - Programmatically Create your own Merge Tool
Requesting Gravatar...
This is awesome and looks a lot like a sketch I made yesterday of a merge helper app when I got frustrated with the process! :)

However, the demo download link is pointing to the Twitter app.... :-(
Left by Andre on Jul 28, 2011 3:46 PM

# re: TFS 2010 SDK: Smart Merge - Programmatically Create your own Merge Tool
Requesting Gravatar...
Hi Andre, Thanks for your comment. Sorry i'll be a typical developer and say that the link is working for me :-) Can you please try the direct link http://geekswithblogs.net/images/geekswithblogs_net/TarunArora/Windows-Live-Writer/TFS-2010-SDK-Smart-Merge-Programmaticall_E80F/TfsSdk-Integrate-Tfs-Twitter-Programmatically.zip If this does not work, please drop me a line on tarun.arora@blablah.avanade.com (remove the blahblah.) and i'll email you the solution.
Left by Tarun Arora on Jul 28, 2011 3:49 PM

# re: TFS 2010 SDK: Smart Merge - Programmatically Create your own Merge Tool
Requesting Gravatar...
Hmmm, then I'be a typical tester and say: Resolution denied :-)

The link is working for me too, but the zip file contains your twitter integration app, not the smart merge app.
Left by Andre on Jul 28, 2011 4:55 PM

# re: TFS 2010 SDK: Smart Merge - Programmatically Create your own Merge Tool
Requesting Gravatar...
Thanks a lot Andre. That is a valid bug (added to the backlog), i'll correct the link in the blog over the weekend. But for now, i have uploaded the solution zip on the skydrive, please feel free to download it from https://skydrive.live.com/?cid=61d0a67d27b527d3&sc=documents&uc=1&id=61D0A67D27B527D3%21253#
Cheers, Tarun
Left by Tarun Arora on Jul 29, 2011 12:15 PM

# re: TFS 2010 SDK: Smart Merge - Programmatically Create your own Merge Tool
Requesting Gravatar...
Hi Tarun,

This link is still mapping to your twitter integration app, not the smart merge app.
am even unable to download TFS2010SmartMerge from Skydrive. I would be great if you can email the zip file. or point me to the right link.

Thanks a lot

Left by coder on Aug 09, 2011 10:30 AM

# re: TFS 2010 SDK: Smart Merge - Programmatically Create your own Merge Tool
Requesting Gravatar...
Hi Coder, I hope you got my email with the zip attachment of the solution. I had also corrected the permissions and made the skydrive link public. If you need any other details, feel free to post here.
Cheers, Tarun
Left by Tarun Arora on Aug 14, 2011 2:10 AM

# re: TFS 2010 SDK: Smart Merge - Programmatically Create your own Merge Tool
Requesting Gravatar...
Hi All,

I have fixed the download link which was earlier downloading the TFS-Twitter Integration link. Please use the following link to download the demo application, https://skydrive.live.com/#!/?cid=61D0A67D27B527D3&id=61D0A67D27B527D3%21253.

Cheers, Tarun
Left by Tarun Arora on Nov 02, 2011 2:14 AM

# re: TFS 2010 SDK: Smart Merge - Programmatically Create your own Merge Tool
Requesting Gravatar...
Pretty good post. I just stumbled upon your blog and wanted to say that I have really enjoyed reading your blog posts.I recently came across your blog and have been reading along. I thought I would leave my first comment. I don’t know what to say except that I have enjoyed reading. Nice blog. I will keep visiting.skin tag
Left by garyjhonson on Nov 10, 2011 11:36 AM

# re: TFS 2010 SDK: Smart Merge - Programmatically Create your own Merge Tool
Requesting Gravatar...
What has to be done, so we can do baseless merge?
Left by Dick Wenning on Nov 21, 2011 9:54 AM

# re: TFS 2010 SDK: Smart Merge - Programmatically Create your own Merge Tool
Requesting Gravatar...
Hi Dick Wenning,

If you want to perform a baseless merge in general then you can do it via TFS command line utilities. For example "Tf merge /baseless c:\data\proj1 c:\data proj2 /recursive"

Please refer to this link for more details, http://msdn.microsoft.com/en-us/library/bb668976.aspx

Do you want to perform the baseless merge via API instead?

Cheers, Tarun
Left by Tarun Arora on Nov 21, 2011 9:57 AM

# re: TFS 2010 SDK: Smart Merge - Programmatically Create your own Merge Tool
Requesting Gravatar...
Nice article, would love to see the code behind it. But can't download the code. Geekswithblogs link still points to your TFS-Twitter demo. and my corp firewall (as i'm sure many others) considers Skydrive a filesharing site, and denies access.

thanks
Left by Brian on Jul 18, 2012 4:21 PM

# re: TFS 2010 SDK: Smart Merge - Programmatically Create your own Merge Tool
Requesting Gravatar...
Hi Brian, emailed you the code. It can also be downloaded from https://skydrive.live.com/?cid=61D0A67D27B527D3&id=61D0A67D27B527D3%21253

Cheers, Tarun
Left by Tarun Arora on Jul 20, 2012 12:05 AM

comments powered by Disqus

Copyright © Tarun Arora [Microsoft MVP] | Powered by: GeeksWithBlogs.net