Last active
July 28, 2019 12:26
-
-
Save hgwr/fb67d28d28331017819b2b2146b9eb94 to your computer and use it in GitHub Desktop.
weekly_git_stats.rb
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 | |
# | |
# Usage: | |
# weekly_git_stats.rb | |
# weekly_git_stats.rb 2019-01-01 2019-07-31 | |
# | |
# see also: cloc --vcs git | |
# | |
require 'date' | |
require 'open3' | |
begin | |
start_date = Date.parse(ARGV[0]) | |
end_date = Date.parse(ARGV[1]) | |
rescue => e | |
STDERR.puts "Warning: #{e.message}" | |
end_date = Date.today | |
start_date = end_date - 365 | |
end | |
# start_str = start_date.strftime("%Y-%m-%d") | |
# end_str = end_date.strftime("%Y-%m-%d") | |
# STDERR.puts "#{start_str} -> #{end_str}" | |
num_weeks = 0 | |
total_changes = 0 | |
current_date = start_date.clone | |
while current_date < end_date do | |
week_start_str = current_date.strftime("%Y-%m-%d") | |
week_end_str = (current_date + 7).strftime("%Y-%m-%d") | |
cmd = %Q(git whatchanged --since="#{week_start_str}" --until="#{week_end_str}" --shortstat) | |
# puts cmd | |
stdout, * = Open3.capture3(cmd) | |
num_changed_lines = 0 | |
stdout.each_line do |line| | |
matched = /([0-9]+) files changed, ([0-9]+) insertions\(\+\), ([0-9]+) deletions\(-\)/.match(line) | |
if matched | |
num_changed_lines += matched[2].to_i + matched[3].to_i | |
end | |
end | |
if num_changed_lines > 0 | |
puts "#{week_start_str} -> #{week_end_str}: #{num_changed_lines}" | |
num_weeks += 1 | |
end | |
total_changes += num_changed_lines | |
current_date += 7 | |
end | |
puts "per week: #{(total_changes / num_weeks).to_f}" if num_weeks > 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment