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 Method Summary collapse

Constructor Details

#initialize(config, context) ⇒ Base

Returns a new instance of Base.



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

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



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

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

#descriptionObject



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

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

#enabled?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/overcommit/hook/base.rb', line 36

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

#execute(cmd) ⇒ Object



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

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

#in_path?(cmd) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#nameObject



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

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

#quiet?Boolean

Returns:

  • (Boolean)


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

def quiet?
  @config['quiet']
end

#required?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/overcommit/hook/base.rb', line 28

def required?
  @config['required']
end

#runObject

Runs the hook.

Raises:

  • (NotImplementedError)


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

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

#run?Boolean

Returns:

  • (Boolean)


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

def run?
  enabled? &&
    (!skip? || required?) &&
    !(requires_modified_files? && applicable_files.empty?)
end

#skip?Boolean

Returns:

  • (Boolean)


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

def skip?
  @config['skip']
end