-
-
Save jsanders/2968558 to your computer and use it in GitHub Desktop.
Find and remove old git branches
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env ruby | |
| require 'erb' | |
| require 'ostruct' | |
| 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 = 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 | |
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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}$" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment