lib/stencil/merge.rb in stencil-0.1.5 vs lib/stencil/merge.rb in stencil-0.1.8

- old
+ new

@@ -1,104 +1,52 @@ class Stencil class Merge class <<self - def project(name, path) - template = Config.read[:projects][name][:template] - branches = Config.read[:projects][name][:branches] - Msg.error_specify_template unless template - template = Config.read[:templates][template.intern] - if template && File.exists?(template[:path]) - - # Add remote template to project - origin = get_origin template[:path] - Msg.template_url origin - add_remote 'template', path, origin - - # Pull template into master if no branches specified - branches = %w(master) if branches.empty? - - # Pull template into each branch - branches.each do |branch| - Msg.merge_remote_branch branch - Cmd.run path, "git pull template #{branch}" - end - else - Msg.template_not_found template - Msg.error_specify_template - end - end - def template(path, push) - Branches.grouped(path).each do |branches| - branches.unshift('master') - progressive(path, branches, push) + Cmd.run(path, "git fetch --all") + + Branches.grouped(path).each do |merger, mergees| + progressive_merge(path, merger, mergees, push) end - Cmd.run path, "git checkout master" - end - - def upstream(name, commit=nil, *branches) - # Project variables - project = Config.read[:projects][name] - branches = project[:branches] if branches.empty? - # Template variables - template = Config.read[:templates][project[:template].intern] - path = template[:path] - - # Add remote project to template and fetch - origin = get_origin project[:path] - Msg.project_url origin - add_remote 'project', path, origin - Cmd.run path, "git fetch project" - - # Get last commit if none specified - unless commit - cmd = "git log HEAD~1..HEAD --pretty=format:'%H'" - commit = Cmd.run(template[:path], cmd).strip - end - - # Cherry pick commit into branches - branches.each do |branch| - output = Cmd.run path, "git checkout #{branch}" - Msg.error(output) if output.downcase.include?('error') - Msg.cherry_pick branch, commit - output = Cmd.run path, "git cherry-pick #{commit}" - Msg.error(output) if output.downcase.include?('fatal') - end + Cmd.run(path, "git checkout master") end private - - def add_remote(name, path, url) - if Cmd.run(path, "git remote").split.include?(name) - Cmd.run path, "git remote rm #{name}" + + def checkout(path, branch) + locals = Branches.read(path, :local) + 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 - Cmd.run path, "git remote add #{name} #{url}" end - def get_origin(path) - origin = Cmd.run path, "git remote show origin" - origin.match(/URL:\s+(\S+)/)[1] - end - - def progressive(path, branches, push) - merger = branches.shift - mergee = branches.first - branches.each do |mergee| - if merger && mergee - ok = mergee[0..merger.length-1] == merger || merger == 'master' - unless merger.empty? || mergee.empty? || !ok - output = Cmd.run path, "git checkout #{mergee}" - Msg.error(output) if output.downcase.include?('error') - Msg.merging_x_into_y merger, mergee - output = Cmd.run path, "git merge #{merger}" - Msg.error(output) if output.downcase.include?('conflict') - Cmd.run(path, "git push origin #{mergee}") if push - end - end + def progressive_merge(path, merger, mergees, push) + branches = Branches.read(path) + return unless branches.include?(merger) + + unless mergees.empty? + checkout(path, merger) + Cmd.run(path, "git pull origin #{merger}") end - progressive(path, branches, push) unless branches.empty? + + mergees = mergees.sort_by { |k, v| k } + mergees.each do |(mergee, mergee_mergees)| + mergee = "#{merger}-#{mergee}" unless merger == 'master' + + 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 end end end \ No newline at end of file