Class: Overcommit::HookRunner

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

Overview

Responsible for loading the hooks the repository has configured and running them, collecting and displaying the results.

Instance Method Summary collapse

Constructor Details

#initialize(config, logger, context, printer) ⇒ HookRunner

Returns a new instance of HookRunner.



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/overcommit/hook_runner.rb', line 11

def initialize(config, logger, context, printer)
  @config = config
  @log = logger
  @context = context
  @printer = printer
  @hooks = []

  @lock = Mutex.new
  @resource = ConditionVariable.new
  @slots_available = @config.concurrency
end

Instance Method Details

#runObject

Loads and runs the hooks registered for this Overcommit::HookRunner.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/overcommit/hook_runner.rb', line 24

def run
  # ASSUMPTION: we assume the setup and cleanup calls will never need to be
  # interrupted, i.e. they will finish quickly. Should further evidence
  # suggest this assumption does not hold, we will have to separately wrap
  # these calls to allow some sort of "are you sure?" double-interrupt
  # functionality, but until that's deemed necessary let's keep it simple.
  InterruptHandler.isolate_from_interrupts do
    # Load hooks before setting up the environment so that the repository
    # has not been touched yet. This way any load errors at this point don't
    # result in Overcommit leaving the repository in a bad state.
    load_hooks

    # Setup the environment without automatically calling
    # `cleanup_environment` on an error. This is because it's possible that
    # the `setup_environment` code did not fully complete, so there's no
    # guarantee that `cleanup_environment` will be able to accomplish
    # anything of value. The safest thing to do is therefore nothing in the
    # unlikely case of failure.
    @context.setup_environment

    begin
      run_hooks
    ensure
      @context.cleanup_environment
    end
  end
end