module GitFlow

Constants

HELP

Attributes

program[RW]
should_run[RW]
trace[RW]

Public Instance Methods

/(command_name) click to toggle source

Return a class to be extended in order to register a GitFlow command if command name is nil, it will be registered as the top level command. Classes implementing commands also provide this method, allowing for sub-command creation.

# File lib/git_bpf/lib/gitflow.rb, line 140
def /(command_name)
  command_name = command_name.to_s unless command_name.nil?
  cls = Class.new { include GitFlow::Mixin }
  (class << cls; self; end).module_eval do
    attr_accessor :help, :documentation, :command
    define_method(:/) do |subcommand|
      raise "Subcommand cannot be nil" unless subcommand
      GitFlow/([command_name, subcommand].compact.join(' '))
    end
    define_method(:inherited) do |subclass|
      subclass.command = command_name
      GitFlow.commands[command_name] = subclass
    end
  end
  cls
end
command(argv) click to toggle source
# File lib/git_bpf/lib/gitflow.rb, line 173
def command(argv)
  cmds = []
  argv.each_with_index do |arg, i|
    arg = argv[0..i].join(' ')
    cmds << commands[arg] if commands.key?(arg)
  end
  cmds.last || commands[nil]
end
commands() click to toggle source
# File lib/git_bpf/lib/gitflow.rb, line 157
def commands
  @commands ||= Hash.new
end
optparse() click to toggle source
# File lib/git_bpf/lib/gitflow.rb, line 161
def optparse
  optparse = opt = OptionParser.new
  opt.separator ' '
  opt.separator 'OPTIONS'
  opt.separator ' '
  opt.on('-h', '--help', 'Display this help') do
    GitFlow.pager; puts opt; throw :exit
  end
  opt.on('--trace', 'Display traces') { GitFlow.trace = true }
  optparse
end
pager() click to toggle source

Pager from nex-3.com/posts/73-git-style-automatic-paging-in-ruby

# File lib/git_bpf/lib/gitflow.rb, line 110
def pager
  return if RUBY_PLATFORM =~ /win32/
  return unless STDOUT.tty?

  read, write = IO.pipe

  unless Kernel.fork # Child process
    STDOUT.reopen(write)
    STDERR.reopen(write) if STDERR.tty?
    read.close
    write.close
    return
  end

  # Parent process, become pager
  STDIN.reopen(read)
  read.close
  write.close

  ENV['LESS'] = 'FSRX' # Don't page if the input is short enough

  Kernel.select [STDIN] # Wait until we have input before we start the pager
  pager = ENV['PAGER'] || 'less'
  exec pager rescue exec '/bin/sh', '-c', pager
end
run(*argv) click to toggle source
# File lib/git_bpf/lib/gitflow.rb, line 182
def run(*argv)
  catch :exit do
    command = self.command(argv).new
    argv = argv[command.class.command.split.length..-1] if command.class.command
    parser = optparse
    parser.banner = "Usage: #{GitFlow.program} #{command.class.command} [options]"
    options = OpenStruct.new
    if command.respond_to?(:options)
      command.options(options).each { |args| parser.on(*args) }
    end
    if command.class.documentation && command.class.documentation != ''
      parser.separator ' '
      parser.separator command.class.documentation.split(/\n/)
    end
    parser.parse!(argv)
    command.execute(options, argv)
  end
end