Class: Overcommit::Hook::Base

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/overcommit/hook/base.rb

Overview

Functionality common to all hooks.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config, context) ⇒ Base

Returns a new instance of Base.

Parameters:



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

def initialize(config, context)
  @config = config.for_hook(self)
  @context = context
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



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

def config
  @config
end

Instance Method Details

#applicable_filesObject

Gets a list of staged files that apply to this hook based on its configured ‘include` and `exclude` lists.



156
157
158
# File 'lib/overcommit/hook/base.rb', line 156

def applicable_files
  @applicable_files ||= modified_files.select { |file| applicable_file?(file) }
end

#commandArray<String>

Return command to execute for this hook.

This is intended to be configurable so hooks can prefix their commands with ‘bundle exec` or similar. It will appends the command line flags specified by the `flags` option after.

Note that any files intended to be passed must be handled by the hook itself.

Returns:

  • (Array<String>)


132
133
134
# File 'lib/overcommit/hook/base.rb', line 132

def command
  Array(@config['command'] || required_executable) + flags
end

#descriptionObject



81
82
83
# File 'lib/overcommit/hook/base.rb', line 81

def description
  @config['description'] || "Running #{name}"
end

#enabled?Boolean

Returns:

  • (Boolean)


93
94
95
# File 'lib/overcommit/hook/base.rb', line 93

def enabled?
  @config['enabled'] != false
end

#execute(cmd) ⇒ Object



110
111
112
# File 'lib/overcommit/hook/base.rb', line 110

def execute(cmd)
  Overcommit::Utils.execute(cmd)
end

#execute_in_background(cmd) ⇒ Object



114
115
116
# File 'lib/overcommit/hook/base.rb', line 114

def execute_in_background(cmd)
  Overcommit::Utils.execute_in_background(cmd)
end

#flagsArray<String>

Return command line flags to be passed to the command.

This excludes the list of files, as that must be handled by the hook itself.

The intention here is to provide flexibility for when a tool removes/renames its flags. Rather than wait for Overcommit to update the flags it uses, you can update your configuration to use the new flags right away without being blocked.

Also note that any flags containing dynamic content must be passed in the hook’s #run method.

Returns:

  • (Array<String>)


150
151
152
# File 'lib/overcommit/hook/base.rb', line 150

def flags
  Array(@config['flags'])
end

#in_path?(cmd) ⇒ Boolean

Returns:

  • (Boolean)


106
107
108
# File 'lib/overcommit/hook/base.rb', line 106

def in_path?(cmd)
  Overcommit::Utils.in_path?(cmd)
end

#nameObject



77
78
79
# File 'lib/overcommit/hook/base.rb', line 77

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

#process_hook_return_value(hook_return_value) ⇒ Array<Symbol,String>

Converts the hook’s return value into a canonical form of a tuple containing status (pass/warn/fail) and output.

This is intended to support various shortcuts for writing hooks so that hook authors don’t need to work with Message objects for simple pass/fail hooks. It also saves you from needing to manually encode logic like “if there are errors, fail; if there are warnings, warn, otherwise pass.” by simply returning an array of Message objects.

Parameters:

  • hook_return_value (Symbol, Array<Symbol,String>, Array<Message>)

Returns:

  • (Array<Symbol,String>)

    tuple of status and output



63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/overcommit/hook/base.rb', line 63

def process_hook_return_value(hook_return_value)
  if hook_return_value.is_a?(Array) &&
     hook_return_value.first.is_a?(Message)
    # Process messages into a status and output
    Overcommit::MessageProcessor.new(
      self,
      @config['problem_on_unmodified_line'],
    ).hook_result(hook_return_value)
  else
    # Otherwise return as-is
    hook_return_value
  end
end

#quiet?Boolean

Returns:

  • (Boolean)


89
90
91
# File 'lib/overcommit/hook/base.rb', line 89

def quiet?
  @config['quiet']
end

#required?Boolean

Returns:

  • (Boolean)


85
86
87
# File 'lib/overcommit/hook/base.rb', line 85

def required?
  @config['required']
end

#required_executableObject



118
119
120
# File 'lib/overcommit/hook/base.rb', line 118

def required_executable
  @config['required_executable']
end

#runObject

Runs the hook.

Raises:

  • (NotImplementedError)


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

def run
  raise NotImplementedError, 'Hook must define `run`'
end

#run?Boolean

Returns:

  • (Boolean)


101
102
103
104
# File 'lib/overcommit/hook/base.rb', line 101

def run?
  enabled? &&
    !(@config['requires_files'] && applicable_files.empty?)
end

#run_and_transformObject

Runs the hook and transforms the status returned based on the hook’s configuration.

Poorly named because we already have a bunch of hooks in the wild that implement ‘#run`, and we needed a wrapper step to transform the status based on any custom configuration.



41
42
43
44
45
46
47
48
49
# File 'lib/overcommit/hook/base.rb', line 41

def run_and_transform
  if output = check_for_executable
    status = :fail
  else
    status, output = process_hook_return_value(run)
  end

  [transform_status(status), output]
end

#skip?Boolean

Returns:

  • (Boolean)


97
98
99
# File 'lib/overcommit/hook/base.rb', line 97

def skip?
  @config['skip']
end