Nope, this is not about the Nirvana cover of the The Vaselines song "Son of a Gun"... No, actually this is about something that Junio C Hamano brought up in <> on the git mailing list. The idea is basically, to be able to jump to the top level directory of a git repository to run commands on the whole repository, that would otherwise behave differently. Like `git-grep'. He posted a shell snippet, that solves the problem by implementing a shell function `cdup':

% cd foo/bar
% emacs *.baz
% cdup
% git grep whatever

When I read that, I thought... Well, I'm cding up to repository-roots all the time, so that's a good idea. But contrary to popular believe, git is not the only version control system on the planet. And it would be cool to have something like that working transparently for all VCSs available. Admittedly, all systems isn't possible. But a lot damn well is. Because zsh's vcs_info sub-system knows all that is needed for no less than a dozen version control systems.

So, I wrote something that does the jumping around transparently. It also keeps a stack of sub-directories of a repository you've visited. Much like the `cd' builtin does for normal directory changes, if properly configured. Now I can do this:

% cd /usr/src/gnus
% vcsuptool list
vcsupstack empty
% cd contrib
% ,
% vcsuptool list
   0  /usr/src/gnus/contrib
% cd lisp/tests
% cd www/
% vcsuptool list
   0  /usr/src/gnus/www/
   1  /usr/src/gnus/lisp/tests
   2  /usr/src/gnus/contrib
% , -1
% pwd
% ,
% pwd
% ,
% pwd

The `vcsuptool' utility allows for full access to the stack the system is keeping. You'd normally not use it - unless you really want/need to. A lot of the behaviour is configurable, including the command name, which is a comma per default. All of that is documented on top of the function file.

If that file is in your `$fpath', you can do this:

autoload -Uz vcsupinit

If you don't like the command you'd use to be a comma (,) you may call vcsupinit differently:

vcsupinit cdup

That would make the command be named `cdup', like Junio called it. Personally, I find that way too long to type. But maybe that's just me. Both `vcsuptool' and `,' come with a decent compsys completion. So, you can take a look at the stack interactively by doing:

% , -<tab>

The code is zsh-only. Sorry ksh and bash folks. It needs to be a very recent zsh, too. A recent CVS build or the upcoming 4.3.12 release will be recent enough.

Posted Sat 28 May 2011 16:09:04 CEST Tags: