GitDiffParser

Gem version Travis-CI Status yard docs

Parse git diff into patches and lines.

Examples

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\n@@ -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">]

patches[0].file
#=> "lib/saddler/reporter/github.rb"

patches[0].changed_lines
#=> [#<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# https://developer.github.com/v3/repos/comments/#create-a-commit-comment\n@@ -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 = Comment.new(sha1, body, nil, nil)\n\n@@ -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.new(diff)
#=> #<GitDiffParser::Patch:0x007fb068ca7a18
# @body="@@ -2,6 +2,7 @@ module Saddler (snip)">

patch.changed_lines
#=> [#<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>]

API

And more details.

Changelog

changelog.md.

Installation

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

Development

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 rubygems.org.

Contributing

  1. Fork it ( https://github.com/packsaddle/ruby-git_diff_parser/fork )
  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

License

git_diff_parser

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