Path: blob/master/tools/modules/module_commits.rb
19849 views
#!/usr/bin/env ruby12##3# This module requires Metasploit: https://metasploit.com/download4# Current source: https://github.com/rapid7/metasploit-framework5##67#8# Check the commit history of a module or tree of modules.9# and sort by number of commits.10#11# Usage: tools/module_commits.rb [module dir | module fname]12#1314require 'find'1516class GitLogLine < Struct.new(:date, :hash, :author, :message)17end1819class CommitHistory < Struct.new(:fname, :total, :authors)20end2122msfbase = __FILE__2324while File.symlink?(msfbase)25msfbase = File.expand_path(File.readlink(msfbase), File.dirname(msfbase))26end27msfbase = File.expand_path(File.join(File.dirname(msfbase), '..', '..'))2829dir = ARGV[0] || File.join(msfbase, "modules", "exploits")30raise ArgumentError, "Need a filename or directory" unless (dir and File.readable? dir)3132def check_commit_history(fname)3334git_cmd = `git log --pretty=format:"%ad %h '%aN' %f" --date=short --date-order #{fname}`35commit_history = []36commits_by_author = {}3738git_cmd.each_line do |line|39parsed_line = line.match(/^([^\s+]+)\s(.{7,})\s'(.*)'\s(.*)[\r\n]*$/)40commit_history << GitLogLine.new(*parsed_line[1,4])41end4243commit_history.each do |logline|44commits_by_author[logline.author] ||= []45commits_by_author[logline.author] << logline.message46end4748puts "Commits for #{fname} #{commit_history.size}"49puts "-" * 725051commits_by_author.sort_by {|k,v| v.size}.reverse.each do |k,v|52puts "%-25s %3d" % [k,v.size]53end5455this_module = CommitHistory.new(fname,commit_history.size,commits_by_author)56return this_module5758end5960@module_stats = []6162Find.find(dir) do |fname|63next unless fname =~ /\.(rb|py)$/ # be either ruby or python64next unless File.file?(fname) && File.readable?(fname)65@module_stats << check_commit_history(fname)66end6768puts "=" * 7269puts "Sorted modules by commit counts"7071@module_stats.sort_by {|m| m.total }.reverse.each do |m|72puts "%-60s %d" % [m.fname, m.total]73end747576