require 'colorize' require_relative '../util/submodule' module GGSM module Merge include Submodule def merge_flow(branch, force_rebase) check_submodule check_un_commit_code arry_conflict = [] foreach_module {|sub| process_merge(arry_conflict, sub, branch) } puts '==> 进入主工程:'.yellow main_rebase = false if force_rebase main_rebase = true end if need_rebase(branch) || force_rebase process_rebase(arry_conflict, branch) puts 'Tip: 主工程rebase模式'.blue else process_merge(arry_conflict, '主工程', branch) puts 'Tip: 主工程merge模式'.blue end if arry_conflict.size > 0 tip = "==> #{arry_conflict.size}个模块冲突:" arry_conflict.reverse.each do |sub| tip = "#{tip} #{sub}" end puts tip.red puts "Tip: 当你解决冲突后,执行\"git add .\",如需推送远程,在主工程执行\"ggsm finish\"".blue else if main_rebase puts "Tip: 如需推送远程,执行\"ggsm finish -f|--force\"".blue else puts "Tip: 如需推送远程,执行\"ggsm finish\"".blue end end end def process_merge(arry_conflict, module_name, branch) result_merge = `git merge #{branch}` puts result_merge if result_merge.include? 'Merge conflict' arry_conflict.push(module_name) end end def process_rebase(arry_conflict, branch) result_merge = `git rebase #{branch}` puts result_merge if result_merge.include? 'Merge conflict' arry_conflict.push('主工程') end end def need_rebase(branch) pattern = /[0-9a-z]{7}(?=\s)/ current_branch = get_current_branch can_rebase(current_branch) {|remote| diff_logs = `git log --oneline #{branch}..#{current_branch}`.split("\n").reverse if diff_logs.length > 0 commit = pattern.match(diff_logs[0].strip)[0] result = `git log #{remote}/#{current_branch} |grep #{commit}` if `git log --oneline #{current_branch}..#{branch}`.split("\n").length > 0 && result.strip != '' return false end end } end def can_rebase(current_branch) remote = `git config --get branch.#{current_branch}.remote` if remote == '' `git branch -r`.split("\n").each do |branch| if branch.strip == "origin/#{current_branch}" remote = 'origin' yield remote end end else yield remote end true end end end