Skip to content

Instantly share code, notes, and snippets.

@jsanders
Forked from thinkerbot/README.md
Created June 21, 2012 21:10
Show Gist options
  • Select an option

  • Save jsanders/2968558 to your computer and use it in GitHub Desktop.

Select an option

Save jsanders/2968558 to your computer and use it in GitHub Desktop.
Find and remove old git branches
#!/bin/bash
############################################################################
# Lists merged branches locally or on a remote
############################################################################
progname="${0##*/}"
version="1.0"
author="Simon Chiang"
usestr="usage: %s [-h] [reference_branch]
Prints branches already merged to the reference branch (by default the
current branch). If a remote branch is given, then branches for that
remote are considered.
"
optstr=" %s %s\n"
while getopts "h" opt
do
case $opt in
d ) delete="true" ;;
h ) printf "$usestr" "$progname"
printf "%s" "$hel"
printf "options:\n"
printf "$optstr" "-h" "prints this help"
printf "\n"
exit 0 ;;
r ) remote="$OPTARG/" ;;
\? ) printf "$usestr" "$progname"
exit 2 ;;
esac
done
shift $(($OPTIND - 1))
############################################################################
current_branch () {
git branch | grep '*' | awk '{print $2}'
}
ref="${1:-$(current_branch)}"
remote=$(printf "%s" "$ref" | sed -ne 's/\/.*//p')
branch_opts=""
if ! [ x"$remote" = x ]
then branch_opts="-r"
fi
############################################################################
set -e
git branch $branch_opts --merged $ref |
tr -d '* ' |
grep "${remote:-.}" |
grep -v "^${ref}$"
#!/usr/bin/env ruby
require 'erb'
require 'ostruct'
template = ERB.new <<-EOF
Hello Mr. or Mrs. "<%= names %>"
These commands will delete branches that have already been merged into master. You were the last person to commit to the branch, so you probably know best whether or not the branch is still needed.
<% branches.sort.each do |(remote, branch, sha)| %>
git push git@github.com:comverge/IntelliSOURCE.git :<%= branch %>\t# <%= sha.strip %><% end %>
Thanks!
EOF
emails={}
while line=gets
email, sha, ref, name = line.split(' ', 4)
remote, branch = ref.split('/')
emails[email] ||= {
:names => name.strip,
:branches => []
}
emails[email][:branches] << [remote, branch, sha]
end
emails.each do |email, data|
data = OpenStruct.new(data)
puts "%%"
puts email
puts "Old Branches on GitHub"
puts template.result(data.send(:binding))
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment