Skip to content

Instantly share code, notes, and snippets.

@sathishvj
Created August 15, 2018 14:15
Show Gist options
  • Save sathishvj/80017f613c67a8db4f07e6a321af8d4e to your computer and use it in GitHub Desktop.
Save sathishvj/80017f613c67a8db4f07e6a321af8d4e to your computer and use it in GitHub Desktop.
check the git status of many repos together
#!/bin/bash
# a script to check the git status of many repos together
# this one is customized to work with a given list of git repos rather than all repos under a specified folder
# based on: https://gist.github.com/aroberts/3018100
# You can add something like this to your .profile/.bashrc to make it convenient to call this from anywhere
# alias allgit='path-to-project/dev-setup/project-git-repos-status.sh'
red="\033[00;31m"
green="\033[00;32m"
yellow="\033[00;33m"
blue="\033[00;34m"
purple="\033[00;35m"
cyan="\033[00;36m"
reset="\033[00m"
# where the script is
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null && pwd )"
# you can set your project dir as a relative path from where this script is
PROJECT_DIR="$SCRIPT_DIR/.."
# list out your project git dirs here
gitdirs=( "/repo/path/1" "/repo/path/2" "/repo/path/3" )
for gitdir in "${gitdirs[@]}"
do
cd "$PROJECT_DIR/$gitdir"
RES=$(git status)
grep 'nothing to commit, working tree clean' <<<${RES} >/dev/null 2>&1
# if it's this string then show nothing
HAS_CLEAN_STRING=$?
grep -E '(Your branch is ahead|Your branch is behind)' <<<${RES} >/dev/null 2>&1
HAS_AHEAD_BEHIND_STRING=$?
if [[ $HAS_CLEAN_STRING -eq 0 && $HAS_AHEAD_BEHIND_STRING -eq 1 ]]; then
#echo "Returning from $gitdir"
continue
fi
# else show a specific string
STAT=""
grep -e 'Untracked' <<<${RES} >/dev/null 2>&1
if [ $? -eq 0 ] ; then
STAT=" $red[Untracked]$reset"
fi
grep -e 'Changes not staged for commit' <<<${RES} >/dev/null 2>&1
if [ $? -eq 0 ] ; then
STAT="$STAT $red[Modified]$reset"
fi
grep -e 'Changes to be committed' <<<${RES} >/dev/null 2>&1
if [ $? -eq 0 ] ; then
STAT="$STAT $green[Staged]$reset"
fi
grep -e 'Your branch is ahead' <<<${RES} >/dev/null 2>&1
if [ $? -eq 0 ] ; then
STAT="$STAT $yellow[Unpushed]$reset"
fi
grep -e 'Your branch is behind' <<<${RES} >/dev/null 2>&1
if [ $? -eq 0 ] ; then
STAT="$STAT $cyan[Unmerged]$reset"
fi
if [ -n "$STAT" ] ; then
#echo "====================================="
echo -e "-> $cyan$gitdir$reset :$STAT"
fi
if [ $HAS_AHEAD_BEHIND_STRING -eq 0 ]; then
# --porcelain has no output in this case
git status $* | grep -E '(ahead|behind)'
else
# much cleaner output that uses less lines
git status --porcelain $*
fi
echo
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment