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, RunAll

Instance Method Summary collapse

Constructor Details

#initialize(config, args) ⇒ Base

Returns a new instance of Base.

Parameters:



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

def initialize(config, args)
  @config = config
  @args = args
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.



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

def cleanup_environment
  # Implemented by subclass
end

#hook_class_nameObject

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



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

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



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

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

#hook_type_nameObject

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



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

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.



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

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.



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

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.



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

def setup_environment
  # Implemented by subclass
end