Method: Overcommit::Utils.execute

Defined in:
lib/overcommit/utils.rb

.execute(initial_args, options = {}) ⇒ Overcommit::Subprocess::Result

Execute a command in a subprocess, capturing exit status and output from both standard and error streams.

This is intended to provide a centralized place to perform any checks or filtering of the command before executing it.

The args option provides a convenient way of splitting up long argument lists which would otherwise exceed the maximum command line length of the OS. It will break up the list into chunks and run the command with the same prefix initial_args, finally combining the output together at the end.

This requires that the external command you are running can have its work split up in this way and still produce the same resultant output when outputs of the individual commands are concatenated back together.

Parameters:

  • initial_args (Array<String>)
  • options (Hash) (defaults to: {})

Options Hash (options):

  • :args (Array<String>)

    long list of arguments to split up

Returns:



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/overcommit/utils.rb', line 175

def execute(initial_args, options = {})
  if initial_args.include?('|')
    raise Overcommit::Exceptions::InvalidCommandArgs,
          'Cannot pipe commands with the `execute` helper'
  end

  result =
    if (splittable_args = options.fetch(:args) { [] }).any?
      debug(initial_args.join(' ') + " ... (#{splittable_args.length} splittable args)")
      Overcommit::CommandSplitter.execute(initial_args, options)
    else
      debug(initial_args.join(' '))
      Overcommit::Subprocess.spawn(initial_args, options)
    end

  debug("EXIT STATUS: #{result.status}")
  debug("STDOUT: #{result.stdout.inspect}")
  debug("STDERR: #{result.stderr.inspect}")

  result
end