Skip to content

Instantly share code, notes, and snippets.

@bigs
Created July 21, 2020 16:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bigs/a1391ab8d3430a40d1225fa40f894083 to your computer and use it in GitHub Desktop.
Save bigs/a1391ab8d3430a40d1225fa40f894083 to your computer and use it in GitHub Desktop.
git hatchet -- excise old branches
#!/bin/bash ruby
require "open3"
sigint_caught = false
Signal.trap("INT") do
sigint_caught = true
throw :sigint
end
def proxy_failure!(o, e, s)
if s.exitstatus != 0
STDOUT.write(o)
STDERR.write(e)
exit s.exitstatus
end
end
o, e, s = Open3.capture3("git status")
proxy_failure!(o, e, s)
o, e, s = Open3.capture3("git branch --list")
proxy_failure!(o, e, s)
branches = o.split("\n").map(&:strip).map { |b| b.gsub(/^\*\s+/, '') }
catch :sigint do
branches.each do |branch|
print "Delete branch \"#{branch}\"? "
if gets.strip.downcase == "y"
puts "--> Deleting \"#{branch}\""
o, e, s = Open3.capture3("git branch -D #{branch}")
proxy_failure!(o, e, s)
else
puts "--x Leaving \"#{branch}\""
end
end
end
if sigint_caught
puts
puts "--x Caught SIGINT, exiting..."
exit
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment