Class: Git::Branches Deprecated

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

Overview

Deprecated.

Use Repository::Branching#branch_list instead

Repository::Branching#branch_list returns Array<Git::BranchInfo> (immutable value objects). Filter it with select(&:remote?) or reject(&:remote?) in place of #remote and #local, and look a branch up by name with branch_list(name).first in place of #[]. Constructing a Git::Branches emits a deprecation warning.

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)

Deprecated.

Creates a new Branches collection populated from the given repository

Parameters:

Raises:

See Also:



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/git/branches.rb', line 39

def initialize(base)
  Git::Deprecation.warn(
    'Git::Branches is deprecated and will be removed in v6.0.0. ' \
    'Use Git::Repository#branch_list instead.'
  )
  @branches = {}
  @lookup = {}

  @base = base

  load_branches
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



127
128
129
# File 'lib/git/branches.rb', line 127

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:



107
108
109
# File 'lib/git/branches.rb', line 107

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:



59
60
61
# File 'lib/git/branches.rb', line 59

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



70
71
72
# File 'lib/git/branches.rb', line 70

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



81
82
83
# File 'lib/git/branches.rb', line 81

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



138
139
140
141
142
143
144
145
146
# File 'lib/git/branches.rb', line 138

def to_s
  out = +''
  @branches.each_value do |b|
    # Git::Branch#current is deprecated too; silence it so one to_s call emits one warning
    current = Git::Deprecation.silence { b.current }
    out << (current ? '* ' : '  ') << b.to_s << "\n"
  end
  out
end