Skip to content

Instantly share code, notes, and snippets.

@psyomn
Created October 6, 2014 08:10
Show Gist options
  • Select an option

  • Save psyomn/ce25f7cf790bb670f16d to your computer and use it in GitHub Desktop.

Select an option

Save psyomn/ce25f7cf790bb670f16d to your computer and use it in GitHub Desktop.
Parse git logs to simple objects
#!/usr/bin/env ruby
# Small crappy script to parse git logs
# Simon Symeonidis
str = 'git --git-dir ~/programming/ruby/wlog/.git log'
result = `#{str}`
class GitLog
def initialize
@commit = @author = @shortlog = @message = ""
end
attr_accessor :commit, :author, :shortlog, :message
end
cur = nil
inmessage = false
gitlogs = []
result.lines.each do |line|
case line
when /^commit/i
inmessage = false
gitlogs.push cur if cur
cur = GitLog.new
cur.commit = line.split[1].strip
when /^author/i
cur.author = line.split[1].strip
when /^date/i, /^\n$/
next
else
if inmessage
cur.message.concat(line)
cur.message.strip!
else
cur.shortlog = line
cur.shortlog.strip!
inmessage = true
end
end
end
gitlogs.push cur if cur
p gitlogs.first
p 'aaaaaaaaaaaaaaaaaaaaa'
p gitlogs.last
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment