Skip to content

Commit

Permalink
Branches.read supports more types, don't merge branch if doesn't exis…
Browse files Browse the repository at this point in the history
…t, track remote branches
  • Loading branch information
winton committed Jul 19, 2013
1 parent d06f4e0 commit e93402d
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 10 deletions.
28 changes: 22 additions & 6 deletions lib/stencil/branches.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,28 @@ class Stencil
class Branches
class <<self

def read(path)
branches = Cmd.run path, 'git branch'
branches = branches.split(/[\s\*]+/)
branches.delete 'master'
branches.delete ''
branches.sort
@@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-]+/)
elsif type == :remote
@@branches[key]
elsif !@@branches[key]
branches = Cmd.run path, 'git branch'
branches = branches.split(/[\s\*]+/)
branches.delete 'master'
branches.delete ''
branches.sort!
@@branches[key] = branches
else
@@branches[key]
end
end

def grouped(path)
Expand Down
2 changes: 1 addition & 1 deletion lib/stencil/cmd.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def run(path, cmd=nil)
output = `#{[ path, cmd ].compact.join} 2>&1`

unless $?.success?
Msg.error "there was a problem\n\nCommand: #{cmd}\n\nOutput:\n#{output}"
Msg.error "#{cmd}\n\n#{output}"
end

output
Expand Down
22 changes: 20 additions & 2 deletions lib/stencil/merge.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,37 @@ def template(path, push)
end

private

def checkout(path, branch)
locals = Branches.read(path, :local) + [ 'master' ]
remotes = Branches.read(path, :remote)

if locals.include?(branch)
Cmd.run(path, "git checkout #{branch}")
elsif remotes.include?(branch)
Cmd.run(path, "git checkout -t origin/#{branch}")
end
end

def progressive_merge(path, merger, mergees, push)
branches = Branches.read(path)
return unless branches.include?(merger) || merger == 'master'

unless mergees.empty?
Cmd.run(path, "git checkout #{merger}")
checkout(path, merger)
Cmd.run(path, "git pull origin #{merger}")
end

mergees = mergees.sort_by { |k, v| k }
mergees.each do |(mergee, mergee_mergees)|
mergee = "#{merger}-#{mergee}" unless merger == 'master'
Cmd.run(path, "git checkout #{mergee}")

next unless branches.include?(mergee)

checkout(path, mergee)
Cmd.run(path, "git merge #{merger}")
Cmd.run(path, "git push origin #{mergee}") if push

progressive_merge(path, mergee, mergee_mergees, push)
end
end
Expand Down
3 changes: 2 additions & 1 deletion spec/stencil_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,14 @@
`git add .`
`git commit -m "#{branch}"`
end

Stencil::Branches.read(@fixture) # warm up @@branches cache
end

describe :initialize do
before :all do
@cmds = [
"git fetch --all",
"git branch",
"git checkout master",
"git pull origin master",
"git checkout a",
Expand Down

0 comments on commit e93402d

Please sign in to comment.