Skip to content

Instantly share code, notes, and snippets.

@dux
Last active August 29, 2015 13:56
Show Gist options
  • Save dux/9325204 to your computer and use it in GitHub Desktop.
Save dux/9325204 to your computer and use it in GitHub Desktop.
Simple git statistics, prints git user and number of files and lines changed in last 14 days
#!/usr/bin/ruby
require 'colorize'
require 'optparse'
options = {}
op = OptionParser.new do |opts|
opts.banner = "Usage: git-stat.rb [options]"
opts.on("-d", "--days N", Integer, "In last x days") do |v|
options[:days] = v
end
opts.on("-f", "--folder STRING", String, "In what FOLDER? Defaults to app/") do |v|
options[:folder] = v
end
end
op.parse!
unless options[:days]
print op.help
print "\nBy @dux / 2014\n"
exit
end
options[:folder] ||= 'app/'
date = Time.now - 60*60*24*options[:days]
max_user_len = 1
report = []
for line in `git shortlog -sn`.split("\n")
git_user = line.split(/\s+/,3)[2]
max_user_len = git_user.length if git_user.length > max_user_len
stat = {}
stat[:added] = 0
stat[:removed] = 0
stat[:files] = {}
for line in `git log --author="#{git_user}" --since=#{date.to_s.split(' ')[0]} --pretty=tformat: --numstat | grep #{options[:folder]} `.split("\n")
added, removed, file = line.split(/\s+/,3)
stat[:added] += added.to_i
stat[:removed] += removed.to_i
stat[:files][file] = 1
end
total = stat[:added] - stat[:removed]
report << [git_user, stat[:files].keys.count, "#{total.to_s.red} lines changed in #{stat[:files].keys.count.to_s.yellow} files. #{stat[:added].to_s.yellow} added and #{stat[:removed].to_s.yellow} removed"]
end
print "#{''.rjust(max_user_len+1)} Git stat in last #{options[:days]} days for folder #{options[:folder]}\n#{''.rjust(max_user_len+2)}-\n"
for el in report.sort{ |a,b| b[1]<=>a[1] }
print "#{el[0].rjust(max_user_len).green}: #{el[2]}\n"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment