Skip to content

Instantly share code, notes, and snippets.

@dominics
Created March 31, 2015 04:59
Show Gist options
  • Save dominics/fa06920ab30acc06d1cd to your computer and use it in GitHub Desktop.
Save dominics/fa06920ab30acc06d1cd to your computer and use it in GitHub Desktop.
git splay
#!/bin/bash
command='/usr/bin/env git'
wd=$PWD
parallel=1
quiet=1
function usage() {
echo "git splay [-p] <directory> <git command>"
echo ""
echo "Runs the given git command under each git repository found by recursively searching the specified directory."
echo ""
echo "Options:"
echo " --parallel (-p): run the command in parallel"
echo " --quiet (-q): don't print the subdirectory headings between commands"
}
function process() {
quiet=$1
i=$2
command=$3
subcommand=$4
wd=$5
if [[ $quiet -eq 1 ]]; then
echo "$i:"
fi
cd "$i"
$command $subcommand
cd "$wd"
if [[ $quiet -eq 1 ]]; then
echo ""
fi
}
while true; do
case $1 in
-p|--parallel)
parallel=0
shift
;;
-q|--quiet)
quiet=0
shift
;;
-h|--help)
usage
exit 0
;;
*)
if [[ ! -z "$dir" ]]; then
break;
fi
dir=$1
shift
;;
esac
done
subcommand="$@"
while IFS= read -r -u3 -d $'\0' i; do
i=`dirname "$i"`
if [[ $parallel -eq 0 ]]; then
(
process "$quiet" "$i" "$command" "$subcommand" "$wd"
) &
else
process "$quiet" "$i" "$command" "$subcommand" "$wd"
fi
done 3< <(find "$dir" -type d -name '.git' -print0)
if [[ $parallel -eq 0 ]]; then
wait
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment