Skip to content

Instantly share code, notes, and snippets.

@rafops
Created December 5, 2015 15:07
Show Gist options
  • Save rafops/652bbd70918ce7144975 to your computer and use it in GitHub Desktop.
Save rafops/652bbd70918ce7144975 to your computer and use it in GitHub Desktop.
Display worked hours statistics based on git log
#!/usr/bin/ruby
require 'date'
author="[email protected]"
period=(ARGV[0].to_i) || 30 # days
after = (DateTime.now - period).strftime('%Y-%m-%d')
log = %x{git log --all --author=#{author} --after=#{after} --date=iso-strict --pretty='%H|%cd'}
daily = {}
log.split(/\n/).each do |line|
commit, date = line.split(/\|/)
parsed_date = DateTime.parse(date)
dk = parsed_date.strftime("%Y-%m-%d")
if daily.has_key?(dk)
daily[dk][:first_commit] = commit
daily[dk][:start_date] = parsed_date
else
daily.store(dk, {
first_commit: commit,
last_commit: commit,
start_date: parsed_date,
end_date: parsed_date
})
end
end
def df(d)
d.strftime('%H:%M')
end
puts "DATE START END HOURS VOLUME DENSITY"
daily.each do |dk,daily|
fc = daily[:first_commit]
lc = daily[:last_commit]
lc = "#{lc}~1" if fc == lc
ed = daily[:end_date]
sd = daily[:start_date]
total_time = ((ed - sd) * 24).to_f.round(1)
volume = %x{git diff #{fc} #{lc}}.size
density = total_time > 0 ? (volume / total_time).round(0) : 0
puts "#{dk} #{df(sd)} ~ #{df(ed)} #{total_time.to_s.rjust(5)} #{volume.to_s.rjust(8)} #{density.to_s.rjust(8)}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment