git_cleaner 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.document +5 -0
- data/.gitignore +21 -0
- data/LICENSE +20 -0
- data/README.rdoc +17 -0
- data/Rakefile +46 -0
- data/VERSION +1 -0
- data/bin/git_cleaner +12 -0
- data/lib/git_cleaner.rb +192 -0
- data/spec/git_cleaner_spec.rb +7 -0
- data/spec/spec.opts +1 -0
- data/spec/spec_helper.rb +9 -0
- metadata +101 -0
data/.document
ADDED
data/.gitignore
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Leigh Caplan
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
= git_cleaner
|
2
|
+
|
3
|
+
Description goes here.
|
4
|
+
|
5
|
+
== Note on Patches/Pull Requests
|
6
|
+
|
7
|
+
* Fork the project.
|
8
|
+
* Make your feature addition or bug fix.
|
9
|
+
* Add tests for it. This is important so I don't break it in a
|
10
|
+
future version unintentionally.
|
11
|
+
* Commit, do not mess with rakefile, version, or history.
|
12
|
+
(if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
|
13
|
+
* Send me a pull request. Bonus points for topic branches.
|
14
|
+
|
15
|
+
== Copyright
|
16
|
+
|
17
|
+
Copyright (c) 2010 Leigh Caplan. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "git_cleaner"
|
8
|
+
gem.summary = %Q{Automatic cleanup of Git branches– with Lighthouse integration!}
|
9
|
+
gem.description = %Q{Git Cleaner allows you to easily remove stale branches based on a number of factors, such as date modified, branch name, and even Lighthouse ticket state.}
|
10
|
+
gem.email = "lcaplan@onehub.com"
|
11
|
+
gem.homepage = "http://github.com/onehub/git_cleaner"
|
12
|
+
gem.authors = ["Leigh Caplan"]
|
13
|
+
gem.add_development_dependency "rspec", ">= 1.2.9"
|
14
|
+
gem.add_dependency 'lighthouse-api', '>= 1.1.0'
|
15
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
16
|
+
end
|
17
|
+
Jeweler::GemcutterTasks.new
|
18
|
+
rescue LoadError
|
19
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
20
|
+
end
|
21
|
+
|
22
|
+
require 'spec/rake/spectask'
|
23
|
+
Spec::Rake::SpecTask.new(:spec) do |spec|
|
24
|
+
spec.libs << 'lib' << 'spec'
|
25
|
+
spec.spec_files = FileList['spec/**/*_spec.rb']
|
26
|
+
end
|
27
|
+
|
28
|
+
Spec::Rake::SpecTask.new(:rcov) do |spec|
|
29
|
+
spec.libs << 'lib' << 'spec'
|
30
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
31
|
+
spec.rcov = true
|
32
|
+
end
|
33
|
+
|
34
|
+
task :spec => :check_dependencies
|
35
|
+
|
36
|
+
task :default => :spec
|
37
|
+
|
38
|
+
require 'rake/rdoctask'
|
39
|
+
Rake::RDocTask.new do |rdoc|
|
40
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
41
|
+
|
42
|
+
rdoc.rdoc_dir = 'rdoc'
|
43
|
+
rdoc.title = "git_cleaner #{version}"
|
44
|
+
rdoc.rdoc_files.include('README*')
|
45
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
46
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
data/bin/git_cleaner
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require File.expand_path(File.dirname(__FILE__) + "/../lib/git_cleaner")
|
4
|
+
|
5
|
+
begin
|
6
|
+
GitCleaner.new.cleanup
|
7
|
+
rescue Interrupt => i
|
8
|
+
puts "\rThank you for using Git Cleaner!"
|
9
|
+
rescue Exception => e
|
10
|
+
puts "Quitting Git Cleaner due to error: #{e.message}"
|
11
|
+
puts e.backtrace
|
12
|
+
end
|
data/lib/git_cleaner.rb
ADDED
@@ -0,0 +1,192 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'lighthouse'
|
3
|
+
require 'optparse'
|
4
|
+
require 'ostruct'
|
5
|
+
|
6
|
+
class GitCleaner
|
7
|
+
CONFIG_FILE = '.git_cleaner'
|
8
|
+
|
9
|
+
attr_accessor :project_name
|
10
|
+
|
11
|
+
def initialize
|
12
|
+
if File.exist?("./#{CONFIG_FILE}")
|
13
|
+
@config = YAML.load_file("./#{CONFIG_FILE}")
|
14
|
+
|
15
|
+
if @config['lighthouse']
|
16
|
+
Lighthouse.token = @config['lighthouse']['token']
|
17
|
+
Lighthouse.account = @config['lighthouse']['account']
|
18
|
+
self.project_name = @config['lighthouse']['project']
|
19
|
+
end
|
20
|
+
else
|
21
|
+
# We might want to warn here if there's no config file at all. Right now it's not a big deal.
|
22
|
+
end
|
23
|
+
|
24
|
+
@options = {:rules => {}}
|
25
|
+
|
26
|
+
OptionParser.new do |opts|
|
27
|
+
opts.banner = "Usage: git_cleanup --remote"
|
28
|
+
|
29
|
+
opts.on('-r', '--remote', 'Clean up remote branches') do |f|
|
30
|
+
@options[:mode] = 'remote'
|
31
|
+
end
|
32
|
+
|
33
|
+
opts.on('-l', '--local', 'Clean up local branches') do
|
34
|
+
@options[:mode] = 'local'
|
35
|
+
end
|
36
|
+
|
37
|
+
opts.on('-t', '--test', 'Test mode - no changes will be made') do
|
38
|
+
@options[:test_mode] = true
|
39
|
+
end
|
40
|
+
|
41
|
+
opts.on('-b', '--before date', 'Automatically delete branches last updated before') do |date|
|
42
|
+
@options[:rules].merge!(:before => eval(date))
|
43
|
+
end
|
44
|
+
|
45
|
+
opts.on('-s', '--state ticket_state', 'Automatically delete branches corresponding to a Lighthouse ticket state') do |state|
|
46
|
+
puts "#{CONFIG_FILE} with Lighthouse token, account, and project are required to match on ticket state" and exit unless Lighthouse.token && Lighthouse.account && project_name
|
47
|
+
@options[:rules].merge!(:state => state)
|
48
|
+
end
|
49
|
+
|
50
|
+
opts.on('-m', '--matching string', 'Automatically delete branches with names matching the string') do |string|
|
51
|
+
@options[:rules].merge!(:matching => string)
|
52
|
+
end
|
53
|
+
end.parse!
|
54
|
+
end
|
55
|
+
|
56
|
+
def get_project(name)
|
57
|
+
projects = Lighthouse::Project.find(:all)
|
58
|
+
project = projects.find {|pr| pr.name == name}
|
59
|
+
end
|
60
|
+
|
61
|
+
def prune_these(local_or_remote, branches)
|
62
|
+
project = get_project(project_name)
|
63
|
+
|
64
|
+
branches.each do |branch|
|
65
|
+
branch_info = OpenStruct.new(:string => branch, :local_or_remote => local_or_remote)
|
66
|
+
|
67
|
+
puts "#{branch_info.string} (updated #{branch_modified(branch_info, :time_ago_in_words)})"
|
68
|
+
|
69
|
+
if project
|
70
|
+
get_lighthouse_status(branch_info, project)
|
71
|
+
puts branch_info.lighthouse_message
|
72
|
+
end
|
73
|
+
|
74
|
+
if @options[:rules].empty?
|
75
|
+
print "keep [return], delete [d]: "
|
76
|
+
user_input = gets
|
77
|
+
user_input.strip!
|
78
|
+
|
79
|
+
case user_input
|
80
|
+
when 'd'
|
81
|
+
send "delete_#{local_or_remote}", branch_info
|
82
|
+
end
|
83
|
+
|
84
|
+
puts "\n"
|
85
|
+
else
|
86
|
+
rules_matched = @options[:rules].map do |rule_name, rule_body|
|
87
|
+
send "match_#{rule_name}", branch_info, rule_body
|
88
|
+
end
|
89
|
+
|
90
|
+
send "delete_#{local_or_remote}", branch_info if rules_matched.all?
|
91
|
+
puts "\n"
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
def branch_modified(branch_info, format = :time_ago_in_words)
|
97
|
+
format_string =
|
98
|
+
case format
|
99
|
+
when :time_ago_in_words
|
100
|
+
"%ar"
|
101
|
+
when :absolute
|
102
|
+
"%aD"
|
103
|
+
end
|
104
|
+
|
105
|
+
`git show --pretty=format:#{format_string} #{branch_info.string}`.split("\n")[0]
|
106
|
+
end
|
107
|
+
|
108
|
+
# git_cleanup --before 1.month.ago
|
109
|
+
def match_before(branch_info, date)
|
110
|
+
last_updated = Time.parse(branch_modified(branch_info, :absolute))
|
111
|
+
last_updated < date
|
112
|
+
rescue
|
113
|
+
false
|
114
|
+
end
|
115
|
+
|
116
|
+
# git_cleanup --state resolved
|
117
|
+
def match_state(branch_info, state)
|
118
|
+
branch_info.lighthouse_state == state
|
119
|
+
end
|
120
|
+
|
121
|
+
# git_cleanup --matching hotfix
|
122
|
+
def match_matching(branch_info, string)
|
123
|
+
branch_info.string =~ /#{string}/
|
124
|
+
end
|
125
|
+
|
126
|
+
def delete_local(branch_info)
|
127
|
+
execute "git branch -D #{branch_info.string}"
|
128
|
+
end
|
129
|
+
|
130
|
+
def delete_remote(branch_info)
|
131
|
+
branch_info.string.gsub!(/(remotes\/)|(origin\/)/, '')
|
132
|
+
execute "git push origin :#{branch_info.string}", "git remote prune origin"
|
133
|
+
end
|
134
|
+
|
135
|
+
def execute(*instructions)
|
136
|
+
if test_mode?
|
137
|
+
puts "Test mode - The following instructions would be executed"
|
138
|
+
|
139
|
+
instructions.each do |instruction|
|
140
|
+
puts instruction
|
141
|
+
end
|
142
|
+
else
|
143
|
+
instructions.each do |instruction|
|
144
|
+
system instruction
|
145
|
+
end
|
146
|
+
end
|
147
|
+
end
|
148
|
+
|
149
|
+
def get_lighthouse_status(branch_info, project)
|
150
|
+
lighthouse_id =
|
151
|
+
(matches = branch_info.string.match(/(^|\/)(\d+)-/)) ? matches[2] : nil
|
152
|
+
|
153
|
+
if lighthouse_id
|
154
|
+
tickets = project.tickets :q => lighthouse_id
|
155
|
+
ticket = tickets.first
|
156
|
+
|
157
|
+
if ticket
|
158
|
+
branch_info.lighthouse_state = ticket.state
|
159
|
+
branch_info.lighthouse_message = "Lighthouse Info:\nTicket ##{lighthouse_id} state - #{ticket.state}"
|
160
|
+
return
|
161
|
+
end
|
162
|
+
end
|
163
|
+
|
164
|
+
branch_info.lighthouse_message = "No Lighthouse Info."
|
165
|
+
rescue StandardError => e
|
166
|
+
puts "Unable to connect to Lighthouse"
|
167
|
+
end
|
168
|
+
|
169
|
+
def cleanup
|
170
|
+
branches = `git branch -a`.split.reject { |name| name == "*" }
|
171
|
+
|
172
|
+
local_branches = branches.select do |branch|
|
173
|
+
!(branch =~ /^remotes\/origin/)
|
174
|
+
end
|
175
|
+
|
176
|
+
remote_branches = branches.select do |branch|
|
177
|
+
!local_branches.include?(branch)
|
178
|
+
end
|
179
|
+
|
180
|
+
local_or_remote = @options[:mode]
|
181
|
+
|
182
|
+
if local_or_remote == 'local'
|
183
|
+
prune_these(local_or_remote, local_branches)
|
184
|
+
else
|
185
|
+
prune_these(local_or_remote, remote_branches)
|
186
|
+
end
|
187
|
+
end
|
188
|
+
|
189
|
+
def test_mode?
|
190
|
+
@options[:test_mode]
|
191
|
+
end
|
192
|
+
end
|
data/spec/spec.opts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,101 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: git_cleaner
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 1
|
8
|
+
- 0
|
9
|
+
version: 0.1.0
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Leigh Caplan
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-06-02 00:00:00 -07:00
|
18
|
+
default_executable: git_cleaner
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: rspec
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - ">="
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
segments:
|
28
|
+
- 1
|
29
|
+
- 2
|
30
|
+
- 9
|
31
|
+
version: 1.2.9
|
32
|
+
type: :development
|
33
|
+
version_requirements: *id001
|
34
|
+
- !ruby/object:Gem::Dependency
|
35
|
+
name: lighthouse-api
|
36
|
+
prerelease: false
|
37
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - ">="
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
segments:
|
42
|
+
- 1
|
43
|
+
- 1
|
44
|
+
- 0
|
45
|
+
version: 1.1.0
|
46
|
+
type: :runtime
|
47
|
+
version_requirements: *id002
|
48
|
+
description: Git Cleaner allows you to easily remove stale branches based on a number of factors, such as date modified, branch name, and even Lighthouse ticket state.
|
49
|
+
email: lcaplan@onehub.com
|
50
|
+
executables:
|
51
|
+
- git_cleaner
|
52
|
+
extensions: []
|
53
|
+
|
54
|
+
extra_rdoc_files:
|
55
|
+
- LICENSE
|
56
|
+
- README.rdoc
|
57
|
+
files:
|
58
|
+
- .document
|
59
|
+
- .gitignore
|
60
|
+
- LICENSE
|
61
|
+
- README.rdoc
|
62
|
+
- Rakefile
|
63
|
+
- VERSION
|
64
|
+
- bin/git_cleaner
|
65
|
+
- lib/git_cleaner.rb
|
66
|
+
- spec/git_cleaner_spec.rb
|
67
|
+
- spec/spec.opts
|
68
|
+
- spec/spec_helper.rb
|
69
|
+
has_rdoc: true
|
70
|
+
homepage: http://github.com/onehub/git_cleaner
|
71
|
+
licenses: []
|
72
|
+
|
73
|
+
post_install_message:
|
74
|
+
rdoc_options:
|
75
|
+
- --charset=UTF-8
|
76
|
+
require_paths:
|
77
|
+
- lib
|
78
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
segments:
|
83
|
+
- 0
|
84
|
+
version: "0"
|
85
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
segments:
|
90
|
+
- 0
|
91
|
+
version: "0"
|
92
|
+
requirements: []
|
93
|
+
|
94
|
+
rubyforge_project:
|
95
|
+
rubygems_version: 1.3.6
|
96
|
+
signing_key:
|
97
|
+
specification_version: 3
|
98
|
+
summary: "Automatic cleanup of Git branches\xE2\x80\x93 with Lighthouse integration!"
|
99
|
+
test_files:
|
100
|
+
- spec/git_cleaner_spec.rb
|
101
|
+
- spec/spec_helper.rb
|