Skip to content

Instantly share code, notes, and snippets.

@hgwr hgwr/weekly_git_stats.rb
Last active Jul 28, 2019

Embed
What would you like to do?
weekly_git_stats.rb
#!/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
You can’t perform that action at this time.