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.
#!/usr/bin/env ruby
begin
require 'optparse'
require 'erb'
require 'ostruct'
OptionParser.new do |opts|
opts.banner = %{
Usage: #{$0} [options] [USERS]
Reads a list of branches from stdin and formats them for deletion based
on the email of the last person to commit to that branch. Filters
for the specified users, all users by default.
Options:
}.lstrip
opts.on("-h", "--help", "print this help") do
puts opts
puts
exit
end
end.parse!
patterns = ARGV.dup
ARGV.clear
# read branches from stdin and sort by email
branches_by_email = Hash.new {|hash, key| hash[key] = [] }
while branch = gets
branch.strip!
logline = `git log --format="%ae,%h,%cr" -n1 '#{branch}'`
email, sha, date = logline.strip.split(',')
msg = "#{sha} (#{date})"
if branch.include?('/')
remote, branch = branch.split('/')
else
remote = ""
end
branches_by_email[email] << [remote, branch, msg]
end
# filter branches by email
unless patterns.empty?
branches_by_email.delete_if do |email, branches|
! patterns.any? {|fragment| email.include?(fragment) }
end
end
# print assignments
template = ERB.new <<-EOF, nil, "<>"
# <%= email %>
<% branches.sort.each do |(remote, branch, msg)| %>
<% if remote.empty? %>
git branch -d <%= branch.ljust(max) %> # <%= msg %>
<% else %>
git push <%= remote %> :<%= branch.ljust(max) %> # <%= msg %>
<% end %>
<% end %>
EOF
branches_by_email.keys.sort.each do |email|
branches = branches_by_email[email]
binding = OpenStruct.new(
:email => email,
:branches => branches,
:max => branches.map {|r,b,m| b.length }.max
).send(:binding)
puts template.result(binding)
end
rescue(Errno::EPIPE)
raise if $DEBUG
if $!.class == Errno::EPIPE
exit 0
else
puts "#{$!.message} (see '#{$0} --help')"
exit 1
end
end
#!/bin/bash
############################################################################
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}$"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment