forked from nickmathis/git-cleanup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
git-cleanup.rb
executable file
·157 lines (134 loc) · 3.64 KB
/
git-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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#! /usr/bin/env ruby
def current_branch
b = `git branch`.split("\n").find { |i| i.match(/^\*/) }
b.gsub("* ","")
end
class GitCleanup
def initialize(mode = nil)
@force = false
@dry = false
case mode
when 'force'
@force = true
when 'dry'
@dry = true
end
end
def clean_all!
if !on_master?
puts "Must run against master"
return
end
fetch
if @dry
merged_branches(true)
else
prune_branches!
merged_branches
clean_local!
clean_remote!
end
end
def fetch
`git fetch`
end
def merged_branches(output = false)
puts "\n********************************************\nBranches already merged into #{current_branch}\n********************************************"
@local_branches = []
@remote_branches = []
`git branch -a --merged`.split("\n").each do |branch|
next if branch.nil?
branch = branch.gsub("*", '').strip
if !!branch.match('remotes')
branch.gsub!('remotes/', '')
git_remote, branch_name = branch.split('/')
next if never_remove_remote.include?(branch_name)
next if branch_name.match /HEAD/
@remote_branches << branch
else
next if never_remove_local.include?(branch)
@local_branches << branch
puts "- #{branch}" if output
end
end
end
def on_master?
current_branch == 'master'
end
def clean_remote!
if !on_master?
puts "Must run against master"
return
end
puts "#{@remote_branches.size} remote branches ready for cleanup"
clean_branches!(@remote_branches, true)
end
def clean_local!
if !on_master?
puts "Must run against master"
return
end
puts "#{@local_branches.size} local branches ready for cleanup"
clean_branches!(@local_branches, false)
end
def never_remove_local
@never_remove_local ||= ['master', 'staging', 'production', 'preproduction','training'] << current_branch
end
def never_remove_remote
@never_remove_remote ||= ['master', 'staging', 'production', 'preproduction','training'] << current_branch
end
def managed_remotes
['origin']
end
def prune_branches!
`git remote`.split("\n").each do |remote|
print "Pruning remote #{remote}..."
STDOUT.flush
system "git remote prune #{remote}"
puts " done!"
end
end
private
def clean_branches!(branch_list, remote)
branch_list.each do |branch|
branch_name = branch
git_remote, branch_name = branch.split('/') if remote
if git_remote && !managed_remotes.include?(git_remote)
puts "Skipping #{git_remote}/#{branch_name} (reason: remote not managed)"
next
end
if @force
puts print "Removing #{branch}"
clean_branch(remote, git_remote, branch_name)
else
invalid_response = true
while (invalid_response)
print "Remove #{branch}? [y/n] "
STDOUT.flush
response = gets.chomp.downcase
if response.match(/^[yn]$/)
invalid_response = false
if response == 'y'
clean_branch(remote, git_remote, branch_name)
else
puts "skipping #{branch_name}"
end
else
puts "Please enter y or n"
end
end
end
end
end
def clean_branch(remote, git_remote, branch_name)
command = remote ? "git push #{git_remote} :#{branch_name}" : "git branch -d #{branch_name}"
system "#{command}"
end
end
mode = nil
ARGV.each do|a|
mode = 'force' if a == "--force"
mode = 'dry' if a == "--dry"
end
gc = GitCleanup.new(mode)
gc.clean_all!