Subversion and the bash prompt

I recently found this entry that shows how you can update your PS1 value to display certain information about your git workspace.  I don't get to use git too much right now, but I use subversion a lot and wondered what there was for that.  I didn't find anything in the bash-completion entries for svn (though I admittedly didn't look too hard) so I whipped up my own solution late last night. One slight disclaimer before seeing the script:  it was late when i wrote this.  There doesn't appear to be any noticeable performance hits other than the initial run of this script but I make no guarantees.  I'm sure it could be optimized but it's snappy enough and might prove to be mildly useful.  It was interesting enough at midnight at least.  :)  Anyway, the code!

#! /bin/sh
extract() {
	TEXT=$( svn info | grep "$1" )
	echo ${TEXT##$1}

if [ -e .svn ]
	URL=`extract "URL: "`
	REPROOT=`extract "Repository Root: "`

	echo "\n\033[01;33m[svn: ${URL##$REPROOT}] \033[01;34m"

This can be displayed in your command prompt by setting your PS1 variable like this:

export PS1='\[\033[01;32m\]\u@\h\[\033[01;34m\] \w $(svn_ps1)\$\[\033[00m\] '

The single quotes are important there to prevent immediate execution of the script. If you use double quotes, it will be evaluated immediately and your prompt won't update as you navigate around. You only need to save the first script as svn_ps1 somewhere on your PATH or name it as you wish and update the PS1 variable accordingly. You can, of course, specify the full path in the PS1 var if you'd like. This setting will put the path within the current subversion repository in yellow text on a new line. If you're not in a subversion workspace, your prompt is unaffected. I had some code in there to strip off the relevant portions of the cwd from that display so you essentially only saw what branch or tag you were or if you were in trunk. With the script as it is, there's some redundancy between the subversion info display and the cwd shown, but I can live with that.

I switch between branches and even version control systems often enough that i'll probably expand on this to work for git/svn/hg/cvs accordingly. Next time I'm up late hacking.

Migrating from Subversion to Mercurial

I recently moved the bot we use in freenode ##java from svn to hg.  Using hg's built in conversion utilities, this process isn't bad at all.  There are a number of steps to set things up, however, some of which aren't entirely as clear as they probably could be.  It would appear that hg's conversion routines don't like https-based svn repos so I wrote up a quick script to help my brother move a project of his to kenai and thought I'd share it here.  The heavy lifting in this script comes almost verbatim from hg convert page but hopefully this is a bit more accessible.

if [ -z "$1" ] then     echo "usage: $0 "     exit fi

if [ -z "`grep hgext.convert ~/.hgrc`" ] then     echo Enabling the conversion extension     echo "[extensions]" >> ~/.hgrc     echo "hgext.convert=" >> ~/.hgrc fi

URL=$1 DEST=mirror-svn HGDEST=mirror-hg

if [ -d "${DEST}" -o -d "${HGDEST}" ] then     echo "${DEST} or ${HGDEST} already exist. Please try working in another directory"     exit fi

svnadmin create ${DEST} echo '#!/bin/sh' > ${DEST}/hooks/pre-revprop-change chmod +x ${DEST}/hooks/pre-revprop-change svnsync init file://`pwd`/${DEST} ${URL} svnsync sync file://`pwd`/${DEST}

mkdir ${HGDEST} hg init ${HGDEST} hg convert file://`pwd`/${DEST} ${HGDEST}

As you can see, it's pretty straightforward.  I ended up using svnsync to get aroung the hg/https/svn problem.  It also makes the conversion much faster.  Once the script is done you can cd into mirror-hg and hg push it wherever you'd like.  There are some options you can do during the conversion like limiting which revisions get converted and mapping usernames and the like.  I've done nothing of the sort here but those shouldn't be too hard to add.  And if you're really that interested in those options then you should be fully capapable of doing that yourself.  :)

Also note, that if you don't need to use svnsync you can skip directly to the hg convert line (well, and the init right before it...) and hg will pull directly from the repository to do its conversion.

There it is.  It's not fancy or earth shattering but hopefully it'll help save you some heartburn.  As always, feedback is welcome.