Visual Studio External Tools for Git and more

I admit it. For an application I’ve had open for literally over 10,000 total hours, there’s stuff about Visual Studio that I have no clue about. None. It’s a fun little revelation each time I inadvertently learn something new or useful. This post is about the most recent such time, when I got to learn about and employ some custom External Tools coupled with a customized Toolbar.

Rob Conery got to do the honors, via the excellent but somehow incomplete Mastering Git screen cast on TekPub. Upon doing his best to convince us that command line tools aren’t that scary, Rob showed off how you can create your own external tools to launch programs/commands right from within Visual Studio 2008. Yes, my friend, you can fully integrate Git into Visual Studio on your own, going from this:

to this:

But seriously, the ability to add your own External Tools to Visual Studio is pretty cool, so lets take a look at how it’s done, whether you want to open up a Git bash or, for example, Windows Explorer.

Note: everything below was done on Windows 7 x64. If you’re on a 32-bit system, your path to various commands will be different.

External Tools

Go to Tools > External Tools…

You’ll probably have a couple listed by default: Dotfuscator and a Create GUID, for example. Notice that these are propagated to the actual Tools menu, in the order specified (second screen-shot above).

Clicking Add, you can create a new tool by specifying a title, command, arguments, initial directory and some other options. Some explanations from the online help:

Command: Enter the path to the file you intend to launch, or choose Browse to navigate to the file. Files types that you can launch include .exe, .bat, .com, .cmd, and .pif. If the file resides on the system path, you can enter just the file name.

Arguments: Specifies the variables that are passed to the tool when launched.

Initial directory: Specifies the working directory of the tool.

Since you may not be familiar with Git, let’s start with a simple tool anyone may find useful. It’s going to open Windows Explorer at the location of the currently opened file (thanks to this .NET Tip of The Day). The argument, if you can’t see in the image is: /select,"$(ItemPath)"

I’m going to hit OK and move this command to the first in the list and now we see it in the Tools menu

Select this menu item and up pops an Explorer window at the location of the current file (or solution directory)! Not impressed? This is arguably useless since there is similar functionality if you right click on a folder in your solution and select "Open Folder in Windows Explorer". Fair enough. I, however, do this right-click-selecting from the context menu all the time and it really does take a bit more effort than I would like, so let’s go with our new Explorer tool and improve the situation just a bit.

Custom Toolbars

Right click on any empty space in your toolbar area and go to "Customize…" and then click "New…" and give it a title. This will create and popup an empty floating toolbar that you can then drag a bunch of commands to.

Click on the Commands tab, select the Tools category, and then drag over "External Command 1" to your toolbar, and voila! There are 24 available custom commands each corresponding to the order number of your defined external tools. Above we moved the “Explorer” tool to the top of the list, so it will represent “External Command 1”. Once you close out the customize tools window, your toolbar will correctly show the “Explorer” tool.

Select a place to dock the toolbar, and now we are only 1 click away from opening Explorer at the location of the current file. I’d say that’s pretty useful, if you ask me.

Lets get on to Git

Git isn’t quite the most friendly tool for GUI driven Windows folks, although there is a set of Git Extensions aimed at making it more intuitive, including a Visual Studio plug-in.

But even if you are comfortable with the command line interface, you can employ some very helpful short-cuts by creating custom External Tools that perform or launch common Git actions. Take for instance, a tool that opens a Git bash at the solution folder (assumed to be the repository location) in one click:

Create a “Git Console” tool as such (and integrate it into a toolbar) and rather than having to open the Git bash by leaving Visual Studio and opening it from a folder or manually navigating to your solution directory, and you have 1-click access to Git right within Visual Studio!

Rob enumerates a handful of tools for the most common functionality such as initializing a new repository, adding all, committing, opening up Gitk and more. The full list of tools and their respective command, arguments and properties is below:

Git Console

Command:

C:\Windows\SysWOW64\cmd.exe

Arguments:

/c ""C:\Program Files (x86)\Git\bin\sh.exe" –login -i"

Initial Dir:

$(SolutionDir)

Additional:

Close on exit

Git Gui

Command:

c:\Program Files (x86)\Git\bin\git.exe

Arguments:

gui

Initial Dir:

$(SolutionDir)

Additional:

Close on exit

Git Init

Command:

C:\Program Files (x86)\Git\bin\git.exe

Arguments:

init

Initial Dir:

$(SolutionDir)

Additional:

Use Output window, Close on exit

Ignore (opens a new .gitignore file)

Command:

C:\Windows\System32\notepad.exe

Arguments:

".gitignore"

Initial Dir:

$(SolutionDir)

Additional:

Close on exit

Add All

Command:

c:\Program Files (x86)\git\bin\git.exe

Arguments:

add .

Initial Dir:

$(SolutionDir)

Additional:

Use Output window, Close on exit

Git Commit

Command:

c:\Program Files (x86)\git\bin\git.exe

Arguments:

commit -am *MESSAGE*

Initial Dir:

$(SolutionDir)

Additional:

Use Output window, Close on exit

Git Status

Command:

c:\Program Files (x86)\git\bin\git.exe

Arguments:

status

Initial Dir:

$(SolutionDir)

Additional:

Use Output window, Close on exit

Gitk

Command:

c:\Program Files (x86)\git\cmd\gitk.cmd

Arguments:

–all

Initial Dir:

$(SolutionDir)

Additional:

Close on exit

Please check out Rob’s Mastering Git screen cast for further details and information. After going through the series I really do plan to test my will and use the console exclusively, and with a little help from these external tools, it shouldn’t be too hard indeed.