## vcs_functions
# This file contains a handful of functions that relate to using some version
# control tool or other.

alias mq='hg -R .hg/patches'

# Function to grab the url of an svn wc
function svnurlof() {
    local url
    url=`svn info $1 2> /dev/null | grep '^URL: ' | sed 's/URL: //'`
    if [ x"$url" = "x" ] ; then
        url=`git svn info $1 2> /dev/null | grep '^URL: ' | sed 's/URL: //'`
    fi
    if [ x"$url" = "x" ] ; then
    	local dir
    	dir=$1
    	if [ x"$dir" = "x" ] ; then
    		dir="."
    	fi
        url=`hg -R $dir svn info 2> /dev/null | grep '^URL: ' | sed 's/URL: //'`
    fi
    if [ x"$url" = "x" ] ; then
        echo -n 'No repo found (tried svn, git-svn, hgsvnclient)'
    fi
    echo $url
}

# Function to clean locks out of svn wcs
function clean_svn_source_trees() {
    for aa in */ ; do
        pushd $aa > /dev/null
        if [ -e .svn ] ; then
            echo $aa 'is an svn checkout, cleaning' && svn cleanup
        fi
        popd  > /dev/null
    done
}

# Function to update source trees of known VCS tools in the working dir.
# "known" means ones I'm forced to use on a regular basis
function update_source_trees() {
    for aa in */ ; do
        pushd $aa > /dev/null
        if [ -e .svn ] ; then
            echo $aa 'is an svn checkout, updating'
            svn up
        elif [ -e .hg/svn ] ; then
            echo $aa 'is an hg-svn checkout, updating'
            hg svn pull
            hg svn gentags
            hg up
        elif [ -e .git/svn ] ; then
            echo $aa 'is a git-svn checkout, updating'
            git svn fetch
            git repack
            git gc
        elif [ -e .git ] ; then
            echo $aa 'is a git checkout, updating'
            git pull origin master
            git repack
            git gc
        elif [ -e .hg ] ; then
            echo $aa 'is an hg checkout, updating' && hg pull && hg up
        elif [ -e _MTN ] ; then
            echo $aa "is an mtn co, updating" && mtn pull && mtn up
        fi
        popd > /dev/null
    done
}

function vcs_current_branch() {
   git branch 2> /dev/null > /dev/null
    if [ $? = 0 ] ; then
        git branch --verbose | grep '^*' | sed 's/* //;s/ /:/;s/ .*//;s/:/ /'
        return 0
    fi
    # when wc-ng comes out, we'll probably do the following
    # svn info 2> /dev/null > /dev/null
    if [ -e .svn ] ; then
        local d=`svn info | grep URL | sed 's/.*://'`
        local br=`echo $d | awk '{
                        if ( index($1, "trunk") ) {
                            print  "trunk"
                        } else {
                            x = index($1, "branches") ;
                            if ( x != 0 ) {
                                sub(".*/branches/", "");
                                sub("/.*", "");
                                print $0
                            }
                        }
                    }'`
        local rev=`svn info | grep Revision | sed 's/.*: /r/'`
        echo $br $rev
        return 0
    fi
    hg branch 2> /dev/null > /dev/null
    if [ $? = 0 ] ; then
        local br=`hg branch | head -c 10`
        local rid=`hg parents | head -n 1 | awk '{print $2}' | head -c 7`
        echo "$br $rid"
        return 0
    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
    git branch 2> /dev/null > /dev/null
    if [ $? = 0 ] ; then
        while [ ! -e .git ] ; do
            cd ..
        done
        return 0
    fi
    if [ -e .svn ] ; then
        while [ -e ../.svn ] ; do
            cd ..
        done
        return 0
    fi
    echo No working copy found'!'
}
