public
Created

  • Download Gist
parse-patch.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
#!/usr/bin/env ruby
def parse_chunk(chunk)
remove_start_line, add_start_line = chunk.scan(/@@ -(\d+),\d+ \+(\d+),\d+/).flatten.map { |line| line.to_i }
{ :removed => chunk.grep(/^[- ]/),
:added => chunk.grep(/^[+ ]/),
:remove_start_line => remove_start_line,
:add_start_line => add_start_line }
end
 
def parse_chunks(file_diff)
file_diff.split(/^(?=@@)/)[1..-1].map {|chunk| parse_chunk(chunk)}
end
 
def extract_filename(patch)
patch.scan(/diff --git a\/(.+?) b\//).flatten.first
end
 
def parse_patch(patch)
patch.split(/^(?=diff --git)/).map do |file_chunk|
{ :file_name => extract_filename(file_chunk),
:chunks => parse_chunks(file_chunk) }
end
end
 
require 'pp'
pp(parse_patch(%x(git diff --cached)))

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.