Class: RightGit::Git::BranchCollection

Inherits:
Object
  • Object
show all
Defined in:
lib/right_git/git/branch_collection.rb

Overview

A collection of Git branches. Acts a bit like an Array, allowing it to be mapped, sorted and compared as such.

Instance Method Summary collapse

Constructor Details

#initialize(repo, *args) ⇒ BranchCollection

Returns a new instance of BranchCollection.

Parameters:

  • repo (Repository)

    to host branch collection

  • args (Array)

    as subset of branches or empty



33
34
35
36
# File 'lib/right_git/git/branch_collection.rb', line 33

def initialize(repo, *args)
  @repo = repo
  @branches = args
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &block) ⇒ Object



96
97
98
99
100
101
102
103
104
# File 'lib/right_git/git/branch_collection.rb', line 96

def method_missing(meth, *args, &block)
  result = @branches.__send__(meth, *args, &block)

  if result.is_a?(::Array)
    BranchCollection.new(@repo, *result)
  else
    result
  end
end

Instance Method Details

#[](argument) ⇒ Object

Accessor that acts like either a Hash or Array accessor



86
87
88
89
90
91
92
93
94
# File 'lib/right_git/git/branch_collection.rb', line 86

def [](argument)
  case argument
  when String
    target = Branch.new(@repo, argument)
    @branches.detect { |b| b == target }
  else
    @branches.__send__(:[], argument)
  end
end

#localBranchCollection

Filters on local branches.

Returns:



46
47
48
49
50
51
52
# File 'lib/right_git/git/branch_collection.rb', line 46

def local
  local = BranchCollection.new(@repo)
  @branches.each do |branch|
    local << branch unless branch.remote?
  end
  local
end

#merged(revision) ⇒ BranchCollection

Queries and filters on branches reachable from the given revision, if any.

Parameters:

  • revision (String)

    for listing reachable merged branches

Returns:



70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/right_git/git/branch_collection.rb', line 70

def merged(revision)
  git_args = ['branch', '-r', '--merged', revision]
  all_merged = @repo.git_output(git_args).lines.map do |line|
    Branch.new(@repo, line)
  end

  merged = BranchCollection.new(@repo)
  @branches.each do |candidate|
    # For some reason Set#include? does not play nice with our overridden comparison operators
    # for branches, so we need to do this the hard way :(
    merged << candidate if all_merged.detect { |b| candidate == b }
  end
  merged
end

#remoteBranchCollection

Filters on remote branches.

Returns:



57
58
59
60
61
62
63
# File 'lib/right_git/git/branch_collection.rb', line 57

def remote
  remote = BranchCollection.new(@repo)
  @branches.each do |branch|
    remote << branch if branch.remote?
  end
  remote
end

#to_sObject Also known as: inspect



38
39
40
# File 'lib/right_git/git/branch_collection.rb', line 38

def to_s
  "#{self.class.name}: #{@branches.inspect}"
end