Skip to content

Instantly share code, notes, and snippets.

@romuloceccon
Last active April 30, 2018 09:58
Show Gist options
  • Save romuloceccon/db37d28d23416e2e24ba8cabdad691fb to your computer and use it in GitHub Desktop.
Save romuloceccon/db37d28d23416e2e24ba8cabdad691fb to your computer and use it in GitHub Desktop.
git stats by author
git ls-files -z | xargs -0n1 git blame -e | perl -e 'while (<>) { s/^.*?<(.*?)>.*$/\1/; print $_; }' | sort -f | uniq -c | sort -n
git log --numstat --pretty=tformat:"%ae|%ai" | ruby stat.rb
require 'date'
def print_stats(fmt, label, commits)
a = r = 0
days = []
commits.each { |c| a += c[1]; r += c[2]; days << c[0] }
c = days.uniq.size
puts(fmt % [label, a, a / c, r, r / c, c, commits.size])
end
def sum_added(arr)
arr.inject(0) { |s, v| s + v[1] }
end
def sorted_stat_keys(stats)
stats.keys.sort { |a, b| sum_added(stats[a]) <=> sum_added(stats[b]) }
end
stats = Hash.new { |h, k| h[k] = [] }
cur_author = nil
cur_commit = nil
while !STDIN.eof?
line = STDIN.readline.strip
if m = line.match(/^(\d+)\s+(\d+)\s+\S/)
added, removed = m[1..2].map { |x| x.to_i }
cur_commit[1] += added
cur_commit[2] += removed
elsif m = line.match(/^([^-].+)\|(.+)/)
cur_author = m[1]
cur_commit = [DateTime.parse(m[2]).strftime('%F'), 0, 0]
stats[cur_author] << cur_commit
end
end
maxlen = stats.keys.map { |x| x.size }.max
fmt = "%-#{maxlen}s %10d %10d %10d %10d %10d %10d"
puts("%-#{maxlen}s added add/day removed rem/day uniq days commits" % "author")
puts("")
sorted_stat_keys(stats).each do |committer|
commits = stats[committer].sort { |a, b| a[1] <=> b[1] }
print_stats(fmt, committer, commits)
s = commits.size
# reject 5% less- and 5% most-prolific commits
lo, hi = (s * 0.05).round, (s * 0.95).round
commits = commits[lo..hi]
print_stats(fmt, ' (center 90%)', commits)
puts("")
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment