Class: Overcommit::HookContext::Base Abstract
- Inherits:
-
Object
- Object
- Overcommit::HookContext::Base
- Defined in:
- lib/overcommit/hook_context/base.rb
Overview
Contains helpers related to the context with which a hook is being run.
It acts as an adapter to the arguments passed to the hook, as well as context-specific information such as staged files, providing a single source of truth for this context.
This is also important to house in a separate object so that any calculations can be memoized across all hooks in a single object, which helps with performance.
Direct Known Subclasses
CommitMsg, PostCheckout, PostCommit, PostMerge, PostRewrite, PreCommit, PrePush, PreRebase, PrepareCommitMsg, RunAll
Instance Method Summary collapse
-
#all_files ⇒ Array<String>
Returns the full list of files tracked by git.
-
#cleanup_environment ⇒ Object
Resets the environment to an appropriate state.
-
#execute_hook(command) ⇒ Object
Executes a command as if it were a regular git hook, passing all command-line arguments and the standard input stream.
-
#hook_class_name ⇒ String
Returns the camel-cased type of this hook (e.g. PreCommit).
-
#hook_script_name ⇒ String
Returns the actual name of the hook script being run (e.g. pre-commit).
-
#hook_type_name ⇒ String
Returns the snake-cased type of this hook (e.g. pre_commit).
-
#initialize(config, args, input) ⇒ Base
constructor
Creates a hook context from the given configuration and input options.
-
#input_lines ⇒ Array<String>
Returns an array of lines passed to the hook via the standard input stream.
-
#input_string ⇒ String
Returns the contents of the entire standard input stream that were passed to the hook.
-
#modified_files ⇒ Array<String>
Returns a list of files that have been modified.
-
#post_fail_message ⇒ String
Returns a message to display on failure.
-
#setup_environment ⇒ Object
Initializes anything related to the environment.
Constructor Details
#initialize(config, args, input) ⇒ Base
Creates a hook context from the given configuration and input options.
21 22 23 24 25 |
# File 'lib/overcommit/hook_context/base.rb', line 21 def initialize(config, args, input) @config = config @args = args @input = input end |
Instance Method Details
#all_files ⇒ Array<String>
Returns the full list of files tracked by git
87 88 89 |
# File 'lib/overcommit/hook_context/base.rb', line 87 def all_files Overcommit::GitRepo.all_files end |
#cleanup_environment ⇒ Object
Resets the environment to an appropriate state.
This is called after the hooks have been run by the [HookRunner]. Different hook types can perform different cleanup operations, which are intended to “undo” the results of the call to #setup_environment.
70 71 72 |
# File 'lib/overcommit/hook_context/base.rb', line 70 def cleanup_environment # Implemented by subclass, if applicable end |
#execute_hook(command) ⇒ Object
Executes a command as if it were a regular git hook, passing all command-line arguments and the standard input stream.
This is intended to be used by ad hoc hooks so developers can link up their existing git hooks with Overcommit.
32 33 34 |
# File 'lib/overcommit/hook_context/base.rb', line 32 def execute_hook(command) Overcommit::Utils.execute(command, args: @args, input: input_string) end |
#hook_class_name ⇒ String
Returns the camel-cased type of this hook (e.g. PreCommit)
39 40 41 |
# File 'lib/overcommit/hook_context/base.rb', line 39 def hook_class_name self.class.name.split('::').last end |
#hook_script_name ⇒ String
Returns the actual name of the hook script being run (e.g. pre-commit).
53 54 55 |
# File 'lib/overcommit/hook_context/base.rb', line 53 def hook_script_name hook_type_name.tr('_', '-') end |
#hook_type_name ⇒ String
Returns the snake-cased type of this hook (e.g. pre_commit)
46 47 48 |
# File 'lib/overcommit/hook_context/base.rb', line 46 def hook_type_name Overcommit::Utils.snake_case(hook_class_name) end |
#input_lines ⇒ Array<String>
Returns an array of lines passed to the hook via the standard input stream.
103 104 105 |
# File 'lib/overcommit/hook_context/base.rb', line 103 def input_lines @input_lines ||= input_string.split("\n") end |
#input_string ⇒ String
Returns the contents of the entire standard input stream that were passed to the hook.
95 96 97 |
# File 'lib/overcommit/hook_context/base.rb', line 95 def input_string @input_string ||= @input.read end |
#modified_files ⇒ Array<String>
Returns a list of files that have been modified.
By default, this returns an empty list. Subclasses should implement if there is a concept of files changing for the type of hook being run.
80 81 82 |
# File 'lib/overcommit/hook_context/base.rb', line 80 def modified_files [] end |
#post_fail_message ⇒ String
Returns a message to display on failure.
110 111 112 |
# File 'lib/overcommit/hook_context/base.rb', line 110 def nil end |
#setup_environment ⇒ Object
Initializes anything related to the environment.
This is called before the hooks are run by the [HookRunner]. Different hook types can perform different setup.
61 62 63 |
# File 'lib/overcommit/hook_context/base.rb', line 61 def setup_environment # Implemented by subclass, if applicable end |