Skip to content

Instantly share code, notes, and snippets.

@srbs
Last active May 17, 2016 07:36
Show Gist options
  • Save srbs/2297160 to your computer and use it in GitHub Desktop.
Save srbs/2297160 to your computer and use it in GitHub Desktop.
Update all git/svn/mercurial repositories in any child directory of this script
#! /bin/bash
#guarantee you are in the directory you are in (src: http://stackoverflow.com/a/246128)
cd -P "$(dirname "$0")"
# basically traverse down all directories until you run across a
# .git/.svn/.hg/.fslckout/CVS directory/file, then update/pull the directory (stop traversing)
#updaterepo <folder type found> <path name to display>
updaterepo()
{
echo "$(tput setaf 2)Updating repository: $(tput setaf 3)$2$(tput sgr0)"
case "$1" in
".git")
# check if git repo is of git-svn type (src: http://stackoverflow.com/a/9086279)
if [ -d .git/svn ] && [ x != x"$(ls -A .git/svn/)" ]
then
git svn rebase
else
git pull
fi
;;
".svn") svn update;;
".hg") hg update;;
".fslckout") fossil pull;;
"CVS") cvs update -d;;
esac
echo
}
#traverses directories looking for .git, .svn, .hg folders or .fslckout file (src: http://steve-parker.org/sh/eg/directories)
#traverse <folder to enter> <path name to display>
traverse()
{
cd "$1"
vc=0
# get all directories in current dir starting with '.' (src: http://superuser.com/a/335397)
for dir in $(find . -mindepth 1 -maxdepth 1 -name ".*" | perl -pe "s/^..//")
do
case "$dir" in
".git"|".svn"|".hg"|".fslckout"|"CVS")
vc=1
updaterepo "$dir" "$2"
;;
esac
done
#do not enter diretory if it is repo controlled
if [ 1 -ne "$vc" ]
then
# loop over directories with spaces (src: http://stackoverflow.com/a/4895508)
for dir in */
do
[ "$dir" != "*/" ] && traverse "$dir" "$2$dir"
done
fi
cd ..
}
# get the current directory name (src: http://stackoverflow.com/a/1371283)
traverse . "${PWD##*/}/"
@srbs
Copy link
Author

srbs commented Aug 9, 2013

Rev.694f20d includes support for Mountain Lion & BSD based systems. (http://www.dirtdon.com/?p=1452)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment