Skip to content

Commit

Permalink
Revert "[Fix #595] Exclude files ignored by git"
Browse files Browse the repository at this point in the history
This reverts commit 56c4cd0.
  • Loading branch information
bbatsov committed Jan 11, 2019
1 parent 743ca45 commit 70cca8b
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 90 deletions.
38 changes: 8 additions & 30 deletions lib/rubocop/target_finder.rb
@@ -1,7 +1,6 @@
# frozen_string_literal: true

require 'set'
require 'open3'

module RuboCop
# This class finds target files to inspect by scanning the directory tree
Expand Down Expand Up @@ -60,13 +59,10 @@ 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, git_files, hidden_files, base_dir_config)
to_inspect?(file, hidden_files, base_dir_config)
end

# Most recently modified file first.
Expand All @@ -75,6 +71,13 @@ 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
Expand All @@ -97,31 +100,6 @@ 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?

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?('/.', '/..')
Expand Down
1 change: 0 additions & 1 deletion manual/configuration.md
Expand Up @@ -229,7 +229,6 @@ ruby interpreters listed under `AllCops`/`RubyInterpreters` are
inspected, unless the file also matches a pattern in
`AllCops`/`Exclude`. Hidden directories (i.e., directories whose names
start with a dot) are not searched by default.
Files ignored by Git are ignored by RuboCop by default.

Here is an example that might be used for a Rails project:

Expand Down
59 changes: 0 additions & 59 deletions spec/rubocop/target_finder_spec.rb
Expand Up @@ -398,65 +398,6 @@
expect(found_basenames).not_to include('ruby2.rb')
end

context 'in git repo directory' do
before do
system 'git init'

create_empty_file('git_regular.rb')
create_empty_file('git_group.rb')
create_empty_file('git_excluded.rb')
create_empty_file('git_glob.rb')
create_empty_file('git with spaces.rb')
create_empty_file('git_dir/thing.rb')
create_file('.gitignore', <<-'CONTENT'.strip_indent)
git_regular.rb
git_gr[ao]up.rb
!git_excluded.rb
git_glob*
git_dir/
CONTENT
end

it 'does not pick files ignored by git' do
expect(found_basenames).not_to include(
'git_regular.rb',
'git_group.rb',
'git_glob.rb',
'git_dir/thing.rb'
)
expect(found_basenames).to include(
'git_excluded.rb',
'git with spaces.rb'
)
end

context 'without `git` executable' do
before do
allow_any_instance_of(Kernel).to receive(:`)
.with(/command -v git/)
.and_return('')

allow_any_instance_of(Kernel).to receive(:`)
.with(start_with('git ls-files'))
.and_raise(Errno::ENOENT, 'git')
end

it 'does not fail' do
expect { found_basenames }.not_to raise_error
end
end

context 'with `*` in argument' do
let(:base_dir) { './git*' }

it 'does not fail' do
expect(found_basenames).not_to include(
'thing.rb'
)
end
end
end

context 'when an exception is raised while reading file' do
around do |example|
original_stderr = $stderr
Expand Down

1 comment on commit 70cca8b

@localhostdotdev
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(I was wondering why this got reverted and it's because of a lack of windows compatibility it seems: 56c4cd0#r31924849)

Please sign in to comment.