Skip to content

Instantly share code, notes, and snippets.

@tychobrailleur
Created December 10, 2011 18:36
Show Gist options
  • Save tychobrailleur/1455894 to your computer and use it in GitHub Desktop.
Save tychobrailleur/1455894 to your computer and use it in GitHub Desktop.
Find the first commit of all committers in a git log file
require 'date'
class Commit
attr_accessor :sha_id, :author, :date, :comment
end
commits = Hash.new
commits_number = Hash.new { |k,v| k[v] = 0 }
comment = nil
current_commit = nil
File.open("/tmp/gitlog") do |file|
file.each_line do |line|
# Example of SHA-1 id
# 27517033c286c78f3d4ed9d9b15b3ad834e1b6d2
if line =~ /^commit ([a-f0-9]{40})$/
if current_commit != nil
current_commit.comment = comment.strip
commits[current_commit.author] = current_commit
commits_number[current_commit.author] += 1
end
comment = nil
current_commit = Commit.new
current_commit.sha_id = $1
elsif line =~ /^Author: (.*)$/
current_commit.author = $1
elsif line =~ /^Date: (.*)$/
current_commit.date = Date.parse($1)
else
if comment != nil
comment += line
else
comment = line
end
end
end
end
Hash[commits.sort_by { |k,v| v.date }].each_pair do |k,v|
puts "#{k}\t#{v.date}\t#{commits_number[k]}\n#{v.comment}"
puts "-" * 50
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment