Skip to content

Commit

Permalink
src/sqlfluff/core/linter: Improve ignore file processing
Browse files Browse the repository at this point in the history
- Don't re-scan entire directories for every sqlfluffignore

- Fix crashes due to recursive symlinks

- Improve warning when an exact file is ignored
  • Loading branch information
CyberShadow committed Oct 14, 2021
1 parent 8619f79 commit 207dd43
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 18 deletions.
44 changes: 26 additions & 18 deletions src/sqlfluff/core/linter/linter.py
Original file line number Diff line number Diff line change
Expand Up @@ -729,18 +729,15 @@ def paths_from_path(

# If it's a directory then expand the path!
buffer = []
ignore_set = set()
ignores = {}
for dirpath, _, filenames in path_walk:
for fname in filenames:
fpath = os.path.join(dirpath, fname)
# Handle potential .sqlfluffignore files
if ignore_files and fname == ignore_file_name:
with open(fpath) as fh:
spec = pathspec.PathSpec.from_lines("gitwildmatch", fh)
matches = spec.match_tree(dirpath)
for m in matches:
ignore_path = os.path.join(dirpath, m)
ignore_set.add(os.path.abspath(ignore_path))
ignores[dirpath] = spec
# We don't need to process the ignore file any futher
continue

Expand All @@ -760,20 +757,31 @@ def paths_from_path(
filtered_buffer = []

for fpath in buffer:
if os.path.abspath(fpath) not in ignore_set:
for ignore_base, ignore_spec in ignores.items():
abs_fpath = os.path.abspath(fpath)
abs_ignore_base = os.path.abspath(ignore_base)
if abs_fpath.startswith(
abs_ignore_base + os.sep
) and ignore_spec.match_file(
os.path.relpath(abs_fpath, abs_ignore_base)
):
# This file is ignored, skip it.
if is_exact_file:
linter_logger.warning(
"Exact file path %s was given but "
"it was ignored by a %s pattern in %s, "
"re-run with `--disregard-sqlfluffignores` to "
"skip %s"
% (
path,
ignore_file_name,
ignore_base,
ignore_file_name,
)
)
break
else:
filtered_buffer.append(os.path.normpath(fpath))
elif is_exact_file:
linter_logger.warning(
"Exact file path %s was given but "
"it was ignored by a %s pattern, "
"re-run with `--disregard-sqlfluffignores` to "
"skip %s"
% (
path,
ignore_file_name,
ignore_file_name,
)
)

# Return
return sorted(filtered_buffer)
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/linter/sqlfluffignore/path_c

0 comments on commit 207dd43

Please sign in to comment.