Last active
April 17, 2019 10:34
-
-
Save tonymarklove/f96b7eb4976c590a27353926bfcd6c4a to your computer and use it in GitHub Desktop.
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 "io/console" | |
| LINE_START = " * [pruned] origin/" | |
| def error(message, error_code=-1) | |
| STDERR.puts message | |
| exit(error_code) | |
| end | |
| def yesno | |
| case $stdin.getch | |
| when "Y" then true | |
| when "y" then true | |
| when "N" then false | |
| when "n" then false | |
| else true | |
| end | |
| end | |
| def pruned_branches | |
| $pruned_branches ||= begin | |
| branches = `git remote prune origin` | |
| error(branches, $?.exitstatus) unless $?.success? | |
| exit if branches.empty? | |
| puts branches | |
| branches.lines.select { |line| line.start_with?(LINE_START) }.map { |line| line.gsub(LINE_START, '').strip } | |
| end | |
| end | |
| def local_branches | |
| $local_branches ||= begin | |
| branches = `git branch` | |
| error(branches, $?.exitstatus) unless $?.success? | |
| branches.lines.map { |line| line[2..-1].strip } | |
| end | |
| end | |
| def deletable_branches | |
| local_branches & pruned_branches | |
| end | |
| def perform_deletion | |
| puts "\nDelete:" | |
| deletable_branches.each do |branch| | |
| print " #{branch}? (Y/n): " | |
| if yesno | |
| puts "deleting" | |
| `git branch -D #{branch}` | |
| else | |
| puts "skip" | |
| end | |
| end | |
| end | |
| perform_deletion if deletable_branches.any? |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment