Skip to content

Instantly share code, notes, and snippets.

@mslinn
Last active April 17, 2019 16:54
Show Gist options
  • Select an option

  • Save mslinn/382b9f03bf2c6a445ff66702d4a28a31 to your computer and use it in GitHub Desktop.

Select an option

Save mslinn/382b9f03bf2c6a445ff66702d4a28a31 to your computer and use it in GitHub Desktop.
Runs a command on all git directories below the current directory or a specified directory.
#!/bin/bash
shopt -s nullglob
function help {
echo "${1}Runs a command on all git directories below the current directory or a specified directory.
Skips directories that contain a file called .ignore.
Usage: $(basename $0) [options] command [directory]
Where:
command is any command to run in each repository directory, such as 'commit'.
Options are:
-d - Debug mode on
-n - Do not supply the git repository directory name to the command
-m - Specify argument to pass to command, after -m
-h - Show this help text
-q - Just show processed directory names
-Q - Don't show any information messages
Examples:
$(basename $0) 'ls -1' # Show directory contents of the top directory of each git repository
$(basename $0) -q 'ls -1' # As above, with less noise
$(basename $0) -Q 'ls -1' # As above, with even less noise (just shows contents of all directories run together)
$(basename $0) -m 'Initial Commit' commit
$(basename $0) -m 'Initial Commit' commit \$work
"
exit 1
}
function forALL {
if [ ! "$QUIET" ]; then echo -e "\n${HIGHLIGHT}Scanning $( pwd )$NORMAL"; fi
for d in */; do
#echo "Directory \"$d\" found"
pushd "$d" > /dev/null
#echo "Working in $( pwd )"
if [ -e ".ignore" ]; then
if [ ! "$QUIET" ]; then echo -e "\n${HIGHLIGHT}Ignoring `pwd`${NORMAL}"; fi
else
if [ -d .git ]; then
if [ ! "$QUIET" == really ]; then echo -e "\n${HIGHLIGHT}Processing `pwd`$NORMAL"; fi
#echo "COMMAND=$COMMAND"
if [ "$PROVIDE_DIRECTORY" ]; then
eval "$COMMAND" `pwd`
else
eval "$COMMAND"
fi
else
forALL .
fi
fi
popd > /dev/null
done
}
export HIGHLIGHT="\e[01;34m"
export NORMAL='\e[00m'
export REPO=origin
export MSG="-"
export PROVIDE_DIRECTORY=true
unset QUIET
while getopts "dhm:n\?qQ" opt; do
case $opt in
d) set -xv ;;
m) export MSG="$OPTARG" ;;
n) unset PROVIDE_DIRECTORY ;;
q) export QUIET=true ;;
Q) export QUIET=really ;;
*) help ;;
esac
done
shift $(($OPTIND-1))
if [ -z "$1" ];then help "Error: No command given to execute.\n\n"; fi
export COMMAND="$1"
shift
if [ "$1" ]; then
cd "$1" > /dev/null
shift
fi
forALL .
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment