rubocop-git 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e847268b1ab1d8961bb6afed3402f75cbc1abf60
4
- data.tar.gz: 713c38e0742d3793d6234ab0ac8d971b3b821cae
3
+ metadata.gz: c672dcd90e02f12fe912671d34296778f4c4126f
4
+ data.tar.gz: 7218bdfe3e468c96c93ebfe7b65761778d8bd097
5
5
  SHA512:
6
- metadata.gz: 3ab7b0885da47ed39deb523fa5dbd7daac6b406b9ebf260982120fa071b8c340eca530e032a04ad959cd61cf1a4ae9d98913d0bb0126fcd87b4d0df5c93508f5
7
- data.tar.gz: c3016b240f51a4645938202dc43c955ef91e95847d6881742bce4baca3ef7c6e9c11068ea005f94d6772f7266ebe7b6ad32f44315b85e5e8ad65afd5259e4f38
6
+ metadata.gz: 3aeafd917cf189510adc77bba476911ce0cc1bc4ea902305c68083e73967eabe23980984711c8ac02f559607b294b4431d8e730228546de27243121da50366cf
7
+ data.tar.gz: bb6f02b01e9eaf7331be756f07cef68aef73d4860c21be8551268a6e0175064537fd7707742ff4a3f979cd47e063faddf7eedc95961b8aa5e93a3cf85d58fc1f
data/README.md CHANGED
@@ -18,7 +18,7 @@ Or install it yourself as:
18
18
 
19
19
  ## Usage
20
20
 
21
- Usage: rubocop-git [options]
21
+ Usage: rubocop-git [options] [[commit] commit]
22
22
  -c, --config FILE Specify configuration file
23
23
  -D, --display-cop-names Display cop names in offense messages
24
24
  --cached git diff --cached
data/lib/rubocop/git.rb CHANGED
@@ -3,7 +3,9 @@ require 'rubocop'
3
3
 
4
4
  if defined?(Rubocop)
5
5
  # rubocop 0.22.0
6
- RuboCop::Formatter = Rubocop::Formatter
6
+ RuboCop::ConfigLoader = Rubocop::ConfigLoader
7
+ RuboCop::Formatter = Rubocop::Formatter
8
+ RuboCop::Version = Rubocop::Version
7
9
  else
8
10
  # rubocop >= 0.23.0
9
11
  Rubocop = RuboCop
@@ -12,8 +14,10 @@ end
12
14
  module RuboCop
13
15
  module Git
14
16
  autoload :CommitFile, 'rubocop/git/commit_file'
17
+ autoload :DiffParser, 'rubocop/git/diff_parser'
15
18
  autoload :FileViolation, 'rubocop/git/file_violation'
16
19
  autoload :Line, 'rubocop/git/line'
20
+ autoload :Options, 'rubocop/git/options'
17
21
  autoload :Patch, 'rubocop/git/patch'
18
22
  autoload :PseudoPullRequest, 'rubocop/git/pseudo_pull_request'
19
23
  autoload :PseudoResource, 'rubocop/git/pseudo_resource'
@@ -5,44 +5,49 @@ module RuboCop
5
5
  module Git
6
6
  class CLI
7
7
  def run(args = ARGV)
8
- options = parse_arguments(args)
9
- Runner.new.run(options)
8
+ @options = Options.new
9
+ parse_arguments(args)
10
+ Runner.new.run(@options)
10
11
  end
11
12
 
12
13
  private
13
14
 
14
15
  def parse_arguments(args)
15
- options = {}
16
+ @options.commits = option_parser.parse(args)
17
+ rescue OptionParser::InvalidOption, Options::Invalid => ex
18
+ warn "ERROR: #{ex.message}"
19
+ $stderr.puts
20
+ warn option_parser
21
+ exit 1
22
+ end
23
+
24
+ def option_parser
25
+ @option_parser ||= OptionParser.new do |opt|
26
+ opt.banner << ' [[commit] commit]'
16
27
 
17
- OptionParser.new do |opt|
18
28
  opt.on('-c', '--config FILE',
19
29
  'Specify configuration file') do |config|
20
- options[:config] = config
30
+ @options.config = config
21
31
  end
22
32
 
23
33
  opt.on('-D', '--display-cop-names',
24
34
  'Display cop names in offense messages') do
25
- options[:rubocop] ||= {}
26
- options[:rubocop][:display_cop_names] = true
35
+ @options.rubocop[:display_cop_names] = true
27
36
  end
28
37
 
29
38
  opt.on('--cached', 'git diff --cached') do
30
- options[:cached] = true
39
+ @options.cached = true
31
40
  end
32
41
 
33
42
  opt.on('--staged', 'synonym of --cached') do
34
- options[:cached] = true
43
+ @options.cached = true
35
44
  end
36
45
 
37
46
  opt.on('--hound',
38
47
  'Hound compatibility mode (require rubocop 0.22.0)') do
39
- options[:hound] = true
48
+ @options.hound = true
40
49
  end
41
-
42
- opt.parse(args)
43
50
  end
44
-
45
- options
46
51
  end
47
52
  end
48
53
  end
@@ -0,0 +1,31 @@
1
+ module RuboCop
2
+ module Git
3
+ class DiffParser
4
+ class << self
5
+ def parse(diff)
6
+ new.parse(diff)
7
+ end
8
+ end
9
+
10
+ def parse(diff)
11
+ files = []
12
+ in_patch = false
13
+
14
+ diff.each_line do |line|
15
+ case line
16
+ when /^diff --git/
17
+ in_patch = false
18
+ when %r{^\+{3} b/(?<path>[^\t\n\r]+)}
19
+ files << PseudoResource.new(Regexp.last_match[:path])
20
+ when /^@@/
21
+ in_patch = true
22
+ end
23
+
24
+ files.last.patch << line if in_patch
25
+ end
26
+
27
+ files
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,86 @@
1
+ module RuboCop
2
+ module Git
3
+ class Options
4
+ class Invalid < StandardError; end
5
+
6
+ HOUND_DEFAULT_CONFIG_FILE =
7
+ File.expand_path('../../../../hound.yml', __FILE__)
8
+
9
+ attr_accessor :config
10
+ attr_reader :cached, :hound, :rubocop
11
+
12
+ def initialize(hash_options = nil)
13
+ @config = nil
14
+ @cached = false
15
+ @hound = false
16
+ @rubocop = {}
17
+ @commits = []
18
+
19
+ from_hash(hash_options) if hash_options
20
+ end
21
+
22
+ def cached=(cached_)
23
+ if cached_ && !@commits.empty?
24
+ fail Invalid, 'cached and commit cannot be specified together'
25
+ end
26
+ @cached = !!cached_
27
+ end
28
+
29
+ def hound=(hound_)
30
+ if hound_ && RuboCop::Version.version != '0.22.0'
31
+ fail Invalid, 'Hound compatibility mode requires rubocop 0.22.0'
32
+ end
33
+ @hound = !!hound_
34
+ end
35
+
36
+ def rubocop=(rubocop_)
37
+ unless rubocop_.is_a?(Hash)
38
+ fail Invalid, "invalid rubocop: #{rubocop_.inspect}"
39
+ end
40
+ @rubocop = rubocop_
41
+ end
42
+
43
+ def commits=(commits)
44
+ unless commits.is_a?(Array) && commits.length <= 2
45
+ fail Invalid, "invalid commits: #{commits.inspect}"
46
+ end
47
+ if !commits.empty? && cached
48
+ fail Invalid, 'cached and commit cannot be specified together'
49
+ end
50
+ @commits = commits
51
+ end
52
+
53
+ def config_path
54
+ if hound
55
+ HOUND_DEFAULT_CONFIG_FILE
56
+ elsif config
57
+ config
58
+ elsif File.exist?(RuboCop::ConfigLoader::DOTFILE)
59
+ RuboCop::ConfigLoader::DOTFILE
60
+ else
61
+ RuboCop::ConfigLoader::DEFAULT_FILE
62
+ end
63
+ end
64
+
65
+ def commit_first
66
+ @commits.length == 1 ? @commits.first + '^' : @commits.first
67
+ end
68
+
69
+ def commit_last
70
+ @commits.last
71
+ end
72
+
73
+ private
74
+
75
+ def from_hash(hash_options)
76
+ hash_options = hash_options.dup
77
+ %w(config cached hound rubocop commits).each do |key|
78
+ public_send("#{key}=", hash_options.delete(key))
79
+ end
80
+ unless hash_options.empty?
81
+ fail Invalid, "invalid keys: #{hash_options.keys.join(' ')}"
82
+ end
83
+ end
84
+ end
85
+ end
86
+ end
@@ -18,7 +18,7 @@ module RuboCop
18
18
  end
19
19
 
20
20
  def config
21
- return unless @options[:hound]
21
+ return unless @options.hound
22
22
  File.read(HOUND_CONFIG_FILE)
23
23
  rescue Errno::ENOENT
24
24
  nil
@@ -31,8 +31,10 @@ module RuboCop
31
31
  end
32
32
 
33
33
  def file_contents(filename)
34
- if @options[:cached]
34
+ if @options.cached
35
35
  `git show :#{filename.shellescape}`
36
+ elsif @options.commit_last
37
+ `git show #{@options.commit_last.shellescape}:#{filename.shellescape}`
36
38
  else
37
39
  File.read(filename)
38
40
  end
@@ -1,6 +1,16 @@
1
1
  module RuboCop
2
2
  module Git
3
- class PseudoResource < Struct.new(:filename, :status, :patch)
3
+ class PseudoResource
4
+ attr_reader :filename, :patch
5
+
6
+ def initialize(filename)
7
+ @filename = filename
8
+ @patch = ''
9
+ end
10
+
11
+ def status
12
+ 'modified'
13
+ end
4
14
  end
5
15
  end
6
16
  end
@@ -1,19 +1,14 @@
1
+ require 'shellwords'
2
+
1
3
  module RuboCop
2
4
  module Git
3
5
  # ref. https://github.com/thoughtbot/hound/blob/be2dd34/app/services/build_runner.rb
4
6
  class Runner
5
- DEFAULT_CONFIG_FILE = '.rubocop.yml'
6
- HOUND_DEFAULT_CONFIG_FILE =
7
- File.expand_path('../../../../hound.yml', __FILE__)
8
-
9
7
  def run(options)
10
- if options[:hound] && RuboCop::Version.version != '0.22.0'
11
- warn 'Hound compatibility mode require rubocop 0.22.0'
12
- exit 1
13
- end
8
+ options = Options.new(options) unless options.is_a?(Options)
14
9
 
15
10
  @options = options
16
- @files = parse_diff(git_diff(options[:cached]))
11
+ @files = DiffParser.parse(git_diff(options))
17
12
 
18
13
  display_violations($stdout)
19
14
  end
@@ -26,8 +21,8 @@ module RuboCop
26
21
 
27
22
  def style_checker
28
23
  StyleChecker.new(pull_request.pull_request_files,
29
- @options[:rubocop],
30
- config_path,
24
+ @options.rubocop,
25
+ @options.config_path,
31
26
  pull_request.config)
32
27
  end
33
28
 
@@ -35,40 +30,17 @@ module RuboCop
35
30
  @pull_request ||= PseudoPullRequest.new(@files, @options)
36
31
  end
37
32
 
38
- def git_diff(cached)
39
- if cached
40
- `git diff --diff-filter=AMCR --cached --find-renames --find-copies`
41
- else
42
- `git diff --diff-filter=AM`
43
- end
44
- end
45
-
46
- def parse_diff(diff)
47
- files = []
48
- in_patch = false
49
-
50
- diff.each_line do |line|
51
- case line
52
- when /^diff --git/
53
- in_patch = false
54
- when %r{^\+{3} b/([^\t\n\r]+)}
55
- files << PseudoResource.new($1, 'modified', '')
56
- when /^@@/
57
- in_patch = true
58
- end
33
+ def git_diff(options)
34
+ args = %w(diff --diff-filter=AMCR --find-renames --find-copies)
59
35
 
60
- files.last.patch << line if in_patch
36
+ if options.cached
37
+ args << '--cached'
38
+ elsif options.commit_last
39
+ args << options.commit_first.shellescape
40
+ args << options.commit_last.shellescape
61
41
  end
62
42
 
63
- files
64
- end
65
-
66
- def config_path
67
- if @options[:hound]
68
- HOUND_DEFAULT_CONFIG_FILE
69
- else
70
- @options[:config] || DEFAULT_CONFIG_FILE
71
- end
43
+ `git #{args.join(' ')}`
72
44
  end
73
45
 
74
46
  def display_violations(io)
@@ -1,5 +1,5 @@
1
1
  module RuboCop
2
2
  module Git
3
- VERSION = '0.0.1'
3
+ VERSION = '0.0.2'
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubocop-git
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Masaki Takeuchi
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-07-02 00:00:00.000000000 Z
11
+ date: 2014-07-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -76,8 +76,10 @@ files:
76
76
  - lib/rubocop/git.rb
77
77
  - lib/rubocop/git/cli.rb
78
78
  - lib/rubocop/git/commit_file.rb
79
+ - lib/rubocop/git/diff_parser.rb
79
80
  - lib/rubocop/git/file_violation.rb
80
81
  - lib/rubocop/git/line.rb
82
+ - lib/rubocop/git/options.rb
81
83
  - lib/rubocop/git/patch.rb
82
84
  - lib/rubocop/git/pseudo_pull_request.rb
83
85
  - lib/rubocop/git/pseudo_resource.rb