So here's a little instruction on how to set up git proxy switching to swtich between accessing your home git repos from work, where you have to go through SOCKS proxy for external access, and git repos on internal network.

Setting the environment variable GIT_PROXY_COMMAND overrides whatever proxy configuration already exists - this is all we need to get git:// repositories to work. To get Git-over-SSH connections to work, which you probably want for push operations, you’ll also need to set up GIT_SSH.

~/.gitconfig-work

First of all, save the .gitconfig you use at work as ~/home/.gitconfig-work. It might look something like this:

[user]
    name = Takeshi Kanemoto
    email = takeshi.kanemoto@bashmygash.com
[color]
    ui = auto
[core]
    attributesfile = ~/.gitattributes

~/.gitconfig-home

Next, we set up a .gitconfig used to access your home git repos, located on host bashmygash.com. The following example assumes that you are using gerrit to manage your git repositories, hence the port 29418.

[user]
    name = Takeshi Kanemoto
    email = takeshi.kanemoto@bashmygash.com
[color]
    ui = auto
[url "ssh://takeshi.kanemoto@tak.atso-net.jp:29418/"]
    insteadOf = git://tak.atso-net.jp/
    pushInsteadOf = git://tak.atso-net.jp/
[core]
    attributesfile = ~/.gitattributes

~/.gitconfig-local

This is a .gitconfig you would use at home, pulling your local git repositories using git:// for faster access.

[user]
    name = Takeshi Kanemoto
    email = tak.kanemoto@private-email.com
[color]
    ui = auto

[url "ssh://tak.kanemoto@localhost:29418/"]
    pushInsteadOf = git://tak.atso-net.jp/
[core]
    attributesfile = ~/.gitattributes

~/bin/git-switch

#!/bin/sh
# Filename: ~/bin/git-switch
# This script switches between work, home, and local
# It MUST be sourced.

echo "I: setting up for $1..."

[ -e ~/.gitconfig ] && rm -v ~/.gitconfig

case $1 in
    home)
        export GIT_SSH="${HOME}/bin/socks-ssh"
        export GIT_PROXY_COMMAND="${HOME}/bin/socks-gw"
        ln -v -s .gitconfig-home ~/.gitconfig
        ;;
    local)
        unset GIT_SSH
        unset GIT_PROXY_COMMAND
        ln -v -s .gitconfig-local ~/.gitconfig
        ;;
    work|*)
        unset GIT_SSH
        unset GIT_PROXY_COMMAND
        ln -v -s .gitconfig-work ~/.gitconfig
esac

echo "I: GIT_SSH=$GIT_SSH"
echo "I: GIT_PROXY_COMMAND=$GIT_PROXY_COMMAND"

echo "I: done!"

~/bin/socks-gw

#!/bin/sh
# Filename: ~/bin/socks-gw
# This script connects to a SOCKS proxy using connect.c
$HOME/bin/connect -H proxy.bashmygash.net:8080 $@

You will need to have a built binary from connect.c in ~/bin/connect

~/bin/socks-ssh

#!/bin/sh
# Filename: ~/bin/socks-ssh
# This script opens an SSH connection through a SOCKS server
ssh -o ProxyCommand="${HOME}/bin/socks-gw %h %p" $@

Usage

Now, all you need to do is:

. ~/bin/git-switch {work|home|local}

Or even better, you can add the following to ~/.bashrc:

alias gits='. ${HOME}/bin/git-switch'

and all you have to do is

gits {work|home|local}

References

http://threebytesfull.com/2008/04/git-with-and-without-proxy/

On 19 November 2010

Comments