git_diff_parser 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ef06c6a36cf828b1806d9f0f8be11fc136424dd1
4
+ data.tar.gz: bec097bbfbc6e10e46d588ae9917383eb8750f7f
5
+ SHA512:
6
+ metadata.gz: c80bd251554bd3183f0ea56de73b908d419fd2e113aa951347114ee225a4bd29d7445cf2a91819a6d5b7e4f62ae08cf49d8862bbd1bdfbb3afb236930cac40fc
7
+ data.tar.gz: bfa1fb12689e07201dff3037ea1c763d32cdfc158ac3792dacbd990c75aa2bf6510b75390af758ba406e646658ce125f622c6af604b3acf7c3aa516416a8a73e
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,17 @@
1
+ AllCops:
2
+ Exclude:
3
+ - 'bundle_bin/*'
4
+ - 'vendor/bundle/**/*'
5
+
6
+ Style/Documentation:
7
+ Enabled: false
8
+
9
+ Metrics/LineLength:
10
+ Max: 100
11
+
12
+ Style/FileName:
13
+ Exclude:
14
+ - exe/*
15
+
16
+ Style/RegexpLiteral:
17
+ MaxSlashes: 0
@@ -0,0 +1 @@
1
+ strategy: 'none'
@@ -0,0 +1,15 @@
1
+ language: ruby
2
+ sudo: false
3
+ rvm:
4
+ - 1.9
5
+ - 2.0
6
+ - 2.1
7
+ - 2.2
8
+ before_install:
9
+ - gem update bundler
10
+ notifications:
11
+ email:
12
+ - ogataken@gmail.com
13
+ branches:
14
+ only:
15
+ - master
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http:contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in git_diff_parser.gemspec
4
+ gemspec
5
+ gem 'byebug' if RUBY_VERSION >= '2.0.0'
6
+ gem 'pry'
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 sanemat
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,138 @@
1
+ # GitDiffParser
2
+
3
+ [![Gem Version](http://img.shields.io/gem/v/git_diff_parser.svg?style=flat)](http://badge.fury.io/rb/git_diff_parser)
4
+ [![Build Status](http://img.shields.io/travis/packsaddle/ruby-git_diff_parser/master.svg?style=flat)](https://travis-ci.org/packsaddle/ruby-git_diff_parser)
5
+
6
+ Parser for `git diff`.
7
+
8
+ ## Examples
9
+
10
+ ```ruby
11
+ diff = <<'EOL'
12
+ diff --git a/lib/saddler/reporter/github.rb b/lib/saddler/reporter/github.rb
13
+ index fc33cdd..0638356 100644
14
+ --- a/lib/saddler/reporter/github.rb
15
+ +++ b/lib/saddler/reporter/github.rb
16
+ @@ -2,6 +2,7 @@
17
+ require 'octokit'
18
+ require 'git'
19
+ require 'saddler/reporter/github/version'
20
+ +require 'saddler/reporter/github/support'
21
+ require 'saddler/reporter/github/helper'
22
+ require 'saddler/reporter/github/client'
23
+ require 'saddler/reporter/github/comment'
24
+ EOL
25
+
26
+ patches = GitDiffParser.parse(diff)
27
+ #=> [#<GitDiffParser::Patch:0x007fb313189430
28
+ # @body="@@ -2,6 +2,7 @@\n require 'octokit'\n require
29
+ # 'git'\n require 'saddler/reporter/github/version'\n+r
30
+ # equire 'saddler/reporter/github/support'\n require 'sad
31
+ # dler/reporter/github/helper'\n require 'saddler/reporte
32
+ # r/github/client'\n require 'saddler/reporter/github/com
33
+ # ment'\n", @file="lib/saddler/reporter/github.rb">]
34
+
35
+ patches[0].file
36
+ #=> "lib/saddler/reporter/github.rb"
37
+
38
+ patches[0].changed_lines
39
+ #=> [#<GitDiffParser::Line:0x007fb3130a53e8
40
+ # @number=5,
41
+ # @content="+require 'saddler/reporter/github/support'\n",
42
+ # @patch_position=4>]
43
+ ```
44
+
45
+ ```ruby
46
+ diff = <<-'EOS'
47
+ @@ -2,6 +2,7 @@ module Saddler
48
+ module Reporter
49
+ module Github
50
+ class CommitComment
51
+ + include Support
52
+ include Helper
53
+
54
+ # https://developer.github.com/v3/repos/comments/#create-a-commit-comment
55
+ @@ -11,7 +12,7 @@ def report(messages, options)
56
+ data = parse(messages)
57
+
58
+ # build comment
59
+ - body = build_body(data)
60
+ + body = concat_body(data)
61
+ return if body.empty?
62
+ comment = Comment.new(sha1, body, nil, nil)
63
+
64
+ @@ -25,20 +26,6 @@ def report(messages, options)
65
+ # create commit_comment
66
+ client.create_commit_comment(comment)
67
+ end
68
+ -
69
+ - def build_body(data)
70
+ - buffer = []
71
+ - files = data['checkstyle']['file'] ||= []
72
+ - files.each do |file|
73
+ - errors = file['error'] ||= []
74
+ - errors.each do |error|
75
+ - severity = error['@severity'] && error['@severity'].upcase
76
+ - message = error['@message']
77
+ - buffer << [severity, message].compact.join(': ')
78
+ - end
79
+ - end
80
+ - buffer.join("\n")
81
+ - end
82
+ end
83
+ end
84
+ end
85
+ EOS
86
+
87
+ patch = GitDiffParser::Patch.new(diff)
88
+ #=> #<GitDiffParser::Patch:0x007fb068ca7a18
89
+ # @body="@@ -2,6 +2,7 @@ module Saddler (snip)">
90
+
91
+ patch.changed_lines
92
+ #=> [#<GitDiffParser::Line:0x007fb068c17af8 @number=5,
93
+ # @content="+ include Support\n", @patch_position=4>,
94
+ # <GitDiffParser::Line:0x007fb068c17580 @number=15,
95
+ # @content="+ body = concat_body(data)\n", @patch_position=13>]
96
+ ```
97
+
98
+ ## Installation
99
+
100
+ Add this line to your application's Gemfile:
101
+
102
+ ```ruby
103
+ gem 'git_diff_parser'
104
+ ```
105
+
106
+ And then execute:
107
+
108
+ $ bundle
109
+
110
+ Or install it yourself as:
111
+
112
+ $ gem install git_diff_parser
113
+
114
+ ## Development
115
+
116
+ 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.
117
+
118
+ 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](https://rubygems.org).
119
+
120
+ ## Contributing
121
+
122
+ 1. Fork it ( https://github.com/packsaddle/ruby-git_diff_parser/fork )
123
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
124
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
125
+ 4. Push to the branch (`git push origin my-new-feature`)
126
+ 5. Create a new Pull Request
127
+
128
+ ## License
129
+ ### git_diff_parser
130
+ Copyright (c) 2015 sanemat. [Licensed under the MIT license](./LICENSE)
131
+
132
+ ### original [thoughtbot/hound](https://github.com/thoughtbot/hound)
133
+
134
+ Copyright (c) 2014 thoughtbot, inc. [Licensed under the MIT license](./thoughtbot-hound/LICENSE)
135
+
136
+ * lib/git_diff_parser/line.rb <- app/models/line.rb
137
+ * lib/git_diff_parser/patch.rb <- app/models/patch.rb
138
+ * spec/git_diff_parser/patch_spec.rb <- spec/models/patch_spec.rb
@@ -0,0 +1,9 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ desc 'Run all specs in spec directory'
5
+ RSpec::Core::RakeTask.new(:spec) do |spec|
6
+ spec.rspec_opts = ['-cfd --backtrace']
7
+ end
8
+
9
+ task default: :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'git_diff_parser'
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require 'irb'
14
+ IRB.start
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'git_diff_parser/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'git_diff_parser'
8
+ spec.version = GitDiffParser::VERSION
9
+ spec.authors = ['sanemat']
10
+ spec.email = ['o.gata.ken@gmail.com']
11
+
12
+ spec.summary = 'Parser for `git diff`.'
13
+ spec.description = 'Parse `git diff` result.'
14
+ spec.homepage = 'https://github.com/packsaddle/ruby-git_diff_parser'
15
+ spec.license = 'MIT'
16
+
17
+ spec.files = `git ls-files -z`
18
+ .split("\x0")
19
+ .reject { |f| f.match(%r{^(test|spec|features)/}) }
20
+ spec.bindir = 'exe'
21
+ spec.executables = spec
22
+ .files
23
+ .grep(%r{^exe/}) { |f| File.basename(f) }
24
+ spec.require_paths = ['lib']
25
+
26
+ spec.add_development_dependency 'bundler', '~> 1.8'
27
+ spec.add_development_dependency 'rake', '~> 10.0'
28
+ spec.add_development_dependency 'rspec'
29
+ end
@@ -0,0 +1,11 @@
1
+ require 'git_diff_parser/version'
2
+ require 'git_diff_parser/line'
3
+ require 'git_diff_parser/patch'
4
+ require 'git_diff_parser/patches'
5
+ require 'git_diff_parser/diff_parser'
6
+
7
+ module GitDiffParser
8
+ def self.parse(contents)
9
+ DiffParser.parse(contents)
10
+ end
11
+ end
@@ -0,0 +1,35 @@
1
+ module GitDiffParser
2
+ class DiffParser
3
+ def self.parse(contents)
4
+ body = false
5
+ file_name = ''
6
+ patch = []
7
+ lines = contents.lines
8
+ line_count = lines.count
9
+ parsed = Patches.new
10
+ lines.each_with_index do |line, count|
11
+ case line.chomp
12
+ when /^diff/
13
+ unless patch.empty?
14
+ parsed << Patch.new(patch.join("\n") + "\n", file: file_name)
15
+ patch.clear
16
+ file_name = ''
17
+ end
18
+ body = false
19
+ when /^\-\-\-/
20
+ when %r{^\+\+\+ b/(?<file_name>.*)}
21
+ file_name = Regexp.last_match[:file_name]
22
+ body = true
23
+ when /^(?<body>[\ @\+\-\\].*)/
24
+ patch << Regexp.last_match[:body] if body
25
+ if !patch.empty? && body && line_count == count + 1
26
+ parsed << Patch.new(patch.join("\n") + "\n", file: file_name)
27
+ patch.clear
28
+ file_name = ''
29
+ end
30
+ end
31
+ end
32
+ parsed
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,18 @@
1
+ module GitDiffParser
2
+ class Line
3
+ attr_reader :number, :patch_position
4
+
5
+ def initialize(options = {})
6
+ fail(ArgumentError('number is required')) unless options[:number]
7
+ fail(ArgumentError('content is required')) unless options[:content]
8
+ fail(ArgumentError('patch_position is required')) unless options[:patch_position]
9
+ @number = options[:number]
10
+ @content = options[:content]
11
+ @patch_position = options[:patch_position]
12
+ end
13
+
14
+ def changed?
15
+ true
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,43 @@
1
+ module GitDiffParser
2
+ class Patch
3
+ RANGE_INFORMATION_LINE = /^@@ .+\+(?<line_number>\d+),/
4
+ MODIFIED_LINE = /^\+(?!\+|\+)/
5
+ NOT_REMOVED_LINE = /^[^-]/
6
+
7
+ attr_accessor :file, :body
8
+
9
+ def initialize(body, options = {})
10
+ @body = body || ''
11
+ @file = options[:file] || options['file'] if options[:file] || options['file']
12
+ end
13
+
14
+ def changed_lines
15
+ line_number = 0
16
+
17
+ lines.each_with_index.inject([]) do |lines, (content, patch_position)|
18
+ case content
19
+ when RANGE_INFORMATION_LINE
20
+ line_number = Regexp.last_match[:line_number].to_i
21
+ when MODIFIED_LINE
22
+ line = Line.new(
23
+ content: content,
24
+ number: line_number,
25
+ patch_position: patch_position
26
+ )
27
+ lines << line
28
+ line_number += 1
29
+ when NOT_REMOVED_LINE
30
+ line_number += 1
31
+ end
32
+
33
+ lines
34
+ end
35
+ end
36
+
37
+ private
38
+
39
+ def lines
40
+ @body.lines
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,12 @@
1
+ require 'delegate'
2
+ module GitDiffParser
3
+ class Patches < DelegateClass(Array)
4
+ def self.[](*ary)
5
+ new(ary)
6
+ end
7
+
8
+ def initialize(*args)
9
+ super Array.new(*args)
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,3 @@
1
+ module GitDiffParser
2
+ VERSION = '2.0.0'
3
+ end
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2014 thoughtbot, inc.
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
metadata ADDED
@@ -0,0 +1,106 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: git_diff_parser
3
+ version: !ruby/object:Gem::Version
4
+ version: 2.0.0
5
+ platform: ruby
6
+ authors:
7
+ - sanemat
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-02-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.8'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.8'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Parse `git diff` result.
56
+ email:
57
+ - o.gata.ken@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - ".rubocop.yml"
65
+ - ".tachikoma.yml"
66
+ - ".travis.yml"
67
+ - CODE_OF_CONDUCT.md
68
+ - Gemfile
69
+ - LICENSE.txt
70
+ - README.md
71
+ - Rakefile
72
+ - bin/console
73
+ - bin/setup
74
+ - git_diff_parser.gemspec
75
+ - lib/git_diff_parser.rb
76
+ - lib/git_diff_parser/diff_parser.rb
77
+ - lib/git_diff_parser/line.rb
78
+ - lib/git_diff_parser/patch.rb
79
+ - lib/git_diff_parser/patches.rb
80
+ - lib/git_diff_parser/version.rb
81
+ - thoughtbot-hound/LICENSE
82
+ homepage: https://github.com/packsaddle/ruby-git_diff_parser
83
+ licenses:
84
+ - MIT
85
+ metadata: {}
86
+ post_install_message:
87
+ rdoc_options: []
88
+ require_paths:
89
+ - lib
90
+ required_ruby_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ required_rubygems_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ requirements: []
101
+ rubyforge_project:
102
+ rubygems_version: 2.4.5
103
+ signing_key:
104
+ specification_version: 4
105
+ summary: Parser for `git diff`.
106
+ test_files: []