Created
December 14, 2013 01:01
-
-
Save jmdeldin/7954311 to your computer and use it in GitHub Desktop.
Script for finding old code.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env ruby | |
# | |
# Find really old code. | |
# | |
# Author: Jon-Michael Deldin | |
# Date: 2013-12-13 | |
# | |
# Usage: | |
# oldies [DIR] | |
# | |
require 'parallel' | |
require 'optparse' | |
$LONGEST_FILENAME = 45 | |
$FMT = "%-#{$LONGEST_FILENAME + 5}s %-12s %-12s" | |
FileEntry = Struct.new(:filename, :oldest_line_date, :newest_line_date) | |
def parse_args(argv) | |
options = {} | |
parser = OptionParser.new do |opts| | |
opts.on('--e LIST', '--exclude', 'Comma-delimited list of directories to exclude') do |exclude| | |
options[:exclude] = exclude.split(/,/) | |
end | |
opts.on('-h', '--help', 'show this screen') do | |
STDERR.puts opts | |
abort | |
end | |
opts.on('-j PROCESSES', '--processes', 'Number of processes to run') do |j| | |
options[:processes] = Integer(j) | |
end | |
end | |
parser.parse!(argv) | |
options[:dir] = argv.first || Dir.getwd | |
options | |
end | |
def dir_regexp(dirs) | |
return nil unless dirs | |
pattern = '^(' + dirs.join('|') + ')' | |
/#{pattern}/ | |
end | |
def blame(dir, exclude, processes) | |
pattern = dir_regexp(exclude) | |
Parallel.map(Dir.glob('**/*.rb'), :in_processes => processes) { |file| | |
next if pattern && file =~ pattern | |
blames = `git blame #{file} 2>/dev/null` | |
next unless $?.success? | |
blames = blames.split(/\n/).map { |line| line[/\d{4}-\d{2}-\d{2}/] }.compact.sort | |
$LONGEST_FILENAME = file.length if file.length > $LONGEST_FILENAME | |
FileEntry.new(file, blames.first, blames.last) | |
}.compact | |
end | |
if $0 == __FILE__ | |
options = parse_args(ARGV) | |
dir = options.fetch(:dir) | |
abort("#{dir} does not exist") unless Dir.exists?(dir) | |
Dir.chdir(dir) | |
entries = blame(dir, options.fetch(:exclude, []), options.fetch(:processes, 2)) | |
hdr = $FMT % %w(filename oldest_line newest_line) | |
puts hdr, "-" * hdr.length | |
entries.sort_by { |e| e.oldest_line_date }.each do |e| | |
puts $FMT % [e.filename, e.oldest_line_date, e.newest_line_date] | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment