Class: Command
- Inherits:
-
Object
- Object
- Command
- Defined in:
- lib/git-utils/command.rb
Direct Known Subclasses
DeleteRemoteBranch, MergeBranch, Open, PullRequest, PushBranch, Switch, Sync, SyncFork
Instance Attribute Summary collapse
-
#args ⇒ Object
Returns the value of attribute args.
-
#cmd ⇒ Object
Returns the value of attribute cmd.
-
#known_options ⇒ Object
Returns the value of attribute known_options.
-
#options ⇒ Object
Returns the value of attribute options.
-
#unknown_options ⇒ Object
Returns the value of attribute unknown_options.
Class Method Summary collapse
-
.run!(command_class, args) ⇒ Object
Runs a command.
Instance Method Summary collapse
-
#current_branch ⇒ Object
Returns the current Git branch.
-
#default_branch ⇒ Object
Returns the default branch for the current repository.
-
#initialize(args = []) ⇒ Command
constructor
A new instance of Command.
-
#origin_url ⇒ Object
Returns the URL for the remote origin.
- #parse ⇒ Object
- #parser ⇒ Object
-
#protocol ⇒ Object
Returns the protocol of the origin URL (defaults to ssh).
- #run! ⇒ Object
-
#service ⇒ Object
Returns the name of the repository service.
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. = OpenStruct.new parse end |
Instance Attribute Details
#args ⇒ Object
Returns the value of attribute args.
6 7 8 |
# File 'lib/git-utils/command.rb', line 6 def args @args end |
#cmd ⇒ Object
Returns the value of attribute cmd.
6 7 8 |
# File 'lib/git-utils/command.rb', line 6 def cmd @cmd end |
#known_options ⇒ Object
Returns the value of attribute known_options.
6 7 8 |
# File 'lib/git-utils/command.rb', line 6 def @known_options end |
#options ⇒ Object
Returns the value of attribute options.
6 7 8 |
# File 'lib/git-utils/command.rb', line 6 def @options end |
#unknown_options ⇒ Object
Returns the value of attribute unknown_options.
6 7 8 |
# File 'lib/git-utils/command.rb', line 6 def @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.
83 84 85 86 87 88 89 90 91 92 93 |
# 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 |
Instance Method Details
#current_branch ⇒ Object
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 |
#default_branch ⇒ Object
Returns the default branch for the current repository. Command retrieved from stackoverflow.com/questions/28666357/git-how-to-get-default-branch
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# 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 ⇒ Object
Returns the URL for the remote origin.
51 52 53 |
# File 'lib/git-utils/command.rb', line 51 def origin_url @origin_url ||= `git config --get remote.origin.url`.strip end |
#parse ⇒ Object
14 15 16 17 18 |
# File 'lib/git-utils/command.rb', line 14 def parse self. = Options::(parser, args) self. = Options::(parser, args) parser.parse!() end |
#parser ⇒ Object
20 21 22 |
# File 'lib/git-utils/command.rb', line 20 def parser OptionParser.new end |
#protocol ⇒ Object
Returns the protocol of the origin URL (defaults to ssh).
72 73 74 75 76 77 78 |
# File 'lib/git-utils/command.rb', line 72 def protocol if origin_url =~ /https?:\/\// 'http' else 'ssh' end end |
#run! ⇒ Object
95 96 97 |
# File 'lib/git-utils/command.rb', line 95 def run! system cmd end |
#service ⇒ Object
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.
59 60 61 62 63 64 65 66 67 68 69 |
# 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 |