Handling Git URIs in Conkeror

Conkeror has been my web browser of choice for years. I love its minimal interface. I love its efficient command system. But more than anything I love how easily I can implement custom behavior with a little bit of JavaScript.

In this article I will show you how I made Conkeror more convenient to use when browsing GitHub.

First of all I need to point out that I clone tons of repositories from GitHub. If a project looks the slightest bit interesting then I will clone it to my computer so I can read through the code. To do this I copy the URI for the repository from GitHub, switch to my terminal, and then run git clone after pasting the URI.

I got sick of manually copy-and-pasting those URIs. So I rolled up my sleeves—uhh on my short-sleeve Dragon Ball Z t-shirt—and decided to make Conkeror automate the process.

RFC 3986 defines the standard syntax for URIs. For my purposes I only care about the ‘scheme’, which is the first part of the URI. So for example, git is the scheme of git://github.com/ejmr/php-mode. Conkeror understands how to deal with common schemes such as http and ftp, not the one for Git though. But it is easy to teach Conkeror how to handle that scheme.

Conkeror uses ‘protocol handlers’ to deal with URI schemes that are not built into the browser. A common way to define a protocol handler is to associate it with an external program. For example, the browser does not know what to do with magnet URIs for torrents. I use Transmission as my BitTorrent client so when I click on a magnet URI I want Conkeror to open that torrent in Transmission. To make that possible all I need to do is define a protocol handler in my Conkeror configuration file like this:

set_protocol_handler("magnet", find_file_in_path("transmission-gtk"));

Now when I click on a magnet URI Conkeror will pass it along to Transmission. Pretty simple.

Now let’s revisit my original goal: I want Conkeror to automatically clone a Git repository when I click on a git URI. I start out with this:

set_protocol_handler("git", make_file(" ... "));

This is almost identical to how I use Transmission with the browser. All I need to do now is create a program that will accept a git URI and clone that repository. I wrote a shell script to do this, git-protocol-handler.sh:

#!/bin/bash

REPOSITORY_URI=${1:?"Missing required URI"}
OUTPUT_DIRECTORY="/home/eric/Temp/Git-Repos"

cd "$OUTPUT_DIRECTORY" &&
git clone "$REPOSITORY_URI" &&
exit 0

Now I can return to my Conkeror configuration and tell it to use this script.

set_protocol_handler("git", make_file("/home/eric/.conkerorrc/git-protocol-handler.sh"));

That’s it! Now when I click on a git URI in my browser it will automatically clone that repository to the directory defined in the shell script above. This is the major reason I love Conkeror: with a little bit of JavaScript and shell scripting I was able to add useful, time-saving behavior to my browser. Conkeror is not the only browser that is configurable in this way, but in my opinion it is the easiest to enhance.

In a future article I will show you how I can build on the code above so that I can clone repositories by pressing only three keys.

Advertisements

Add Your Thoughts

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s