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, options = {}) ⇒ Configuration

Creates a configuration from the given hash.

Parameters:

  • hash (Hash)

    loaded YAML config file as a hash

  • options (Hash) (defaults to: {})
  • default (Hash)

    a customizable set of options

  • logger (Hash)

    a customizable set of options



13
14
15
16
17
18
19
20
# File 'lib/overcommit/configuration.rb', line 13

def initialize(hash, options = {})
  @options = options.dup
  @options[:logger] ||= Overcommit::Logger.silent
  @hash = hash # Assign so validator can read original values
  unless options[:validate] == false
    @hash = Overcommit::ConfigurationValidator.new.validate(self, hash, options)
  end
end

Instance Method Details

#==(other) ⇒ Object



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

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

#[](key) ⇒ Array, ...

Access the configuration as if it were a hash.

Parameters:

  • key (String)

Returns:

  • (Array, Hash, Number, String)


30
31
32
# File 'lib/overcommit/configuration.rb', line 30

def [](key)
  @hash[key]
end

#all_builtin_hook_configsHash

Returns configuration for all built-in hooks in each hook type.

Returns:

  • (Hash)


70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/overcommit/configuration.rb', line 70

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_configsHash

Returns configuration for all hooks in each hook type.

Returns:

  • (Hash)


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

def all_hook_configs
  smart_merge(all_builtin_hook_configs, all_plugin_hook_configs)
end

#all_plugin_hook_configsHash

Returns configuration for all plugin hooks in each hook type.

Returns:

  • (Hash)


89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/overcommit/configuration.rb', line 89

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.tr('-', '_'))
    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.



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/overcommit/configuration.rb', line 155

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

  if only_hooks.any? || skipped_hooks.include?('all') || skipped_hooks.include?('ALL')
    @hash[hook_type]['ALL']['skip'] = true
  end

  only_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'] = false
  end

  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

#concurrencyObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/overcommit/configuration.rb', line 40

def concurrency
  @concurrency ||=
    begin
      cores = Overcommit::Utils.processor_count
      content = @hash.fetch('concurrency', '%<processors>d')
      if content.is_a?(String)
        concurrency_expr = content % { processors: cores }

        a, op, b = concurrency_expr.scan(%r{(\d+)\s*([+\-*\/])\s*(\d+)})[0]
        if a
          a.to_i.send(op, b.to_i)
        else
          concurrency_expr.to_i
        end
      else
        content.to_i
      end
    end
end

#enabled_ad_hoc_hooks(hook_context) ⇒ Object

Returns the ad hoc hooks that have been enabled for a hook type.



121
122
123
124
125
126
# File 'lib/overcommit/configuration.rb', line 121

def enabled_ad_hoc_hooks(hook_context)
  @hash[hook_context.hook_class_name].keys.
    reject { |hook_name| hook_name == 'ALL' }.
    select { |hook_name| ad_hoc_hook?(hook_context, hook_name) }.
    select { |hook_name| hook_enabled?(hook_context, hook_name) }
end

#enabled_builtin_hooks(hook_context) ⇒ Object

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



113
114
115
116
117
118
# File 'lib/overcommit/configuration.rb', line 113

def enabled_builtin_hooks(hook_context)
  @hash[hook_context.hook_class_name].keys.
    reject { |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.



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/overcommit/configuration.rb', line 129

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.



149
150
151
# File 'lib/overcommit/configuration.rb', line 149

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.



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

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

#plugin_hook?(hook_context_or_type, hook_name) ⇒ Boolean

Returns:

  • (Boolean)


179
180
181
182
183
184
185
186
187
188
189
# File 'lib/overcommit/configuration.rb', line 179

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

#previous_signature?true, false

Return whether a previous signature has been recorded for this configuration.

Returns:

  • (true, false)


203
204
205
# File 'lib/overcommit/configuration.rb', line 203

def previous_signature?
  !stored_signature.empty?
end

#signature_changed?true, false

Return whether the signature for this configuration has changed since it was last calculated.

Returns:

  • (true, false)


195
196
197
# File 'lib/overcommit/configuration.rb', line 195

def signature_changed?
  signature != stored_signature
end

#update_signature!Object

Update the currently stored signature for this hook.



232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# File 'lib/overcommit/configuration.rb', line 232

def update_signature!
  result = Overcommit::Utils.execute(
    %w[git config --local] + [signature_config_key, signature]
  )

  verify_signature_value = @hash['verify_signatures'] ? 1 : 0
  result &&= Overcommit::Utils.execute(
    %W[git config --local #{verify_signature_config_key} #{verify_signature_value}]
  )

  unless result.success?
    raise Overcommit::Exceptions::GitConfigError,
          "Unable to write to local repo git config: #{result.stderr}"
  end
end

#verify_signatures?true, false

Returns whether this configuration should verify itself by checking the stored configuration for the repo.

Returns:

  • (true, false)


211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/overcommit/configuration.rb', line 211

def verify_signatures?
  return false if ENV['OVERCOMMIT_NO_VERIFY']
  return true if @hash['verify_signatures'] != false

  result = Overcommit::Utils.execute(
    %W[git config --local --get #{verify_signature_config_key}]
  )

  if result.status == 1 # Key doesn't exist
    return true
  elsif result.status != 0
    raise Overcommit::Exceptions::GitConfigError,
          "Unable to read from local repo git config: #{result.stderr}"
  end

  # We don't cast since we want to allow anything to count as "true" except
  # a literal zero
  result.stdout.strip != '0'
end