2013-02-06 17:13:30 +00:00
|
|
|
#!/usr/bin/env ruby
|
|
|
|
|
2014-04-03 20:26:25 +00:00
|
|
|
# Check that modules actually pass msftidy checks before committing
|
|
|
|
# or after merging.
|
2013-02-06 17:13:30 +00:00
|
|
|
#
|
2014-04-03 20:26:25 +00:00
|
|
|
# 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
|
2013-02-06 17:13:30 +00:00
|
|
|
#
|
2014-04-03 20:26:25 +00:00
|
|
|
# If you are in the top-level dir, the symlink commands would be:
|
2013-02-06 17:13:30 +00:00
|
|
|
#
|
2014-04-07 17:40:11 +00:00
|
|
|
# ln -sf ../../tools/dev/pre-commit-hook.rb .git/hooks/pre-commit
|
|
|
|
# ln -sf ../../tools/dev/pre-commit-hook.rb .git/hooks/post-merge
|
2013-02-06 17:13:30 +00:00
|
|
|
#
|
2014-04-03 20:26:25 +00:00
|
|
|
# That way, you will track changes to this script when it updates
|
|
|
|
# (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).
|
2013-02-06 17:13:30 +00:00
|
|
|
|
2014-04-02 15:19:43 +00:00
|
|
|
def merge_error_message
|
|
|
|
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
|
|
|
|
|
2013-02-06 17:13:30 +00:00
|
|
|
valid = true # Presume validity
|
|
|
|
files_to_check = []
|
|
|
|
|
2014-04-02 15:19:43 +00:00
|
|
|
# Who called us? If it's a post-merge check things operate a little
|
|
|
|
# differently.
|
2014-04-03 16:10:20 +00:00
|
|
|
puts "[*] Running msftidy.rb in #{$0} mode"
|
2014-04-02 15:19:43 +00:00
|
|
|
|
|
|
|
case $0
|
|
|
|
when /post-merge/
|
|
|
|
base_caller = :post_merge
|
|
|
|
when /pre-commit/
|
2014-04-03 16:10:20 +00:00
|
|
|
base_caller = :pre_commit
|
2014-04-02 15:19:43 +00:00
|
|
|
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
|
2013-02-06 17:13:30 +00:00
|
|
|
|
2014-04-02 15:19:43 +00:00
|
|
|
changed_files.each_line do |fname|
|
2013-02-06 17:13:30 +00:00
|
|
|
fname.strip!
|
|
|
|
next unless File.exist?(fname) and File.file?(fname)
|
|
|
|
next unless fname =~ /modules.+\.rb/
|
|
|
|
files_to_check << fname
|
|
|
|
end
|
|
|
|
|
|
|
|
if files_to_check.empty?
|
2014-04-02 15:19:43 +00:00
|
|
|
puts "--- No Metasploit modules to check ---"
|
2013-02-06 17:13:30 +00:00
|
|
|
else
|
2014-04-02 15:19:43 +00:00
|
|
|
puts "--- Checking new and changed module syntax with tools/msftidy.rb ---"
|
2013-02-06 17:13:30 +00:00
|
|
|
files_to_check.each do |fname|
|
|
|
|
cmd = "ruby ./tools/msftidy.rb #{fname}"
|
|
|
|
msftidy_output= %x[ #{cmd} ]
|
|
|
|
puts "#{fname} - msftidy check passed" if msftidy_output.empty?
|
|
|
|
msftidy_output.each_line do |line|
|
|
|
|
valid = false
|
|
|
|
puts line
|
|
|
|
end
|
|
|
|
end
|
2014-04-02 15:19:43 +00:00
|
|
|
puts "-" * 72
|
2013-02-06 17:13:30 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
unless valid
|
2014-04-02 15:19:43 +00:00
|
|
|
if base_caller == :post_merge
|
|
|
|
puts merge_error_message
|
|
|
|
exit(0x10)
|
|
|
|
else
|
|
|
|
puts "[!] msftidy.rb objected, aborting commit"
|
|
|
|
puts "[!] To bypass this check use: git commit --no-verify"
|
|
|
|
puts "-" * 72
|
|
|
|
exit(0x01)
|
|
|
|
end
|
|
|
|
|
2013-02-06 17:13:30 +00:00
|
|
|
end
|