view .shell.d/50.vcs_functions.sh @ 307:e37b00236907

zshrc: work around my shell function that turns on utf8 in screen The zsh built in which was getting confused by the function, and always claimed screen was installed. I'm finally running into machines with tmux but not screen, so I noticed.
author Augie Fackler <raf@durin42.com>
date Sat, 19 Jan 2013 19:29:58 -0600
parents 901e85174513
children 51aaf8bcd6ec
line wrap: on
line source

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

function ezsvnsync() {
    if [ x"$1" = "x" ] ; then
        echo 'usage: ez-svn-sync repo url'
        return 0
    fi
    local repo
    repo=$1
    local url
    url=$2
    svnadmin create $repo
    echo '#!/bin/sh' >> $repo/hooks/pre-revprop-change
    echo 'exit 0' >> $repo/hooks/pre-revprop-change
    chmod +x $repo/hooks/pre-revprop-change
    svnsync init file://`pwd`/$repo $url
    svnsync sync file://`pwd`/$repo
}
alias ez-svn-sync=ezsvnsync

# 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, hgsubversion)'
    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() {
    if ! ls -l | egrep ^d > /dev/null ; then
        ls -l | egrep ^d
        return
    fi
    local hgpath
    for aa in */ ; do
        pushd $aa > /dev/null || continue
        if [ -e .svn ] ; then
            echo $aa 'is an svn checkout, updating'
            svn up
        elif [ -e README.txt ] \
            && [ -e format ] \
            && grep "This is a Subversion repository; use the 'svnadmin' tool to examine" README.txt > /dev/null \
            && svn pl --revprop -r 0 file://$(pwd) | fgrep svn:sync-from-url  > /dev/null\
            ; then
            echo "$aa looks like an svnsync clone, syncing"
            svnsync sync file://$(pwd)
        elif [ -e .git/svn ] ; then
            echo $aa 'is a git-svn checkout, updating'
            git svn fetch
        elif [ -e .git ] ; then
            echo $aa 'is a git checkout, updating'
            for remote in $(git remote) ; do
                git fetch $remote
            done
        elif [ -e .hg ] ; then
            echo $aa 'is an hg checkout, updating'
            for hgpath in $(HGRCPATH=/dev/null hg paths | sed 's/.* = //g' | sort | uniq ) ; do
                hg pull --update $hgpath
            done
            if [ -e .hg/patches/.hg ] ; then
               echo $aa 'has an mq, updating that too...'
               hg -R .hg/patches pull -u
            fi
        elif [ -e _MTN ] ; then
            echo $aa "is an mtn co, updating" && mtn pull && mtn up
        else
            update_source_trees
        fi
        popd > /dev/null
    done
}
alias update-source-trees=update_source_trees

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 bm=$(hg book | egrep ' \* ' | awk '{print $2}')
        local br=`hg branch | head -c 10`
        local rid=`hg parents --template '{node}\n' | head -n 1 | head -c 7`
        if [ -z "$bm" ] ; then
          echo "$br $rid"
        else
          echo "$bm $rid"
        fi
        return 0
    fi
    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 dvcsroot=$(find_dvcs_root)
    if [ -n $dvcsroot ] ; then
        cd $dvcsroot
        return 0
    fi
    if [ -e .svn ] ; then
        while [ -e ../.svn ] ; do
            cd ..
        done
        return 0
    fi
    echo No working copy found'!'
}