Class: GitCleanup
- Inherits:
-
Object
- Object
- GitCleanup
- Defined in:
- lib/git_cleanup.rb
Instance Method Summary collapse
- #branch_modified(branch_info, format = :time_ago_in_words) ⇒ Object
- #cleanup ⇒ Object
- #delete_local(branch_info) ⇒ Object
- #delete_remote(branch_info) ⇒ Object
- #execute(*instructions) ⇒ Object
- #get_lighthouse_status(branch_info, project) ⇒ Object
- #get_project(name) ⇒ Object
-
#initialize ⇒ GitCleanup
constructor
A new instance of GitCleanup.
-
#match_before(branch_info, date) ⇒ Object
git_cleanup –before 1.month.ago.
-
#match_matching(branch_info, string) ⇒ Object
git_cleanup –matching hotfix.
-
#match_state(branch_info, state) ⇒ Object
git_cleanup –state resolved.
- #prune_these(local_or_remote, branches) ⇒ Object
- #test_mode? ⇒ Boolean
Constructor Details
#initialize ⇒ GitCleanup
Returns a new instance of GitCleanup.
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 |
# File 'lib/git_cleanup.rb', line 9 def initialize @config = Buildmeister::Base.load_config Lighthouse.token = @config['token'] Lighthouse.account = @config['account'] = {:rules => {}} OptionParser.new do |opts| opts. = "Usage: git_cleanup --remote" opts.on('-r', '--remote', 'Clean up remote branches') do |f| [:mode] = 'remote' end opts.on('-l', '--local', 'Clean up local branches') do [:mode] = 'local' end opts.on('-t', '--test', 'Test mode - no changes will be made') do [:test_mode] = true end opts.on('-b', '--before date', 'Automatically delete branches last updated before') do |date| [:rules].merge!(:before => eval(date)) end opts.on('-s', '--state ticket_state', 'Automatically delete branches corresponding to a Lighthouse ticket state') do |state| [:rules].merge!(:state => state) end opts.on('-m', '--matching string', 'Automatically delete branches with names matching the string') do |string| [:rules].merge!(:matching => string) end end.parse! end |
Instance Method Details
#branch_modified(branch_info, format = :time_ago_in_words) ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/git_cleanup.rb', line 85 def branch_modified(branch_info, format = :time_ago_in_words) format_string = case format when :time_ago_in_words "%ar" when :absolute "%aD" end `git show --pretty=format:#{format_string} #{branch_info.string}`.split("\n")[0] end |
#cleanup ⇒ Object
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/git_cleanup.rb', line 156 def cleanup branches = `git branch -a`.split.reject { |name| name == "*" } local_branches = branches.select do |branch| !(branch =~ /^remotes\/origin/) end remote_branches = branches.select do |branch| !local_branches.include?(branch) end local_or_remote = [:mode] if local_or_remote == 'local' prune_these(local_or_remote, local_branches) else prune_these(local_or_remote, remote_branches) end end |
#delete_local(branch_info) ⇒ Object
115 116 117 |
# File 'lib/git_cleanup.rb', line 115 def delete_local(branch_info) execute "git branch -D #{branch_info.string}" end |
#delete_remote(branch_info) ⇒ Object
119 120 121 122 |
# File 'lib/git_cleanup.rb', line 119 def delete_remote(branch_info) branch_info.string.gsub!(/(remotes\/)|(origin\/)/, '') execute "git push origin :#{branch_info.string}", "git remote prune origin" end |
#execute(*instructions) ⇒ Object
124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/git_cleanup.rb', line 124 def execute(*instructions) if test_mode? puts "Test mode - The following instructions would be executed" instructions.each do |instruction| puts instruction end else instructions.each do |instruction| system instruction end end end |
#get_lighthouse_status(branch_info, project) ⇒ Object
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/git_cleanup.rb', line 138 def get_lighthouse_status(branch_info, project) lighthouse_id = (matches = branch_info.string.match(/(^|\/)(\d+)-/)) ? matches[2] : nil if lighthouse_id tickets = project.tickets :q => lighthouse_id ticket = tickets.first if ticket branch_info.lighthouse_state = ticket.state branch_info. = "Lighthouse Info:\nTicket ##{lighthouse_id} state - #{ticket.state}" return end end branch_info. = "No Lighthouse Info." end |
#get_project(name) ⇒ Object
45 46 47 48 |
# File 'lib/git_cleanup.rb', line 45 def get_project(name) projects = Lighthouse::Project.find(:all) project = projects.find {|pr| pr.name == name} end |
#match_before(branch_info, date) ⇒ Object
git_cleanup –before 1.month.ago
98 99 100 101 102 103 |
# File 'lib/git_cleanup.rb', line 98 def match_before(branch_info, date) last_updated = Time.parse(branch_modified(branch_info, :absolute)) last_updated < date rescue false end |
#match_matching(branch_info, string) ⇒ Object
git_cleanup –matching hotfix
111 112 113 |
# File 'lib/git_cleanup.rb', line 111 def match_matching(branch_info, string) branch_info.string =~ /#{string}/ end |
#match_state(branch_info, state) ⇒ Object
git_cleanup –state resolved
106 107 108 |
# File 'lib/git_cleanup.rb', line 106 def match_state(branch_info, state) branch_info.lighthouse_state == state end |
#prune_these(local_or_remote, branches) ⇒ Object
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 |
# File 'lib/git_cleanup.rb', line 50 def prune_these(local_or_remote, branches) project = get_project(@config['project_name']) branches.each do |branch| branch_info = OpenStruct.new(:string => branch, :local_or_remote => local_or_remote) puts "#{branch_info.string} (updated #{branch_modified(branch_info, :time_ago_in_words)})" if project get_lighthouse_status(branch_info, project) puts branch_info. end if [:rules].empty? print "keep [return], delete [d]: " user_input = gets user_input.strip! case user_input when 'd' send "delete_#{local_or_remote}", branch_info end puts "\n" else rules_matched = [:rules].map do |rule_name, rule_body| send "match_#{rule_name}", branch_info, rule_body end send "delete_#{local_or_remote}", branch_info if rules_matched.all? puts "\n" end end end |
#test_mode? ⇒ Boolean
176 177 178 |
# File 'lib/git_cleanup.rb', line 176 def test_mode? [:test_mode] end |