diff --git a/lib/rubocop/git/cli.rb b/lib/rubocop/git/cli.rb index 04b0cc1..a654323 100644 --- a/lib/rubocop/git/cli.rb +++ b/lib/rubocop/git/cli.rb @@ -44,6 +44,12 @@ def option_parser @options.rubocop[:display_cop_names] = true end + opt.on('-p', + '--paths PATHS', + 'Limit diff to the named path(s)') do |paths| + @options.paths = paths + end + opt.on('--cached', 'git diff --cached') do @options.cached = true end diff --git a/lib/rubocop/git/options.rb b/lib/rubocop/git/options.rb index 138ae70..c7f8579 100644 --- a/lib/rubocop/git/options.rb +++ b/lib/rubocop/git/options.rb @@ -7,7 +7,7 @@ class Invalid < StandardError; end File.expand_path('../../../../hound.yml', __FILE__) attr_accessor :config - attr_reader :cached, :hound, :rubocop + attr_reader :cached, :hound, :rubocop, :paths def initialize(hash_options = nil) @config = nil @@ -15,6 +15,7 @@ def initialize(hash_options = nil) @hound = false @rubocop = {} @commits = [] + @paths = [] from_hash(hash_options) if hash_options end @@ -47,6 +48,13 @@ def commits=(commits) @commits = commits end + def paths=(path) + @paths = path.split ' ' + @paths.unshift '--' if @paths.any? + rescue + fail Invalid, "invalid paths: #{paths.inspect}" + end + def config_file if hound HOUND_DEFAULT_CONFIG_FILE @@ -71,7 +79,7 @@ def commit_last def from_hash(hash_options) hash_options = hash_options.dup - %w(config cached hound rubocop commits).each do |key| + %w(config cached hound rubocop commits paths).each do |key| public_send("#{key}=", hash_options.delete(key)) end unless hash_options.empty? diff --git a/lib/rubocop/git/runner.rb b/lib/rubocop/git/runner.rb index 8410af6..9c780f2 100644 --- a/lib/rubocop/git/runner.rb +++ b/lib/rubocop/git/runner.rb @@ -8,10 +8,10 @@ def run(options) options = Options.new(options) unless options.is_a?(Options) @options = options - @files = DiffParser.parse(git_diff(options)) + @files = DiffParser.parse(git_diff) display_violations($stdout) - + exit(1) if violations.any? end @@ -32,17 +32,8 @@ def pull_request @pull_request ||= PseudoPullRequest.new(@files, @options) end - def git_diff(options) - args = %w(diff --diff-filter=AMCR --find-renames --find-copies) - - if options.cached - args << '--cached' - elsif options.commit_last - args << options.commit_first.shellescape - args << options.commit_last.shellescape - end - - `git #{args.join(' ')}` + def git_diff + `git #{diff_args.join ' '}` end def display_violations(io) @@ -58,6 +49,19 @@ def display_violations(io) formatter.finished(@files.map(&:filename).freeze) end + + def diff_args + args = %w(diff --diff-filter=AMCR --find-renames --find-copies) + + if @options.cached + args << '--cached' + elsif @options.commit_last + args << @options.commit_first.shellescape + args << @options.commit_last.shellescape + end + + args.concat @options.paths + end end end end