Skip to content

Instantly share code, notes, and snippets.

@deeGraYve
Forked from jsanders/README.md
Created October 30, 2013 17:00
Show Gist options
  • Select an option

  • Save deeGraYve/7236185 to your computer and use it in GitHub Desktop.

Select an option

Save deeGraYve/7236185 to your computer and use it in GitHub Desktop.
#!/bin/bash
############################################################################
# Lists merged branches locally or on a remote
############################################################################
progname="${0##*/}"
version="1.0"
author="Simon Chiang"
usestr="usage: %s [-h] [-r remote] [ref]\n"
optstr=" %s %s\n"
remote=""
while getopts "hr:" opt
do
case $opt in
d ) delete="true" ;;
h ) printf "$usestr" "$progname"
printf "$optstr" "-h" "prints this help"
printf "$optstr" "-r" "operate on named remote"
printf "\n"
exit 0 ;;
r ) remote="$OPTARG" ;;
\? ) printf "$usestr" "$progname"
exit 2 ;;
esac
done
shift $(($OPTIND - 1))
ref="${2:-master}"
############################################################################
git="git"
git_branch="$git branch"
branch_filter="."
if ! [ x"$remote" = x ]
then
git_branch="$git_branch -r"
branch_filter="$remote/"
fi
############################################################################
set -e
# list reverted commits
reverted_commits () {
$git log "$ref" |
grep 'This reverts commit' |
awk '{ print substr($4, 1, 7) }' |
tr -d '.,'
}
# list branched merged to ref
branches () {
$git_branch -v | tr -d '*'
}
merged_branches () {
$git_branch --merged "$ref" | tr -d '*'
}
# list sha+branches that have been merged,
# minus any reverts and the ref commit itself
join <(merged_branches) <(branches) |
awk '{ print $2 " " $1 }' |
grep "$branch_filter" |
grep -v -e "^$($git log --format='%h' -n 1 "$ref")" |
join -v1 - <(reverted_commits)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment