Class: Overcommit::HookContext::Base Abstract

Inherits:
Object
  • Object
show all
Defined in:
lib/overcommit/hook_context/base.rb

Overview

This class is abstract.

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, **options) ⇒ Base

Creates a hook context from the given configuration and input options.

Parameters:

  • config (Overcommit::Configuration)
  • args (Array<String>)
  • input (IO)

    standard input stream

  • options (Hash)

    cli options



22
23
24
25
26
27
28
# File 'lib/overcommit/hook_context/base.rb', line 22

def initialize(config, args, input, **options)
  @config = config
  @args = args
  @input = input
  @options = options
  @input_mutex = Mutex.new
end

Instance Method Details

#all_filesArray<String>

Returns the full list of files tracked by git

Returns:

  • (Array<String>)


90
91
92
# File 'lib/overcommit/hook_context/base.rb', line 90

def all_files
  Overcommit::GitRepo.all_files
end

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



73
74
75
# File 'lib/overcommit/hook_context/base.rb', line 73

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.



35
36
37
# File 'lib/overcommit/hook_context/base.rb', line 35

def execute_hook(command)
  Overcommit::Utils.execute(command, args: @args, input: input_string)
end

#hook_class_nameString

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

Returns:

  • (String)


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

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

#hook_script_nameString

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

Returns:

  • (String)


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

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

#hook_type_nameString

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

Returns:

  • (String)


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

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

#input_linesArray<String>

Returns an array of lines passed to the hook via the standard input stream.

Returns:

  • (Array<String>)


112
113
114
# File 'lib/overcommit/hook_context/base.rb', line 112

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

#input_stringString

Returns the contents of the entire standard input stream that were passed to the hook.

Returns:

  • (String)


98
99
100
101
102
103
104
105
106
# File 'lib/overcommit/hook_context/base.rb', line 98

def input_string
  return @input_string if defined?(@input_string)

  @input_mutex.synchronize do
    @input_string = @input.read unless defined?(@input_string)
  end

  @input_string
end

#modified_filesArray<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.

Returns:

  • (Array<String>)


83
84
85
# File 'lib/overcommit/hook_context/base.rb', line 83

def modified_files
  []
end

#post_fail_messageString

Returns a message to display on failure.

Returns:

  • (String)


119
120
121
# File 'lib/overcommit/hook_context/base.rb', line 119

def post_fail_message
  nil
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.



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

def setup_environment
  # Implemented by subclass, if applicable
end