Skip to content

Instantly share code, notes, and snippets.

@felipec
Last active May 27, 2021 23:42
Show Gist options
  • Save felipec/ce491d351c88e4acbdbf6bc02a47223d to your computer and use it in GitHub Desktop.
Save felipec/ce491d351c88e4acbdbf6bc02a47223d to your computer and use it in GitHub Desktop.
git-top-trailers
#!/usr/bin/env ruby
ROLES_REGEX = /^([^ \t\n\[\]\(\):]+): ([^<>]+) <(\S+?)>$/
$roles = Hash.new(0)
log_args = [ '--no-merges', '--format=%b%x00' ]
IO.popen(%w[git log] + log_args + ARGV) do |pipe|
pipe.each("\0\n", chomp: true) do |commit|
trailer = commit.scrub.split("\n\n").last
next unless trailer
trailer.scan(ROLES_REGEX) do
role = $1.downcase
$roles[role] += 1 if role != 'signed-off-by'
end
end
end
def quantiles(data, probs)
values = data.sort
probs.map do |prob|
x = 1 + (values.count - 1) * prob
mod = x % 1
(1 - mod) * values[x.floor - 1] + (mod) * values[x.ceil - 1]
end
end
def show_group(title, threshold, top: true)
puts [nil, title, '-' * title.size]
quantile_sum = 0
$roles.sort_by(&:last).reverse.each_with_index do |(role, count), i|
next if top ? (count <= threshold) : count > threshold
puts "%3d. %s (%d)" % [i + 1, role, count]
quantile_sum += count
end
puts [nil, 'Total wealth: %d%%' % [(100 * quantile_sum.to_f / $total).round]]
end
$total = $roles.values.sum
$q = quantiles($roles.values, [ 0.99, 0.90, 0.75, 0.50 ])
puts '1%%: %d, 10%% %d, 25%% %d, 50%% %d' % $q
top_1, top_10, top_25, median = $q
show_group('Top 1%', top_1)
show_group('Top 10%', top_10)
show_group('Top 25%', top_25)
show_group('Bottom 50%', median, top: false)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment