Skip to content

Instantly share code, notes, and snippets.

@timcharper
Created June 29, 2010 21:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timcharper/457860 to your computer and use it in GitHub Desktop.
Save timcharper/457860 to your computer and use it in GitHub Desktop.
#!/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)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment