Created
January 29, 2018 21:34
-
-
Save mlc/2199846f4d56524ef4829e9425e10720 to your computer and use it in GitHub Desktop.
This file contains 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 | |
# | |
# Utility to count the number of lines added to a git repository by month and file extension. | |
# | |
require 'csv' | |
require 'set' | |
stats = {} | |
date = nil | |
open("|git log --numstat", "r").each_line do |line| | |
case line | |
when /^Date:[[:space:]]+... ([A-Za-z]{3}) [1-3]?[0-9] [0-2][0-9]:[0-5][0-9]:[0-5][0-9] ([0-9]{4})/ | |
date = $1 + " " + $2 | |
stats[date] = {} unless stats.has_key?(date) | |
when /^Date/ | |
raise line | |
when /^([0-9]+)[[:space:]]+([0-9]+)[[:space:]]+.*?\.([a-z]+)$/ | |
ext = $3 | |
added = $1.to_i | |
stats[date][ext] = (stats[date][ext] || 0) + added | |
end | |
end | |
keys = stats.inject(Set.new) { |accum, (k,v)| accum | v.keys }.to_a.sort | |
csv = CSV.generate do |csv| | |
csv << ["Month"] + keys | |
stats.each do |date, data| | |
csv << [date] + keys.map{|k| data[k] || 0} | |
end | |
end | |
puts csv |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment