Class: Overcommit::HookContext::Base

Inherits:
Object
  • Object
show all
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.

Instance Method Summary collapse

Constructor Details

#initialize(config, args, input) ⇒ Base

Returns a new instance of Base.

Parameters:



16
17
18
19
20
# File 'lib/overcommit/hook_context/base.rb', line 16

def initialize(config, args, input)
  @config = config
  @args = args
  @input = input
end

Instance Method Details

#cleanup_environmentObject

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.



50
51
52
# File 'lib/overcommit/hook_context/base.rb', line 50

def cleanup_environment
  # Implemented by subclass
end

#hook_class_nameObject

Returns the camel-cased type of this hook (e.g. PreCommit)



23
24
25
# File 'lib/overcommit/hook_context/base.rb', line 23

def hook_class_name
  self.class.name.split('::').last
end

#hook_script_nameObject

Returns the actual name of the hook script being run (e.g. pre-commit).



33
34
35
# File 'lib/overcommit/hook_context/base.rb', line 33

def hook_script_name
  hook_type_name.gsub('_', '-')
end

#hook_type_nameObject

Returns the snake-cased type of this hook (e.g. pre_commit)



28
29
30
# File 'lib/overcommit/hook_context/base.rb', line 28

def hook_type_name
  Overcommit::Utils.snake_case(hook_class_name)
end

#input_linesObject

Returns an array of lines passed to the hook via STDIN.



63
64
65
# File 'lib/overcommit/hook_context/base.rb', line 63

def input_lines
  @input_lines ||= @input.read.split("\n")
end

#modified_filesObject

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.



58
59
60
# File 'lib/overcommit/hook_context/base.rb', line 58

def modified_files
  []
end

#setup_environmentObject

Initializes anything related to the environment.

This is called before the hooks are run by the [HookRunner]. Different hook types can perform different setup.



41
42
43
# File 'lib/overcommit/hook_context/base.rb', line 41

def setup_environment
  # Implemented by subclass
end