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
29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/git/branches.rb', line 29 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').
118 119 120 |
# File 'lib/git/branches.rb', line 118 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
98 99 100 |
# File 'lib/git/branches.rb', line 98 def each(&) @branches.values.each(&) end |
#local ⇒ Array<Git::Branch>
Returns all local (non-remote-tracking) branches
50 51 52 |
# File 'lib/git/branches.rb', line 50 def local reject(&:remote) end |
#remote ⇒ Array<Git::Branch>
Returns all remote-tracking branches
61 62 63 |
# File 'lib/git/branches.rb', line 61 def remote self.select(&:remote) end |
#size ⇒ Integer
Returns the number of branches in the collection
72 73 74 |
# File 'lib/git/branches.rb', line 72 def size @branches.size end |
#to_s ⇒ String
Returns a string listing all branches, prefixed with * for the current branch
129 130 131 132 133 134 135 |
# File 'lib/git/branches.rb', line 129 def to_s out = +'' @branches.each_value do |b| out << (b.current ? '* ' : ' ') << b.to_s << "\n" end out end |