/
blame.rb
41 lines (34 loc) · 1.06 KB
/
blame.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
require 'digest/md5'
module GitRank
module Blame
class << self
def calculate(options = {})
options[:exline] ||= []
authors = Hash.new {|h, k| h[k] = h[k] = Hash.new(0)}
options_digest = Digest::MD5.hexdigest(options[:exline].to_s)
get_files_to_blame.each do |file|
lines = blame(file)
lines.each do |line|
next if options[:exline].any? { |exline| line =~ /#{exline}/ }
# Get author info out of the line
# This will probably need improvements if people try to
# use this with weird names
line =~ / \((.*?)\d/
raise line unless $1
authors[$1.strip][file] += 1
end
end
authors
end
private
def blame(file)
lines = `git blame -w #{file}`.lines
puts "git blame failed on #{file}" unless $?.exitstatus == 0
lines
end
def get_files_to_blame
Dir.glob("**/*").reject { |f| !File.file? f or f =~ /\.git/ or File.binary? f }
end
end
end
end