Class: Command

Inherits:
Object
  • Object
show all
Defined in:
lib/git-utils/command.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = []) ⇒ Command

Returns a new instance of Command.



8
9
10
11
12
# File 'lib/git-utils/command.rb', line 8

def initialize(args = [])
  self.args = args
  self.options = OpenStruct.new
  parse
end

Instance Attribute Details

#argsObject

Returns the value of attribute args.



6
7
8
# File 'lib/git-utils/command.rb', line 6

def args
  @args
end

#cmdObject

Returns the value of attribute cmd.



6
7
8
# File 'lib/git-utils/command.rb', line 6

def cmd
  @cmd
end

#known_optionsObject

Returns the value of attribute known_options.



6
7
8
# File 'lib/git-utils/command.rb', line 6

def known_options
  @known_options
end

#optionsObject

Returns the value of attribute options.



6
7
8
# File 'lib/git-utils/command.rb', line 6

def options
  @options
end

#unknown_optionsObject

Returns the value of attribute unknown_options.



6
7
8
# File 'lib/git-utils/command.rb', line 6

def unknown_options
  @unknown_options
end

Class Method Details

.run!(command_class, args) ⇒ Object

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



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/git-utils/command.rb', line 64

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

Instance Method Details

#current_branchObject

Returns the current Git branch.



25
26
27
# File 'lib/git-utils/command.rb', line 25

def current_branch
  @current_branch ||= `git rev-parse --abbrev-ref HEAD`.strip
end

#origin_urlObject

Returns the URL for the remote origin.



30
31
32
# File 'lib/git-utils/command.rb', line 30

def origin_url
  @origin_url ||= `git config --get remote.origin.url`.strip
end

#parseObject



14
15
16
17
18
# 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

#parserObject



20
21
22
# File 'lib/git-utils/command.rb', line 20

def parser
  OptionParser.new
end

#protocolObject

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



53
54
55
56
57
58
59
# File 'lib/git-utils/command.rb', line 53

def protocol
  if origin_url =~ /https?:\/\//
    'http'
  else
    'ssh'
  end
end

#run!Object



76
77
78
# File 'lib/git-utils/command.rb', line 76

def run!
  system cmd
end

#serviceObject

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.



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/git-utils/command.rb', line 40

def service
  if origin_url =~ /github/i
    'github'
  elsif origin_url =~ /bitbucket/i
    'bitbucket'
  elsif origin_url =~ /stash/i
    'stash'
  else
    ''
  end
end