require_relative '../hook/hooks' module GGSM module Submodule include Hooks def check_submodule sub_str = `git submodule` if sub_str.empty? puts '所在目录工程下不存在Submodule,请检查所在目录!'.red exit 1 end correct_dir check_hooks end def get_submodule pattern = /(?<=\s)[\/0-9a-zA-Z]*(?=\s)/ sub_status = `git submodule` sub_status = sub_status.split(/\n/) match = pattern.match(sub_status[0]) if match==nil puts '==> 初始化子模块'.yellow `git submodule update --init --recursive` sub_status = `git submodule` sub_status = sub_status.split(/\n/) end result = [] sub_status.each do |sub| match = pattern.match(sub.strip) result.push(match[0]) end result end def check_submodule_status(is_sync) modified_sub = get_modified_submodule if !is_sync && modified_sub.size > 0 subs = '' modified_sub.each do |sub| subs = "#{sub} , #{subs}" end puts "\n存在未跟踪的子模块提交: #{subs[0...subs.size-2]} \n\n请选择操作:\n1 丢弃子模块的改动 2 追踪子模块(仅追踪子模块)\n3 追踪所有改变\n4 取消".red input = STDIN.gets.chomp.upcase if input == '1' elsif input == '2' add = 'git add' modified_sub.each do |sub| add = "#{add} #{sub}" end system "#{add};git commit" elsif input == '3' system 'git add .;git commit' else puts '==> 取消操作'.blue exit 0 end end end def get_modified_submodule result = [] status = `git status -s`.split(/\n/) subs = get_submodule status.each do |sub| array_name = sub.split(' ') name = array_name[array_name.size-1] if subs.include?name result.push(name) end end result end def get_submodule_commit sub_tree = 'git ls-tree HEAD | grep "160000"' sub_commits = `#{sub_tree}` if sub_commits.strip == '' && (File.directory? 'submodules') Dir.chdir 'submodules' sub_commits = `#{sub_tree}` Dir.chdir '..' end pattern = /(?<=\s)[0-9a-zA-Z]{40}(?=\s)/ sub_commits = sub_commits.split(/\n/) result = [] sub_commits.each do |sub| match = pattern.match(sub.strip) result.push(match[0][0...7]) end result end def check_un_commit_code subs = get_submodule project_path = Dir.pwd subs.each do |sub| Dir.chdir sub status = `git status --ignore-submodules | grep 'nothing to commit'` if status.strip == '' puts "#{sub} 有未提交的代码".red exit 1 end Dir.chdir project_path end status = `git status --ignore-submodules | grep 'nothing to commit'` if status.strip == '' puts '主工程 有未提交的代码'.red exit 1 end end def foreach_module subs = get_submodule project_path = Dir.pwd subs.each do |sub| Dir.chdir sub puts "==> 进入#{sub}:".yellow yield sub, subs.index(sub) Dir.chdir project_path end end def get_current_branch `git branch | grep "*"`.split('* ')[1].split("\n")[0] end def tip_contact_author latest_commit = `git log test -1 --oneline | grep ''`.split(' ')[0] author = `git show #{latest_commit}|grep "Author:"`.chomp puts "请联系#{author} 推送远程缺失的commit".red end def correct_dir if File.exist?('.git') return end Dir.chdir '..' correct_dir end # 获取#{branch}上HEAD的commit id def get_head_commit(branch) `git rev-parse #{branch}` end end end