Simplify the recursive-search check and kill the mutants of its option parsing

This commit is contained in:
2026-11-13 21:46:03 +01:00
parent 153a85c78f
commit e0c8209750
2 changed files with 47 additions and 18 deletions
+22 -15
View File
@@ -14,7 +14,7 @@ from rule_hook.call import Call, text
from project_config.settings import env_files
from failure import Failure
from rule_hook.git import guarded_git
from command_lines.shell_command import Launch, launched, lexed, OPERATOR, resolved, Simple, simple_commands
from command_lines.shell_command import launched, lexed, OPERATOR, resolved, simple_commands
# Tools that search a folder's files: grep only given an option that makes it recursive, rg and ag always.
GREPS = frozenset({"egrep", "fgrep", "grep"})
@@ -90,37 +90,44 @@ def searches(command: str, cwd: str, home: str, path: pathlib.Path) -> bool:
if launch.tool in GREPS and not any(RECURSIVE_GREP.fullmatch(option) or option.endswith("recurse") for option in [*options, *operands]):
continue
globs = globs_of(launch.arguments) if launch.tool == "rg" else []
if any(holds(folder, path) and not skips(launch.tool, options, globs, folder, path) for folder in folders(simple, launch, options, operands, home)):
if any(holds(folder, path) and not skips(launch.tool, options, globs, folder, path) for folder in folders(simple.directory, options, operands, home)):
return True
return False
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."""
end = arguments.index("--") if "--" in arguments else len(arguments)
options = [word for word in arguments[:end] if word.startswith("-") and word != "-"]
return options, [word for word in arguments[:end] if word not in options] + arguments[end + 1:]
options: list[str] = []
operands: list[str] = []
ended = False
for word in arguments:
if ended or not word.startswith("-"):
operands.append(word)
elif word == "--":
ended = True
else:
options.append(word)
return options, operands
def folders(simple: Simple, launch: Launch, options: list[str], operands: list[str], home: str) -> list[str]:
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 the directory it runs in where none names a file or a folder. An option's value is an operand here; one that names no file or folder is dropped like any word that does not."""
if not any(PATTERN_OPTION.fullmatch(option) for option in options):
# The first operand is the pattern.
operands = operands[1:]
named = [resolved(simple.directory, home_word(operand), home) for operand in operands]
named = [resolved(directory, home_word(operand), home) for operand in operands]
existing = [word for word in named if os.path.exists(word)]
return [word for word in existing if os.path.isdir(word)] if existing else [simple.directory]
return [word for word in existing if os.path.isdir(word)] if existing else [directory]
def globs_of(arguments: list[str]) -> list[str]:
"""The globs of rg's -g, --glob and --iglob options; --iglob's matching without regard to case is not translated."""
found = []
for at, word in enumerate(arguments):
option = GLOB_OPTION.fullmatch(word)
if option:
given = option.group(1) or option.group(2)
glob = given if given else "".join(arguments[at + 1:at + 2])
# --iglob matches without regard to case, which globbed() does not translate; braces make it count as matching.
found.append("{" + glob + "}" if word.startswith("--iglob") else glob)
found.append(given if given else arguments[at + 1] if at + 1 < len(arguments) else "")
return found
@@ -132,14 +139,14 @@ def skips(tool: str, options: list[str], globs: list[str], folder: str, path: pa
if positive:
return not any(globbed(glob, folder, path) for glob in positive)
long = [option for option in options if option.startswith("--")]
short = "".join(option[1:] for option in options if not option.startswith("--"))
unrestricted = short.count("u") + long.count("--unrestricted")
clusters = [option[1:] for option in options if not option.startswith("--")]
unrestricted = sum(cluster.count("u") for cluster in clusters) + long.count("--unrestricted")
if tool == "rg":
hidden_lifted = "--hidden" in long or "." in short or unrestricted >= 2
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 "U" in short or "a" in short or "--skip-vcs-ignores" in long or "--all-types" in long
ignored_lifted = unrestricted >= 1 or any("U" in cluster or "a" in cluster for cluster in clusters) or "--skip-vcs-ignores" in long or "--all-types" in long
return (hidden_below(folder, path) and not hidden_lifted) or (ignored(path) and not ignored_lifted)