Logo
HOWTO: Get the Git Changes in the Team Explorer of Visual Studio from a package

Author: Carlos J. Quintero (Microsoft MVP) Applies to: Microsoft Visual Studio 2013
Date: April 2015   Microsoft Visual Studio 2015
       
Introduction

The Team Explorer of Visual Studio 2012 and higher is composed of pages (such as Home, My Work, Pending Changes, Work Items, Builds, etc.) and in turn each page is composed of sections. For example, the Git Changes page is composed of the Included Changes, Excluded Changes and Untracked Files sections. While an extension (such as a package) could interact directly as a client with some TFS server or Git Repository, in some cases your extension may be interested in the information of the different pages and sections of the Team Explorer client, for example to provide new pages, new sections inside an existing page, new menu entries in the context menus of the Team Explorer, etc.

More Information

The following package creates a menu command that, when clicked, shows the information about the Git Changes page. The required assemblies are the following:

  • Microsoft.TeamFoundation.Controls.dll (in folder C:\Program Files (x86)\Microsoft Visual Studio <version>\Common7\IDE\ReferenceAssemblies\v4.5\)
  • Microsoft.TeamFoundation.Git.Controls.dll (in folder C:\Program Files (x86)\Microsoft Visual Studio <version>\Common7\IDE\ReferenceAssemblies\v4.5\)
Language: C#   Copy Code Copy Code (IE only)
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.ComponentModel.Design;
using Microsoft.VisualStudio.Shell;

namespace Company.VSPackageTeamExplorer
{
   [PackageRegistration(UseManagedResourcesOnly = true)]
   [InstalledProductRegistration("#110", "#112", "1.0", IconResourceID = 400)]
   [ProvideMenuResource("Menus.ctmenu", 1)]
   [Guid(GuidList.guidVSPackageTeamExplorerPkgString)]
   public sealed partial class VSPackageTeamExplorerPackage : Package
   {
      public VSPackageTeamExplorerPackage()
      {
      }

      protected override void Initialize()
      {
         base.Initialize();

         // Add our command handlers for menu (commands must exist in the .vsct file)
         OleMenuCommandService mcs = GetService(typeof(IMenuCommandService)) as OleMenuCommandService;
         if (null != mcs)
         {
            // Create the command for the menu item.
            CommandID menuCommandID = new CommandID(GuidList.guidVSPackageTeamExplorerCmdSet, (int)PkgCmdIDList.cmdidTeamExplorerInfo);
            MenuCommand menuItem = new MenuCommand(MenuItemCallback, menuCommandID);
            mcs.AddCommand(menuItem);
         }
      }

      private void MenuItemCallback(object sender, EventArgs e)
      {
         Microsoft.TeamFoundation.Controls.ITeamExplorer teamExplorer;

         teamExplorer = base.GetService(typeof(Microsoft.TeamFoundation.Controls.ITeamExplorer))
            as Microsoft.TeamFoundation.Controls.ITeamExplorer;

         ShowGitChangesPageInformation(teamExplorer, Microsoft.TeamFoundation.Controls.TeamExplorerPageIds.GitChanges);
      }

      private void ShowGitChangesPageInformation(Microsoft.TeamFoundation.Controls.ITeamExplorer teamExplorer, string pageGuid)
      {
         Microsoft.TeamFoundation.Controls.ITeamExplorerPage teamExplorerPage;
         Microsoft.TeamFoundation.Git.Controls.Extensibility.IChangesExt changesExt;

         teamExplorerPage = teamExplorer.NavigateToPage(new Guid(pageGuid), null);

         changesExt = teamExplorerPage.GetExtensibilityService(typeof(Microsoft.TeamFoundation.Git.Controls.Extensibility.IChangesExt))
            as Microsoft.TeamFoundation.Git.Controls.Extensibility.IChangesExt;

         if (changesExt != null)
         {
            ShowChangesProperties(changesExt);

            ShowChanges("IncludedChanges", changesExt.IncludedChanges);

            ShowChanges("SelectedIncludedChanges", changesExt.SelectedIncludedChanges);

            ShowChanges("ExcludedChanges", changesExt.ExcludedChanges);

            ShowChanges("SelectedExcludedChanges", changesExt.SelectedExcludedChanges);

            ShowUntrackedFiles("UntrackedFiles", changesExt.UntrackedFiles);

            ShowUntrackedFiles("SelectedUntrackedFiles", changesExt.SelectedUntrackedFiles);

            ShowWorkItemIds("WorkItemIds", changesExt.WorkItemIds);

            ShowWorkItemIds("SelectedWorkItemIds", changesExt.SelectedWorkItemIds);
         }
      }

      private void ShowChangesProperties(Microsoft.TeamFoundation.Git.Controls.Extensibility.IChangesExt changesExt)
      {
         System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();

         stringBuilder.AppendLine("CommitMessage :" + changesExt.CommitMessage);

         System.Windows.Forms.MessageBox.Show(stringBuilder.ToString());
      }

      private void ShowChanges(string description,
         IReadOnlyList<Microsoft.TeamFoundation.Git.Controls.Extensibility.IChangesPendingChangeItem> changes)
      {
         foreach (Microsoft.TeamFoundation.Git.Controls.Extensibility.IChangesPendingChangeItem change in changes)
         {
            ShowChange(description, change);
         }
      }

      private void ShowChange(string description, Microsoft.TeamFoundation.Git.Controls.Extensibility.IChangesPendingChangeItem change)
      {
         System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();

         stringBuilder.AppendLine(description + ":");
         stringBuilder.AppendLine();

         stringBuilder.AppendLine("ChangeType: " + change.ChangeType);
         stringBuilder.AppendLine("LocalItem: " + change.LocalItem);
         stringBuilder.AppendLine("SourceLocalItem: " + change.SourceLocalItem);

         System.Windows.Forms.MessageBox.Show(stringBuilder.ToString());
      }

      private void ShowUntrackedFiles(string description,
         IReadOnlyList<Microsoft.TeamFoundation.Git.Controls.Extensibility.IChangesUntrackedItem> untrackedFiles)
      {
         foreach (Microsoft.TeamFoundation.Git.Controls.Extensibility.IChangesUntrackedItem untrackedFile in untrackedFiles)
         {
            ShowUntrackedFile(description, untrackedFile);
         }
      }

      private void ShowUntrackedFile(string description,
         Microsoft.TeamFoundation.Git.Controls.Extensibility.IChangesUntrackedItem untrackedFile)
      {
         System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();

         stringBuilder.AppendLine(description + ":");
         stringBuilder.AppendLine();

         stringBuilder.AppendLine("LocalItem: " + untrackedFile.LocalItem);

         System.Windows.Forms.MessageBox.Show(stringBuilder.ToString());
      }

      private void ShowWorkItemIds(string description, IReadOnlyList<int> workItemIds)
      {
         foreach (int workItemId in workItemIds)
         {
            ShowWorkItemId(description, workItemId);
         }
      }

      private void ShowWorkItemId(string description, int workItemId)
      {
         System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();

         stringBuilder.AppendLine(description + ":");
         stringBuilder.AppendLine();

         stringBuilder.AppendLine("WorkItemId: " + workItemId);

         System.Windows.Forms.MessageBox.Show(stringBuilder.ToString());
      }
   }
}

Related articles



Go to the 'Visual Studio Extensibility (VSX)' web site for more articles like this (Articles section)