Class: Kamaze::Project::Tools::Git::Hooks::PreCommit

Inherits:
BaseHook show all
Defined in:
lib/kamaze/project/tools/git/hooks/pre_commit.rb

Overview

PreCommit hook

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

Sample of use:

retcode = tools.fetch(:git).hooks[:pre_commit].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

Instance Method Summary collapse

Constructor Details

This class inherits a constructor from Kamaze::Project::Tools::Git::Hooks::BaseHook

Instance Method Details

#_process_index(index, options = {}) ⇒ Kamaze::Project::Tools::Git::Status::Index (protected)

Parameters:

Returns:

Raises:

  • (SystemExit)


63
64
65
66
67
68
69
70
71
72
73
74
75
76
# 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

#process_index(options = {}) {|Array<Kamaze::Project::Tools::Git::Status::File>| ... } ⇒ Object

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

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

Parameters:

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

Options Hash (options):

  • :allow_unsafe (Boolean)

    Unsafe index is processed (false)

  • :allow_empty (Boolean)

    Empty index is processed (false)

Yields:

Yield Returns:

  • (Fixnum)

Raises:

  • (SystemExit)


49
50
51
52
53
54
# 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