git-timecost/bin/git-timetrack-log

271 lines
5 KiB
Text
Raw Normal View History

2012-12-31 11:15:31 +00:00
#!/usr/bin/env ruby
# vim: set syntax=ruby ts=4 sw=4 noet :
require 'pp'
require 'date'
require 'optparse'
class GitExtractor
class Commit
attr_accessor :author, :commit, :date, :note
def initialize commit
@commit = commit
@note = nil
@author = nil
@date = nil
end
end
class Range
attr_accessor :time_start, :time_stop, :commits
2012-12-31 11:36:55 +00:00
def initialize config, commit
@config = config
2012-12-31 11:15:31 +00:00
@time_stop = DateTime.parse(commit.date)
@time_start = @time_stop - (1.5 / 24.0)
@commits = [commit]
self
end
def merge range
# B -----[----]----
# A --[----]------
# = ---[------]----
# minimum of both
new_start = if range.time_start < @time_start then range.time_start
else @time_start
end
new_end = if range.time_stop >= @time_stop then range.time_stop
else @time_stop
end
@time_start = new_start
@time_stop = new_end
@commits.concat range.commits
end
def overlap? range
result = false
# Ref ----[----]-----
# overlapping :
# A -[----]--------
# B -------[----]--
# C -[----------]--
# D ------[]-------
# non-overlapping :
# E -[]------------
# F -----------[]--
start_before_start = (range.time_start < @time_start)
start_after_start = (range.time_start >= @time_start)
start_after_stop = (range.time_start >= @time_stop)
start_before_stop = (range.time_start < @time_stop)
stop_before_stop = (range.time_stop < @time_stop)
stop_after_stop = (range.time_stop >= @time_stop)
stop_before_start = (range.time_stop < @time_start)
stop_after_start = (range.time_stop >= @time_start)
# A case
if start_before_start and start_before_stop and
stop_after_start and stop_before_stop then
result = true
end
# B case
if start_after_start and start_before_stop and
stop_after_start and stop_after_stop then
result = true
end
# C case
if start_before_start and start_before_stop and
stop_after_start and stop_after_stop then
result = true
end
# D case
if start_after_start and start_before_stop and
stop_after_start and stop_before_stop then
result = true
end
return result
end
def fixed_start
return @time_start + (0.5/24.0)
end
def diff
return ("%.2f" % ((@time_stop - fixed_start).to_f * 24)).to_f
end
def to_s
val = "(%s) %s - %s\n" % [diff, fixed_start, @time_stop]
@commits.each do |commit|
2012-12-31 11:36:55 +00:00
lines = []
unless @config[:author_filter_enable] then
lines.push commit.author
end
lines.concat commit.note.split(/\n/)
r = lines.map{ |s| "\t %s" % s }.join "\n"
r[1] = '*'
2012-12-31 11:15:31 +00:00
val += r + "\n"
end
return val
end
end
class RangeList
def initialize
@ranges = []
end
def add range
merged = false
# merge
@ranges.each do |old|
#pp old
if old.overlap? range then
old.merge range
merged = true
end
end
# add if needed
if not merged then
@ranges.push range
end
end
2012-12-31 11:36:55 +00:00
def each
@ranges.each do |r|
yield r
end
end
2012-12-31 11:15:31 +00:00
def sum
result = 0
@ranges.each do |r|
result += r.diff
end
return result
end
end
def initialize
# FIXME: accept multiple authors
@config = {
2012-12-31 11:36:55 +00:00
:author_filter_enable => false,
2012-12-31 11:15:31 +00:00
:author_filter => ".*?",
:verbose => false
}
@rangelist = nil
end
def parse_cmdline args
2012-12-31 11:36:55 +00:00
opts = OptionParser.new do |opts|
2012-12-31 11:15:31 +00:00
opts.banner = "Usage: #{File.basename $0} [options]"
2012-12-31 11:36:55 +00:00
opts.on_tail("-v","--verbose", "Run verbosely") do |v|
2012-12-31 11:15:31 +00:00
@config[:verbose] = true
end
2012-12-31 11:36:55 +00:00
opts.on_tail("-h","--help", "Show this help") do
puts opts
exit 0
end
2012-12-31 11:15:31 +00:00
opts.on("-a","--author AUTHOR", "Only keep commits for AUTHOR") do |author|
2012-12-31 11:36:55 +00:00
puts "set commit author to #{author}"
2012-12-31 11:15:31 +00:00
@config[:author_filter] = author
2012-12-31 11:36:55 +00:00
@config[:author_filter_enable] = true
2012-12-31 11:15:31 +00:00
end
end
2012-12-31 11:36:55 +00:00
opts.parse! args
2012-12-31 11:15:31 +00:00
end
2012-12-31 11:36:55 +00:00
def analyze
2012-12-31 11:15:31 +00:00
# git log
# foreach, create time range (before) + logs
process = IO.popen ["git", "log", "--date=iso"]
@rangelist = RangeList.new
commit = nil
loop do
line = process.gets
break if line.nil?
line.strip!
case line
when /^commit (.*)$/ then
id = $1
# merge ranges & push
unless commit.nil? then
2012-12-31 11:36:55 +00:00
range = Range.new @config, commit
@rangelist.add range
2012-12-31 11:15:31 +00:00
end
commit = Commit.new id
#puts "commit #{id}"
when /^Author:\s*(.*?)\s*$/ then
2012-12-31 11:36:55 +00:00
unless commit.nil? then
commit.author = $1
2012-12-31 11:15:31 +00:00
2012-12-31 11:36:55 +00:00
if not commit.author =~ /#{@config[:author_filter]}/ then
commit = nil
# reject
end
2012-12-31 11:15:31 +00:00
end
when /^Date:\s*(.*?)\s*$/ then
2012-12-31 11:36:55 +00:00
unless commit.nil? then
commit.date = $1
end
2012-12-31 11:15:31 +00:00
when /^\s*$/ then
# skip
else
# add as note
2012-12-31 11:36:55 +00:00
unless commit.nil? then
commit.note = if commit.note.nil? then line
else commit.note + "\n" + line
end
end
2012-12-31 11:15:31 +00:00
end
def exec
end
end
end
def report
2012-12-31 11:36:55 +00:00
@rangelist.each do |r|
puts r.to_s + "\n"
end
puts "TOTAL: %.2f hours" % @rangelist.sum
2012-12-31 11:15:31 +00:00
exit 0
end
end
app = GitExtractor.new
app.parse_cmdline ARGV
2012-12-31 11:36:55 +00:00
app.analyze
2012-12-31 11:15:31 +00:00
app.report