Skip to content

Instantly share code, notes, and snippets.

@pietvanzoen
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pietvanzoen/1a6a018943038811e249 to your computer and use it in GitHub Desktop.
Save pietvanzoen/1a6a018943038811e249 to your computer and use it in GitHub Desktop.
List current branch in multiple repos and show status. Optionally pass `-f` or `--fetch` to run a fetch on each branch first.
#!/bin/bash
# root repo folder
REPOROOT="$HOME/Projects"
# folder search glob
REPOGLOB="cozy[-_]*"
OUTDATA=""
fetch_msg="Fetching.."
for REPO in `find ${REPOROOT} -type d -iname "${REPOGLOB}" -maxdepth 1`
do
# get repo name
basename=$(basename ${REPO})
# get branch
branchname=$(cd ${REPO} && git symbolic-ref --short HEAD)
# optionally run fetch before getting status
if [[ "$1" == "--fetch" || "$1" == "-f" ]]; then
printf "$fetch_msg"
cd ${REPO} && git fetch origin $branchname 2>/dev/null
fetch_msg=".."
fi
# color branch
# red == master
# blue == develop
# everything else == yellow
branch="$branchname"
case "$branch" in
"master")
branch="$(tput setf 4)$branch$(tput sgr0)" ;; # red
"develop")
branch="$(tput setf 1)$branch$(tput sgr0)" ;; # blue
*)
branch="$(tput setf 6)$branch$(tput sgr0)" # yellow
esac
# get git status
# - == deleted files
# + == untracked files
# * == modified files
status=""
git_status="$(cd ${REPO} && git status 2>/dev/null)"
if [[ "$git_status" == *deleted* ]]; then
status+="-"
fi
if [[ "$git_status" == *Untracked[[:space:]]files:* ]]; then
status+="+"
fi
if [[ "$git_status" == *modified:* ]]; then
status+="*"
fi
# color git status
if [[ "$status" = "" ]]; then
status=""
else
status=" $(tput setf 2)($status)$(tput sgr0)"
fi
# get repo diff with origin
rev_list=$(cd ${REPO} && git rev-list --left-right --count origin/$branchname..HEAD)
ahead=$(echo $rev_list | cut -f2 -d " " )
behind=$(echo $rev_list | cut -f1 -d " " )
diff=""
if [[ $ahead -gt 0 ]]; then
diff+="$ahead ahead"
fi
if [[ $behind -gt 0 ]]; then
if ! [[ "$diff" == "" ]]; then
diff+=", "
fi
diff+="$behind behind"
fi
if [[ "$diff" = "" ]]; then
diff="$(tput setf 0)[up-to-date]$(tput sgr0)"
else
diff="$(tput setf 3)[$diff]$(tput sgr0)"
fi
# final string output with gray space. pipe
OUTDATA+="$basename|$branch$status $diff\n"
done
if [[ "$1" == "--fetch" || "$1" == "-f" ]]; then
printf "\n"
fi
# print output in columns. pipe | is used to identify columns
printf "$OUTDATA" | column -t -s "|"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment