Class: Git::Branches

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/git/branches.rb

Overview

Collection of all Git branches in a repository

Wraps both local and remote-tracking branches and provides filtering, enumeration, and name-based lookup.

Examples:

Enumerate all branches

branches = repo.branches
branches.each { |b| puts b.name }

Instance Method Summary collapse

Constructor Details

#initialize(base)

Creates a new Branches collection populated from the given repository

Parameters:

Raises:



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').

Examples:

Look up a branch by short name

repo.branches['main']

Look up a remote-tracking branch

repo.branches['working/master']

Parameters:

  • branch_name (#to_s)

    the name of the branch to retrieve

Returns:

  • (Git::Branch, nil)

    the matching branch, or nil if not found



118
119
120
# File 'lib/git/branches.rb', line 118

def [](branch_name)
  @lookup[branch_name.to_s]
end

#eachEnumerator<Git::Branch> #each {|branch| ... } ⇒ Array<Git::Branch>

Iterates over every branch in the collection

Overloads:

  • #eachEnumerator<Git::Branch>

    Returns an enumerator over all branches.

    Examples:

    Get an enumerator over all branches

    enum = repo.branches.each

    Returns:

    • (Enumerator<Git::Branch>)

      an enumerator over all branches

  • #each {|branch| ... } ⇒ Array<Git::Branch>

    Returns the full list of branches.

    Examples:

    Print every branch name

    repo.branches.each { |b| puts b.name }

    Yields:

    • (branch)

      passes each branch to the block

    Yield Parameters:

    Yield Returns:

    • (void)

    Returns:



98
99
100
# File 'lib/git/branches.rb', line 98

def each(&)
  @branches.values.each(&)
end

#localArray<Git::Branch>

Returns all local (non-remote-tracking) branches

Examples:

List local branch names

repo.branches.local.map(&:name)

Returns:



50
51
52
# File 'lib/git/branches.rb', line 50

def local
  reject(&:remote)
end

#remoteArray<Git::Branch>

Returns all remote-tracking branches

Examples:

List remote branch names

repo.branches.remote.map(&:name)

Returns:

  • (Array<Git::Branch>)

    the remote-tracking branches



61
62
63
# File 'lib/git/branches.rb', line 61

def remote
  self.select(&:remote)
end

#sizeInteger

Returns the number of branches in the collection

Examples:

Count all branches

repo.branches.size  # => 3

Returns:

  • (Integer)

    the total number of branches



72
73
74
# File 'lib/git/branches.rb', line 72

def size
  @branches.size
end

#to_sString

Returns a string listing all branches, prefixed with * for the current branch

Examples:

Display all branches

puts repo.branches.to_s

Returns:

  • (String)

    a formatted branch listing



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