|
1 | 1 | #!/usr/bin/env ruby
|
2 | 2 |
|
3 |
| -# Check that modules actually pass msftidy checks first. |
4 |
| -# To install this script, make this your pre-commit hook your local |
5 |
| -# metasploit-framework clone. For example, if you have checked out |
6 |
| -# the Metasploit Framework to: |
| 3 | +# Check that modules actually pass msftidy checks before committing |
| 4 | +# or after merging. |
7 | 5 | #
|
8 |
| -# /home/mcfakepants/git/metasploit-framework |
| 6 | +# Simply symlink this script to your local .git/hooks/pre-commit script |
| 7 | +# and your .git/hooks/post-merge scripts. Note the lack of a trailing |
| 8 | +# .rb |
9 | 9 | #
|
10 |
| -# then you will copy this script to: |
| 10 | +# If you are in the top-level dir, the symlink commands would be: |
11 | 11 | #
|
12 |
| -# /home/mcfakepants/git/metasploit-framework/.git/hooks/pre-commit |
| 12 | +# ln -sf ../../tools/dev/pre-commit-hook.rb .git/hooks/pre-commit |
| 13 | +# ln -sf ../../tools/dev/pre-commit-hook.rb .git/hooks/post-merge |
13 | 14 | #
|
14 |
| -# You must mark it executable (chmod +x), and do not name it |
15 |
| -# pre-commit.rb (just pre-commit) |
16 |
| -# |
17 |
| -# If you want to keep up on changes with this hook, just: |
18 |
| -# |
19 |
| -# ln -sf <this file> <path to commit hook> |
| 15 | +# That way, you will track changes to this script when it updates |
| 16 | +# (rarely). If you'd prefer to copy it directly, that's okay, too (mark |
| 17 | +# it +x and don't name it filename.rb, just filename). |
| 18 | + |
| 19 | +def merge_error_message |
| 20 | + msg = [] |
| 21 | + msg << "[*] This merge contains modules failing msftidy.rb" |
| 22 | + msg << "[*] Please fix this if you intend to publish these" |
| 23 | + msg << "[*] modules to a popular metasploit-framework repo" |
| 24 | + puts "-" * 72 |
| 25 | + puts msg.join("\n") |
| 26 | + puts "-" * 72 |
| 27 | +end |
20 | 28 |
|
21 | 29 | valid = true # Presume validity
|
22 | 30 | files_to_check = []
|
23 | 31 |
|
24 |
| -results = %x[git diff --cached --name-only] |
| 32 | +# Who called us? If it's a post-merge check things operate a little |
| 33 | +# differently. |
| 34 | +puts "[*] Running msftidy.rb in #{$0} mode" |
| 35 | + |
| 36 | +case $0 |
| 37 | +when /post-merge/ |
| 38 | + base_caller = :post_merge |
| 39 | +when /pre-commit/ |
| 40 | + base_caller = :pre_commit |
| 41 | +else |
| 42 | + base_caller = :msftidy |
| 43 | +end |
| 44 | + |
| 45 | +if base_caller == :post_merge |
| 46 | + changed_files = %x[git diff --name-only HEAD^ HEAD] |
| 47 | +else |
| 48 | + changed_files = %x[git diff --cached --name-only] |
| 49 | +end |
25 | 50 |
|
26 |
| -results.each_line do |fname| |
| 51 | +changed_files.each_line do |fname| |
27 | 52 | fname.strip!
|
28 | 53 | next unless File.exist?(fname) and File.file?(fname)
|
29 | 54 | next unless fname =~ /modules.+\.rb/
|
30 | 55 | files_to_check << fname
|
31 | 56 | end
|
32 | 57 |
|
33 | 58 | if files_to_check.empty?
|
34 |
| - puts "--- No Metasploit modules to check, committing. ---" |
| 59 | + puts "--- No Metasploit modules to check ---" |
35 | 60 | else
|
36 |
| - puts "--- Checking module syntax with tools/msftidy.rb ---" |
| 61 | + puts "--- Checking new and changed module syntax with tools/msftidy.rb ---" |
37 | 62 | files_to_check.each do |fname|
|
38 | 63 | cmd = "ruby ./tools/msftidy.rb #{fname}"
|
39 | 64 | msftidy_output= %x[ #{cmd} ]
|
|
43 | 68 | puts line
|
44 | 69 | end
|
45 | 70 | end
|
46 |
| - puts "-" * 52 |
| 71 | + puts "-" * 72 |
47 | 72 | end
|
48 | 73 |
|
49 | 74 | unless valid
|
50 |
| - puts "msftidy.rb objected, aborting commit" |
51 |
| - puts "To bypass this check use: git commit --no-verify" |
52 |
| - puts "-" * 52 |
53 |
| - exit(1) |
| 75 | + if base_caller == :post_merge |
| 76 | + puts merge_error_message |
| 77 | + exit(0x10) |
| 78 | + else |
| 79 | + puts "[!] msftidy.rb objected, aborting commit" |
| 80 | + puts "[!] To bypass this check use: git commit --no-verify" |
| 81 | + puts "-" * 72 |
| 82 | + exit(0x01) |
| 83 | + end |
| 84 | + |
54 | 85 | end
|
0 commit comments