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:



13
14
15
16
# File 'lib/overcommit/hook/base.rb', line 13

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

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



9
10
11
# File 'lib/overcommit/hook/base.rb', line 9

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.



87
88
89
# File 'lib/overcommit/hook/base.rb', line 87

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

#descriptionObject



43
44
45
# File 'lib/overcommit/hook/base.rb', line 43

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

#enabled?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/overcommit/hook/base.rb', line 55

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

#executableObject



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

def executable
  @config['required_executable']
end

#execute(cmd) ⇒ Object



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

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

#in_path?(cmd) ⇒ Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/overcommit/hook/base.rb', line 69

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

#install_commandObject



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

def install_command
  @config['install_command']
end

#nameObject



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

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

#quiet?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/overcommit/hook/base.rb', line 51

def quiet?
  @config['quiet']
end

#required?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/overcommit/hook/base.rb', line 47

def required?
  @config['required']
end

#runObject

Runs the hook.

Raises:

  • (NotImplementedError)


19
20
21
# File 'lib/overcommit/hook/base.rb', line 19

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

#run?Boolean

Returns:

  • (Boolean)


63
64
65
66
67
# File 'lib/overcommit/hook/base.rb', line 63

def run?
  enabled? &&
    (!skip? || required?) &&
    !(requires_modified_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.



29
30
31
32
33
34
35
36
37
# File 'lib/overcommit/hook/base.rb', line 29

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

  [transform_status(status), output]
end

#skip?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/overcommit/hook/base.rb', line 59

def skip?
  @config['skip']
end