class Kamaze::Project::Tools::Git::Hooks::PreCommit

PreCommit hook

Provide helper methods intended to write hooks relating to pre-commit

Sample of use:

“`ruby retcode = tools.fetch(:git).hooks.process_index do |files|

tools.fetch(:rubocop).prepare do |c|
  c.patterns = files.reject(&:deleted?).map(&:to_s)
end.run

exit(retcode) unless retcode.zero?

end “`

Public Instance Methods

process_index(options = {}) { |to_a.freeze| ... } click to toggle source

Process index (files)

Exits with a status code, raising “SystemExit“.

| code | constant | reason | |———|———————–|—————–| | “125“ | “Errno::ECANCELED“ | Index is empty | | “95“ | “Errno::EOPNOTSUPP“ | Index is unsafe | | “0“ | “Errno::NOERROR“ | Success |

@param [Hash] options @option options [Boolean] :allow_unsafe

Unsafe index is processed (``false``)

@option options [Boolean] :allow_empty

Empty index is processed (``false``)

@yield [Array<Kamaze::Project::Tools::Git::Status::File>] @yieldreturn [Fixnum] @raise [SystemExit]

“:allow_empty“ and “:allow_unsafe“ options are available, to continue processing even if “empty“ or “unsafe“.

# File lib/kamaze/project/tools/git/hooks/pre_commit.rb, line 49
def process_index(options = {})
  index = _process_index(repository.status.index, options)
  self.retcode = Errno::NOERROR::Errno

  yield(index.to_a.freeze) if block_given?
end

Protected Instance Methods

_process_index(index, options = {}) click to toggle source

@param [Kamaze::Project::Tools::Git::Status::Index] index @param [Hash] options @raise [SystemExit]

@return [Kamaze::Project::Tools::Git::Status::Index]

# File lib/kamaze/project/tools/git/hooks/pre_commit.rb, line 63
def _process_index(index, options = {})
  { empty: Errno::ECANCELED, unsafe: Errno::EOPNOTSUPP }.each do |type, v|
    with_exit_on_failure do
      key = "allow_#{type}".to_sym
      options[key] = options.keys.include?(key) ? !!options[key] : false

      next if options[key]

      self.retcode = v.const_get(:Errno) if index.public_send("#{type}?")
    end
  end

  index
end