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.



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

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.



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

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

#descriptionObject



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

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

#enabled?Boolean

Returns:

  • (Boolean)


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

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

#execute(cmd) ⇒ Object



67
68
69
# File 'lib/overcommit/hook/base.rb', line 67

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

#in_path?(cmd) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#nameObject



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

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

#quiet?Boolean

Returns:

  • (Boolean)


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

def quiet?
  @config['quiet']
end

#required?Boolean

Returns:

  • (Boolean)


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

def required?
  @config['required']
end

#runObject

Runs the hook.

Raises:

  • (NotImplementedError)


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

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

#run?Boolean

Returns:

  • (Boolean)


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

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.



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

def run_and_transform
  status, output = run

  [transform_status(status), output]
end

#skip?Boolean

Returns:

  • (Boolean)


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

def skip?
  @config['skip']
end