class Hub::Runner

The Hub runner expects to be initialized with `ARGV` and primarily exists to run a git command.

The actual functionality, that is, the code it runs when it needs to augment a git command, is kept in the `Hub::Commands` module.

Attributes

args[R]

Public Class Methods

execute(*args) click to toggle source

Shortcut

# File lib/hub/runner.rb, line 16
def self.execute(*args)
  new(*args).execute
end
new(*args) click to toggle source
# File lib/hub/runner.rb, line 10
def initialize(*args)
  @args = Args.new(args)
  Commands.run(@args)
end

Public Instance Methods

command() click to toggle source

A string representation of the command that would run.

# File lib/hub/runner.rb, line 21
def command
  if args.skip?
    ''
  else
    commands.join('; ')
  end
end
commands() click to toggle source

An array of all commands as strings.

# File lib/hub/runner.rb, line 30
def commands
  args.commands.map do |cmd|
    if cmd.respond_to?(:join)
      # a simplified `Shellwords.join` but it's OK since this is only used to inspect
      cmd.map { |arg| arg = arg.to_s; (arg.index(' ') || arg.empty?) ? "'#{arg}'" : arg }.join(' ')
    else
      cmd.to_s
    end
  end
end
exec(*args) click to toggle source

Special-case `echo` for Windows

Calls superclass method
# File lib/hub/runner.rb, line 70
def exec *args
  if args.first == 'echo' && Context::windows?
    puts args[1..-1].join(' ')
  else
    super
  end
end
execute() click to toggle source

Runs the target git command with an optional callback. Replaces the current process.

If `args` is empty, this will skip calling the git command. This allows commands to print an error message and cancel their own execution if they don't make sense.

# File lib/hub/runner.rb, line 47
def execute
  if args.noop?
    puts commands
  elsif not args.skip?
    execute_command_chain args.commands
  end
end
execute_command_chain(commands) click to toggle source

Runs multiple commands in succession; exits at first failure.

# File lib/hub/runner.rb, line 56
def execute_command_chain commands
  commands.each_with_index do |cmd, i|
    if cmd.respond_to?(:call) then cmd.call
    elsif i == commands.length - 1
      # last command in chain
      STDOUT.flush; STDERR.flush
      exec(*cmd)
    else
      exit($?.exitstatus) unless system(*cmd)
    end
  end
end