Skip to content

Instantly share code, notes, and snippets.

@mslinn
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mslinn/9bc1707537da18f8a861 to your computer and use it in GitHub Desktop.
Save mslinn/9bc1707537da18f8a861 to your computer and use it in GitHub Desktop.
Recursive foreach, expects to find a tree of git projects
#!/bin/bash
# From ScalaCourses.com Introduction to Play Framework with Scala course
# Execute a command in all git directories below current directory or specified directory
# Skips directories that contain a file called .ignore
#
# Usage:
# foreach . git pull
# foreach . git grep "word or phrase"
#
# Using printf insteach of echo -e for Mac OS
# See http://stackoverflow.com/questions/4435853/echo-outputs-e-parameter-in-bash-scripts-how-can-i-prevent-this
HIGHLIGHT="\e[01;34m"
NORMAL='\e[00m'
function help {
if [ "$1" ]; then echo "$1"; fi
echo "Usage: $(basename $0) dirName command..."
exit -1
}
if [ -z "$1" ]; then help "No directory specified"; fi
DIR="$1"
shift
CMD="$@"
if [ -z "$CMD" ]; then help "No command(s) specified"; fi
function doit {
local d="$1"
if [ -d "$d" ]; then
#echo "Looking for $d/.ignore"
if [ -e "$d/.ignore" ]; then
printf "%b\n" "\n${HIGHLIGHT}Ignoring $d${NORMAL}"
else
cd "$d" > /dev/null
if [ -d ".git" ]; then
printf "%b\n" "\n${HIGHLIGHT}`pwd`$NORMAL"
$CMD
elif [ ! -d .svn ] && [ ! -d CVS ]; then
scan *
fi
cd .. > /dev/null
fi
fi
#echo "Exiting doit: pwd=`pwd`"
}
function scan {
#echo "`pwd`"
#echo "About to scan $*"
for x in $*; do
doit "$x"
done
}
cd "$1" > /dev/null
printf "%b\n" "${HIGHLIGHT}Scanning ${PWD}${NORMAL}"
scan *
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment