Skip to content

Instantly share code, notes, and snippets.

@holderbaum
Created October 24, 2014 08:05
Show Gist options
  • Save holderbaum/49e7756dc270d60b7b1a to your computer and use it in GitHub Desktop.
Save holderbaum/49e7756dc270d60b7b1a to your computer and use it in GitHub Desktop.
def extract_log(dir)
`(cd #{dir} && git log --oneline --shortstat --pretty=format:%aE)`
end
def extract_stats(log_string)
log_string.split(/\n\n/).map do |log_entry|
StatEntry.new log_entry
end
end
def log(msg)
$stderr.puts msg
end
class StatEntry
def initialize(log_entry)
self.author, self.stats = log_entry.split(/\n/)
end
def changes
splitted_numbers[0] || 0
end
def insertions
splitted_numbers[1] || 0
end
def deletions
splitted_numbers[2] || 0
end
def to_hash
{}
end
def inspect
"#<StatEntry #{author}: c#{changes} +#{insertions} -#{deletions}>"
end
public
attr_reader :author
protected
attr_writer :author
attr_accessor :stats
private
def splitted_numbers
stats.split(',').map do |stat_string|
match = stat_string.strip.match(/([0-9]+)/)
match ? match[1].to_i : 0
end
end
end
class AccumulatedEntry
def initialize(author)
self.author = author
self.commits = 0
self.changes = 0
self.insertions = 0
self.deletions = 0
end
def accumulate(stat_entry)
self.commits += 1
self.changes += stat_entry.changes
self.insertions += stat_entry.insertions
self.deletions += stat_entry.deletions
end
attr_reader :author, :commits, :changes, :insertions, :deletions
protected
attr_writer :author, :commits, :changes, :insertions, :deletions
end
statistics = Hash.new do |stats, author|
stats[author] = AccumulatedEntry.new(author)
end
total_count = ARGV.size
log "Running over #{total_count} repos"
ARGV.each.with_index do |bare_repo, i|
log " * #{bare_repo} (#{i+1}/#{total_count})"
stat_entries = extract_stats(extract_log(bare_repo))
stat_entries.each do |stat_entry|
statistics[stat_entry.author].accumulate stat_entry
end
end
puts 'author,commits,changes,insertions,deletions'
statistics.values.each do |accumulated_entry|
puts [
accumulated_entry.author,
accumulated_entry.commits,
accumulated_entry.changes,
accumulated_entry.insertions,
accumulated_entry.deletions
].join(',')
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment