class Hub::Context::GitReader

Shells out to git to get output of its commands

Attributes

executable[R]

Public Class Methods

new(executable = nil, &read_proc) click to toggle source
# File lib/hub/context.rb, line 17
def initialize(executable = nil, &read_proc)
  @executable = executable || 'git'
  # caches output when shelling out to git
  read_proc ||= lambda { |cache, cmd|
    str = command_to_string(cmd)
    result = silence_stderr { %x{#{str}}.chomp }
    cache[cmd] = $?.success? && !result.empty? ? result : nil
  }
  @cache = Hash.new(&read_proc)
end

Public Instance Methods

add_exec_flags(flags) click to toggle source
# File lib/hub/context.rb, line 28
def add_exec_flags(flags)
  @executable = Array(executable).concat(flags)
end
read(cmd) click to toggle source
# File lib/hub/context.rb, line 38
def read(cmd)
  @cache[cmd]
end
read_config(cmd, all = false) click to toggle source
# File lib/hub/context.rb, line 32
def read_config(cmd, all = false)
  config_cmd = ['config', (all ? '--get-all' : '--get'), *cmd]
  config_cmd = config_cmd.join(' ') unless cmd.respond_to? :join
  read config_cmd
end
stub!(values) click to toggle source
# File lib/hub/context.rb, line 50
def stub!(values)
  @cache.update values
end
stub_command_output(cmd, value) click to toggle source
# File lib/hub/context.rb, line 46
def stub_command_output(cmd, value)
  @cache[cmd] = value.nil? ? nil : value.to_s
end
stub_config_value(key, value, get = '--get') click to toggle source
# File lib/hub/context.rb, line 42
def stub_config_value(key, value, get = '--get')
  stub_command_output "config #{get} #{key}", value
end

Private Instance Methods

command_to_string(cmd) click to toggle source
# File lib/hub/context.rb, line 61
def command_to_string(cmd)
  full_cmd = to_exec(cmd)
  full_cmd.respond_to?(:shelljoin) ? full_cmd.shelljoin : full_cmd.join(' ')
end
silence_stderr() { || ... } click to toggle source
# File lib/hub/context.rb, line 66
def silence_stderr
  oldio = STDERR.dup
  STDERR.reopen(NULL)
  yield
ensure
  STDERR.reopen(oldio)
end
to_exec(args) click to toggle source
# File lib/hub/context.rb, line 56
def to_exec(args)
  args = Shellwords.shellwords(args) if args.respond_to? :to_str
  Array(executable) + Array(args)
end