Keith Rarick

One-Click Cloning

Here’s an easy way to enable all those git:// URIs you see in the git repos.

First, save the following shell script somewhere in your path, name it “git-web-clone”, and make it executable.

#!/usr/bin/env bash

###
### Customize this to the directory where you want to keep all your clones.
###
WORK_DIRECTORY="$HOME/src"

main() {
  (
    set -e

    cd $WORK_DIRECTORY

    dir="$(basename "$uri")"
    fulldir="$PWD/$dir"

    if test -d "$fulldir"
    then
      notify-send -i info "Repo Already Exists" "$fulldir"
      exit 0
    fi

    notify-send -u low -c transfer "Cloning" "$uri"

    git clone "$uri"

    notify-send -c transfer.complete "Clone Complete" "$fulldir"
  )
}

# Pretty much everything after here is error handling.

uri="$1"

if test -z "$uri"
then
  notify-send -i error "No URI to Clone"
  exit 1
fi

out="$( { main "$@" ; } 2>&1 )"
res=$?

if test $res -ne 0
then
  notify-send -i error -c transfer.error "Error Cloning Repository" "$uri

Output:
$out"
fi

# vim: et sw=2 sts=2

Then, register a protocol handler in Firefox:

Visit about:config; right-click and select New → String, enter “network.protocol-handler.app.git” as the name, and “/path/to/git-web-clone” as the value.

Then right-click and select New → Boolean, enter “network.protocol-handler.external.git” as the name, and select true as the value.

For bonus points, let’s register a protocol handler for the rest of Gnome:

$ gconftool-2 --set --type=string /desktop/gnome/url-handlers/git/command '/path/to/git-web-clone "%s"'
$ gconftool-2 --set --type=bool /desktop/gnome/url-handlers/git/enabled true
$ gconftool-2 --set --type=bool /desktop/gnome/url-handlers/git/need-terminal false

Now, clicking on any git:// URI will clone the repo into your work directory. Nice!

Comments