-
Notifications
You must be signed in to change notification settings - Fork 0
/
git-branch-cleanup.rb
executable file
·72 lines (60 loc) · 1.5 KB
/
git-branch-cleanup.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/env ruby
#
# This script opens an editor with a list of branches, those not deleted from
# the buffer will be removed from the repository.
def find_usable_editor
%w(nano vi emacs).each do |try|
`which #{try}`
return try if $?.success?
end
warn 'EDITOR not set and could not find an avialable editor.'
exit 1
end
def confirm(msg)
loop do
print "#{msg} (y/N)? "
case gets.chomp
when 'y', 'Y'
return true
when 'n', 'N'
return false
end
puts
end
end
EDITOR = ENV['EDITOR'] || find_usable_editor
TMPFILE = ".git-branch-cleanup.#{$PID}"
# check for git
`which git`
unless $?.success?
warn 'Could not find git in PATH'
exit 1
end
File.open(TMPFILE, 'w') do |f|
f.puts '# Uncomment the lines of branches you wish to delete.'
f.puts '# To abort, exit your editor and press N when prompted.'
branches = `git branch -vv`
branches.each_line do |l|
f.puts "# #{l}"
end
end
at_exit { File.delete(TMPFILE) }
system("#{EDITOR} #{TMPFILE}")
target_branches = []
File.read(TMPFILE).each_line do |line|
next if line.match(/\A\s*#/)
if line.match(/\A[\s*]+(\S+)\s+/)
target_branches << Regexp.last_match(1)
end
end
if target_branches.empty?
warn 'No branches were selected for deletion -- exiting!'
exit
end
puts 'This script will delete the following branches:'
target_branches.each { |t| puts " - #{t}" }
if confirm('Continue')
target_branches.each { |t| system("git branch -D #{t}") }
else
puts 'Cancelled, no branches were deleted!'
end