Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions lib/rubocop/git/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 10 additions & 2 deletions lib/rubocop/git/options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ 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
@cached = false
@hound = false
@rubocop = {}
@commits = []
@paths = []

from_hash(hash_options) if hash_options
end
Expand Down Expand Up @@ -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
Expand All @@ -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?
Expand Down
30 changes: 17 additions & 13 deletions lib/rubocop/git/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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)
Expand All @@ -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