Land #3188, deluxe msftidy post-merge hook

bug/bundler_fix
William Vu 2014-04-07 14:38:19 -05:00
commit 79f82be35d
No known key found for this signature in database
GPG Key ID: E761DCB4C1629024
3 changed files with 62 additions and 44 deletions

View File

@ -1,11 +1,12 @@
language: ruby language: ruby
env: MSF_SPOTCHECK_RECENT=1
before_install: before_install:
- rake --version - rake --version
- sudo apt-get update -qq - sudo apt-get update -qq
- sudo apt-get install -qq libpcap-dev - sudo apt-get install -qq libpcap-dev
- ln -sf ../../tools/dev/pre-commit-hook.rb ./.git/hooks/post-merge
- ls -la ./.git/hooks
- ./.git/hooks/post-merge
before_script: before_script:
- ./tools/msftidy.rb
- cp config/database.yml.travis config/database.yml - cp config/database.yml.travis config/database.yml
- bundle exec rake --version - bundle exec rake --version
- bundle exec rake db:create - bundle exec rake db:create

View File

@ -1,29 +1,54 @@
#!/usr/bin/env ruby #!/usr/bin/env ruby
# Check that modules actually pass msftidy checks first. # Check that modules actually pass msftidy checks before committing
# To install this script, make this your pre-commit hook your local # or after merging.
# metasploit-framework clone. For example, if you have checked out
# the Metasploit Framework to:
# #
# /home/mcfakepants/git/metasploit-framework # Simply symlink this script to your local .git/hooks/pre-commit script
# and your .git/hooks/post-merge scripts. Note the lack of a trailing
# .rb
# #
# then you will copy this script to: # If you are in the top-level dir, the symlink commands would be:
# #
# /home/mcfakepants/git/metasploit-framework/.git/hooks/pre-commit # ln -sf ../../tools/dev/pre-commit-hook.rb .git/hooks/pre-commit
# ln -sf ../../tools/dev/pre-commit-hook.rb .git/hooks/post-merge
# #
# You must mark it executable (chmod +x), and do not name it # That way, you will track changes to this script when it updates
# pre-commit.rb (just pre-commit) # (rarely). If you'd prefer to copy it directly, that's okay, too (mark
# # it +x and don't name it filename.rb, just filename).
# If you want to keep up on changes with this hook, just:
# def merge_error_message
# ln -sf <this file> <path to commit hook> msg = []
msg << "[*] This merge contains modules failing msftidy.rb"
msg << "[*] Please fix this if you intend to publish these"
msg << "[*] modules to a popular metasploit-framework repo"
puts "-" * 72
puts msg.join("\n")
puts "-" * 72
end
valid = true # Presume validity valid = true # Presume validity
files_to_check = [] files_to_check = []
results = %x[git diff --cached --name-only] # Who called us? If it's a post-merge check things operate a little
# differently.
puts "[*] Running msftidy.rb in #{$0} mode"
results.each_line do |fname| case $0
when /post-merge/
base_caller = :post_merge
when /pre-commit/
base_caller = :pre_commit
else
base_caller = :msftidy
end
if base_caller == :post_merge
changed_files = %x[git diff --name-only HEAD^ HEAD]
else
changed_files = %x[git diff --cached --name-only]
end
changed_files.each_line do |fname|
fname.strip! fname.strip!
next unless File.exist?(fname) and File.file?(fname) next unless File.exist?(fname) and File.file?(fname)
next unless fname =~ /modules.+\.rb/ next unless fname =~ /modules.+\.rb/
@ -31,9 +56,9 @@ results.each_line do |fname|
end end
if files_to_check.empty? if files_to_check.empty?
puts "--- No Metasploit modules to check, committing. ---" puts "--- No Metasploit modules to check ---"
else else
puts "--- Checking module syntax with tools/msftidy.rb ---" puts "--- Checking new and changed module syntax with tools/msftidy.rb ---"
files_to_check.each do |fname| files_to_check.each do |fname|
cmd = "ruby ./tools/msftidy.rb #{fname}" cmd = "ruby ./tools/msftidy.rb #{fname}"
msftidy_output= %x[ #{cmd} ] msftidy_output= %x[ #{cmd} ]
@ -43,12 +68,18 @@ else
puts line puts line
end end
end end
puts "-" * 52 puts "-" * 72
end end
unless valid unless valid
puts "msftidy.rb objected, aborting commit" if base_caller == :post_merge
puts "To bypass this check use: git commit --no-verify" puts merge_error_message
puts "-" * 52 exit(0x10)
exit(1) else
puts "[!] msftidy.rb objected, aborting commit"
puts "[!] To bypass this check use: git commit --no-verify"
puts "-" * 72
exit(0x01)
end
end end

View File

@ -4,14 +4,13 @@
# Check (recursively) for style compliance violations and other # Check (recursively) for style compliance violations and other
# tree inconsistencies. # tree inconsistencies.
# #
# by jduck and friends # by jduck, todb, and friends
# #
require 'fileutils' require 'fileutils'
require 'find' require 'find'
require 'time' require 'time'
CHECK_OLD_RUBIES = !!ENV['MSF_CHECK_OLD_RUBIES'] CHECK_OLD_RUBIES = !!ENV['MSF_CHECK_OLD_RUBIES']
SPOTCHECK_RECENT = !!ENV['MSF_SPOTCHECK_RECENT']
if CHECK_OLD_RUBIES if CHECK_OLD_RUBIES
require 'rvm' require 'rvm'
@ -549,25 +548,12 @@ end
dirs = ARGV dirs = ARGV
if SPOTCHECK_RECENT @exit_status = 0
msfbase = %x{\\git rev-parse --show-toplevel}.strip
if File.directory? msfbase
Dir.chdir(msfbase)
else
$stderr.puts "You need a git binary in your path to use this functionality."
exit(0x02)
end
last_release = %x{\\git tag -l #{DateTime.now.year}\\*}.split.last
new_modules = %x{\\git diff #{last_release}..HEAD --name-only --diff-filter A modules}
dirs = dirs | new_modules.split
end
# Don't print an error if there's really nothing to check.
unless SPOTCHECK_RECENT
if dirs.length < 1 if dirs.length < 1
$stderr.puts "Usage: #{File.basename(__FILE__)} <directory or file>" $stderr.puts "Usage: #{File.basename(__FILE__)} <directory or file>"
exit(0x01) @exit_status = 1
end exit(@exit_status)
end end
dirs.each do |dir| dirs.each do |dir|