Skip to content

Instantly share code, notes, and snippets.

@Revolucent
Last active July 22, 2018 23:39
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Revolucent/a93d08a8c1692465dc0f to your computer and use it in GitHub Desktop.
Save Revolucent/a93d08a8c1692465dc0f to your computer and use it in GitHub Desktop.
The Git Gatling Gun: Perform operations on multiple side-by-side git repositories.
#!/bin/bash
# Git Gat: The Git Gatling Gun!
# Perform operations machine-gun style on side-by-side git repositories.
#
# Save this somewhere on your path as git-gat (or whatever you want to call it).
# Usage:
#
# git gat status
# git gat -f status # Perform operation even if the repo is dirty.
# git gat tag v1.0.2
# etc.
#
# Be careful when performing batch operations on multiple git repos.
# Check whether there are uncommitted changes in
# any of the repositories.
# These are commands that are safe to run even if a repo is dirty.
SAFELIST=( rev-parse status );
FORCE=false
if [[ $1 =~ ^-[fF]$ ]]; then
FORCE=true;
shift;
fi;
if [[ $FORCE = false ]]; then
for ITEM in "${SAFELIST[@]}"; do
if [[ $1 = $ITEM ]]; then
FORCE=true;
break;
fi;
done;
fi;
if [[ $FORCE = false ]]; then
for REPO in *; do
if [[ -d $REPO ]]; then
cd "$REPO";
# Is this a git repo?
git rev-parse --git-dir > /dev/null 2>&1;
if [[ $? -eq 0 ]]; then
if [ \! -z "$(git status --porcelain)" ]; then
read -p "You have uncommitted changes in the $REPO repository. Are you sure you want to proceed? [y/n] " -n 1 -r
echo;
if ! [[ $REPLY =~ ^[Yy]$ ]]; then
exit 1;
fi;
fi;
fi;
cd ..;
fi;
done;
fi;
# Perform the actual git operation in each repo.
for REPO in *; do
if [[ -d $REPO ]]; then
cd "$REPO";
git rev-parse --git-dir > /dev/null 2>&1;
if [ $? -eq 0 ]; then
if [[ $1 != only ]]; then
echo "$REPO:"; # The only command handles this itself.
fi;
git "$@";
if [[ $1 != only ]]; then
echo;
fi;
fi;
cd ..;
fi;
done;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment