module GGSM module Submodule def check_submodule sub_str = `git submodule` if sub_str.empty? puts '所在目录工程下不存在Submodule,请检查所在目录!'.red exit 1 end switch_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 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 switch_dir if File.exist?('.git') return end Dir.chdir '..' switch_dir end def check_hooks version = `ggsm v` subs = get_submodule subs.each do |sub| cp_hooks(version, sub) end cp_hooks(version, nil) end def cp_hooks(version, sub) if File.exist?('.git') if sub != nil config_path = ".git/modules/#{sub}/hooks/ggsm" else config_path = '.git/hooks/ggsm' end target_path = config_path.split('/ggsm')[0] if !File.exist?(config_path) || version != IO.read(config_path) path = `gem which ggsm`.split('/ggsm.rb')[0] `cp #{path}/ggsm/hook/commit-msg #{target_path}/commit-msg` `cp #{path}/ggsm/hook/pre-commit #{target_path}/pre-commit` file = File.new(config_path, 'w') file << version file.close if sub == nil puts '已更新Hooks'.blue end end end end end end