lib/todidnt/git_history.rb in todidnt-0.3.1 vs lib/todidnt/git_history.rb in todidnt-0.4.1
- old
+ new
@@ -1,14 +1,18 @@
module Todidnt
class GitHistory
attr_accessor :blames
- def initialize
+ def initialize(opts)
@history = []
@blames = {}
@unmatched_deletions = []
+
+ # Contributor threshold (e.g. only show as a separate contributor
+ # if they've contributed to > N% of TODOs).
+ @threshold = opts[:threshold]
end
def timeline!
# TODO: It would probably be better/simpler to just Marshal the
# GitHistory object itself.
@@ -63,10 +67,12 @@
commit = nil
seen_commits = Set.new
count = 0
command.execute! do |line|
+ line.encode!('UTF-8', 'binary', invalid: :replace, undef: :replace, replace: '')
+
if (diff = /diff --git a\/(.*) b\/(.*)/.match(line))
filename = diff[1]
elsif (diff = /^\+(.*TODO.*)/.match(line))
patch_additions << diff[1] unless filename =~ TodoLine::IGNORE
elsif (diff = /^\-(.*TODO.*)/.match(line))
@@ -150,20 +156,26 @@
timespan = max_commit_date - min_commit_date
# Figure out what the interval should be based on the total timespan.
if timespan > 86400 * 365 * 10 # 10+ years
interval = 86400 * 365 # years
+ timespan_label = 'years'
elsif timespan > 86400 * 365 * 5 # 5-10 years
interval = 86400 * (365 / 2) # 6 months
+ timespan_label = 'months'
elsif timespan > 86400 * 365 # 2-5 years
interval = 86400 * (365 / 4) # 3 months
+ timespan_label = 'months'
elsif timespan > 86400 * 30 * 6 # 6 months-3 year
interval = 86400 * 30 # months
- elsif timespan > 86400 * 1 # 1 month - 6 months
+ timespan_label = 'months'
+ elsif timespan > 86400 * 14 * 1 # 1/2 month - 6 months
interval = 86400 * 7
- else # 0 - 2 months
+ timespan_label = 'days'
+ else # 0 - 1/2 months
interval = 86400 # days
+ timespan_label = 'days'
end
original_interval_start = Time.new(min_commit_date.year, min_commit_date.month, min_commit_date.day).to_i
interval_start = original_interval_start
interval_end = interval_start + interval
@@ -173,11 +185,11 @@
current_bucket_authors = {}
bucket_total = 0
# Add the first bucket of 0
buckets << {
- :timestamp => Time.at(interval_start).strftime('%D'),
+ :timestamp => format_date(Time.at(interval_start), timespan_label),
:authors => {},
:total => 0
}
i = 0
@@ -198,11 +210,11 @@
# If we're on the last slice, or the next slice would have been
# in a new bucket, finish the current bucket.
if i == (@history.length - 1) || @history[i + 1][:timestamp] >= interval_end
buckets << {
- :timestamp => ([Time.at(interval_end), max_commit_date].min).strftime('%D'),
+ :timestamp => format_date(([Time.at(interval_end), max_commit_date].min), timespan_label),
:authors => current_bucket_authors,
:total => bucket_total
}
interval_start += interval
interval_end += interval
@@ -219,11 +231,11 @@
significant_authors = {}
other_count = 0
bucket[:authors].each do |author, count|
# Only include the author if they account for more than > 3% of
# the TODOs in this bucket.
- if count > bucket[:total] * 0.03
+ if count > bucket[:total] * @threshold
significant_authors[author] = count
authors << author
else
other_count += count
end
@@ -242,7 +254,19 @@
end
[buckets, authors]
end
+ private
+
+ def format_date(date, timespan_label)
+ case timespan_label
+ when 'years'
+ date.strftime('%Y')
+ when 'months'
+ date.strftime('%-m/%y')
+ when 'days'
+ date.strftime('%-m/%-d')
+ end
+ end
end
end