Skip to content

Commit 4f3b4ac

Browse files
author
Aiden Scandella
committed
Extract `Reporter' class
Slimming down classes, separating concerns. Next step is to get all 'puts'-ing out of the GitHook class. Change-Id: I21b91f4a7c020e33354c197f75d97c9611061d05 Reviewed-on: https://gerrit.causes.com/22363 Tested-by: Aiden Scandella <aiden@causes.com> Reviewed-by: Shane da Silva <shane@causes.com>
1 parent 3b6c714 commit 4f3b4ac

File tree

3 files changed

+79
-64
lines changed

3 files changed

+79
-64
lines changed

lib/overcommit.rb

+1
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33
require 'overcommit/git_hook'
44
require 'overcommit/hook_specific_check'
55
require 'overcommit/staged_file'
6+
require 'overcommit/reporter'
67
require 'overcommit/utils'
78
require 'overcommit/version'

lib/overcommit/git_hook.rb

+8-64
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def initialize
3333
plugin_dirs << REPO_SPECIFIC_DIR if File.directory?(REPO_SPECIFIC_DIR)
3434

3535
plugin_dirs.each do |dir|
36-
Dir[File.join(dir, hook_name, '*.rb')].each do |plugin|
36+
Dir[File.join(dir, Overcommit::Utils.hook_name, '*.rb')].each do |plugin|
3737
unless skip_checks.include? File.basename(plugin, '.rb')
3838
begin
3939
require plugin
@@ -44,14 +44,15 @@ def initialize
4444
end
4545
end
4646
end
47-
48-
@width = 70 - (HookRegistry.checks.map { |s| s.name.length }.max || 0)
4947
end
5048

5149
def run(*args)
5250
exit if requires_modified_files? && modified_files.empty?
5351

54-
puts "Running #{hook_name} checks"
52+
reporter = Reporter.new(Overcommit::Utils.hook_name, HookRegistry.checks)
53+
54+
reporter.print_header
55+
5556
results = HookRegistry.checks.map do |check_class|
5657
check = check_class.new(*args)
5758
next if check.skip?
@@ -65,76 +66,19 @@ def run(*args)
6566

6667
status, output = check.run_check
6768

68-
print_incremental_result(title, status, output, check.stealth?)
69+
reporter.print_incremental_result(title, status, output, check.stealth?)
6970
[status, output]
7071
end.compact
7172

72-
print_result results
73-
end
74-
75-
def hook_name
76-
Overcommit::Utils.hook_name
73+
reporter.print_result results
7774
end
7875

79-
protected
76+
private
8077

8178
# If true, only run this check when there are modified files.
8279
def requires_modified_files?
8380
false
8481
end
85-
86-
def print_incremental_result(title, status, output, stealth = false)
87-
if stealth
88-
return if status == :good
89-
print title
90-
end
91-
92-
print '.' * (@width - title.length)
93-
case status
94-
when :good
95-
success('OK')
96-
when :bad
97-
error('FAILED')
98-
print_report(output)
99-
when :warn
100-
warning output
101-
when :stop
102-
warning 'UH OH'
103-
print_report(output)
104-
else
105-
error '???'
106-
print_report("Check didn't return a status")
107-
exit 1
108-
end
109-
end
110-
111-
def print_result(results)
112-
puts
113-
case final_result(results)
114-
when :good
115-
success "+++ All #{hook_name} checks passed"
116-
exit 0
117-
when :bad
118-
error "!!! One or more #{hook_name} checks failed"
119-
exit 1
120-
when :stop
121-
warning "*** One or more #{hook_name} checks needs attention"
122-
warning "*** If you really want to commit, use SKIP_CHECKS"
123-
warning "*** (takes a space-separated list of checks to skip, or 'all')"
124-
exit 1
125-
end
126-
end
127-
128-
def final_result(results)
129-
states = (results.transpose.first || []).uniq
130-
return :bad if states.include?(:bad)
131-
return :stop if states.include?(:stop)
132-
return :good
133-
end
134-
135-
def print_report(*report)
136-
puts report.flatten.map{ |line| " #{line}" }.join("\n")
137-
end
13882
end
13983
end
14084
end

lib/overcommit/reporter.rb

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
module Overcommit
2+
class Reporter
3+
include ConsoleMethods
4+
5+
def initialize(name, checks)
6+
@name = name
7+
@checks = checks
8+
@width = 70 - (@checks.map { |s| s.name.length }.max || 0)
9+
end
10+
11+
def print_header
12+
puts "Running #{@name} checks"
13+
end
14+
15+
def print_incremental_result(title, status, output, stealth = false)
16+
if stealth
17+
return if status == :good
18+
print title
19+
end
20+
21+
print '.' * (@width - title.length)
22+
case status
23+
when :good
24+
success('OK')
25+
when :bad
26+
error('FAILED')
27+
print_report(output)
28+
when :warn
29+
warning output
30+
when :stop
31+
warning 'UH OH'
32+
print_report(output)
33+
else
34+
error '???'
35+
print_report("Check didn't return a status")
36+
exit 1
37+
end
38+
end
39+
40+
def print_result(results)
41+
puts
42+
case final_result(results)
43+
when :good
44+
success "+++ All #{@name} checks passed"
45+
exit 0
46+
when :bad
47+
error "!!! One or more #{@name} checks failed"
48+
exit 1
49+
when :stop
50+
warning "*** One or more #{@name} checks needs attention"
51+
warning "*** If you really want to commit, use SKIP_CHECKS"
52+
warning "*** (takes a space-separated list of checks to skip, or 'all')"
53+
exit 1
54+
end
55+
end
56+
57+
private
58+
59+
def final_result(results)
60+
states = (results.transpose.first || []).uniq
61+
return :bad if states.include?(:bad)
62+
return :stop if states.include?(:stop)
63+
return :good
64+
end
65+
66+
def print_report(*report)
67+
puts report.flatten.map{ |line| " #{line}" }.join("\n")
68+
end
69+
end
70+
end

0 commit comments

Comments
 (0)