Frecklefoot Frecklefoot - 2 years ago 231
C# Question

Query Git programmatically in Visual Studio

We just transitioned from using TFS to using Git as our VCS. We are connected to Git and can check-in and such with ease.

But we have a Visual Studio extension that needs to be rewritten for Git. Using TFS programmatically was rather straight-forward, but I can't get anything working with Git. The Microsoft docs are either wrong or I'm doing it wrong. For example, to get a reference to a Git repository, use

and you get that via
. That (supposedly) exists in the
namespace. But when trying to add a reference, Visual Studio claims that namespace doesn't exist; only

Does anyone know of some reliable documentation for using Git via Visual Studio? Or how to do it?

Answer Source

The namespace you mentioned is on the TFS server API. It's not available in the TFS Client Object Model. To work with git locally you're going to make sure you have a local clone and then you can talk to it using either LibGit2 (unmanaged), LibGitSharp (Managed) or by invoking and parsing git.exe directly.

Visual Studio doesn't ship with it's own object model for accessing Git, it really depends on these standard open source libraries. Visual Studio up to 2015 ships with a version of LibGitSharp. The next version of Visual Studio (out soon) ships with a new approach to accessing git repositories and wraps git.exe, performing actions against git out-of-process. A wrapper can be found in the Team Explorer extension folder, but I'm not sure on the support levels available on those assemblies:

C:\Program Files (x86)\Microsoft Visual Studio\VS15Preview\Common7\IDE\CommonExtensions\Microsoft\TeamFoundation\Team Explorer

If you need to check the available git repositories on the TFS server, you should use the REST api to query the available repo's, then drop to cloning a local git repository after fetching the right repository to clone.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download