Skip to content

Instantly share code, notes, and snippets.

@msr1k
Created September 17, 2015 04:24
Show Gist options
  • Save msr1k/436d482db8815fed0b47 to your computer and use it in GitHub Desktop.
Save msr1k/436d482db8815fed0b47 to your computer and use it in GitHub Desktop.
Make git log result an array of hash object
# encoding: cp932
module Git
module Log
def self.get
# `git log --all --name-status`
`git log --all --date=iso --name-status`.encode( "cp932", "utf-8" )
end
def self.parse log_string
ary = []
hash = nil
line_break = 0
log_string.each_line do |l|
if l =~ /^commit (\h{40})$/
ary.push hash if hash
hash = { commit: $1.chomp }
line_break = 0
end
next unless hash
if l =~ /^commit (\h{40})$/
# DO NOTHING
elsif l =~ /^Merge: (.*)$/
hash[:merge] = $1.chomp
elsif l =~ /^Author: (.*)$/
hash[:author] = $1.chomp
elsif l =~ /^Date: (.*)$/
hash[:date] = $1.chomp
elsif l =~ /^\n$/
line_break += 1
# DO NOTHING
elsif l =~ /^ (.*)$/
hash[:comment] ||= ""
hash[:comment] += $1.chomp
elsif line_break >= 2
hash[:files] ||= []
hash[:files].push( l.chomp )
end
end
ary.push hash
ary
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment