Skip to content

Instantly share code, notes, and snippets.

@chrismdp
Created September 21, 2010 06:14
Show Gist options
  • Save chrismdp/589281 to your computer and use it in GitHub Desktop.
Save chrismdp/589281 to your computer and use it in GitHub Desktop.
def authors_from_commit(commit)
authors = []
commit.chomp.split(' and ').map{|a| authors.push a}
authors
end
def authors_from_commits(commits)
authors = []
commits.each do |commit|
authors += authors_from_commit(commit)
end
return authors
end
def group_by_author(authors)
stats = Hash.new(0)
authors.map do |author|
stats[author] += 1
end
stats
end
def by_value(hash)
out = ""
hash.sort{|a,b| b[1]<=>a[1]}.each do |elem|
out += "#{elem[0]} (#{elem[1]})\n"
end
out
end
def assertEqual(expected, actual)
if expected == actual
print '.'
else
puts "\nFAIL: #{expected} expected, but got #{actual}"
end
end
def test
# Split paired authors into names
authors = authors_from_commit("Bob Test and Charles Code")
assertEqual("Bob Test", authors[0])
assertEqual("Charles Code", authors[1])
# Respect single authors too
assertEqual("Derek Driven", authors_from_commit("Derek Driven")[0])
# Count authors up
commits = ['Bob Test', 'Charles and Bob Test']
stats = group_by_author(authors_from_commits(commits))
assertEqual(2, stats['Bob Test'])
assertEqual(1, stats['Charles'])
# Print out by descending commit order
output = by_value(group_by_author(authors_from_commits(commits)))
assertEqual(output, "Bob Test (2)\nCharles (1)\n")
puts "\nTest Run Finished"
end
def run
committers = `git log --pretty=format:%an`
puts by_value(group_by_author(authors_from_commits(committers)))
end
ARGV[0] == '-t' ? test : run
@chrismdp
Copy link
Author

This script prints rows for each author on the project sorted like so:

Alice Coder (45)
Bob Hacker (43)
etc.

It respects hitch-style pairing syntax (e.g. Alice Coder and Bob Hacker dev+alice+bob@acme.com) and credits the commit to each person.

@chrismdp
Copy link
Author

Fully tested now with a made up test 'framework'. Run with -t to run tests.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment