Created
February 18, 2023 02:33
-
-
Save mlarraz/dac05e8846a6b9bbc7ade4ef6243b60b to your computer and use it in GitHub Desktop.
Find top committers for a file in a git repo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require "json" | |
require "fileutils" | |
file = ARGV[0] | |
repo = `git rev-parse --show-toplevel`.strip | |
path = Dir.pwd.gsub(repo, File.basename(repo)) | |
cache = Dir.home + "/.cache/top_committers/#{path}/#{file}" | |
FileUtils.mkdir_p(File.dirname(cache)) | |
if File.exist?(cache) | |
json = JSON.load_file(cache) | |
sha = json["last_commit"] | |
commits = json["commits"].map { |h| h.transform_keys(&:to_sym) } | |
str = `git log --pretty="format:%an" --numstat #{sha}..HEAD -- #{file}` | |
else | |
commits = [] | |
str = `git log --pretty="format:%an" --numstat -- #{file}` | |
end | |
commits += str.split("\n\n").map do |l| | |
data = l.split(/\t|\n/)[0..2] | |
{ | |
author: data[0], | |
added: data[1], | |
deleted: data[2], | |
} | |
end | |
File.write(cache, JSON.dump({last_commit: `git rev-parse HEAD`[0..6], commits: commits})) | |
totals = commits.group_by { |c| c[:author] }.transform_values do |arr| | |
arr.sum { |c| c[:added].to_i + c[:deleted].to_i } | |
end | |
puts "#{file} by author:" | |
puts totals.sort_by { |_,v| -v }.map { |k,v| "#{k}: #{v}" } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment