diff .shell.d/50.vcs_functions.sh @ 283:533bde18976e

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.
author Augie Fackler <durin42@gmail.com>
date Tue, 27 Mar 2012 13:41:40 -0500
parents 3350b219b653
children 901e85174513
line wrap: on
line diff
--- 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