Files

138 lines
7.8 KiB
Python

"""Whether a search of a folder reaches a file, for the `env-file-read` rule: a Grep with its glob, and a Bash `ag`, `grep -r` or `rg`.
A search reaches a file in a folder it searches unless the tool skips the file. ripgrep, which a Grep runs, and `ag` skip a file that the checkout holding it ignores; a Bash `rg` or `ag` also skips a hidden file and one below a hidden folder of the searched folder, which a Grep doesn't. `grep -r` doesn't skip any file. A glob changes what ripgrep skips, as `globbed` and `skips` say.
Each judgment refuses more where it is unsure: an option it doubts lifts a skip, and a pipe's input isn't told apart from the directory, so `x | rg y` in a folder holding the file is refused too."""
import itertools
import os
import pathlib
import re
from failure import Failure
from rule_hook.git import guarded_git
from command_lines.shell_command import home_word, Launch, launched, resolved, simple_commands
GREPS = frozenset({"egrep", "fgrep", "grep"})
SEARCHERS = GREPS | {"ag", "rg"}
# grep searches folders only with an option that makes it recursive: -r and -R in a cluster, --recursive, and `-d recurse` as one word or two.
RECURSIVE_GREP = re.compile(r"-[^-]*[rR].*|--(?:dereference-)?recursive|--directories=recurse|-[^-]*drecurse")
# The option before a separate `recurse` word that makes grep recursive.
DIRECTORIES_OPTION = re.compile(r"-[^-]*d|--directories")
# An option that gives the pattern, so that no operand is the pattern: -e and -f in a cluster, and --regexp and --file.
PATTERN_OPTION = re.compile(r"-[^-]*[ef].*|--(?:regexp|file)(?:=.*)?")
# rg's --glob and --iglob, and -g at the start of a word: group 1 or 2 holds the glob when it is in the same word, else it is the next word.
GLOB_OPTION = re.compile(r"--i?glob(?:=(.*))?|-g(.*)")
# The options with which ag searches an ignored file, besides -u and --unrestricted.
AG_LIFTS_IGNORED = frozenset({"--all-text", "--all-types", "--skip-vcs-ignores"})
# What a Grep's glob holds beyond literals, `*`, `?` and `/`: forms globbed() does not translate.
UNMATCHED = re.compile(r"[{}\[\]\\,\s]|/$")
def searches(command: str, cwd: str, home: str, path: pathlib.Path) -> bool:
"""Whether a simple command of the command recursively searches a folder that holds path, and its tool does not skip path there."""
for simple in simple_commands(command, cwd, home):
launch = launched(simple.words, SEARCHERS)
if launch is None:
continue
options, operands = split(launch.arguments)
if launch.tool in GREPS and not recursive(launch.arguments, options):
continue
if any(holds(folder, path) and not skips(launch, options, folder, path) for folder in folders(simple.directory, options, operands, home)):
return True
return False
def recursive(arguments: list[str], options: list[str]) -> bool:
"""Whether a grep's options make it search folders."""
pairs = itertools.pairwise(arguments)
return any(RECURSIVE_GREP.fullmatch(option) for option in options) or any(value == "recurse" and DIRECTORIES_OPTION.fullmatch(option) for option, value in pairs)
def split(arguments: list[str]) -> tuple[list[str], list[str]]:
"""A search's arguments as its options and its operands: the words after `--` are operands."""
options: list[str] = []
operands: list[str] = []
words = iter(arguments)
for word in words:
if word == "--":
# It takes the rest of the words, which ends the loop.
operands.extend(words)
elif word.startswith("-"):
options.append(word)
else:
operands.append(word)
return options, operands
def folders(directory: str, options: list[str], operands: list[str], home: str) -> list[str]:
"""The folders a search reads: its operands that are folders, or else the directory it runs in. Other operands are dropped: an option's value, such as 3 in `-A 3`, is one, and so is a file."""
if not any(PATTERN_OPTION.fullmatch(option) for option in options):
# The first operand is the pattern, or the value of an option before it.
operands = operands[1:]
named = [resolved(directory, home_word(operand), home) for operand in operands]
return [word for word in named if os.path.isdir(word)] or [directory]
def globs_of(arguments: list[str]) -> tuple[list[str], bool]:
"""The globs that rg's -g, --glob and --iglob options give, without the ones that exclude files, and whether rg matches them without regard to case, which globbed() does not translate."""
found = []
for at, word in enumerate(arguments):
option = GLOB_OPTION.fullmatch(word)
if option:
given = option.group(1) or option.group(2)
if not given and at + 1 < len(arguments):
given = arguments[at + 1]
if given and not given.startswith("!"):
found.append(given)
return found, any(word.startswith("--iglob") or word == "--glob-case-insensitive" for word in arguments)
def skips(launch: Launch, options: list[str], folder: str, path: pathlib.Path) -> bool:
"""Whether the tool passes over path when it searches folder. A grep doesn't pass over any file. rg searches only the files that its globs match, where it has a glob that doesn't exclude files; a glob holding a `/` counts as matching, since rg anchors it at the directory it runs in, which is not told from the folder searched. Otherwise rg and ag pass over a hidden path below folder and an ignored file, unless an option lifts that."""
if launch.tool in GREPS:
return False
if launch.tool == "rg":
globs, insensitive = globs_of(launch.arguments)
if globs:
return not (insensitive or any("/" in glob or globbed(glob, folder, path) for glob in globs))
long = [option for option in options if option.startswith("--")]
clusters = [option[1:] for option in options if not option.startswith("--")]
unrestricted = sum(cluster.count("u") for cluster in clusters) + long.count("--unrestricted")
if launch.tool == "rg":
hidden_lifted = "--hidden" in long or any("." in cluster for cluster in clusters) or unrestricted >= 2
ignored_lifted = unrestricted >= 1 or any(option.startswith("--no-ignore") for option in long)
else:
hidden_lifted = "--hidden" in long or unrestricted >= 1
ignored_lifted = unrestricted >= 1 or any(re.search("[Uat]", cluster) for cluster in clusters) or not AG_LIFTS_IGNORED.isdisjoint(long)
return (hidden_below(folder, path) and not hidden_lifted) or (ignored(path) and not ignored_lifted)
def hidden_below(folder: str, path: pathlib.Path) -> bool:
"""Whether path, or a folder on the way to it from folder, has a name that starts with a dot."""
return any(part.startswith(".") for part in os.path.relpath(os.path.realpath(path), os.path.realpath(folder)).split("/"))
def holds(folder: str, path: pathlib.Path) -> bool:
try:
return pathlib.Path(os.path.realpath(path)).is_relative_to(os.path.realpath(folder))
except ValueError:
# A folder whose path holds a NUL byte holds no file.
return False
def ignored(path: pathlib.Path) -> bool:
"""Whether the checkout holding path ignores it: ripgrep and ag skip such a file in a folder they search, ripgrep unless a glob matches it."""
return not isinstance(guarded_git(str(path.parent), "check-ignore", "--quiet", path.name), Failure)
def globbed(glob: str, searched: str, path: pathlib.Path) -> bool:
"""Whether a Grep's glob can match path, as ripgrep matches it: one without a `/` against the file's name, one with a `/` against the path from the searched folder. A glob holding a form this does not translate, such as `{a,b}`, counts as matching."""
if not glob:
return False
if UNMATCHED.search(glob):
return True
anchored = glob.removeprefix("/") if "/" in glob else f"**/{glob}"
# A `**` that is not a whole folder name matches as `*` in ripgrep; crossing folders there only refuses more.
translated = {"**/": "(?:.*/)?", "**": ".*", "*": "[^/]*", "?": "[^/]"}
pattern = "".join(translated.get(piece, re.escape(piece)) for piece in re.split(r"(\*\*/|\*\*|\*|\?)", anchored))
return re.fullmatch(pattern, os.path.relpath(os.path.realpath(path), os.path.realpath(searched))) is not None