Class: Overcommit::CommandSplitter

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

Overview

Distributes a list of arguments over multiple invocations of a command.

This accomplishes the same functionality provided by `xargs` but in a cross-platform way that does not require any pre-existing tools.

One of the tradeoffs with this approach is that we no longer deal with a single exit status from a command, but multiple (one for each invocation).

This will return a struct similar to `Subprocess::Result` but with additional `statuses`, `stdouts`, and `stderrs` fields so hook authors can actually see the results of each invocation. If they don't care, the standard `status`, `stdout`, and `stderr` will still work but be a aggregation/concatenation of all statuses/outputs.

Defined Under Namespace

Classes: Result

Class Method Summary collapse

Class Method Details

.execute(initial_args, options) ⇒ Object


54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/overcommit/command_splitter.rb', line 54

def execute(initial_args, options)
  options = options.dup

  if (splittable_args = (options.delete(:args) { [] })).empty?
    raise Overcommit::Exceptions::InvalidCommandArgs,
          'Must specify list of arguments to split on'
  end

  # Execute each chunk of arguments in serial. We don't parallelize (yet)
  # since in theory we want to support parallelization at the hook level
  # and not within individual hooks.
  results = extract_argument_lists(initial_args, splittable_args).map do |arg_list|
    Overcommit::Subprocess.spawn(arg_list, options)
  end

  Result.new(results.map(&:status), results.map(&:stdout), results.map(&:stderr))
end