Class: Overcommit::Hook::Base
- Inherits:
-
Object
- Object
- Overcommit::Hook::Base
- Extended by:
- Forwardable
- Defined in:
- lib/overcommit/hook/base.rb
Overview
Functionality common to all hooks.
Direct Known Subclasses
CommitMsg::Base, PostCheckout::Base, PostCommit::Base, PreCommit::Base
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
Instance Method Summary collapse
-
#applicable_files ⇒ Object
Gets a list of staged files that apply to this hook based on its configured ‘include` and `exclude` lists.
- #description ⇒ Object
- #enabled? ⇒ Boolean
- #executable ⇒ Object
- #execute(cmd) ⇒ Object
- #in_path?(cmd) ⇒ Boolean
-
#initialize(config, context) ⇒ Base
constructor
A new instance of Base.
- #name ⇒ Object
-
#process_hook_return_value(hook_return_value) ⇒ Array<Symbol,String>
Converts the hook’s return value into a canonical form of a tuple containing status (pass/warn/fail) and output.
- #quiet? ⇒ Boolean
- #required? ⇒ Boolean
-
#run ⇒ Object
Runs the hook.
- #run? ⇒ Boolean
-
#run_and_transform ⇒ Object
Runs the hook and transforms the status returned based on the hook’s configuration.
- #skip? ⇒ Boolean
Constructor Details
#initialize(config, context) ⇒ Base
Returns a new instance of Base.
25 26 27 28 |
# File 'lib/overcommit/hook/base.rb', line 25 def initialize(config, context) @config = config.for_hook(self) @context = context end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
21 22 23 |
# File 'lib/overcommit/hook/base.rb', line 21 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.
120 121 122 |
# File 'lib/overcommit/hook/base.rb', line 120 def applicable_files @applicable_files ||= modified_files.select { |file| applicable_file?(file) } end |
#description ⇒ Object
81 82 83 |
# File 'lib/overcommit/hook/base.rb', line 81 def description @config['description'] || "Running #{name}" end |
#enabled? ⇒ Boolean
93 94 95 |
# File 'lib/overcommit/hook/base.rb', line 93 def enabled? @config['enabled'] != false end |
#executable ⇒ Object
114 115 116 |
# File 'lib/overcommit/hook/base.rb', line 114 def executable @config['required_executable'] end |
#execute(cmd) ⇒ Object
110 111 112 |
# File 'lib/overcommit/hook/base.rb', line 110 def execute(cmd) Overcommit::Utils.execute(cmd) end |
#in_path?(cmd) ⇒ Boolean
106 107 108 |
# File 'lib/overcommit/hook/base.rb', line 106 def in_path?(cmd) Overcommit::Utils.in_path?(cmd) end |
#name ⇒ Object
77 78 79 |
# File 'lib/overcommit/hook/base.rb', line 77 def name self.class.name.split('::').last end |
#process_hook_return_value(hook_return_value) ⇒ Array<Symbol,String>
Converts the hook’s return value into a canonical form of a tuple containing status (pass/warn/fail) and output.
This is intended to support various shortcuts for writing hooks so that hook authors don’t need to work with Message objects for simple pass/fail hooks. It also saves you from needing to manually encode logic like “if there are errors, fail; if there are warnings, warn, otherwise pass.” by simply returning an array of Message objects.
63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/overcommit/hook/base.rb', line 63 def process_hook_return_value(hook_return_value) if hook_return_value.is_a?(Array) && hook_return_value.first.is_a?(Message) # Process messages into a status and output Overcommit::MessageProcessor.new( self, @config['problem_on_unmodified_line'], ).hook_result(hook_return_value) else # Otherwise return as-is hook_return_value end end |
#quiet? ⇒ Boolean
89 90 91 |
# File 'lib/overcommit/hook/base.rb', line 89 def quiet? @config['quiet'] end |
#required? ⇒ Boolean
85 86 87 |
# File 'lib/overcommit/hook/base.rb', line 85 def required? @config['required'] end |
#run ⇒ Object
Runs the hook.
31 32 33 |
# File 'lib/overcommit/hook/base.rb', line 31 def run raise NotImplementedError, 'Hook must define `run`' end |
#run? ⇒ Boolean
101 102 103 104 |
# File 'lib/overcommit/hook/base.rb', line 101 def run? enabled? && !(@config['requires_files'] && applicable_files.empty?) end |
#run_and_transform ⇒ Object
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.
41 42 43 44 45 46 47 48 49 |
# File 'lib/overcommit/hook/base.rb', line 41 def run_and_transform if output = check_for_executable status = :fail else status, output = process_hook_return_value(run) end [transform_status(status), output] end |
#skip? ⇒ Boolean
97 98 99 |
# File 'lib/overcommit/hook/base.rb', line 97 def skip? @config['skip'] end |