Class: Hiiro::Git::Branch
- Inherits:
-
Object
- Object
- Hiiro::Git::Branch
- Defined in:
- lib/hiiro/git/branch.rb
Instance Attribute Summary collapse
-
#head ⇒ Object
readonly
Returns the value of attribute head.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#ref ⇒ Object
readonly
Returns the value of attribute ref.
-
#upstream ⇒ Object
readonly
Returns the value of attribute upstream.
Class Method Summary collapse
Instance Method Summary collapse
- #checkout ⇒ Object
- #current? ⇒ Boolean
- #delete(force: false) ⇒ Object
- #exists? ⇒ Boolean
-
#initialize(name:, ref: nil, upstream: nil, head: nil, current: false) ⇒ Branch
constructor
A new instance of Branch.
- #local? ⇒ Boolean
- #remote? ⇒ Boolean
- #to_h ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(name:, ref: nil, upstream: nil, head: nil, current: false) ⇒ Branch
Returns a new instance of Branch.
17 18 19 20 21 22 23 |
# File 'lib/hiiro/git/branch.rb', line 17 def initialize(name:, ref: nil, upstream: nil, head: nil, current: false) @name = name @ref = ref || "refs/heads/#{name}" @upstream = upstream @head = head @current = current end |
Instance Attribute Details
#head ⇒ Object (readonly)
Returns the value of attribute head.
4 5 6 |
# File 'lib/hiiro/git/branch.rb', line 4 def head @head end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
4 5 6 |
# File 'lib/hiiro/git/branch.rb', line 4 def name @name end |
#ref ⇒ Object (readonly)
Returns the value of attribute ref.
4 5 6 |
# File 'lib/hiiro/git/branch.rb', line 4 def ref @ref end |
#upstream ⇒ Object (readonly)
Returns the value of attribute upstream.
4 5 6 |
# File 'lib/hiiro/git/branch.rb', line 4 def upstream @upstream end |
Class Method Details
.current ⇒ Object
11 12 13 14 15 |
# File 'lib/hiiro/git/branch.rb', line 11 def self.current name = `git branch --show-current 2>/dev/null`.strip return nil if name.empty? new(name: name, current: true) end |
.from_format_line(line, format: :short) ⇒ Object
6 7 8 9 |
# File 'lib/hiiro/git/branch.rb', line 6 def self.from_format_line(line, format: :short) # Parses output from git branch --format new(name: line.strip) end |
Instance Method Details
#checkout ⇒ Object
37 38 39 |
# File 'lib/hiiro/git/branch.rb', line 37 def checkout system('git', 'checkout', name) end |
#current? ⇒ Boolean
25 26 27 |
# File 'lib/hiiro/git/branch.rb', line 25 def current? @current end |
#delete(force: false) ⇒ Object
41 42 43 44 |
# File 'lib/hiiro/git/branch.rb', line 41 def delete(force: false) flag = force ? '-D' : '-d' system('git', 'branch', flag, name) end |
#exists? ⇒ Boolean
46 47 48 |
# File 'lib/hiiro/git/branch.rb', line 46 def exists? system('git', 'show-ref', '--verify', '--quiet', ref) end |
#local? ⇒ Boolean
29 30 31 |
# File 'lib/hiiro/git/branch.rb', line 29 def local? ref.start_with?('refs/heads/') end |
#remote? ⇒ Boolean
33 34 35 |
# File 'lib/hiiro/git/branch.rb', line 33 def remote? ref.start_with?('refs/remotes/') end |
#to_h ⇒ Object
54 55 56 57 58 59 60 61 62 |
# File 'lib/hiiro/git/branch.rb', line 54 def to_h { name: name, ref: ref, upstream: upstream, head: head, current: current?, }.compact end |
#to_s ⇒ Object
50 51 52 |
# File 'lib/hiiro/git/branch.rb', line 50 def to_s name end |