Skip to content

Instantly share code, notes, and snippets.

@pete
Created January 25, 2011 22:19
Show Gist options
  • Save pete/795805 to your computer and use it in GitHub Desktop.
Save pete/795805 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'time'
Commands = {
:git => 'git annotate %s',
:svn => 'svn blame --force %s',
}
Filters = Hash.new(lambda { |line| line })
Filters[:git] = lambda { |line|
line.force_encoding Encoding::BINARY
revision, name, time, line =
/(\w+)\s*\(([^\t]+)\t([^\t]+)\t[^\)]+\)(.*)/.match(line).to_a[1..-1]
time = Time.parse time
"%s %s %10s | %s\n" % [
revision, time.strftime('%F %R'), name[0,10], line
]
}
files = ARGV.dup #.map { |a| File.expand_path a }
noexist = files.select { |f| !File.exist?(f) }
unless noexist.empty?
$stderr.puts "Fictitious files passed as args:\n\t#{noexist.join(' ')}"
exit 1
end
def figure_out_repotype path = Dir.pwd
while path != '/'
if File.directory?(File.join(path, ".git"))
return :git
elsif File.directory?(File.join(path, ".svn"))
return :svn
end
path = File.dirname path
end
$stderr.puts "Couldn't figure out what sort of repo this is!"
exit 2
end
type = figure_out_repotype
command = Commands[type]
files.each { |f|
print *IO.popen(command % f, 'r').readlines.map(&Filters[type])
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment