Class: Overcommit::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/overcommit/configuration.rb

Overview

Stores configuration for Overcommit and the hooks it runs.

Instance Method Summary collapse

Constructor Details

#initialize(hash) ⇒ Configuration

Creates a configuration from the given hash.



5
6
7
8
# File 'lib/overcommit/configuration.rb', line 5

def initialize(hash)
  @hash = hash
  validate
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



10
11
12
# File 'lib/overcommit/configuration.rb', line 10

def ==(other)
  super || @hash == other.hash
end

#apply_environment!(hook_type, env) ⇒ Object

Applies additional configuration settings based on the provided environment variables.



49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/overcommit/configuration.rb', line 49

def apply_environment!(hook_type, env)
  skipped_hooks = "#{env['SKIP']} #{env['SKIP_CHECKS']} #{env['SKIP_HOOKS']}".split(/[:, ]/)

  if skipped_hooks.include?('all') || skipped_hooks.include?('ALL')
    @hash[hook_type]['ALL']['skip'] = true
  else
    skipped_hooks.select { |hook_name| hook_exists?(hook_type, hook_name) }.
                  map { |hook_name| Overcommit::Utils.camel_case(hook_name) }.
                  each do |hook_name|
      @hash[hook_type][hook_name] ||= {}
      @hash[hook_type][hook_name]['skip'] = true
    end
  end
end

#enabled_builtin_hooks(hook_type) ⇒ Object

Returns the built-in hooks that have been enabled for a hook type.



22
23
24
25
26
# File 'lib/overcommit/configuration.rb', line 22

def enabled_builtin_hooks(hook_type)
  @hash[hook_type].keys.
    select { |hook_name| hook_name != 'ALL' }.
    select { |hook_name| hook_enabled?(hook_type, hook_name) }
end

#for_hook(hook, hook_type = nil) ⇒ Object

Returns a non-modifiable configuration for a hook.



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

def for_hook(hook, hook_type = nil)
  unless hook_type
    components = hook.class.name.split('::')
    hook = components.last
    hook_type = components[-2]
  end

  # Merge hook configuration with special 'ALL' config
  smart_merge(@hash[hook_type]['ALL'], @hash[hook_type][hook] || {}).freeze
end

#merge(config) ⇒ Object

Merges the given configuration with this one, returning a new Overcommit::Configuration. The provided configuration will either add to or replace any options defined in this configuration.



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

def merge(config)
  self.class.new(smart_merge(@hash, config.hash))
end

#plugin_directoryObject

Returns absolute path to the directory that external hook plugins should be loaded from.



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

def plugin_directory
  File.join(Overcommit::Utils.repo_root, @hash['plugin_directory'] || '.githooks')
end