Class: RightGit::Git::Branch
- Inherits:
-
Object
- Object
- RightGit::Git::Branch
- Defined in:
- lib/right_git/git/branch.rb
Overview
A branch in a Git repository. Has some proxy methods that make it act a bit like a string, whose value is the name of the branch. This allows branches to be sorted, matched against Regexp, and certain other string-y operations.
Defined Under Namespace
Classes: BranchError
Constant Summary collapse
- BRANCH_NAME =
'[#A-Za-z0-9._\/-]+'- BRANCH_INFO =
/^(\* | )?(#{BRANCH_NAME})( -> #{BRANCH_NAME})?$/- BRANCH_FULLNAME =
/(remotes\/)?(#{BRANCH_NAME})/- DEFAULT_DISPLAY_WIDTH =
40- ELLIPSIS =
'...'
Instance Attribute Summary collapse
-
#fullname ⇒ Object
readonly
Returns the value of attribute fullname.
-
#repo ⇒ Object
readonly
Returns the value of attribute repo.
Instance Method Summary collapse
-
#<=>(other) ⇒ Integer
Comparison value.
-
#==(other) ⇒ TrueClass|FalseClass
True if equivalent.
-
#=~(other) ⇒ Integer
Match offset.
-
#delete ⇒ TrueClass
Deletes this (local or remote) branch.
-
#display(width = DEFAULT_DISPLAY_WIDTH) ⇒ String
For display in a column of given width.
-
#initialize(repo, line) ⇒ Branch
constructor
A new instance of Branch.
-
#name ⇒ String
Name of branch sans origin (if any).
-
#remote? ⇒ TrueClass|FalseClass
True if branch is remote.
-
#to_s ⇒ String
(also: #inspect)
Stringized.
Constructor Details
#initialize(repo, line) ⇒ Branch
Returns a new instance of Branch.
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/right_git/git/branch.rb', line 46 def initialize(repo, line) match = BRANCH_INFO.match(line) if match && (fullname = match[2]) match = BRANCH_FULLNAME.match(fullname) if match @fullname = match[2] @remote = !!(match[1] || fullname.index('/')) @repo = repo else raise BranchError, 'Unreachable due to already matching name pattern' end else raise BranchError, "Unrecognized branch info: #{line.inspect}" end end |
Instance Attribute Details
#fullname ⇒ Object (readonly)
Returns the value of attribute fullname.
42 43 44 |
# File 'lib/right_git/git/branch.rb', line 42 def fullname @fullname end |
#repo ⇒ Object (readonly)
Returns the value of attribute repo.
42 43 44 |
# File 'lib/right_git/git/branch.rb', line 42 def repo @repo end |
Instance Method Details
#<=>(other) ⇒ Integer
Returns comparison value.
86 87 88 89 90 91 92 |
# File 'lib/right_git/git/branch.rb', line 86 def <=>(other) if other.kind_of?(self.class) @fullname <=> other.fullname else raise ::ArgumentError, 'Wrong type' end end |
#==(other) ⇒ TrueClass|FalseClass
Returns true if equivalent.
76 77 78 79 80 81 82 |
# File 'lib/right_git/git/branch.rb', line 76 def ==(other) if other.kind_of?(self.class) @fullname == other.fullname else false end end |
#=~(other) ⇒ Integer
Returns match offset.
70 71 72 |
# File 'lib/right_git/git/branch.rb', line 70 def =~(other) @fullname =~ other end |
#delete ⇒ TrueClass
Deletes this (local or remote) branch.
127 128 129 130 131 132 133 134 |
# File 'lib/right_git/git/branch.rb', line 127 def delete if self.remote? @repo.vet_output("push origin :#{self.name}") else @repo.vet_output("branch -D #{@fullname}") end true end |
#display(width = DEFAULT_DISPLAY_WIDTH) ⇒ String
For display in a column of given width.
116 117 118 119 120 121 122 |
# File 'lib/right_git/git/branch.rb', line 116 def display(width = DEFAULT_DISPLAY_WIDTH) if @fullname.length >= width (@fullname[0..(width - ELLIPSIS.length - 1)] + ELLIPSIS).ljust(width) else @fullname.ljust(width) end end |
#name ⇒ String
Returns name of branch sans origin (if any).
100 101 102 103 104 105 106 107 108 109 |
# File 'lib/right_git/git/branch.rb', line 100 def name if remote? #remove the initial remote-name in the branch (origin/master --> master) bits = @fullname.split('/') bits.shift bits.join('/') else @fullname end end |
#remote? ⇒ TrueClass|FalseClass
Returns true if branch is remote.
95 96 97 |
# File 'lib/right_git/git/branch.rb', line 95 def remote? @remote end |
#to_s ⇒ String Also known as: inspect
Returns stringized.
63 64 65 |
# File 'lib/right_git/git/branch.rb', line 63 def to_s "#{self.class.name}: #{@fullname.inspect}" end |