4

I'm new to extending Visual Studio and I'm trying to find way to find which source control system is used by current solution.

I created VsPackage project and I am able to obtain reference to solution via IVsSolution and to hook up to solution events via IVsSolutionEvents.

Inside OnAfterSolutionOpen (or possibly some other if there's an alternative) I would like to act differently basing on whether the solution uses TFS or Git or something else. How can I obtain this information?

I plan to support as many Visual Studio versions as possible, but if it isn't possible I would like to support at least VS2012 and higher.

Grzegorz Sławecki
  • 1,727
  • 14
  • 27

1 Answers1

5

Ok, after several hours of digging I've found a solution to this. Thanks to the article of Mark Rendle and the source code for his NoGit extension I've found, that the list of registered source control plugins is located in registry: HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\12.0_Config\SourceControlProviders (in case of VS 2013).

So now, we can have both plugin guid, and the name of the provider. This sample code can fetch those values:

var key = @"Software\Microsoft\VisualStudio\" + "12.0" + @"_Config\SourceControlProviders";

var subkey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(key);
var providerNames = subkey.GetSubKeyNames().Dump();
var dict = new Dictionary<Guid, String>();
foreach (var provGuidString in subkey.GetSubKeyNames())
{
    var provName = (string)subkey.OpenSubKey(provGuidString).GetValue("");
    dict.Add(Guid.Parse(provGuidString), provName);
}

Now, there are two ways I've found to obtain guid of currently active provider.

important update: Apparently the second way of obtaining currently active plugin does not work as expected. I strongly advise using first solution.

  1. This is the way that bases on the extension mentioned earlier:

    var getProvider = GetService(typeof(IVsRegisterScciProvider)) as IVsGetScciProviderInterface;
    Guid pGuid;
    getProvider.GetSourceControlProviderID(out pGuid);
    
  2. Or we can just go to HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\12.0\CurrentSourceControlProvider and get the default value of this key:

    var key2 = @"Software\Microsoft\VisualStudio\12.0\CurrentSourceControlProvider";
    var guidString = (string)Microsoft.Win32.Registry.CurrentUser.OpenSubKey(key2).GetValue("");
    var currentGuid = Guid.Parse(guidString);
    

Now we just take var activeProviderName = dict[currentGuid]; and that's all.

Grzegorz Sławecki
  • 1,727
  • 14
  • 27