Mike Hommey: Adding some VCS information in bash prompt
I don’t spend a lot of time customizing my “working” environment nowadays, like enhancing vim configuration, or tweaking the shell. But when I read MadCoder’s zsh git-enabled prompt, I though it was too convenient to not have something like that. Except I don’t work with git only (sadly, but that’s changing), and I don’t like colours in prompt (and a 2 lines prompt is too much).
Anyways, since I have a bunch of directories in my $HOME that contain either svk, svn, mercurial, or git working trees, I thought it would be nice to have some information about all this on my prompt.
After a few iterations, here are the sample results:
The base directory of a git working tree can be deduced from the result of
For the ref, I wanted something like the name of the directory that has been checked out at the top-level directory (usually “trunk” or a branch name), followed by the revision number.
mh@namakemono:~/dd/packages$
mh@namakemono:(svn)~/dd/packages/iceape[trunk:39972]debian/patches$
mh@namakemono:(svk)~/dd/packages/libxml2[trunk:1308]include/libxml$
mh@namakemono:(hg)~/moz/cvs-trunk-mirror[default]uriloader/exthandler$
mh@namakemono:(git)~/git/webkit[debian]JavaScriptCore/wtf$
The script follows, with a bit of explanation intertwined.
_bold=$(tput bold)
_normal=$(tput sgr0)
tput is a tool I only dicovered recently, and avoids the need to know the escape codes. There are also options for cursor placement, colours, etc. It lies in the ncurses-bin package, if you want to play with it.
__vcs_dir()
local vcs base_dir sub_dir ref
sub_dir()
local sub_dir
sub_dir=$(readlink -f "$ PWD ")
sub_dir=$ sub_dir#$1
echo $ sub_dir#/
We declare as much as possible as local (even functions), so that we avoid cluttering the whole environment. sub_dir is going to be used in several places below, which is why we declare it as a function. It outputs the current directory, relative to the directory given as argument.
git_dir()
base_dir=$(git-rev-parse --show-cdup 2>/dev/null) return 1
base_dir=$(readlink -f "$base_dir/..")
sub_dir=$(git-rev-parse --show-prefix)
sub_dir=$ sub_dir%/
ref=$(git-symbolic-ref -q HEAD git-name-rev --name-only HEAD 2>/dev/null)
ref=$ ref#refs/heads/
vcs="git"
This is the first function to detect a working tree, for git this time. Each of these functions set the 4 variables we declared earlier: vcs, base_dir, sub_dir and ref. They are, respectively, the VCS type, the top-level directory of the working tree, the current directory, relative to base_dir, and the branch, revision or a reference in the repository, depending on the VCS in use. These functions return 1 if the current directory is not in a working tree of the currently considered VCS.The base directory of a git working tree can be deduced from the result of
git-rev-parse --show-cdup, which gives the way up to the top-level directory, relative to the current directory. readlink -f then gives the canonical top-level directory. The current directory, relative to the top-level, is simply given by git-rev-parse --show-prefix.git-name-rev --name-only HEAD gives a nice reference for the current HEAD, especially if you’re on a detached head. But this can turn out to do a lot of work, introducing a slight lag when you cd for the first time in the git working tree, while most of the time, the HEAD is just a symbolic ref. This is why we first try git-symbolic-ref --name-only HEAD.
svn_dir()
[ -d ".svn" ] return 1
base_dir="."
while [ -d "$base_dir/../.svn" ]; do base_dir="$base_dir/.."; done
base_dir=$(readlink -f "$base_dir")
sub_dir=$(sub_dir "$ base_dir ")
ref=$(svn info "$base_dir" awk '/^URL/ sub(".*/","",$0); r=$0 /^Revision/ sub("[^0-9]*","",$0); print r":"$0 ')
vcs="svn"
Detecting an svn working tree is easier : it contains a .svn directory, be it top-level or sub directory. We look up the top-level directory by checking the last directory containing a .svn sub directory on the way up. This obviously doesn’t work if you checkout under another svn working tree, but I don’t do such things.For the ref, I wanted something like the name of the directory that has been checked out at the top-level directory (usually “trunk” or a branch name), followed by the revision number.
svk_dir()
[ -f ~/.svk/config ] return 1
base_dir=$(awk '/: *$/ sub(/^ */,"",$0); sub(/: *$/,"",$0); if (match("'$ PWD '", $0"(/ $)")) print $0; d=1; /depotpath/ && d == 1 sub(".*/","",$0); r=$0 /revision/ && d == 1 print r ":" $2; exit 1 ' ~/.svk/config) && return 1
ref=$ base_dir##*
base_dir=$ base_dir%%
*
sub_dir=$(sub_dir "$ base_dir ")
vcs="svk"
svk doesn’t have repository files in the working tree, so we would have to ask svk itself if the current directory is a working tree. Unfortunately, svk is quite slow at that (not that it takes several seconds, but that induces a noticeable delay to display the prompt), so we have to parse its config file by ourselves. We avoid running awk twice by outputing both the informations we are looking for, separated by a carriage return, and then do some tricks with bash variable expansion.
hg_dir()
base_dir="."
while [ ! -d "$base_dir/.hg" ]; do base_dir="$base_dir/.."; [ $(readlink -f "$ base_dir ") = "/" ] && return 1; done
base_dir=$(readlink -f "$base_dir")
sub_dir=$(sub_dir "$ base_dir ")
ref=$(< "$ base_dir /.hg/branch")
vcs="hg"
I don’t use mercurial much, but I happen to have exactly one working tree (a clone of http://hg.mozilla.org/cvs-trunk-mirror/), so I got some basic information. There is no way we can ask mercurial itself for information, it is too slow for that (main culprit being the python interpreter startup), so we take the informations we can (and since I don’t know much about mercurial, that’s really basic). Note that if you’re deep in the VFS tree, but not in a mercurial working tree, the while loop may be slow. I didn’t bother much looking for a better solution.
git_dir
svn_dir
svk_dir
hg_dir
base_dir="$PWD"
Here we just run all these functions one by one, stopping at the first that matches. Adding some more for other VCS would be easy.
echo "$ vcs:+($vcs) $ _bold $ base_dir/$HOME/~ $ _normal $ vcs:+[$ref]$ _bold $ sub_dir $ _normal "
PS1='$ debian_chroot:+($debian_chroot) \u@\h:$(___vcs_dir)\$ '
Finally, we set up the prompt, so that it looks nice with all the gathered information.
Update: made the last lines of the script a little better and factorized.
Via
The Reader monad is a simple and easy-to-use structure from the Haskell library that solves a common problem.
Last week I helped
There are quite a few important events related to Ubuntu -- and to free
software communities more generally -- in the Boston area in the next
few weeks. I plan to participate in many of them.
First, this coming Saturday, October 13, there will be an Ubuntu
I've been meaning to write this for weeks. Hopefully I can still remember
everything.
So Sarah had a c-section the day before we were scheduled to have our
c-section class. We'd both read a bit though, so it wasn't like we were
going in totally uninformed and unprepared..
Firstly, Stanford Hospital's operating room area is rather mind-blowing.
Sarah was transferred from Lucille Packard Children's Hospital to Stanford
because they could get an operating room there sooner. The transfer was
trivial, just roll the whole bed down the corridor, through the magic double
doors into the operating rooms. The doors in question have a whole heap of
video cameras pointing straight down, so they see everyone that gets wheeled
in and out, which I found interesting. Good for records of patient movements
I guess.
Anyway, through those doors is a maze of corridors. There was a light-up
board hanging from the ceiling in the middle of the main corridor with
lights for all of the ORs. I think there were about 20. The illuminated ones
were available. We went down a corridor, and past a few ORs. Outside of each
one is a big stainless-steel scrub area.
We got to "our" OR, and they transferred Sarah onto the operating table.
Because of her heart condition, they wanted to have an arterial line in her
for the duration of the procedure. This is some sort of gadget that goes
into an artery and monitors your blood pressure beat by beat.
The "interesting" thing with the anaesthesiologist was the New Zealand woman
that we'd seen when we had an anaesthesiologist months prior, but the
appointment was with a different anaesthesiologist, and this New Zealand
woman was "observing". At the time, the anaesthesiologist we had the
appointment with told us that she was going to be on vacation at the time of
the delivery, so she wouldn't be attending. So I don't know if the Kiwi
woman got promoted out of "observing" to "doing" in that period, or what,
but she reintroduced herself to us in LPCH prior to moving to Stanford as
being our anaesthesiologist for the delivery.
Anyway, in the OR, they handed her the arterial line, in it's plastic
packaging, and the first thing she asked was whether it came with anything
else to put it in. At this point I figured she hadn't put one of these in
before. (The answer to her question, by the way, was "no")
She then proceeded to have a terrible time getting the line into Sarah's
arm. Normally intravenous lines go into a vein, but this has to go into an
artery. Apparently when you go poking an artery, it spasms. It then becomes
very hard to get it into a moving target. She had a few goes on one arm,
then gave up, and had to move over to the other arm.
Sarah doesn't like needles at the best of times, and getting this honking
great big thing in was no picnic. The bruising on both her wrists afterwards
was phenomenal. She tends to bruise fairly spectacularly whenever they stick
a needle in her. I almost stepped in and asked if it was really necessary
that she have it, because they were having so much trouble getting it in,
and it was clearly hurting Sarah. I think someone else ended up getting it
in, not the anaesthesiologist. Not "that" one anyway. There were heaps of
people in the OR.
Next, came the epidural. Again, we had the inexperienced anaesthesiologist.
This time she said to someone else how she was so nervous because there were
"too many eyes". Not really reassuring. They gave Sarah a numbing injection
first, and then proceeded to poke her in back with the epidural catheter
needle thing.
Sarah was flinching when they did it, and they kept yelling at her not to
move. Apparently it didn't hurt, but it caused discomfort every
time they tried to get it in. Eventually the associate professor of
anaesthesiology stepped in and got it in. Later, they said that Sarah had a
slightly curved spine or something, and that had made it a bit more
complicated.
I'm not begrudging the obviously inexperienced anaesthesiologist for
anything. Stanford is a teaching hospital, and doctors have to get their
experience somehow, but I think that when the patient is conscious, it's
important to come across a little more confident.
So once they got all of that sorted out, they could get her lying down on
the table and get down to business.
Apparently one of the side-effects of the epidural is that it can cause your
body to think it's got hypothermia. Part way through the procedure, Sarah
started shaking uncontrollably. I can't remember whether she complained
about feeling cold or not. She also got a really itchy face. Apparently
that's a side-effect of morphine.
Sarah also doesn't tolerate anaesthetics very well, and started feeling
nauseous part way through the procedure. Sure enough, as has happened
countless times previously post-operatively, she threw up. At least we had
warning and I got a kidney dish in place in time.
That was about all the eventful surgical stuff. They had the usual screen up
from about her neck area, so I couldn't see what they were doing. After
Joshua was delivered, one of the nurses asked me if I wanted to see him, and
took me around from her left shoulder where I'd been sitting to around below
her right foot, to where they had him in one of those perspex portable crib
things. I could see a lot of red out of the corner of my eye, and I could
have seen a lot more if I'd bothered to look, but I was more interested in
seeing my son at that stage.
They let me bring him back around to where I'd been sitting previously, to
show Sarah, but it was hard getting him up to a point where she could see
him properly, and she was partially out of it by this stage. I think she
dozed off for a little bit not long after that.
I forget what time we got into the OR, but they started the operation in
earnest at about 10:00am, and we were out of there by 10:45am if memory
serves. The nurse in charge of the recovery room really didn't want me
there, so I went back to LPCH with one of the nurses to spend some time with
him while he was weighed and measured, and I took some photos, and held him.
This was good, because I got to have a good cry on my own with him.
So overall, in my "I'll never be squeezing a watermelon out of my nether
regions" blokey perspective, having seen what Sarah went through, and how
much discomfort that it looked like she was going through, both in the OR
and afterwards, I'm not convinced c-sections are any less painful than a
vaginal birth (taking the average case of both). I think you're trading some
"short term" (depends on how long your labour is I guess) pain for some
"long term" pain while you recover from your incision. Granted, Sarah wasn't
really laid up all that badly for all that long, but it still didn't look
all that pleasant.
I guess really only the women who have done both delivery methods can really
comment. All I know is I found it very difficult watching Sarah go through
so much pain. So much so that I'm not really looking forward to that again.
Maybe, hopefully, after her
Apparently Scott James Remnant in his article on
I'm constantly amazed how much useful information about Debian is published
daily (on web sites, blogs and mailing lists.) I'm also amazed about how
much time I spend searching web pages I've seen in the past but haven't
bookmarked. So
Like any sensible software developer, I have a close relationship with revision control systems. In my previous job, I was an SCM Engineer (see 
Hello World,
a tape streamer of one of our customers died two days ago and we haven’t anymore warranty on this device, a new infrastructure will be realized soon anyway. So, we needed a cheap temporary solution. We bought a WD network harddisk for home use and “remote access over the internet”, even if we only need local access to it. It’s a 500 GB harddisk and you find several short GPL references in the crappy 5-pages-manual. Ok, this sounded already interesting. I read the manual and see that only Windows 2000 and Windows XP are supported and that you have to install a very proprietary Windows tool to start using the harddisk. Later there would also be a webinterface available, after the initialization.
Ok, so I wanted to know if it’s possible to access the drive also with other operating systems. I connected the RJ45 port of the harddisk with my ethernet card with a cross-over cable and started to sniff with ngrep on the interface. After waiting a while I got this message some strange from a APIPA address to a Multicast address. Ok, assigned my local interface an address in the APIPA range and connected with my browser to it:
T 169.254.71.58:80 -> 169.254.71.57:56819 [A]
[Gnaaaaa, sorry 
A ‘grml-live -t /dev/shm -c GRMLBASE,I386 -s etch’ and not even 5(!) minutes later I’ve my 135MB small grml:
So using grml-live you can build your own, customized Linux Live-CD without having to deal with
So, I have just installed 
This week my son Nilagriva started preschool so on Sunday we had his
As uclibc's removal from Debian has been