Class: Git::Branches
Overview
Collection of all Git branches in a repository
Wraps both local and remote-tracking branches and provides filtering, enumeration, and name-based lookup.
Instance Method Summary collapse
-
#[](branch_name) ⇒ Git::Branch?
Returns the branch with the given name.
-
#each
Iterates over every branch in the collection.
-
#initialize(base)
constructor
Creates a new Branches collection populated from the given repository.
-
#local ⇒ Array<Git::Branch>
Returns all local (non-remote-tracking) branches.
-
#remote ⇒ Array<Git::Branch>
Returns all remote-tracking branches.
-
#size ⇒ Integer
Returns the number of branches in the collection.
-
#to_s ⇒ String
Returns a string listing all branches, prefixed with
*for the current branch.
Constructor Details
#initialize(base)
Creates a new Branches collection populated from the given repository
27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/git/branches.rb', line 27 def initialize(base) @branches = {} @lookup = {} @base = base branch_repository.branches_all.each do |branch_info| branch = Git::Branch.new(base, branch_info) @branches[branch_info.refname] = branch index_branch_lookup(branch, refname: branch_info.refname) end end |
Instance Method Details
#[](branch_name) ⇒ Git::Branch?
Returns the branch with the given name
Supports short names ('main'), remote-qualified names
('working/master'), and full refspec names
('remotes/working/master').
116 117 118 |
# File 'lib/git/branches.rb', line 116 def [](branch_name) @lookup[branch_name.to_s] end |
#each ⇒ Enumerator<Git::Branch> #each {|branch| ... } ⇒ Array<Git::Branch>
Iterates over every branch in the collection
96 97 98 |
# File 'lib/git/branches.rb', line 96 def each(&) @branches.values.each(&) end |
#local ⇒ Array<Git::Branch>
Returns all local (non-remote-tracking) branches
48 49 50 |
# File 'lib/git/branches.rb', line 48 def local reject(&:remote) end |
#remote ⇒ Array<Git::Branch>
Returns all remote-tracking branches
59 60 61 |
# File 'lib/git/branches.rb', line 59 def remote self.select(&:remote) end |
#size ⇒ Integer
Returns the number of branches in the collection
70 71 72 |
# File 'lib/git/branches.rb', line 70 def size @branches.size end |
#to_s ⇒ String
Returns a string listing all branches, prefixed with * for the current branch
127 128 129 130 131 132 133 |
# File 'lib/git/branches.rb', line 127 def to_s out = +'' @branches.each_value do |b| out << (b.current ? '* ' : ' ') << b.to_s << "\n" end out end |