69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
|
# File 'lib/todidnt.rb', line 69
def self.history(options)
GitRepo.new(options[:path]).run do |path|
log = GitCommand.new(:log, [['-G', 'TODO'], ['--format="COMMIT %an %ae %at"'], ['-p'], ['-U0']])
history = []
blame_hash = {}
puts "Going through log..."
patch_additions = []
patch_deletions = []
filename = nil
total = log.output_lines.count
log.output_lines.reverse.each do |line|
if (summary = /^COMMIT (.*) (.*) (.*)/.match(line))
name = summary[1]
email = summary[2]
time = summary[3]
unless filename =~ TodoLine::IGNORE
patch_additions.each do |line|
blame_hash[line] ||= []
blame_hash[line] << name
end
deletions_by_author = {}
patch_deletions.each do |line|
author = blame_hash[line] && blame_hash[line].pop
if author
deletions_by_author[author] ||= 0
deletions_by_author[author] += 1
else
puts "BAD BAD can't find original author: #{line}"
end
end
history << {
:timestamp => time.to_i,
:author => name,
:additions => patch_additions.count,
:deletions => deletions_by_author[name] || 0
}
deletions_by_author.delete(name)
deletions_by_author.each do |author, deletion_count|
history << {
:timestamp => time.to_i,
:author => author,
:additions => 0,
:deletions => deletion_count
}
end
end
patch_additions = []
patch_deletions = []
elsif (diff = /diff --git a\/(.*) b\/(.*)/.match(line))
filename = diff[1]
elsif (diff = /^\+(.*TODO.*)/.match(line))
patch_additions << diff[1]
elsif (diff = /^\-(.*TODO.*)/.match(line))
patch_deletions << diff[1]
end
end
history.sort_by! {|slice| slice[:timestamp]}
min_commit_date = Time.at(history.first[:timestamp])
max_commit_date = Time.at(history.last[:timestamp])
timespan = max_commit_date - min_commit_date
if timespan > 86400 * 365 * 10 interval = 86400 * 365 elsif timespan > 86400 * 365 * 5 interval = 86400 * (365 / 2) elsif timespan > 86400 * 365 interval = 86400 * (365 / 4) elsif timespan > 86400 * 30 * 6 interval = 86400 * 30 elsif timespan > 86400 * 1 interval = 86400 * 7
else interval = 86400 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
puts "Finalizing timeline..."
buckets = []
current_bucket_authors = {}
bucket_total = 0
i = 0
while i < history.length
should_increment = false
slice = history[i]
author = slice[:author]
if slice[:timestamp] >= interval_start && slice[:timestamp] < interval_end
current_bucket_authors[author] ||= 0
current_bucket_authors[author] += slice[:additions] - slice[:deletions]
bucket_total += slice[:additions] - slice[:deletions]
should_increment = true
end
if i == (history.length - 1) || history[i + 1][:timestamp] >= interval_end
buckets << {
:timestamp => Time.at(interval_start).strftime('%D'),
:authors => current_bucket_authors,
:total => bucket_total
}
interval_start += interval
interval_end += interval
current_bucket_authors = current_bucket_authors.clone
end
i += 1 if should_increment
end
authors = Set.new
contains_other = false
buckets.each do |bucket|
significant_authors = {}
other_count = 0
bucket[:authors].each do |author, count|
if count > bucket[:total] * 0.03
significant_authors[author] = count
authors << author
else
other_count += count
end
end
if other_count > 0
significant_authors['Other'] = other_count
contains_other = true
end
bucket[:authors] = significant_authors
end
if contains_other
authors << 'Other'
end
file_path = HTMLGenerator.generate(:history, :data => {:history => buckets.map {|h| h[:authors].merge('Date' => h[:timestamp]) }, :authors => authors.to_a})
Launchy.open("file://#{file_path}")
end
end
|