Class: Stencil::Branches

Inherits:
Object
  • Object
show all
Defined in:
lib/stencil/branches.rb

Constant Summary collapse

@@branches =
{}

Class Method Summary collapse

Class Method Details

.grouped(path) ⇒ Object



30
31
32
33
34
35
36
37
38
# File 'lib/stencil/branches.rb', line 30

def grouped(path)
  branches = (read(path) - [ 'master' ]).inject({}) do |hash, branch|
    branch.split('-').inject(hash) do |h, b|
      h[b] ||= {}
    end
    hash
  end
  { 'master' => branches }
end

.read(path, type = :all) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/stencil/branches.rb', line 9

def read(path, type=:all)
  key = "#{type}:#{path}"

  if type == :all
    (read(path, :remote) + read(path, :local)).uniq
  elsif type == :remote && !@@branches[key]
    branches = Cmd.run path, 'git branch -a'
    @@branches[key] = branches.scan(/origin\/([\w-]+\b$)/).flatten.uniq
  elsif type == :remote
    @@branches[key]
  elsif !@@branches[key]
    branches = Cmd.run path, 'git branch'
    branches = branches.split(/[\s\*]+/)
    branches.delete ''
    branches.sort!
    @@branches[key] = branches
  else
    @@branches[key]
  end
end