class Git::Branches
Collection of all Git branches in a repository
Wraps both local and remote-tracking branches and provides filtering, enumeration, and name-based lookup.
@example Enumerate all branches
branches = repo.branches branches.each { |b| puts b.name }
@api public
Public Class Methods
Source
# File lib/git/branches.rb, line 27 def initialize(base) @branches = {} @lookup = {} @base = base branch_repository.branch_list.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
Creates a new Branches collection populated from the given repository
@param base [Git::Repository] the repository to enumerate
branches from
@return [void]
@raise [Git::FailedError] if git exits with a non-zero exit status
Public Instance Methods
Source
# File lib/git/branches.rb, line 116 def [](branch_name) @lookup[branch_name.to_s] end
Returns the branch with the given name
Supports short names (‘’main’‘), remote-qualified names (`’working/master’‘), and full refspec names (`’remotes/working/master’‘).
@example Look up a branch by short name
repo.branches['main']
@example Look up a remote-tracking branch
repo.branches['working/master']
@param branch_name [#to_s] the name of the branch to retrieve
@return [Git::Branch, nil] the matching branch, or ‘nil` if not found
Source
# File lib/git/branches.rb, line 96 def each(&) @branches.values.each(&) end
Iterates over every branch in the collection
@overload each
@example Get an enumerator over all branches enum = repo.branches.each @return [Enumerator<Git::Branch>] an enumerator over all branches
@overload each(&block)
@example Print every branch name
repo.branches.each { |b| puts b.name }
@return [Array<Git::Branch>] the full list of branches
@yield [branch] passes each branch to the block
@yieldparam branch [Git::Branch] a branch in the repository
@yieldreturn [void]
Source
# File lib/git/branches.rb, line 48 def local reject(&:remote) end
Returns all local (non-remote-tracking) branches
@example List local branch names
repo.branches.local.map(&:name)
@return [Array<Git::Branch>] the local branches
Source
# File lib/git/branches.rb, line 59 def remote self.select(&:remote) end
Returns all remote-tracking branches
@example List remote branch names
repo.branches.remote.map(&:name)
@return [Array<Git::Branch>] the remote-tracking branches
Source
# File lib/git/branches.rb, line 70 def size @branches.size end
Returns the number of branches in the collection
@example Count all branches
repo.branches.size # => 3
@return [Integer] the total number of branches
Source
# 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
Returns a string listing all branches, prefixed with ‘*` for the current branch
@example Display all branches
puts repo.branches.to_s
@return [String] a formatted branch listing
Private Instance Methods
Source
# File lib/git/branches.rb, line 141 def branch_repository @base end
@return [Git::Repository] the repository used to enumerate branches
@api private
Source
# File lib/git/branches.rb, line 156 def index_branch_lookup(branch, refname:) @lookup[refname] ||= branch @lookup[branch.full] ||= branch return unless branch.full.start_with?('remotes/') # Mirror git compatibility: allow omitting a leading "remotes/". @lookup[branch.full.delete_prefix('remotes/')] ||= branch end
Indexes all supported lookup keys for a branch without mutating the canonical ‘@branches` collection used by enumeration
@param branch [Git::Branch] the branch to index
@param refname [String] the full refname key to use for primary lookup
@return [void]
@api private