#!/bin/sh
#=============================================================================#
#                simple menu for git checkout (select branch)                 #
#=============================================================================#
# (c) Denis Smirnov <mithraen@freesource.info                 	14 Oct 2006   #
#=============================================================================#

# is this git repo?
if [ ! -d .git ]; then
	echo "This is not git repo"
	exit -1
fi

# Generate menu
T_M=`mktemp`
echo '--menu "Select branch for checkout" 0 0 0' >> $T_M

# Get default
DEFAULT="`git branch | grep '^\*' | sed 's/^..//' | tr -d "\n"`"
if [ "x$DEFAULT" != "x" ]; then
    echo "\"$DEFAULT\" \"\"" >> $T_M
fi

# Get list
git branch | grep '^ ' | sed 's/^..\(.*\)/"\1" ""/' | sort >> $T_M

T1=`mktemp`
dialog --file $T_M 2> $T1
RC=$?
stty sane
clear
if [ $RC -eq 0 ]; then
    git checkout `cat $T1`
fi

rm -f $T_M $T1
