class Command

Attributes

args[RW]
cmd[RW]
known_options[RW]
options[RW]
unknown_options[RW]

Public Class Methods

new(args = []) click to toggle source
# File lib/git-utils/command.rb, line 8
def initialize(args = [])
  self.args = args
  self.options = OpenStruct.new
  parse
end
run!(command_class, args) click to toggle source

Runs a command. If the argument array contains ‘–debug’, returns the command that would have been run.

# File lib/git-utils/command.rb, line 83
def self.run!(command_class, args)
  debug = args.delete('--debug')
  command = command_class.new(args)
  if debug
    puts command.cmd
    return 1
  else
    command.run!
    return 0
  end
end

Public Instance Methods

current_branch() click to toggle source

Returns the current Git branch.

# File lib/git-utils/command.rb, line 25
def current_branch
  @current_branch ||= `git rev-parse --abbrev-ref HEAD`.strip
end
default_branch() click to toggle source

Returns the default branch for the current repository. Command retrieved from stackoverflow.com/questions/28666357/git-how-to-get-default-branch

# File lib/git-utils/command.rb, line 32
def default_branch
  branch_name = `git symbolic-ref --short refs/remotes/origin/HEAD \
                 | sed 's@^origin/@@'`.strip
  if branch_name.empty?
    $stderr.puts "Repository configuration error"
    $stderr.puts "Missing reference to refs/remotes/origin/HEAD"
    $stderr.puts "Run"
    $stderr.puts
    $stderr.puts "  git remote set-head origin <default branch>"
    $stderr.puts
    $stderr.puts "where <default branch> is the default branch name"
    $stderr.puts "(typically `main`, `master`, or `trunk`)"
    $stderr.puts "and then rerun the command"
    exit(1)
  end
  @default_branch ||= branch_name
end
origin_url() click to toggle source

Returns the URL for the remote origin.

# File lib/git-utils/command.rb, line 51
def origin_url
  @origin_url ||= `git config --get remote.origin.url`.strip
end
parse() click to toggle source
# File lib/git-utils/command.rb, line 14
def parse
  self.known_options   = Options::known_options(parser, args)
  self.unknown_options = Options::unknown_options(parser, args)
  parser.parse!(known_options)
end
parser() click to toggle source
# File lib/git-utils/command.rb, line 20
def parser
  OptionParser.new
end
protocol() click to toggle source

Returns the protocol of the origin URL (defaults to ssh).

# File lib/git-utils/command.rb, line 72
def protocol
  if origin_url =~ /https?:\/\//
    'http'
  else
    'ssh'
  end
end
run!() click to toggle source
# File lib/git-utils/command.rb, line 95
def run!
  system cmd
end
service() click to toggle source

Returns the name of the repository service. It’s currently GitHub, Bitbucket, or Stash. We return blank for an unknown service; the command will still often work in that case.

# File lib/git-utils/command.rb, line 59
def service
  if origin_url =~ /github/i
    'github'
  elsif origin_url =~ /bitbucket/i
    'bitbucket'
  elsif origin_url =~ /stash/i
    'stash'
  else
    ''
  end
end

Private Instance Methods

argument_string(args) click to toggle source

Returns an argument string based on given arguments. The main trick is to add in quotes for option arguments when necessary. For example, [‘-a’, ‘-m’, ‘foo bar’] becomes ‘-a -m “foo bar”’

# File lib/git-utils/command.rb, line 106
def argument_string(args)
  args.inject([]) do |opts, opt|
    opts << (opt =~ /^-/ ? opt : opt.inspect)
  end.join(' ')
end
deliver?() click to toggle source
# File lib/git-utils/command.rb, line 116
def deliver?
  options.deliver
end
finish?() click to toggle source
# File lib/git-utils/command.rb, line 112
def finish?
  options.finish
end