Sleeping Cyborg

{ Jonathan D. Page · Twitter · Github · Formspring }
{ Kevin Keller }

Mercurial Current Branch Bash Prompt Revisited

by Jonathan D. Page on 17 June 2011. [Comments]

Since I published Mercurial current branch in your Bash prompt in April, I’ve made some improvements.

The first is that, in a freshly initialized Mercurial repo, reporoot/.hg/branch isn’t written, so fasthgbranch doesn’t find anything. The fix for this is simple: just output “default” if you find a Mercurial repo, but no branch file.

The second is that it now works on Mac OS X (and probably, by extension, *BSD). I recently got a programming job (where we use Mercurial!), and my work computer is a Mac. Porting to OS X involved two changes. Firstly, I had to replace the Linux-only system call get_current_dir_name() with the equivalent getcwd(NULL, 0), which is also a Linux extension to POSIX, but happens to work on OS X too. Secondly, OS X sed behaves slightly differently from GNU sed. GNU sed supports both “&” and “\0” to mean “the entire match”, while OS X sed only supports &.

All of these improvements have been reflected in the original article.