Git and SVN Status in the Bash Prompt
Developer — 30 Mar 2009 06:14 — 486 days ago

Here’s my take on displaying Git and SVN status information in the Bash prompt:

# Prompt setup, with SCM status
parse_git_branch() {
	local DIRTY STATUS
	STATUS=$(git status 2>/dev/null)
	[ $? -eq 128 ] && return
	[[ "$STATUS" == *'working directory clean'* ]] || DIRTY=' *'
	echo "($(git branch 2>/dev/null | sed -e '/^[^*]/d' -e 's/* //')$DIRTY)"
}

parse_svn_revision() {
	local DIRTY REV=$(svn info 2>/dev/null | grep Revision | sed -e 's/Revision: //')
	[ "$REV" ] || return
	[ "$(svn st)" ] && DIRTY=' *'
	echo "(r$REV$DIRTY)"
}

PS1='\u@\h:\W$(parse_git_branch)$(parse_svn_revision) \$ '

Put this into your ~/.bashrc script and you should see the Git/SVN status in your prompt if your working directory is a sandbox.

For SVN, it displays the current revision:

liyanage@primavera:foobar(r9851 *) $

For Git, it displays the current branch:

liyanage@primavera:build-entropy-php(php-53 *) $

For both, a * means that there are local changes to the working directory

An alternative version of parse_svn_revision(), based on David’s comment below:

parse_svn_revision() {
	local REV=$(svnversion 2>/dev/null)
	[ $? -eq 0 ] || return
	[ "$REV" == 'exported' ] && return
	echo "($REV)"
}

With that, the prompt looks like this:

liyanage@primavera:foobar(10027:10028M) $

This version is faster on large SVN trees. The original one above sometimes introduces a noticeable delay before the prompt appears.


Comments
Posted by David on 31 Mar 2009 09:35

Für SVN gehts einfacher:

NAME
svnversion - Produce a compact version number for a working copy.

SYNOPSIS
svnversion [wc_path [trail_url]]

Posted by Marc on 31 Mar 2009 09:51

svnversion kannte ich nicht, danke für den Tip!

Powered By blojsom