Skip to content

Instantly share code, notes, and snippets.

@mbildner
Last active August 29, 2015 14:23
Show Gist options
  • Save mbildner/9ebebbb35b55f714f6f7 to your computer and use it in GitHub Desktop.
Save mbildner/9ebebbb35b55f714f6f7 to your computer and use it in GitHub Desktop.
kill remote branches over N days old
require 'date'
MAX_DAYS_AGO = 8
class Branch
attr_accessor :name
def initialize(name)
@name = name
end
def master?
@name.include? 'master'
end
def tracked?
@name.include? '->'
end
def destroy(dry_run=false)
command = destroy_command
dry_run ? "DRY RUN: #{command}" : `#{command}`
end
def older_than_days number_of_days
date = Date.parse(log.split(' ')[1])
Date.today - date > MAX_DAYS_AGO
end
private
def log
command = "git log #{@name} -1"
`#{command}`
end
def destroy_command
"git push origin :#{@name.sub('origin/','')}"
end
end
`git branch -r`.lines.map(&:strip).map{|n| Branch.new(n)}.select do |branch|
branch.name.start_with? 'origin/'
end.reject do |branch|
branch.master?
end.reject do |branch|
branch.tracked?
end.select do |branch|
branch.older_than_days MAX_DAYS_AGO
end.each { |branch|
puts branch.destroy
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment