Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/tools/dev/pre-commit-hook.rb
Views: 11766
#!/usr/bin/env ruby12require "open3"34#5# Check that modules actually pass msftidy checks before committing6# or after merging.7#8# Simply symlink this script to your local .git/hooks/pre-commit script9# and your .git/hooks/post-merge scripts. Note the lack of a trailing10# .rb11#12# If you are in the top-level dir, the symlink commands would be:13#14# ln -sf ../../tools/dev/pre-commit-hook.rb .git/hooks/pre-commit15# ln -sf ../../tools/dev/pre-commit-hook.rb .git/hooks/post-merge16#17# That way, you will track changes to this script when it updates18# (rarely). If you'd prefer to copy it directly, that's okay, too (mark19# it +x and don't name it filename.rb, just filename).20#2122def run(command, exception: true)23puts command24stdout, status = ::Open3.capture2(command)25if !status.success? && exception26raise "Command failed with status (#{status.exitstatus}): #{command}"27end2829stdout30end3132def merge_error_message33msg = []34msg << "[*] This merge contains modules failing msftidy.rb"35msg << "[*] Please fix this if you intend to publish these"36msg << "[*] modules to a popular metasploit-framework repo"37puts "-" * 7238puts msg.join("\n")39puts "-" * 7240end4142valid = true # Presume validity43files_to_check = []4445# Who called us? If it's a post-merge check things operate a little46# differently.47puts "[*] Running msftidy.rb in #{$0} mode"4849case $050when /post-merge/51base_caller = :post_merge52when /pre-commit/53base_caller = :pre_commit54else55base_caller = :msftidy56end5758if base_caller == :post_merge59changed_files = run('git diff --name-only HEAD^ HEAD')60else61changed_files = run('git diff --cached --name-only')62end6364changed_files.each_line do |fname|65fname.strip!66next unless File.exist?(fname)67next unless File.file?(fname)68next unless fname =~ /^modules.+\.rb/69files_to_check << fname70end7172if files_to_check.empty?73puts "--- No Metasploit modules to check ---"74else75puts "--- Checking new and changed module syntax with tools/dev/msftidy.rb ---"76files_to_check.each do |fname|77command = "bundle exec ruby ./tools/dev/msftidy.rb #{fname}"78msftidy_output, status = ::Open3.capture2(command)79valid = false unless status.success?80puts "#{fname} - msftidy check passed" if msftidy_output.empty?81msftidy_output.each_line do |line|82puts line83end84end85puts "-" * 7286end8788unless valid89if base_caller == :post_merge90puts merge_error_message91exit(0x10)92else93puts "[!] msftidy.rb objected, aborting commit"94puts "[!] To bypass this check use: git commit --no-verify"95puts "-" * 7296exit(0x01)97end9899end100101102