Skip to content

Instantly share code, notes, and snippets.

@vbjay
Forked from Mark-Booth/git-branch-status
Created June 3, 2013 20:51
Show Gist options
  • Save vbjay/5701299 to your computer and use it in GitHub Desktop.
Save vbjay/5701299 to your computer and use it in GitHub Desktop.
#!/bin/bash
# hosted at https://gist.github.com/Mark-Booth/5058384
# forked from https://gist.github.com/lth2h/4177524 @ ae184f1 by mark.booth
# forked from https://gist.github.com/jehiah/1288596 @ e357c1e by lth2h
# ideas from https://github.com/kortina/bakpak/blob/master/bin/git-branches-vs-origin-master
# this prints out some branch status
# (similar to the '... ahead' info you get from git status)
# example:
# $ git branch-status -a
# dns_check (ahead 1) | (behind 112) origin/master
# master (ahead 2) | (behind 0) origin/master
# $ git branch-status
# master (ahead 2) | (behind 0) origin/master
usage="$(basename "$0") [-hav] -- summarise status of branch(es)
where:
-h show this help text
-a show all branches, not just the current one
-m shows branch(es) with respect to origin/master
-v verbose, show output even if counts are zero"
while getopts 'hamv' option; do
case "$option" in
h) echo "$usage"
exit
;;
a) filter=refs/heads
;;
m) originmaster=true
;;
v) verbose=true
;;
?) printf "illegal option: '%s'\n" "$OPTARG" >&2
echo "$usage" >&2
exit 1
;;
esac
done
shift $((OPTIND - 1))
if [ -z $filter ] ; then
filter=$(git symbolic-ref -q HEAD)
fi
git for-each-ref --format="%(refname:short) %(upstream:short)" $filter | \
while read local remote
do
if [ $originmaster ] ; then
remote=origin/master
fi
[ -z "$remote" ] && continue
DELTAS=$(git rev-list --left-right ${local}...${remote})
LEFT_AHEAD=$(echo "$DELTAS" | grep -c '^<')
RIGHT_AHEAD=$(echo "$DELTAS" | grep -c '^>')
if [ $verbose ] || [ $LEFT_AHEAD -gt 0 ] || [ $RIGHT_AHEAD -gt 0 ] ; then
echo "$local (ahead $LEFT_AHEAD) | (behind $RIGHT_AHEAD) $remote"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment