Skip to content

Instantly share code, notes, and snippets.

@leonid-shevtsov
Created December 2, 2010 10:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leonid-shevtsov/725094 to your computer and use it in GitHub Desktop.
Save leonid-shevtsov/725094 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'optparse'
require 'time'
options = {}
OptionParser.new do |opts|
opts.banner = "Usage: git-timesheet [options]\nProduce a per-day activity report from the log of the current git repository.\n\n"
opts.on("-s", "--since [TIME]", "Start date for the report (default is 1 week ago)") do |time|
options[:since] = time
end
opts.on("-a", "--author [EMAIL]", "User for the report (default is the author set in git config)") do |author|
options[:author] = author
end
opts.on(nil, '--authors', 'List all available authors') do |authors|
options[:authors] = authors
end
end.parse!
options[:since] ||= '1 week ago'
if options[:authors]
authors = `git log --no-merges --simplify-merges --format="%an (%ae)" --since="#{options[:since].gsub('"','\\"')}"`.strip.split("\n").uniq
puts authors.join("\n")
else
options[:author] ||= `git config --get user.email`
log_lines = `git log --no-merges --simplify-merges --author="#{options[:author].gsub('"','\\"')}" --format="%ad %s <%h>" --date=iso --since="#{options[:since].gsub('"','\\"')}"`.split("\n")
day_entries = log_lines.inject({}) {|days, line|
timestamp = Time.parse line.slice!(0,25)
day = timestamp.strftime("%Y-%m-%d")
days[day] ||= []
days[day] << timestamp.strftime("%H:%S ") + line.strip
days
}.sort{|a,b| a[0]<=>b[0]}
puts day_entries.map{|day, entries| "#{day}\n#{'='*10}\n\n#{entries.sort.join("\n")}\n\n"}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment