summaryrefslogtreecommitdiffstats
path: root/lib/git/branches.rb
blob: 60ab16bb2cfa6499cabe54e536d0f1014d18588f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
module Git
  
  # object that holds all the available branches
  class Branches
    include Enumerable
    
    @base = nil
    @branches = nil
    
    def initialize(base)
      @branches = {}
      
      @base = base
            
      @base.lib.branches_all.each do |b|
        @branches[b[0]] = Git::Branch.new(@base, b[0])
      end
    end

    def local
      self.select { |b| !b.remote }
    end
    
    def remote
      self.select { |b| b.remote }
    end
    
    # array like methods

    def size
      @branches.size
    end    
    
    def each
      @branches.each do |k, b|
        yield b
      end
    end
    
    def [](symbol)
      @branches[symbol.to_s]
    end
    
    def to_s
      out = ''
      @branches.each do |k, b|
        if b.current
          out += "* " + b.to_s + "\n"
        else
          out += "  " + b.to_s + "\n"
        end
      end
      out
    end
    
  end
end