Class: Overcommit::Hook::Base
- Inherits:
-
Object
- Object
- Overcommit::Hook::Base
show all
- Extended by:
- Forwardable
- Defined in:
- lib/overcommit/hook/base.rb
Overview
Functionality common to all hooks.
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(config, context) ⇒ Base
Returns a new instance of Base.
11
12
13
14
|
# File 'lib/overcommit/hook/base.rb', line 11
def initialize(config, context)
@config = config.for_hook(self)
@context = context
end
|
Instance Attribute Details
#config ⇒ Object
Returns the value of attribute config.
9
10
11
|
# File 'lib/overcommit/hook/base.rb', line 9
def config
@config
end
|
Instance Method Details
#applicable_files ⇒ Object
Gets a list of staged files that apply to this hook based on its configured include and exclude lists.
73
74
75
|
# File 'lib/overcommit/hook/base.rb', line 73
def applicable_files
@applicable_files ||= modified_files.select { |file| applicable_file?(file) }
end
|
#description ⇒ Object
37
38
39
|
# File 'lib/overcommit/hook/base.rb', line 37
def description
@config['description'] || "Running #{name}"
end
|
#enabled? ⇒ Boolean
49
50
51
|
# File 'lib/overcommit/hook/base.rb', line 49
def enabled?
@config['enabled'] != false
end
|
#execute(cmd) ⇒ Object
67
68
69
|
# File 'lib/overcommit/hook/base.rb', line 67
def execute(cmd)
Overcommit::Utils.execute(cmd)
end
|
#in_path?(cmd) ⇒ Boolean
63
64
65
|
# File 'lib/overcommit/hook/base.rb', line 63
def in_path?(cmd)
Overcommit::Utils.in_path?(cmd)
end
|
#name ⇒ Object
33
34
35
|
# File 'lib/overcommit/hook/base.rb', line 33
def name
self.class.name.split('::').last
end
|
#quiet? ⇒ Boolean
45
46
47
|
# File 'lib/overcommit/hook/base.rb', line 45
def quiet?
@config['quiet']
end
|
#required? ⇒ Boolean
41
42
43
|
# File 'lib/overcommit/hook/base.rb', line 41
def required?
@config['required']
end
|
#run ⇒ Object
17
18
19
|
# File 'lib/overcommit/hook/base.rb', line 17
def run
raise NotImplementedError, 'Hook must define `run`'
end
|
#run? ⇒ Boolean
57
58
59
60
61
|
# File 'lib/overcommit/hook/base.rb', line 57
def run?
enabled? &&
(!skip? || required?) &&
!(requires_modified_files? && applicable_files.empty?)
end
|
Runs the hook and transforms the status returned based on the hook’s configuration.
Poorly named because we already have a bunch of hooks in the wild that implement #run, and we needed a wrapper step to transform the status based on any custom configuration.
27
28
29
30
31
|
# File 'lib/overcommit/hook/base.rb', line 27
def run_and_transform
status, output = run
[transform_status(status), output]
end
|
#skip? ⇒ Boolean
53
54
55
|
# File 'lib/overcommit/hook/base.rb', line 53
def skip?
@config['skip']
end
|