Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'set' | ||
require 'open3' | ||
|
||
module RuboCop | ||
# This class finds target files to inspect by scanning the directory tree | ||
|
@@ -59,10 +60,13 @@ def target_files_in_dir(base_dir = Dir.pwd) | |
end | ||
all_files = find_files(base_dir, File::FNM_DOTMATCH) | ||
hidden_files = Set.new(all_files - find_files(base_dir, 0)) | ||
|
||
git_files = ls_git_files(base_dir) | ||
|
||
base_dir_config = @config_store.for(base_dir) | ||
|
||
target_files = all_files.select do |file| | ||
to_inspect?(file, hidden_files, base_dir_config) | ||
to_inspect?(file, git_files, hidden_files, base_dir_config) | ||
end | ||
|
||
# Most recently modified file first. | ||
|
@@ -71,13 +75,6 @@ def target_files_in_dir(base_dir = Dir.pwd) | |
target_files | ||
end | ||
|
||
def to_inspect?(file, hidden_files, base_dir_config) | ||
return false if base_dir_config.file_to_exclude?(file) | ||
return true if !hidden_files.include?(file) && ruby_file?(file) | ||
|
||
base_dir_config.file_to_include?(file) | ||
end | ||
|
||
# Search for files recursively starting at the given base directory using | ||
# the given flags that determine how the match is made. Excluded files will | ||
# be removed later by the caller, but as an optimization find_files removes | ||
|
@@ -100,6 +97,31 @@ def find_files(base_dir, flags) | |
Dir.glob(pattern, flags).select { |path| FileTest.file?(path) } | ||
end | ||
|
||
private | ||
|
||
def ls_git_files(base_dir) | ||
return if `sh -c 'command -v git'`.empty? | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
agross
|
||
|
||
output, _error, status = Open3.capture3( | ||
'git', 'ls-files', '-z', base_dir, | ||
'--exclude-standard', '--others', '--cached', '--modified' | ||
) | ||
|
||
return unless status.success? | ||
|
||
output.split("\0").map { |git_file| "#{base_dir}/#{git_file}" } | ||
end | ||
|
||
def to_inspect?(file, git_files, hidden_files, base_dir_config) | ||
return false if base_dir_config.file_to_exclude?(file) | ||
|
||
if !hidden_files.include?(file) && ruby_file?(file) | ||
return git_files.nil? || git_files.include?(file) | ||
end | ||
|
||
base_dir_config.file_to_include?(file) | ||
end | ||
|
||
def toplevel_dirs(base_dir, flags) | ||
Dir.glob(File.join(base_dir, '*'), flags).select do |dir| | ||
File.directory?(dir) && !dir.end_with?('/.', '/..') | ||
|
How is this working under Windows? Testing on my machine such a line would yield an error. Is this code not reached on Windows systems?