Class: GitHooks::Runner

Inherits:
Object show all
Defined in:
lib/githooks/runner.rb

Overview

rubocop:disable Metrics/ClassLength

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Runner

rubocop:disable Metrics/AbcSize



34
35
36
37
38
39
40
41
42
43
# File 'lib/githooks/runner.rb', line 34

def initialize(options = {}) # rubocop:disable Metrics/AbcSize
  @repo_path  = Pathname.new(options.delete('repo') || Dir.getwd)
  @repository = Repository.new(@repo_path)
  @hook_path  = acquire_hooks_path(options.delete('hooks-path') || @repository.config.hooks_path || @repository.path)
  @script     = options.delete('script') || @repository.hooks_script
  @options    = IndifferentAccessOpenStruct.new(options)

  GitHooks.verbose = !!ENV['GITHOOKS_VERBOSE']
  GitHooks.debug   = !!ENV['GITHOOKS_DEBUG']
end

Instance Method Details

#attachObject



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/githooks/runner.rb', line 78

def attach
  entry_path   = Pathname.new(script || hook_path).realdirpath
  hook_phases  = options.hooks || Hook::VALID_PHASES
  bootstrapper = Pathname.new(options.bootstrap).realpath if options.bootstrap

  if entry_path.directory?
    if repository.hooks_path
      fail Error::AlreadyAttached, "Repository [#{repo_path}] already attached to hook path #{repository.hooks_path} - Detach to continue."
    end
    repository.config.set('hooks-path', entry_path)
  elsif entry_path.executable?
    if repository.hooks_script
      fail Error::AlreadyAttached, "Repository [#{repo_path}] already attached to script #{repository.hooks_script}. Detach to continue."
    end
    repository.config.set('script', entry_path)
  else
    fail ArgumentError, "Provided path '#{entry_path}' is neither a directory nor an executable file."
  end

  gitrunner = bootstrapper
  gitrunner ||= SystemUtils.which('githooks-runner')
  gitrunner ||= (GitHooks::BIN_PATH + 'githooks-runner').realpath

  hook_phases.each do |hook|
    hook = (@repository.hooks + hook).to_s
    puts "Linking #{gitrunner} -> #{hook}" if GitHooks.verbose
    FileUtils.ln_sf gitrunner.to_s, hook
  end
end

#detach(hook_phases = nil) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/githooks/runner.rb', line 108

def detach(hook_phases = nil)
  (hook_phases || Hook::VALID_PHASES).each do |hook|
    next unless (repo_hook = (@repository.hooks + hook)).symlink?
    puts "Removing hook '#{hook}' from repository at: #{repository.path}" if GitHooks.verbose
    FileUtils.rm_f repo_hook
  end

  active_hooks = Hook::VALID_PHASES.select { |hook| (@repository.hooks + hook).exist? }

  if active_hooks.empty?
    puts 'All hooks detached. Removing configuration section.'
    repository.config.remove_section(repo_path: repository.path)
  else
    puts "Keeping configuration for active hooks: #{active_hooks.join(', ')}"
  end
end

#listObject



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/githooks/runner.rb', line 125

def list
  unless script || hook_path
    fail Error::NotAttached, 'Repository currently not configured. Usage attach to setup for use with githooks.'
  end

  if (executables = repository.config.pre_run_execute).size > 0
    puts 'PreRun Executables (in execution order):'
    puts executables.collect { |exe| "  #{exe}" }.join("\n")
    puts
  end

  if script
    puts 'Main Test Script:'
    puts "  #{script}"
    puts
  end

  if hook_path
    puts 'Main Testing Library with Tests (in execution order):'
    puts '  Tests loaded from:'
    puts "    #{hook_path}"
    puts

    SystemUtils.quiet { load_tests(true) }

    %w{ pre-commit commit-msg }.each do |phase|
      next unless Hook.phases[phase]

      puts "  Phase #{phase.camelize}:"
      Hook.phases[phase].sections.each_with_index do |section, section_index|
        printf "    %3d: %s\n", section_index + 1, section.title
        section.actions.each_with_index do |action, action_index|
          printf "      %3d: %s\n", action_index + 1, action.title
          action.limiters.each_with_index do |limiter, limiter_index|
            type, value = limiter.type.inspect, limiter.only
            value = value.first if value.size == 1
            printf "          Limiter %d: %s -> %s\n", limiter_index + 1, type, value.inspect
          end
        end
      end
    end

    puts
  end

  if (executables = repository.config.post_run_execute).size > 0
    puts 'PostRun Executables (in execution order):'
    executables.each do |exe|
      puts "  #{exe}"
    end
    puts
  end
rescue Error::NotAGitRepo
  puts "Unable to find a valid git repo in #{repository}."
  puts 'Please specify path to repo via --repo <path>' if GitHooks::SCRIPT_NAME == 'githooks'
  raise
end

#runObject

rubocop:disable CyclomaticComplexity, MethodLength, AbcSize, PerceivedComplexity



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/githooks/runner.rb', line 46

def run
  options.staged = options.staged.nil? ? true : options.staged

  if options.skip_pre
    puts 'Skipping PreRun Executables'
  else
    run_externals('pre-run-execute')
  end

  if script && !(options.ignore_script || GitHooks.ignore_script)
    command = "#{script} #{Pathname.new($0)} #{Shellwords.join(ARGV)};"
    puts "Kernel#exec(#{command.inspect})" if GitHooks.verbose
    exec(command)
  elsif hook_path
    load_tests && start
  else
    puts %q"I can't figure out what to run! Specify either path or script to give me a hint..."
  end

  if options.skip_post
    puts 'Skipping PostRun Executables'
  else
    run_externals('post-run-execute')
  end
rescue SystemStackError => e
  puts "#{e.class.name}: #{e.message}\n\t#{e.backtrace.join("\n\t")}"
rescue GitHooks::Error::NotAGitRepo => e
  puts "Unable to find a valid git repo in #{repository}."
  puts 'Please specify path to repository via --repo <path>' if GitHooks::SCRIPT_NAME == 'githooks'
  raise e
end