Class: Mercurial::Shell

Inherits:
Object
  • Object
show all
Defined in:
lib/mercurial-ruby/shell.rb

Overview

This class makes it easy for you to execute shell commands. Use it to execute hg commands that don’t have proper wrappers yet.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(repository) ⇒ Shell

Returns a new instance of Shell.



79
80
81
# File 'lib/mercurial-ruby/shell.rb', line 79

def initialize(repository)
  @repository = repository
end

Instance Attribute Details

#repositoryObject (readonly)

Returns the value of attribute repository.



9
10
11
# File 'lib/mercurial-ruby/shell.rb', line 9

def repository
  @repository
end

Class Method Details

.run(cmd, options = {}) ⇒ Object

Creates a Command instance and executes it. Supports a few neat options:

:append_hg

You don’t have to worry about specifying a correct path to your hg binary every time you want to execute a command:

Shell.run("help", :append_hg => true)

Arguments interpolation

Interpolation make your commands secure by wrapping everything in single quotes and sanitizing them:

Shell.run(["clone ? ?", url, destination], :append_hg => true)

:in

Allows you to execute commands in a specific directory:

Shell.run('log', :in => repository_path)

:pipe

Gives you piping flexibility:

Shell.run('log', :pipe => "grep '9:0f41dd2ec166' -wc", :in => repository_path, :append_hg => true)

Same as running hg log | grep ‘9:0f41dd2ec166’ -wc

:timeout

Specify execution timeout in seconds for your command:

Shell.run("/usr/bin/long-running-task", :timeout => 5)

Timeout::Error will be raised on timeout.

:cache

Caching is enabled by default for all commands. Use this setting to avoid it:

Shell.run("init", :append_hg => true, :cache => false)


54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/mercurial-ruby/shell.rb', line 54

def self.run(cmd, options={})
  build = []

  if options[:append_hg]
    cmd = append_command_with(hg_binary_path, cmd)
  end

  if dir = options.delete(:in)
    build << interpolate_arguments(["cd ?", dir])
  end
  
  if cmd.kind_of?(Array)
    cmd = interpolate_arguments(cmd)
  end

  build << cmd
  to_run = build.join(' && ')

  if pipe_cmd = options[:pipe]
    to_run << " | #{ pipe_cmd }"
  end

  Mercurial::Command.new(to_run, options).execute
end

Instance Method Details

#hg(cmd, options = {}) ⇒ Object

This method is a shortcut to Shell.run(cmd, :append_hg => true, :in => repository.path).



86
87
88
89
90
91
# File 'lib/mercurial-ruby/shell.rb', line 86

def hg(cmd, options={})
  options[:in] ||= repository.path
  options[:repository] = repository
  options[:append_hg] = true
  run(cmd, options)
end

#run(cmd, options = {}) ⇒ Object



93
94
95
# File 'lib/mercurial-ruby/shell.rb', line 93

def run(cmd, options={})
  self.class.run(cmd, options)
end