class Mercurial::Command

This class represents a shell command. You probably don't want to deal with this yourself, use the {Mercurial::Shell Shell} class instead.

Attributes

command[RW]
repository[RW]
timeout[RW]
use_cache[RW]

Public Class Methods

new(cmd, options={}) click to toggle source
# File lib/mercurial-ruby/command.rb, line 15
def initialize(cmd, options={})
  @command    = cmd
  @repository = options[:repository]
  @use_cache  = options[:cache].nil? || options[:cache] == false ? false : true
  @timeout    = options[:timeout] ? options[:timeout].to_i : global_execution_timeout.to_i
end

Public Instance Methods

execute() click to toggle source
# File lib/mercurial-ruby/command.rb, line 22
def execute
  if cache_commands?
    execute_with_caching
  else
    execute_without_caching
  end      
end

Private Instance Methods

cache_commands?() click to toggle source
# File lib/mercurial-ruby/command.rb, line 32
def cache_commands?
  repository && !repository.cache_disabled_by_override? && cache_store && use_cache
end
cache_key() click to toggle source
# File lib/mercurial-ruby/command.rb, line 79
def cache_key
  "hg.#{ repository.mtime }." + Digest::MD5.hexdigest(command)
end
cache_store() click to toggle source
# File lib/mercurial-ruby/command.rb, line 36
def cache_store
  Mercurial.configuration.cache_store
end
debug(msg) click to toggle source
# File lib/mercurial-ruby/command.rb, line 83
def debug(msg)
  if Mercurial.configuration.debug_mode
    puts msg
  end
end
execute_with_caching() click to toggle source
# File lib/mercurial-ruby/command.rb, line 44
def execute_with_caching
  cache_store.fetch(cache_key, &execution_proc)
end
execute_without_caching() click to toggle source
# File lib/mercurial-ruby/command.rb, line 48
def execute_without_caching
  execution_proc.call
end
execution_proc() click to toggle source
# File lib/mercurial-ruby/command.rb, line 52
def execution_proc
  Proc.new do
    debug(command)
    result, error, = '', ''
    status = Open4.popen4(command) do |pid, stdin, stdout, stderr|
      Timeout.timeout(timeout) do
        while tmp = stdout.read(102400)
          result += tmp
        end
      end

      while tmp = stderr.read(1024)
        error += tmp
      end
    end
    raise_error_if_needed(status, error)
    result
  end
end
global_execution_timeout() click to toggle source
# File lib/mercurial-ruby/command.rb, line 40
def global_execution_timeout
  Mercurial.configuration.shell_timeout
end
raise_error_if_needed(status, error) click to toggle source
# File lib/mercurial-ruby/command.rb, line 72
def raise_error_if_needed(status, error)
  return if status.exitstatus == 0
  if error && error != ''
    raise CommandError, error
  end
end