class Git::CommandLine


@api public
Runs a git command and returns the result

def build_git_cmd(args)

Other tags:
    Api: - private

Returns:
  • (Array) -
def build_git_cmd(args)
  raise ArgumentError.new('The args array can not contain an array') if args.any? { |a| a.is_a?(Array) }
  [binary_path, *global_opts, *args].map { |e| e.to_s }
end

def initialize(env, binary_path, global_opts, logger)

Parameters:
  • logger (Logger) -- the logger to use
  • global_opts (Array) -- global options to pass to git
  • env (Hash) -- environment variables to set
def initialize(env, binary_path, global_opts, logger)
  @env = env
  @binary_path = binary_path
  @global_opts = global_opts
  @logger = logger
end

def post_process(raw_output, normalize, chomp)

Other tags:
    Api: - private

Returns:
  • (String, nil) -

Parameters:
  • raw_output (#string) -- the output to post-process
def post_process(raw_output, normalize, chomp)
  if raw_output.respond_to?(:string)
    output = raw_output.string.dup
    output = output.lines.map { |l| Git::EncodingUtils.normalize_encoding(l) }.join if normalize
    output.chomp! if chomp
    output
  else
    nil
  end
end

def post_process_all(raw_outputs, normalize, chomp)

Other tags:
    Api: - private

Returns:
  • (Array) - the processed output of each command output object that supports `#string`

Parameters:
  • chomp (Boolean) -- whether to chomp the output of each writer
  • normalize (Boolean) -- whether to normalize the output of each writer
  • raw_outputs (Array) -- the output to post-process
def post_process_all(raw_outputs, normalize, chomp)
  Array.new.tap do |result|
    raw_outputs.each { |raw_output| result << post_process(raw_output, normalize, chomp) }
  end
end

def process_result(result, normalize, chomp, timeout)

Other tags:
    Api: - private

Raises:
  • (Git::ProcessIOError) - if an exception was raised while collecting subprocess output
  • (Git::TimeoutError) - if the command times out
  • (Git::SignaledError) - if the command was signaled
  • (Git::FailedError) - if the command failed

Returns:
  • (Git::CommandLineResult) - the result of the command to return to the caller

Parameters:
  • timeout (Numeric, nil) -- the maximum seconds to wait for the command to complete
  • chomp (Boolean) -- whether to chomp the output of each writer
  • normalize (Boolean) -- whether to normalize the output of each writer
  • result (ProcessExecuter::Command::Result) -- the result it is a Process::Status and include command, stdout, and stderr
def process_result(result, normalize, chomp, timeout)
  command = result.command
  processed_out, processed_err = post_process_all([result.stdout, result.stderr], normalize, chomp)
  logger.info { "#{command} exited with status #{result}" }
  logger.debug { "stdout:\n#{processed_out.inspect}\nstderr:\n#{processed_err.inspect}" }
  Git::CommandLineResult.new(command, result, processed_out, processed_err).tap do |processed_result|
    raise Git::TimeoutError.new(processed_result, timeout) if result.timeout?
    raise Git::SignaledError.new(processed_result) if result.signaled?
    raise Git::FailedError.new(processed_result) unless result.success?
  end
end

def run(*args, out: nil, err: nil, normalize:, chomp:, merge:, chdir: nil, timeout: nil)

Raises:
  • (Git::TimeoutError) - if the command times out
  • (Git::ProcessIOError) - if an exception was raised while collecting subprocess output
  • (Git::FailedError) - if the command returned a non-zero exitstatus
  • (Git::SignaledError) - if the command was terminated because of an uncaught signal
  • (ArgumentError) - if `args` is not an array of strings

Returns:
  • (Git::CommandLineResult) - the output of the command

Parameters:
  • timeout (Numeric, nil) -- the maximum seconds to wait for the command to complete
  • chdir (String) -- the directory to run the command in
  • merge (Boolean) -- whether to merge stdout and stderr in the string returned
  • chomp (Boolean) -- whether to chomp the output
  • normalize (Boolean) -- whether to normalize the output to a valid encoding
  • err (#write) -- the object to write stderr to or nil to ignore stderr
  • out (#write, nil) -- the object to write stdout to or nil to ignore stdout
  • args (Array) -- the command line arguements to pass to git

Other tags:
    Example: Capture stderr in a StringIO object -
    Example: Capture stdout in a temporary file -
    Example: Run a command and without normalizing the output -
    Example: Run a command and return the chomped output -
    Example: The args array should be splatted into the parameter list -
    Example: Run a command and return the output -
def run(*args, out: nil, err: nil, normalize:, chomp:, merge:, chdir: nil, timeout: nil)
  git_cmd = build_git_cmd(args)
  begin
    result = ProcessExecuter.run(env, *git_cmd, out: out, err: err, merge:, chdir: (chdir || :not_set), timeout: timeout, raise_errors: false)
  rescue ProcessExecuter::Command::ProcessIOError => e
    raise Git::ProcessIOError.new(e.message), cause: e.exception.cause
  end
  process_result(result, normalize, chomp, timeout)
end