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, PreCommit::Mdl, PrePush::Base, PreRebase::Base, PrepareCommitMsg::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
includeandexcludelists. -
#command ⇒ Array<String>
Return command to execute for this hook.
- #description ⇒ Object
- #enabled? ⇒ Boolean
- #excluded? ⇒ Boolean
-
#execute(cmd, options = {}) ⇒ #status, ...
Execute a command in a separate process.
- #execute_in_background(cmd) ⇒ Object
-
#flags ⇒ Array<String>
Return command line flags to be passed to the command.
- #in_path?(cmd) ⇒ Boolean
-
#included_files ⇒ Object
Gets a list of all files that apply to this hook based on its configured
includeandexcludelists. -
#initialize(config, context) ⇒ Base
constructor
A new instance of Base.
- #name ⇒ Object
- #parallelize? ⇒ Boolean
- #processors ⇒ 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.
27 28 29 30 |
# File 'lib/overcommit/hook/base.rb', line 27 def initialize(config, context) @config = config.for_hook(self) @context = context end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
23 24 25 |
# File 'lib/overcommit/hook/base.rb', line 23 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.
164 165 166 |
# File 'lib/overcommit/hook/base.rb', line 164 def applicable_files @applicable_files ||= select_applicable(modified_files) 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.
140 141 142 |
# File 'lib/overcommit/hook/base.rb', line 140 def command Array(@config['command'] || required_executable) + flags end |
#description ⇒ Object
58 59 60 |
# File 'lib/overcommit/hook/base.rb', line 58 def description @config['description'] || "Run #{name}" end |
#enabled? ⇒ Boolean
78 79 80 |
# File 'lib/overcommit/hook/base.rb', line 78 def enabled? @config['enabled'] != false end |
#excluded? ⇒ Boolean
82 83 84 |
# File 'lib/overcommit/hook/base.rb', line 82 def excluded? exclude_branches.any? { |p| File.fnmatch(p, current_branch) } end |
#execute(cmd, options = {}) ⇒ #status, ...
Execute a command in a separate process.
If splittable_args is specified, ensures that those arguments are concatenated onto the end of the cmd arguments, but split up so that the operating system’s maximum command length is not exceeded. This is useful for splitting up long file lists.
114 115 116 |
# File 'lib/overcommit/hook/base.rb', line 114 def execute(cmd, = {}) Overcommit::Utils.execute(cmd, ) end |
#execute_in_background(cmd) ⇒ Object
118 119 120 |
# File 'lib/overcommit/hook/base.rb', line 118 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.
158 159 160 |
# File 'lib/overcommit/hook/base.rb', line 158 def flags Array(@config['flags']) end |
#in_path?(cmd) ⇒ Boolean
96 97 98 |
# File 'lib/overcommit/hook/base.rb', line 96 def in_path?(cmd) Overcommit::Utils.in_path?(cmd) end |
#included_files ⇒ Object
Gets a list of all files that apply to this hook based on its configured include and exclude lists.
170 171 172 |
# File 'lib/overcommit/hook/base.rb', line 170 def included_files @included_files ||= select_applicable(all_files) end |
#name ⇒ Object
54 55 56 |
# File 'lib/overcommit/hook/base.rb', line 54 def name self.class.name.split('::').last end |
#parallelize? ⇒ Boolean
66 67 68 |
# File 'lib/overcommit/hook/base.rb', line 66 def parallelize? @config['parallelize'] != false end |
#processors ⇒ Object
70 71 72 |
# File 'lib/overcommit/hook/base.rb', line 70 def processors @config.fetch('processors') { 1 } end |
#quiet? ⇒ Boolean
74 75 76 |
# File 'lib/overcommit/hook/base.rb', line 74 def quiet? @config['quiet'] end |
#required? ⇒ Boolean
62 63 64 |
# File 'lib/overcommit/hook/base.rb', line 62 def required? @config['required'] end |
#required_executable ⇒ Object
122 123 124 |
# File 'lib/overcommit/hook/base.rb', line 122 def required_executable @config['required_executable'] end |
#required_libraries ⇒ Object
126 127 128 |
# File 'lib/overcommit/hook/base.rb', line 126 def required_libraries Array(@config['required_library'] || @config['required_libraries']) end |
#run ⇒ Object
Runs the hook.
33 34 35 |
# File 'lib/overcommit/hook/base.rb', line 33 def run raise NotImplementedError, 'Hook must define `run`' end |
#run? ⇒ Boolean
90 91 92 93 94 |
# File 'lib/overcommit/hook/base.rb', line 90 def run? enabled? && !excluded? && !(@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.
43 44 45 46 47 48 49 50 51 52 |
# File 'lib/overcommit/hook/base.rb', line 43 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
86 87 88 |
# File 'lib/overcommit/hook/base.rb', line 86 def skip? @config['skip'] end |