lib/stencil/branches.rb in stencil-0.1.5 vs lib/stencil/branches.rb in stencil-0.1.8
- old
+ new
@@ -2,34 +2,40 @@
class Stencil
class Branches
class <<self
- def read(path)
- branches = Cmd.run path, 'git branch'
- branches = branches.split(/\s+/)
- branches.delete '*'
- branches.delete 'master'
- branches
+ @@branches = {}
+
+ 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
def grouped(path)
- groups, ignore = [], []
- branches = read(path).sort { |a, b| a.length <=> b.length }
- branches.each do |branch|
- next if ignore.include?(branch)
- groups << [ branch ] + group(branches, branch)
- ignore += groups.last
+ branches = (read(path) - [ 'master' ]).inject({}) do |hash, branch|
+ branch.split('-').inject(hash) do |h, b|
+ h[b] ||= {}
+ end
+ hash
end
- groups
- end
-
- private
-
- def group(branches, branch)
- branches.select do |b|
- b != branch && b[0..branch.length-1] == branch
- end
+ { 'master' => branches }
end
end
end
end
\ No newline at end of file