# HG changeset patch # User Augie Fackler # Date 1332873700 18000 # Node ID 533bde18976e9af4c0653495d52a9878040d7ed3 # Parent ba2fd0bed641c0d5b0c780c64f9e09d05a77c96d find_dvcs_root: new function to find dvcs root dir more safely This compares the discovered root dir lengths in order to give the most specific repository path possible. diff --git a/.shell.d/50.vcs_functions.sh b/.shell.d/50.vcs_functions.sh --- a/.shell.d/50.vcs_functions.sh +++ b/.shell.d/50.vcs_functions.sh @@ -135,16 +135,30 @@ function vcs_current_branch() { return 1 } +function find_dvcs_root() { + local hgroot=`hg root 2> /dev/null` + local gitroot=$(git rev-parse --show-toplevel 2> /dev/null) + local hglen=$(expr length $hgroot) + local gitlen=$(expr length $gitroot) + if [ $hglen -ge $gitlen ] ; then + if [ -n "$hgroot" ] ; then + echo $hgroot + return 0 + fi + else + if [ -n "$gitroot" ] ; then + echo $gitroot + return 0 + fi + fi + return 1 +} + # change to the root dir of the current wc function wcroot() { - local root=`hg root 2> /dev/null` - if [ x$root != 'x' ] ; then - cd $root - return 0 - fi - root=$(git rev-parse --show-toplevel 2> /dev/null) - if [ $? = 0 ] ; then - cd $root + local dvcsroot=$(find_dvcs_root) + if [ -n $dvcsroot ] ; then + cd $dvcsroot return 0 fi if [ -e .svn ] ; then