Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@gilesbowkett
Created April 16, 2019 20:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gilesbowkett/e40e76c9699c2fc3db153aeaa9d3ef32 to your computer and use it in GitHub Desktop.
Save gilesbowkett/e40e76c9699c2fc3db153aeaa9d3ef32 to your computer and use it in GitHub Desktop.
easily clean git branches
ruby bin/clean-branches.rb $* >/dev/null 2>/dev/null && git for-each-ref --sort='-authordate:iso8601' --format=' %(authordate:relative)%09%(refname:short)' refs/heads
# this script will delete all fully-merged branches, except for
# branches marked "refactor." it also skips over deployment branches
# like master, development, etc.
require 'open3'
def get_branches
git_branch = "git branch"
branches = []
Open3.popen3(git_branch) do |stdin, stdout, stderr, wait_thr|
branches = stdout.read.split("\n")
end
branches
end
def whitespace(branches)
branches.map {|branch| branch.strip}
end
def get_rid_of_current_branch_indicator(branches)
branches.map do |branch|
if branch.match(/^\* (.+)/)
branch.match(/^\* (.+)/)[1]
else
branch
end
end
end
def filter_out_deployment_branches(branches)
deployment_branches = %w{
master
development
rc
}
branches - deployment_branches
end
def filter_out_refactoring_branches(branches)
branches.select do |branch|
! branch.match(/refactor/)
end
end
branches = get_branches
branches = whitespace(branches)
branches = get_rid_of_current_branch_indicator(branches)
branches = filter_out_deployment_branches(branches)
branches = filter_out_refactoring_branches(branches)
branches.each do |branch|
puts "attempting to delete branch: #{branch}"
git_delete_branch = "git br -d #{branch}"
Open3.popen3(git_delete_branch) do |stdin, stdout, stderr, wait_thr|
stdout.each_line {|line| puts line}
stderr.each_line {|line| puts line}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment