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, PostMerge::Base, PostRewrite::Base, PreCommit::Base, PreCommit::Jsl, PrePush::Base, PreRebase::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.
-
#command ⇒ Array<String>
Return command to execute for this hook.
- #description ⇒ Object
- #enabled? ⇒ Boolean
- #execute(cmd) ⇒ Object
- #execute_in_background(cmd) ⇒ Object
-
#flags ⇒ Array<String>
Return command line flags to be passed to the command.
- #in_path?(cmd) ⇒ Boolean
-
#initialize(config, context) ⇒ Base
constructor
A new instance of Base.
- #name ⇒ Object
- #quiet? ⇒ Boolean
- #required? ⇒ Boolean
- #required_executable ⇒ Object
- #required_libraries ⇒ Object
-
#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.
135 136 137 |
# File 'lib/overcommit/hook/base.rb', line 135 def applicable_files @applicable_files ||= modified_files.select { |file| applicable_file?(file) } end |
#command ⇒ Array<String>
Return command to execute for this hook.
This is intended to be configurable so hooks can prefix their commands with ‘bundle exec` or similar. It will appends the command line flags specified by the `flags` option after.
Note that any files intended to be passed must be handled by the hook itself.
111 112 113 |
# File 'lib/overcommit/hook/base.rb', line 111 def command Array(@config['command'] || required_executable) + flags end |
#description ⇒ Object
56 57 58 |
# File 'lib/overcommit/hook/base.rb', line 56 def description @config['description'] || "Running #{name}" end |
#enabled? ⇒ Boolean
68 69 70 |
# File 'lib/overcommit/hook/base.rb', line 68 def enabled? @config['enabled'] != false end |
#execute(cmd) ⇒ Object
85 86 87 |
# File 'lib/overcommit/hook/base.rb', line 85 def execute(cmd) Overcommit::Utils.execute(cmd) end |
#execute_in_background(cmd) ⇒ Object
89 90 91 |
# File 'lib/overcommit/hook/base.rb', line 89 def execute_in_background(cmd) Overcommit::Utils.execute_in_background(cmd) end |
#flags ⇒ Array<String>
Return command line flags to be passed to the command.
This excludes the list of files, as that must be handled by the hook itself.
The intention here is to provide flexibility for when a tool removes/renames its flags. Rather than wait for Overcommit to update the flags it uses, you can update your configuration to use the new flags right away without being blocked.
Also note that any flags containing dynamic content must be passed in the hook’s #run method.
129 130 131 |
# File 'lib/overcommit/hook/base.rb', line 129 def flags Array(@config['flags']) end |
#in_path?(cmd) ⇒ Boolean
81 82 83 |
# File 'lib/overcommit/hook/base.rb', line 81 def in_path?(cmd) Overcommit::Utils.in_path?(cmd) end |
#name ⇒ Object
52 53 54 |
# File 'lib/overcommit/hook/base.rb', line 52 def name self.class.name.split('::').last end |
#quiet? ⇒ Boolean
64 65 66 |
# File 'lib/overcommit/hook/base.rb', line 64 def quiet? @config['quiet'] end |
#required? ⇒ Boolean
60 61 62 |
# File 'lib/overcommit/hook/base.rb', line 60 def required? @config['required'] end |
#required_executable ⇒ Object
93 94 95 |
# File 'lib/overcommit/hook/base.rb', line 93 def required_executable @config['required_executable'] end |
#required_libraries ⇒ Object
97 98 99 |
# File 'lib/overcommit/hook/base.rb', line 97 def required_libraries Array(@config['required_library'] || @config['required_libraries']) 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
76 77 78 79 |
# File 'lib/overcommit/hook/base.rb', line 76 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 50 |
# File 'lib/overcommit/hook/base.rb', line 41 def run_and_transform if output = check_for_requirements status = :fail else result = Overcommit::Utils.with_environment(@config.fetch('env', {})) { run } status, output = process_hook_return_value(result) end [transform_status(status), output] end |
#skip? ⇒ Boolean
72 73 74 |
# File 'lib/overcommit/hook/base.rb', line 72 def skip? @config['skip'] end |