Gem version Travis-CI Status yard docs

Parse git diff into patches and lines.


Parse entire git diff

diff = "diff --git a/lib/saddler/reporter/github.rb b/lib/saddler/reporter/github.rb\nindex fc33cdd..0638356 100644\n--- a/lib/saddler/reporter/github.rb\n+++ b/lib/saddler/reporter/github.rb\[email protected]@ -2,6 +2,7 @@\n require 'octokit'\n require 'git'\n require 'saddler/reporter/github/version'\n+require 'saddler/reporter/github/support'\n require 'saddler/reporter/github/helper'\n require 'saddler/reporter/github/client'\n require 'saddler/reporter/github/comment'\n"

patches = GitDiffParser.parse(diff)
#=> [#<GitDiffParser::Patch:0x007fb313189430
# @body="@@ -2,6 +2,7 @@\n require 'octokit'\n require 'git'\n require 'saddler/reporter/github/version'\n+r (snip)",
# @file="lib/saddler/reporter/github.rb">]

#=> "lib/saddler/reporter/github.rb"

#=> [#<GitDiffParser::Line:0x007fb3130a53e8
# @number=5,
# @content="+require 'saddler/reporter/github/support'\n",
# @patch_position=4>]

Parse single patch

diff = "@@ -2,6 +2,7 @@ module Saddler\nmodule Reporter\nmodule Github\nclass CommitComment\n+        include Support\ninclude Helper\n\n#\[email protected]@ -11,7 +12,7 @@ def report(messages, options)\ndata = parse(messages)\n\n# build comment\n-          body = build_body(data)\n+          body = concat_body(data)\nreturn if body.empty?\ncomment =, body, nil, nil)\n\[email protected]@ -25,20 +26,6 @@ def report(messages, options)\n# create commit_comment\nclient.create_commit_comment(comment)\nend\n-\n-        def build_body(data)\n-          buffer = []\n-          files = data['checkstyle']['file'] ||= []\n-          files.each do |file|\n-            errors = file['error'] ||= []\n-            errors.each do |error|\n-              severity = error['@severity'] && error['@severity'].upcase\n-              message = error['@message']\n-              buffer << [severity, message].compact.join(': ')\n-            end\n-          end\n-          buffer.join(\"\\n\")\n-        end\nend\nend\nend\n"

patch =
#=> #<GitDiffParser::Patch:0x007fb068ca7a18
# @body="@@ -2,6 +2,7 @@ module Saddler (snip)">

#=> [#<GitDiffParser::Line:0x007fb068c17af8 @number=5,
# @content="+        include Support\n", @patch_position=4>,
# <GitDiffParser::Line:0x007fb068c17580 @number=15,
# @content="+          body = concat_body(data)\n", @patch_position=13>]


And more details.



Add this line to your application's Gemfile:

gem 'git_diff_parser'

And then execute:

$ bundle

Or install it yourself as:

$ gem install git_diff_parser


After checking out the repo, run bin/setup to install dependencies. Then, run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release to create a git tag for the version, push git commits and tags, and push the .gem file to


  1. Fork it ( )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request



Copyright (c) 2015 sanemat. Licensed under the MIT license

original thoughtbot/hound

Copyright (c) 2014 thoughtbot, inc. Licensed under the MIT license

  • lib/git_diff_parser/line.rb <- app/models/line.rb
  • lib/git_diff_parser/patch.rb <- app/models/patch.rb
  • spec/git_diff_parser/patch_spec.rb <- spec/models/patch_spec.rb