Class: Hub::Context::GitReader

Inherits:
Object
  • Object
show all
Defined in:
lib/hub/context.rb

Overview

Shells out to git to get output of its commands

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(executable = nil, &read_proc) ⇒ GitReader

Returns a new instance of GitReader.



17
18
19
20
21
22
23
24
25
26
# 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

Instance Attribute Details

#executableObject (readonly)

Returns the value of attribute executable.



15
16
17
# File 'lib/hub/context.rb', line 15

def executable
  @executable
end

Instance Method Details

#add_exec_flags(flags) ⇒ Object



28
29
30
# File 'lib/hub/context.rb', line 28

def add_exec_flags(flags)
  @executable = Array(executable).concat(flags)
end

#read(cmd) ⇒ Object



38
39
40
# File 'lib/hub/context.rb', line 38

def read(cmd)
  @cache[cmd]
end

#read_config(cmd, all = false) ⇒ Object



32
33
34
35
36
# 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) ⇒ Object



50
51
52
# File 'lib/hub/context.rb', line 50

def stub!(values)
  @cache.update values
end

#stub_command_output(cmd, value) ⇒ Object



46
47
48
# 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') ⇒ Object



42
43
44
# File 'lib/hub/context.rb', line 42

def stub_config_value(key, value, get = '--get')
  stub_command_output "config #{get} #{key}", value
end