Class: Overcommit::Configuration
- Inherits:
-
Object
- Object
- Overcommit::Configuration
- Defined in:
- lib/overcommit/configuration.rb
Overview
Stores configuration for Overcommit and the hooks it runs.
Instance Method Summary collapse
- #==(other) ⇒ Object (also: #eql?)
-
#all_builtin_hook_configs ⇒ Hash
Returns configuration for all built-in hooks in each hook type.
-
#all_hook_configs ⇒ Hash
Returns configuration for all hooks in each hook type.
-
#all_plugin_hook_configs ⇒ Hash
Returns configuration for all plugin hooks in each hook type.
-
#apply_environment!(hook_context, env) ⇒ Object
Applies additional configuration settings based on the provided environment variables.
-
#enabled_builtin_hooks(hook_context) ⇒ Object
Returns the built-in hooks that have been enabled for a hook type.
-
#for_hook(hook, hook_type = nil) ⇒ Object
Returns a non-modifiable configuration for a hook.
-
#initialize(hash, options = {}) ⇒ Configuration
constructor
Creates a configuration from the given hash.
-
#merge(config) ⇒ Object
Merges the given configuration with this one, returning a new Configuration.
-
#plugin_directory ⇒ Object
Returns absolute path to the directory that external hook plugins should be loaded from.
- #plugin_hook?(hook_context_or_type, hook_name) ⇒ Boolean
- #verify_plugin_signatures? ⇒ Boolean
Constructor Details
#initialize(hash, options = {}) ⇒ Configuration
Creates a configuration from the given hash.
10 11 12 13 14 |
# File 'lib/overcommit/configuration.rb', line 10 def initialize(hash, = {}) @options = .dup @options[:logger] ||= Overcommit::Logger.silent @hash = Overcommit::ConfigurationValidator.new.validate(hash, ) end |
Instance Method Details
#==(other) ⇒ Object Also known as: eql?
16 17 18 |
# File 'lib/overcommit/configuration.rb', line 16 def ==(other) super || @hash == other.hash end |
#all_builtin_hook_configs ⇒ Hash
Returns configuration for all built-in hooks in each hook type.
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/overcommit/configuration.rb', line 41 def all_builtin_hook_configs hook_configs = {} Overcommit::Utils.supported_hook_type_classes.each do |hook_type| hook_names = @hash[hook_type].keys.reject { |name| name == 'ALL' } hook_configs[hook_type] = Hash[ hook_names.map do |hook_name| [hook_name, for_hook(hook_name, hook_type)] end ] end hook_configs end |
#all_hook_configs ⇒ Hash
Returns configuration for all hooks in each hook type.
34 35 36 |
# File 'lib/overcommit/configuration.rb', line 34 def all_hook_configs smart_merge(all_builtin_hook_configs, all_plugin_hook_configs) end |
#all_plugin_hook_configs ⇒ Hash
Returns configuration for all plugin hooks in each hook type.
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/overcommit/configuration.rb', line 60 def all_plugin_hook_configs hook_configs = {} Overcommit::Utils.supported_hook_types.each do |hook_type| hook_type_class_name = Overcommit::Utils.camel_case(hook_type) directory = File.join(plugin_directory, hook_type.gsub('-', '_')) plugin_paths = Dir[File.join(directory, '*.rb')].sort hook_names = plugin_paths.map do |path| Overcommit::Utils.camel_case(File.basename(path, '.rb')) end hook_configs[hook_type_class_name] = Hash[ hook_names.map do |hook_name| [hook_name, for_hook(hook_name, Overcommit::Utils.camel_case(hook_type))] end ] end hook_configs end |
#apply_environment!(hook_context, env) ⇒ Object
Applies additional configuration settings based on the provided environment variables.
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/overcommit/configuration.rb', line 118 def apply_environment!(hook_context, env) skipped_hooks = "#{env['SKIP']} #{env['SKIP_CHECKS']} #{env['SKIP_HOOKS']}".split(/[:, ]/) hook_type = hook_context.hook_class_name if skipped_hooks.include?('all') || skipped_hooks.include?('ALL') @hash[hook_type]['ALL']['skip'] = true else skipped_hooks.select { |hook_name| hook_exists?(hook_context, 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_context) ⇒ Object
Returns the built-in hooks that have been enabled for a hook type.
84 85 86 87 88 89 |
# File 'lib/overcommit/configuration.rb', line 84 def enabled_builtin_hooks(hook_context) @hash[hook_context.hook_class_name].keys. select { |hook_name| hook_name != 'ALL' }. select { |hook_name| built_in_hook?(hook_context, hook_name) }. select { |hook_name| hook_enabled?(hook_context, hook_name) } end |
#for_hook(hook, hook_type = nil) ⇒ Object
Returns a non-modifiable configuration for a hook.
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/overcommit/configuration.rb', line 92 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 hook_config = smart_merge(@hash[hook_type]['ALL'], @hash[hook_type][hook] || {}) # Need to specially handle `enabled` option since not setting it does not # necessarily mean the hook is disabled hook_config['enabled'] = hook_enabled?(hook_type, hook) hook_config.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.
112 113 114 |
# File 'lib/overcommit/configuration.rb', line 112 def merge(config) self.class.new(smart_merge(@hash, config.hash)) end |
#plugin_directory ⇒ Object
Returns absolute path to the directory that external hook plugins should be loaded from.
23 24 25 |
# File 'lib/overcommit/configuration.rb', line 23 def plugin_directory File.join(Overcommit::Utils.repo_root, @hash['plugin_directory'] || '.githooks') end |
#plugin_hook?(hook_context_or_type, hook_name) ⇒ Boolean
134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/overcommit/configuration.rb', line 134 def plugin_hook?(hook_context_or_type, hook_name) hook_type_name = if hook_context_or_type.is_a?(String) Overcommit::Utils.snake_case(hook_context_or_type) else hook_context_or_type.hook_type_name end hook_name = Overcommit::Utils.snake_case(hook_name) File.exist?(File.join(plugin_directory, hook_type_name, "#{hook_name}.rb")) end |
#verify_plugin_signatures? ⇒ Boolean
27 28 29 |
# File 'lib/overcommit/configuration.rb', line 27 def verify_plugin_signatures? @hash['verify_plugin_signatures'] != false end |