Skip to content

Instantly share code, notes, and snippets.

@citrus
Created December 18, 2011 02:31
Show Gist options
  • Save citrus/1492180 to your computer and use it in GitHub Desktop.
Save citrus/1492180 to your computer and use it in GitHub Desktop.
Gets time spent on a project with git log.
#! /usr/bin/env ruby
# encoding: UTF-8
require 'date'
since = ARGV.shift || Date.today
logs = `git log --since="#{since}" --pretty=format:"%H,%ae,%ai"`
all_commits = logs.split("\n").map{|i| i.split(",") }
if all_commits.empty?
puts "No commits found since #{since}"
exit
end
def parse_time(time)
DateTime.parse(time).to_time
end
def get_hours(commits)
t1 = parse_time(commits.first[2])
t2 = parse_time(commits.last[2])
((t1 - t2) / 3600).round(3)
end
def stats_for(label, commits)
indent = " " * (20 - label.length).abs
hours = get_hours(commits)
print "#{indent}#{label}:\t"
print hours
print " hours"
puts
end
stats_for("Total", all_commits)
all_commits.group_by{|i| i[1] }.each do |user, user_commits|
stats_for(user, user_commits)
end
@tomgross
Copy link

Ruby 1.9 is needed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment