Last active
August 29, 2015 14:06
-
-
Save mslinn/9bc1707537da18f8a861 to your computer and use it in GitHub Desktop.
Recursive foreach, expects to find a tree of git projects
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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