Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 20th, 2012  |  syntax: None  |  size: 1.38 KB  |  hits: 9  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #! /bin/bash
  2.  
  3. #guarantee you are in the directory you are in (src: http://stackoverflow.com/a/246128)
  4. cd -P "$(dirname "$0")"
  5.  
  6.  
  7. # basically traverse down all directories until you run across a
  8. # .git/.svn/.hg directory, then update/pull the directory (stop traversing)
  9.  
  10. #updaterepo <folder type found> <path name to display>
  11. updaterepo()
  12. {
  13.         echo "$(tput setaf 2)Updating repository: $(tput setaf 3)$2$(tput sgr0)"
  14.        
  15.         case "$1" in
  16.         ".git/")
  17.                         # check if git repo is of git-svn type (src: http://stackoverflow.com/a/9086279)
  18.                         if [ -d .git/svn  ] && [ x != x"$(ls -A .git/svn/)" ]
  19.                         then
  20.                                 git svn rebase
  21.                         else
  22.                                 git pull
  23.                         fi
  24.                         ;;
  25.         ".svn/")        svn update;;
  26.         ".hg/")         hg update;;
  27.         esac
  28.         echo
  29. }
  30.  
  31. #traverses directories looking for .git, .svn, .hg folders (src: http://steve-parker.org/sh/eg/directories)
  32. #traverse <folder to enter> <path name to display>
  33. traverse()
  34. {
  35.         cd "$1"
  36.        
  37.         vc=0
  38.         # get all directories in current dir starting with '.'
  39.         # without . & .. (must have 2-3 chars after the '.')
  40.         for dir in $(ls -d .*/ | grep -P -e "^\..{2,3}/$")
  41.         do
  42.                 case "$dir" in
  43.                 ".git/"|".svn/"|".hg/")
  44.                         vc=1
  45.                         updaterepo "$dir" "$2"
  46.                         ;;
  47.                 esac
  48.         done
  49.        
  50.         #do not enter diretory if it is repo controlled
  51.         if [ 1 -ne "$vc" ]
  52.         then
  53.                 for dir in $(ls -d */)
  54.                 do
  55.                         traverse "$dir" "$2$dir"
  56.                 done
  57.         fi
  58.        
  59.         cd ..
  60. }
  61.  
  62. # get the current directory name (src: http://stackoverflow.com/a/1371283)
  63. traverse . "${PWD##*/}/"