-
Notifications
You must be signed in to change notification settings - Fork 4
/
git-related
executable file
·328 lines (280 loc) · 7.08 KB
/
git-related
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
#!/usr/bin/env ruby
require 'parseopt'
$since = '5-years-ago'
$threshold = 10
$patches = []
$rev_args = []
$show_commits = false
$roles = false
$base_rev = 'master'
$files = false
$persons = {}
$mailmaps = {}
$base_dir = %x[git rev-parse --show-toplevel].chomp
ROLE_MAP = {
'signed-off-by' => :signer,
'acked-by' => :acker,
'reviewed-by' => :reviewer,
'helped-by' => :helper,
'mentored-by' => :mentor,
'suggested-by' => :advisor,
'cc' => :observer,
'noticed-by' => :sentry,
'tested-by' => :tester,
'improved-by' => :enhancer,
'thanks-to' => :helper,
'based-on-patch-by' => :patcher,
'based-on-a-patch-by' => :patcher,
'contributions-by' => :contributor,
'co-authored-by' => :coauthor,
'requested-by' => :solicitor,
'original-patch-by ' => :patcher,
}
ROLE_REGEX = /^([^ \t\n\[\]\(\):]+): ([^<>]+) <(\S+?)>$/
def get_mailmap(filename)
return unless File.exists?(filename)
n = /([^<>]+?)/
e = /<(\S+)>/
File.foreach(filename) do |line|
case line.gsub(/\s*#.*$/, '')
when /^(?:#{n})?(?:\s*#{e})?\s+#{e}$/
$mailmaps[[nil, $3]] = [ $1, $2 ]
when /^#{n}\s+#{e}(?:\s+#{n})?(?:\s+#{e})?$/
$mailmaps[[$3, $4]] = [ $1, $2 ]
end
end
end
get_mailmap(File.join($base_dir, '.mailmap'))
mailmap_file = %x[git config mailmap.file].chomp
get_mailmap(mailmap_file)
opts = ParseOpt.new
opts.usage = 'git related [options] <files | rev-list options>'
opts.on('t', 'threshold', 'Minimum percentage threshold of participation') do |v|
$threshold = v.to_i
end
opts.on('s', 'since', 'How far back to search for relevant commits') do |v|
$since = v
end
opts.on('c', 'commits', 'List commits instead of persons') do |v|
$show_commits = v
end
opts.on('r', 'roles', 'Display specific roles') do |v|
$roles = v
end
opts.on('f', 'files', 'Gather file history (not blame)') do |v|
$files = v
end
opts.parse
class Person
attr_reader :roles
def initialize(name, email)
@name = name
@email = email
@commits = {}
@roles = Hash.new(0)
end
def add_role(commit, role)
@commits[commit] = true
@roles[role] += 1
end
def weight
@commits.size
end
def <=>(b)
weight <=> b.weight
end
def to_s
'%s <%s>' % [@name, @email]
end
def self.get(name, email)
map = $mailmaps[[name, email]]
map ||= $mailmaps[[nil, email]]
map ||= $mailmaps[[name, nil]]
map ||= []
map_name, map_email = map
name, email = [ map_name || name, map_email || email ]
$persons[email.downcase] ||= Person.new(name, email)
end
end
class Commit
attr_accessor :boundary
def initialize(id)
@id = id
end
def parse(data)
in_body = author = nil
header, body = data.split("\n\n", 2)
if header =~ /^author ([^<>]+) <(\S+)> /
author = Person.get($1, $2)
author.add_role(@id, :author)
end
return unless $roles
trailer = body.split("\n\n").last
trailer.scan(ROLE_REGEX) do
person = Person.get($2, $3)
role = ROLE_MAP[$1.downcase]
next if role == :signer and person == author
person.add_role(@id, role)
end
end
end
class Commits
def initialize
@items = {}
@main_commits = {}
@sources = Hash.new { |hash, key| hash[key] = [] }
end
def size
@items.size
end
def each(&block)
@items.each(&block)
end
def list
@items.keys
end
def import
return if @items.empty?
IO.popen(%w[git cat-file --batch], 'r+', external_encoding: 'utf-8') do |pipe|
pipe.write(@items.keys.join("\n"))
pipe.close_write
pipe.each do |line|
if line =~ /^(\h{40}) commit (\d+)/
id, len = $1, $2
data = pipe.read($2.to_i)
@items[id].parse(data)
end
end
end
end
def get_blame(source, from, ranges)
ranges = ranges.map { |range| '-L%u,+%u' % range }
id = nil
IO.popen(['git', 'blame', '--incremental', '-C', '-C',
*ranges,
'--since', $since, from + '~',
'--', source], external_encoding: 'utf-8') do |pipe|
pipe.each do |line|
if line =~ /^\h{40}/
id = $&
@items[id] ||= Commit.new(id) if not @main_commits.include?(id)
elsif line == "boundary\n"
@items[id].boundary = true
end
end
end
end
def scan(io)
id = nil
source = nil
io.each do |line|
case line
when /^From (\h{40}) /, /^\0(\h{40})$/
id = $1
@main_commits[id] = true
when /^---\s+(\S+)$/
source = $1 != '/dev/null' ? $1[2..-1] : nil
when /^@@ -(\d+)(?:,(\d+))?/
next unless source and id
start, len = [$1, $2]
next if !len || len == '0'
@sources[[source, id]] << [start, len]
end
end
end
def blame
if not $files
Dir.chdir($base_dir) do
@sources.each do |(source, id), ranges|
get_blame(source, id, ranges);
end
end
@items.reject! { |_,commit| commit.boundary }
else
files = @sources.map { |(file, _), _| file }.uniq
IO.popen(%w[git rev-list -n 30 --no-merges] + [$base_rev, '--'] + files) do |pipe|
pipe.each(chomp: true) do |id|
@items[id] ||= Commit.new(id) if not @main_commits.include?(id)
end
end
end
end
def from_patches(files)
files.each do |file|
File.open(file) do |f|
scan(f)
end
end
end
def from_rev_args(args)
%x[git rev-parse --verify --quiet @{upstream}]
default = $?.success? ? '@{upstream}' : 'master'
revs = IO.popen(%W[git rev-parse --revs-only --default #{default} --symbolic] + args).readlines(chomp: true)
if revs.size == 1
r = revs[0]
$base_rev = r
r = '^' + r if r[0] != '-'
args = [ '@', r ]
else
args = revs
$base_rev = revs[1][1..-1]
end
if not $files
IO.popen(%w[git log -C --format=%x00%H -p] + args) do |pipe|
scan(pipe)
end
else
IO.popen(%w[git log -C --format= --name-only] + args) do |pipe|
pipe.each(chomp: true) do |file|
@sources[[file, nil]] = true
end
end
end
end
end
ARGV.each do |e|
if File.exists?(e)
$patches << e
else
$rev_args << e
end
end
commits = Commits.new
if $patches.empty?
commits.from_rev_args($rev_args)
else
commits.from_patches($patches)
end
commits.blame
commits.import
if $show_commits
cmd = nil
case $show_commits
when 'raw'
puts commits.list
when 'full'
cmd = %w[git log --patch --no-walk]
when 'long'
cmd = %w[git log --no-walk]
else
cmd = %w[git log --oneline --no-walk]
end
system(*cmd + commits.list) if cmd
exit 0
end
$persons.values.sort.reverse.each do |person|
percent = 100 * person.weight.to_f / commits.size
next if percent < $threshold
unless $roles
summary = '%i%%' % percent.round
else
roles = person.roles.sort_by(&:last).reverse.map do |role, role_count|
role_percent = (100 * role_count.to_f / commits.size).round
next if role_percent == 0
'%s: %i%%' % [role, role_percent]
end
summary = roles.compact.join(', ')
end
format = STDOUT.isatty ? "%s \e[1;34m(%s)\e[0m" : '%s (%s)'
puts format % [person, summary]
end