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) |
while read sha ref
do git log -n1 --format="%ae %h $ref %an" $sha
done
#!/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