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.

Direct Known Subclasses

CommitMsg, PostCheckout, PreCommit

Instance Method Summary collapse

Constructor Details

#initialize(config, args, input) ⇒ Base

Returns a new instance of Base.



14
15
16
17
18
# File 'lib/overcommit/hook_context/base.rb', line 14

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.



48
49
50
# File 'lib/overcommit/hook_context/base.rb', line 48

def cleanup_environment
  # Implemented by subclass
end

#hook_class_nameObject

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



21
22
23
# File 'lib/overcommit/hook_context/base.rb', line 21

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).



31
32
33
# File 'lib/overcommit/hook_context/base.rb', line 31

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

#hook_type_nameObject

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



26
27
28
# File 'lib/overcommit/hook_context/base.rb', line 26

def hook_type_name
  Overcommit::Utils.snake_case(hook_class_name)
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.



56
57
58
# File 'lib/overcommit/hook_context/base.rb', line 56

def modified_files
  []
end

#modified_lines(_file) ⇒ Object

Returns a set of lines that have been modified for a file.

By default, this returns an empty set. Subclasses should implement if there is a concept of files changing for the type of hook being run.



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

def modified_lines(_file)
  Set.new
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.



39
40
41
# File 'lib/overcommit/hook_context/base.rb', line 39

def setup_environment
  # Implemented by subclass
end