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.



120
121
122
# File 'lib/overcommit/hook/base.rb', line 120

def applicable_files
  @applicable_files ||= modified_files.select { |file| applicable_file?(file) }
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

#executableObject



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

def executable
  @config['required_executable']
end

#execute(cmd) ⇒ Object



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

def execute(cmd)
  Overcommit::Utils.execute(cmd)
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

#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