checkstyle_filter-git 0.1.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: 382b94865284cf1a5f9627e8426b7635752173c5
4
+ data.tar.gz: b09bd20fa60c6d44a7220eff0939b914fb8d367b
5
+ SHA512:
6
+ metadata.gz: 75d273fe98a77b39efd0face240793b969ec47a810e794086d948ee9bd4365bf0e4e12b67ef19c72dcc69ed416de489d296441c8f46e09783082d0591b3abe51
7
+ data.tar.gz: a698bc2ce90a62039a0e1936ad2fe7c0ab1a8a64d57e090dcf25178caed3813d85618707401d8d23708c69a65b3d8128bf24d22d9f5d5dadd724ae492b79d9bb
@@ -0,0 +1,15 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # Ignore bundler binstubs
12
+ /bundle_bin
13
+
14
+ # Ignore bundled files
15
+ /vendor/bundle/*
@@ -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,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 checkstyle_filter-git.gemspec
4
+ gemspec
5
+
6
+ gem 'byebug' if RUBY_VERSION >= '2.0.0'
@@ -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,134 @@
1
+ # CheckstyleFilter::Git
2
+
3
+ [![Gem Version](http://img.shields.io/gem/v/checkstyle_filter-git.svg?style=flat)](http://badge.fury.io/rb/checkstyle_filter-git)
4
+ [![Build Status](http://img.shields.io/travis/packsaddle/ruby-checkstyle_filter-git/master.svg?style=flat)](https://travis-ci.org/packsaddle/ruby-checkstyle_filter-git)
5
+
6
+ ## What is this?
7
+
8
+ 1. checkstyle format errors
9
+
10
+ ```xml
11
+ <?xml version='1.0'?>
12
+ <checkstyle>
13
+ <file name='/Users/sane/work/ruby-study/checkstyle_filter-git/checkstyle_filter-git.gemspec'/>
14
+ <file name='/Users/sane/work/ruby-study/checkstyle_filter-git/Gemfile'/>
15
+ <file name='/Users/sane/work/ruby-study/checkstyle_filter-git/bin/console'/>
16
+ <file name='/Users/sane/work/ruby-study/checkstyle_filter-git/example/invalid.rb'>
17
+ <error line='3' column='100' severity='info' message='Line is too long. [188/100]' source='com.puppycrawl.tools.checkstyle.Metrics/LineLength'/>
18
+ </file>
19
+ <file name='/Users/sane/work/ruby-study/checkstyle_filter-git/exe/checkstyle_filter-git'/>
20
+ <file name='/Users/sane/work/ruby-study/checkstyle_filter-git/lib/checkstyle_filter/git/cli.rb'>
21
+ <error line='14' column='6' severity='info' message='Assignment Branch Condition size for diff is too high. [54.73/15]' source='com.puppycrawl.tools.checkstyle.Metrics/AbcSize'/>
22
+ </file>
23
+ <checkstyle>
24
+ ```
25
+
26
+ 2. git diff
27
+
28
+ ```diff
29
+ diff --git a/example/invalid.rb b/example/invalid.rb
30
+ new file mode 100644
31
+ index 0000000..b13da3a
32
+ --- /dev/null
33
+ +++ b/example/invalid.rb
34
+ @@ -0,0 +1,5 @@
35
+ +class Invalid
36
+ + def foo
37
+ + 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
38
+ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'
39
+ + end
40
+ +end
41
+ diff --git a/lib/checkstyle_filter/git/cli.rb b/lib/checkstyle_filter/git/cli.rb
42
+ index 6d503a3..e45bdef 100644
43
+ --- a/lib/checkstyle_filter/git/cli.rb
44
+ +++ b/lib/checkstyle_filter/git/cli.rb
45
+ @@ -61,10 +61,12 @@ def file_element_file_in_git_diff?(file_name, parsed_git_diff)
46
+ .include?(Pathname.new(file_name).expand_path)
47
+ end
48
+
49
+ - def file_element_error_line_no_in_modified?(file_name, parsed_git_diff, line)
50
+ + def file_element_error_line_no_in_modified?(file_name, parsed_git_diff, line_no)
51
+ require 'pathname'
52
+ diff_pairs = parsed_git_diff
53
+ - .select { |diff| Pathname.new(diff[:file_name]).expand_path == Pathname.new(file_name).expand_path }
54
+ + .select do |diff|
55
+ + Pathname.new(diff[:file_name]).expand_path == Pathname.new(file_name).expand_path
56
+ + end
57
+ return false if diff_pairs.empty?
58
+ modified_lines = Set.new
59
+ diff_pairs.map do |diff_pair|
60
+ @@ -72,7 +74,7 @@ def file_element_error_line_no_in_modified?(file_name, parsed_git_diff, line)
61
+ modified_lines << line.number
62
+ end
63
+ end
64
+ - modified_lines.include?(line)
65
+ + modified_lines.include?(line_no)
66
+ end
67
+ end
68
+ end
69
+ ```
70
+
71
+ 3. filter! output **only errors** on modified lines
72
+
73
+ ```
74
+ $ cat checkstyle.xml | checkstyle_filter-git diff HEAD~1..origin/master
75
+ ```
76
+
77
+ ```xml
78
+ <?xml version='1.0'?>
79
+ <checkstyle>
80
+ <file name='/Users/sane/work/ruby-study/checkstyle_filter-git/checkstyle_filter-git.gemspec'/>
81
+ <file name='/Users/sane/work/ruby-study/checkstyle_filter-git/Gemfile'/>
82
+ <file name='/Users/sane/work/ruby-study/checkstyle_filter-git/bin/console'/>
83
+ <file name='/Users/sane/work/ruby-study/checkstyle_filter-git/example/invalid.rb'>
84
+ <error line='3' column='100' severity='info' message='Line is too long. [188/100]' source='com.puppycrawl.tools.checkstyle.Metrics/LineLength'/>
85
+ </file>
86
+ <file name='/Users/sane/work/ruby-study/checkstyle_filter-git/exe/checkstyle_filter-git'/>
87
+ <file name='/Users/sane/work/ruby-study/checkstyle_filter-git/lib/checkstyle_filter/git/cli.rb'>
88
+ </file>
89
+ <checkstyle>
90
+ ```
91
+
92
+ ## Expected usage
93
+
94
+ ```
95
+ git diff -z --name-only origin/master.. \
96
+ | xargs -0 rubocop-select \
97
+ | xargs rubocop \
98
+ --require rubocop/formatter/checkstyle_formatter \
99
+ --format RuboCop::Formatter::CheckstyleFormatter \
100
+ | checkstyle_filter-git diff origin/master.. \
101
+ | saddler run \
102
+ --require github/pull-request-comment-formatter \
103
+ --format Github::PullRequestCommentFormatter
104
+ ```
105
+
106
+ ## Installation
107
+
108
+ Add this line to your application's Gemfile:
109
+
110
+ ```ruby
111
+ gem 'checkstyle_filter-git'
112
+ ```
113
+
114
+ And then execute:
115
+
116
+ $ bundle
117
+
118
+ Or install it yourself as:
119
+
120
+ $ gem install checkstyle_filter-git
121
+
122
+ ## Development
123
+
124
+ 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. Run `bundle exec checkstyle_filter-git` to use the code located in this directory, ignoring other installed copies of this gem.
125
+
126
+ 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).
127
+
128
+ ## Contributing
129
+
130
+ 1. Fork it ( https://github.com/[my-github-username]/checkstyle_filter-git/fork )
131
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
132
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
133
+ 4. Push to the branch (`git push origin my-new-feature`)
134
+ 5. Create a new Pull Request
@@ -0,0 +1,9 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << 'test'
6
+ t.test_files = FileList['test/**/test*.rb']
7
+ end
8
+
9
+ task default: :test
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'checkstyle_filter/git'
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,31 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'checkstyle_filter/git/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'checkstyle_filter-git'
8
+ spec.version = CheckstyleFilter::Git::VERSION
9
+ spec.authors = ['sanemat']
10
+ spec.email = ['o.gata.ken@gmail.com']
11
+
12
+ spec.summary = 'Filter checkstyle format errors on ONLY modified lines.'
13
+ spec.description = 'Filter errors(checkstyle) by git diff.'
14
+ spec.homepage = 'https://github.com/packsaddle/ruby-checkstyle_filter-git'
15
+ spec.license = 'MIT'
16
+
17
+ spec.files =
18
+ `git ls-files -z`
19
+ .split("\x0")
20
+ .reject { |f| f.match(%r{^(test|spec|features)/}) }
21
+ spec.bindir = 'exe'
22
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
23
+ spec.require_paths = ['lib']
24
+
25
+ spec.add_runtime_dependency 'thor'
26
+ spec.add_development_dependency 'git-diff-parser'
27
+
28
+ spec.add_development_dependency 'bundler', '~> 1.8'
29
+ spec.add_development_dependency 'rake', '~> 10.0'
30
+ spec.add_development_dependency 'minitest'
31
+ end
@@ -0,0 +1,9 @@
1
+ <?xml version='1.0'?>
2
+ <checkstyle>
3
+ <file name='/Users/sane/work/ruby-study/checkstyle_filter-git/checkstyle_filter-git.gemspec'/>
4
+ <file name='/Users/sane/work/ruby-study/checkstyle_filter-git/example/invalid.rb'>
5
+ <error line='3' column='100' severity='info' message='Line is too long. [188/100]' source='com.puppycrawl.tools.checkstyle.Metrics/LineLength'/>
6
+ </file>
7
+ <file name='/Users/sane/work/ruby-study/checkstyle_filter-git/exe/checkstyle_filter-git'/>
8
+ <file name='/Users/sane/work/ruby-study/checkstyle_filter-git/lib/checkstyle_filter/git/cli.rb'/>
9
+ </checkstyle>
@@ -0,0 +1,26 @@
1
+ [
2
+ {
3
+ "sha": "80adf33bacc9f387b6ac2e099945326b3e699fda",
4
+ "filename": "lib/tachikoma/settings.rb",
5
+ "status": "modified",
6
+ "additions": 23,
7
+ "deletions": 2,
8
+ "changes": 25,
9
+ "blob_url": "https://github.com/sanemat/tachikoma/blob/216e050fac8cb7133b8b19a1c0292f48e5994645/lib/tachikoma/settings.rb",
10
+ "raw_url": "https://github.com/sanemat/tachikoma/raw/216e050fac8cb7133b8b19a1c0292f48e5994645/lib/tachikoma/settings.rb",
11
+ "contents_url": "https://api.github.com/repos/sanemat/tachikoma/contents/lib/tachikoma/settings.rb?ref=216e050fac8cb7133b8b19a1c0292f48e5994645",
12
+ "patch": "@@ -1,6 +1,27 @@\n require 'pathname'\n+\n module Tachikoma\n- def self.root\n- Pathname.new(File.dirname(File.dirname(File.dirname(__FILE__))))\n+ def self.root_path\n+ @root_path ||= Pathname.new(File.dirname(File.dirname(File.dirname(__FILE__))))\n+ end\n+\n+ def self.root_path=(root_path)\n+ @root_path = Pathname.new File.expand_path(root_path)\n+ end\n+\n+ def self.data_path\n+ @data_path ||= root_path.parent.join('data')\n+ end\n+\n+ def self.data_path=(data_path)\n+ @data_path = Pathname.new File.expand_path(data_path)\n+ end\n+\n+ def self.repos_path\n+ @repos_path ||= root_path.parent.join('repos')\n+ end\n+\n+ def self.repos_path=(repos_path)\n+ @repos_path = Pathname.new File.expand_path(repos_path)\n end\n end"
13
+ },
14
+ {
15
+ "sha": "d71ff33adad9125f0939681435e2ad3531ca26cb",
16
+ "filename": "lib/tasks/app.rake",
17
+ "status": "modified",
18
+ "additions": 9,
19
+ "deletions": 11,
20
+ "changes": 20,
21
+ "blob_url": "https://github.com/sanemat/tachikoma/blob/216e050fac8cb7133b8b19a1c0292f48e5994645/lib/tasks/app.rake",
22
+ "raw_url": "https://github.com/sanemat/tachikoma/raw/216e050fac8cb7133b8b19a1c0292f48e5994645/lib/tasks/app.rake",
23
+ "contents_url": "https://api.github.com/repos/sanemat/tachikoma/contents/lib/tasks/app.rake?ref=216e050fac8cb7133b8b19a1c0292f48e5994645",
24
+ "patch": "@@ -2,12 +2,10 @@ require 'httparty'\n require 'multi_json'\n require 'safe_yaml'\n require 'uri'\n+require 'tachikoma'\n \n namespace :tachikoma do\n @default_timestamp_format = '%Y%m%d%H%M%S'\n- @root_path = File.expand_path(File.join(__FILE__, '..', '..', '..'))\n- @data_path = File.join(@root_path, 'data')\n- @repos_path = File.join(@root_path, 'repos')\n \n # build_for = fenix-knight, github_token_key = TOKEN_FENIX_KNIGHT\n def github_token_key(build_for)\n@@ -50,9 +48,9 @@ namespace :tachikoma do\n if ENV['LOCAL_DATA_PATH'] && ENV['LOCAL_DATA_REMOTE_URL']\n raise \"local data path is empty\" if ENV['LOCAL_DATA_PATH'] == ''\n raise \"remote git repository of local data is empty\" if ENV['LOCAL_DATA_REMOTE_URL'] == ''\n- @data_path = File.absolute_path(ENV['LOCAL_DATA_PATH'])\n- rm_rf(@data_path) if Dir.exists?(@data_path)\n- sh \"git clone #{ENV['LOCAL_DATA_REMOTE_URL']} #{@data_path}\"\n+ Tachikoma.data_path = File.absolute_path(ENV['LOCAL_DATA_PATH'])\n+ rm_rf(Tachikoma.data_path) if Dir.exists?(Tachikoma.data_path)\n+ sh \"git clone #{ENV['LOCAL_DATA_REMOTE_URL']} #{Tachikoma.data_path}\"\n raise \"failed to clone remote repo: perhaps wrong git clone URL? #{ENV['LOCAL_DATA_REMOTE_URL']}\" unless $?.success?\n else\n warn \"`fetch_data` task requires LOCAL_DATA_PATH and LOCAL_DATA_REMOTE_URL environment variables\"\n@@ -65,7 +63,7 @@ namespace :tachikoma do\n @git_name = 'bot-motoko'\n @git_email = 'bot-motoko@al.sane.jp'\n @configure =\n- YAML.load_file(File.join(@data_path, \"#{@build_for}.yaml\"))\n+ YAML.load_file(File.join(Tachikoma.data_path, \"#{@build_for}.yaml\"))\n @fetch_url = @configure['url']\n @base_remote_branch = 'origin/master'\n @authorized_url = authorized_url_with_type(@fetch_url, @configure['type'], @github_token, @git_name)\n@@ -90,18 +88,18 @@ namespace :tachikoma do\n end\n \n task :clean do\n- rm_rf(Dir.glob('repos/*'))\n+ rm_rf(Dir.glob(Tachikoma.repos_path.to_s))\n end\n \n desc 'fetch'\n task fetch: :clean do\n- mkdir_p('repos')\n- sh \"git clone #{@fetch_url} repos/#{@build_for}\"\n+ mkdir_p(Tachikoma.repos_path.to_s)\n+ sh \"git clone #{@fetch_url} #{Tachikoma.repos_path.to_s}/#{@build_for}\"\n end\n \n desc 'bundle'\n task :bundle do\n- Dir.chdir(\"repos/#{@build_for}\") do\n+ Dir.chdir(\"#{Tachikoma.repos_path.to_s}/#{@build_for}\") do\n Bundler.with_clean_env do\n sh %Q!sed -i -e 's/^ruby/#ruby/' Gemfile!\n sh \"git config user.name #{@git_name}\""
25
+ }
26
+ ]
@@ -0,0 +1,5 @@
1
+ class Invalid
2
+ def foo
3
+ 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'
4
+ end
5
+ end
@@ -0,0 +1,19 @@
1
+ require '../lib/checkstyle_filter/git'
2
+
3
+ # format examples:
4
+ # https://gist.github.com/sanemat/5416e4f701922a47773a
5
+
6
+ # git diff -z --name-only origin/master..HEAD \
7
+ # | xargs -0 bundle exec rubocop-select \
8
+ # | xargs rubocop \
9
+ # --require rubocop/formatter/checkstyle_formatter \
10
+ # --format RuboCop::Formatter::CheckstyleFormatter \
11
+ # | checkstyle_filter-git diff origin/master..HEAD
12
+
13
+ # raw_xml
14
+ # git_diff file_name, line_no_start, line_no_end
15
+ # git_diff.files.each |file|
16
+ # file.lines = Set[1,2,5]
17
+ # end
18
+ # reformed = raw_xml.check_style.file=file.line[1,2,5]
19
+ # reformed.to_xml
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'checkstyle_filter/git'
4
+ require 'checkstyle_filter/git/cli'
5
+
6
+ CheckstyleFilter::Git::CLI.start(ARGV)
@@ -0,0 +1,9 @@
1
+ require 'checkstyle_filter/git/version'
2
+ require 'git/diff/parser'
3
+ require 'checkstyle_filter/git/diff_parser'
4
+
5
+ module CheckstyleFilter
6
+ module Git
7
+ # Your code goes here...
8
+ end
9
+ end
@@ -0,0 +1,82 @@
1
+ require 'thor'
2
+ require 'open3'
3
+
4
+ module CheckstyleFilter
5
+ module Git
6
+ class CLI < Thor
7
+ def self.exit_on_failure?
8
+ true
9
+ end
10
+
11
+ desc 'diff', 'Filter using `git diff`'
12
+ option :data
13
+ option :file
14
+ def diff(commit_ish = nil)
15
+ data = \
16
+ if options[:data]
17
+ options[:data]
18
+ elsif options[:file]
19
+ File.read(options[:file])
20
+ elsif !$stdin.tty?
21
+ ARGV.clear
22
+ ARGF.read
23
+ end
24
+
25
+ abort if !data || data.empty?
26
+
27
+ command = ['git', 'diff', '--no-color', commit_ish].compact
28
+ git_diff, _, _ = Open3.capture3(*command)
29
+ parsed = ::CheckstyleFilter::Git::DiffParser.parse(git_diff)
30
+
31
+ # TODO: split to class
32
+ require 'rexml/document'
33
+ document = REXML::Document.new data
34
+ document.elements.each('/checkstyle/file') do |file_element|
35
+ file_name = file_element.attribute('name').value
36
+ next unless file_element_file_in_git_diff?(file_name, parsed)
37
+ file_element.elements.each('error') do |error_element|
38
+ line = error_element.attribute('line') && error_element.attribute('line').value.to_i
39
+ if file_element_error_line_no_in_modified?(file_name, parsed, line)
40
+ error_element.remove
41
+ end
42
+ end
43
+ end
44
+
45
+ puts document.to_s
46
+ end
47
+
48
+ desc 'version', 'Show the CheckstyleFilter/Git version'
49
+ map %w(-v --version) => :version
50
+
51
+ def version
52
+ puts "CheckstyleFilter/Git version #{::CheckstyleFilter::Git::VERSION}"
53
+ end
54
+
55
+ no_commands do
56
+ def file_element_file_in_git_diff?(file_name, parsed_git_diff)
57
+ require 'pathname'
58
+ diff_files = parsed_git_diff.map { |one| one[:file_name] }
59
+ diff_files
60
+ .map { |file| Pathname.new(file).expand_path }
61
+ .include?(Pathname.new(file_name).expand_path)
62
+ end
63
+
64
+ def file_element_error_line_no_in_modified?(file_name, parsed_git_diff, line_no)
65
+ require 'pathname'
66
+ diff_pairs = parsed_git_diff
67
+ .select do |diff|
68
+ Pathname.new(diff[:file_name]).expand_path == Pathname.new(file_name).expand_path
69
+ end
70
+ return false if diff_pairs.empty?
71
+ modified_lines = Set.new
72
+ diff_pairs.map do |diff_pair|
73
+ diff_pair[:patch].changed_lines.map do |line|
74
+ modified_lines << line.number
75
+ end
76
+ end
77
+ modified_lines.include?(line_no)
78
+ end
79
+ end
80
+ end
81
+ end
82
+ end
@@ -0,0 +1,39 @@
1
+ module CheckstyleFilter
2
+ module Git
3
+ class DiffParser
4
+ def self.parse(contents)
5
+ body = false
6
+ file_name = ''
7
+ patch = []
8
+ lines = contents.lines
9
+ line_count = lines.count
10
+ parsed = []
11
+ lines.each_with_index do |line, count|
12
+ case line.chomp
13
+ when /^diff/
14
+ unless patch.empty?
15
+ parsed << { file_name: file_name,
16
+ patch: ::Git::Diff::Parser::Patch.new(patch.join("\n")) }
17
+ patch.clear
18
+ file_name = ''
19
+ end
20
+ body = false
21
+ when /^\-\-\-/
22
+ when %r{^\+\+\+ b/(?<file_name>.*)}
23
+ file_name = Regexp.last_match[:file_name]
24
+ body = true
25
+ when /^(?<body>[\ @\+\-\\].*)/
26
+ patch << Regexp.last_match[:body] if body
27
+ if !patch.empty? && body && line_count == count + 1
28
+ parsed << { file_name: file_name,
29
+ patch: ::Git::Diff::Parser::Patch.new(patch.join("\n")) }
30
+ patch.clear
31
+ file_name = ''
32
+ end
33
+ end
34
+ end
35
+ parsed
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,5 @@
1
+ module CheckstyleFilter
2
+ module Git
3
+ VERSION = '0.1.0'
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,135 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: checkstyle_filter-git
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - sanemat
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-02-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: thor
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: git-diff-parser
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.8'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.8'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: minitest
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: Filter errors(checkstyle) by git diff.
84
+ email:
85
+ - o.gata.ken@gmail.com
86
+ executables:
87
+ - checkstyle_filter-git
88
+ extensions: []
89
+ extra_rdoc_files: []
90
+ files:
91
+ - ".gitignore"
92
+ - ".rubocop.yml"
93
+ - ".travis.yml"
94
+ - CODE_OF_CONDUCT.md
95
+ - Gemfile
96
+ - LICENSE.txt
97
+ - README.md
98
+ - Rakefile
99
+ - bin/console
100
+ - bin/setup
101
+ - checkstyle_filter-git.gemspec
102
+ - example/example1.xml
103
+ - example/github-pull-requests-files-response.json
104
+ - example/invalid.rb
105
+ - example/simple.rb
106
+ - exe/checkstyle_filter-git
107
+ - lib/checkstyle_filter/git.rb
108
+ - lib/checkstyle_filter/git/cli.rb
109
+ - lib/checkstyle_filter/git/diff_parser.rb
110
+ - lib/checkstyle_filter/git/version.rb
111
+ homepage: https://github.com/packsaddle/ruby-checkstyle_filter-git
112
+ licenses:
113
+ - MIT
114
+ metadata: {}
115
+ post_install_message:
116
+ rdoc_options: []
117
+ require_paths:
118
+ - lib
119
+ required_ruby_version: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - ">="
122
+ - !ruby/object:Gem::Version
123
+ version: '0'
124
+ required_rubygems_version: !ruby/object:Gem::Requirement
125
+ requirements:
126
+ - - ">="
127
+ - !ruby/object:Gem::Version
128
+ version: '0'
129
+ requirements: []
130
+ rubyforge_project:
131
+ rubygems_version: 2.4.5
132
+ signing_key:
133
+ specification_version: 4
134
+ summary: Filter checkstyle format errors on ONLY modified lines.
135
+ test_files: []