fix(hook): leave sed/awk flag clusters, continued pipes and stdin programs quiet

Review follow-up: `sed -Ei`/`-ni` edit in place, a pipe continued on the
next line or written `|&` still feeds the next command, and an awk or sed
program with no file operand reads stdin. None of them searches the
codebase. Quoted spans become one placeholder word, so a quoted program
still counts as an operand.

(cherry picked from commit 5ce9bfb04b)
This commit is contained in:
Andre Brait authored and safishamsi committed 2026-11-02 18:02:00 +00:00
1 parent c6040bdbd3
commit 049c8647df
2 files changed
+38 -14

No files matched your search

+29 -12
View File
@@ -1092,8 +1092,9 @@ def _bash_invokes_search(cmd_str: str) -> bool:
quoted spans, split on shell operators, and match the executable at each
command position (wrappers like sudo/xargs/env skipped; `git grep` and
`VAR=x grep ...` still count; a search-tool name inside prose does not).
`sed` and `awk` count too, unless they read a pipe or edit in place: run on
files, they are a grep or a read of the codebase by another name.
`sed` and `awk` count too when they open a file themselves: run on files, they
are a grep or a read of the codebase by another name. Reading a pipe, editing
in place, or naming no file (a program alone reads stdin) does not count.
"""
text = cmd_str
# Drop heredoc bodies: from the line after `<<WORD` through the line that
@@ -1111,16 +1112,24 @@ def _bash_invokes_search(cmd_str: str) -> bool:
break
text = text[: nl_idx + 1] + text[t.end():]
m = _HEREDOC_OPEN_RE.search(text, nl_idx + 1)
# Drop quoted spans (backslash escapes inside double quotes are irrelevant
# here - anything quoted is an argument, never the executable).
text = re.sub(r"'[^']*'", " ", text)
text = re.sub(r'"[^"]*"', " ", text)
# Replace quoted spans with one placeholder word (backslash escapes inside
# double quotes are irrelevant here - anything quoted is an argument, never
# the executable, but a quoted sed/awk program is still an operand).
text = re.sub(r"'[^']*'", "Q", text)
text = re.sub(r'"[^"]*"', "Q", text)
# Split into command segments at shell operators / substitution boundaries,
# keeping each operator so a segment knows whether it reads a pipe.
# keeping each operator so a segment knows whether it reads a pipe. A pipe
# carries over the empty segments of `|&` and of a pipeline continued on
# the next line.
parts = re.split(r"(\|\|?|[;&\n]|\$\(|`|\(|\)|\{|\})", text)
piped = False
for index in range(0, len(parts), 2):
piped = index > 0 and parts[index - 1] == "|"
if index > 0 and parts[index - 1] in ("|", "||"):
piped = parts[index - 1] == "|"
tokens = parts[index].split()
if not tokens:
continue
segment_piped, piped = piped, False
i = 0
while i < len(tokens):
tok = tokens[i]
@@ -1141,10 +1150,18 @@ def _bash_invokes_search(cmd_str: str) -> bool:
t2 == "grep" for t2 in tokens[i + 1:i + 4] if not t2.startswith("-")
):
return True
if name in ("sed", "awk") and not piped and not any(
t2.startswith(("-i", "--in-place")) for t2 in tokens[i + 1:]
):
return True
if name in ("sed", "awk") and not segment_piped:
rest = tokens[i + 1:]
in_place = any(
t2.startswith("--in-place")
or (t2.startswith("-i") if name == "awk" else
t2.startswith("-") and not t2.startswith("--") and "i" in t2)
for t2 in rest
)
# The program and at least one file; `x=1` is an awk assignment.
operands = [t2 for t2 in rest if not t2.startswith("-") and "=" not in t2]
if not in_place and len(operands) >= 2:
return True
break # first real token decides this segment
return False
+9 -2
View File
@@ -74,14 +74,21 @@ def test_sed_and_awk_reading_files_fire(cmd):
assert _bash_invokes_search(cmd) is True
# On the receiving end of a pipe they only reshape another command's output, and
# `sed -i` edits; neither is looking for something in the codebase.
# On the receiving end of a pipe they only reshape another command's output, `sed -i`
# (alone or in a flag cluster) edits, and a program with no file operand reads stdin;
# none of them is looking for something in the codebase.
@pytest.mark.parametrize("cmd", [
"ps aux | awk '{print $2}'",
"git log --oneline | sed -n 1,5p",
"echo x | sed s/a/b/",
"sed -i 's/a/b/' f.txt",
"gh pr list | awk -F'\\t' '{print $1}'",
"sed -Ei 's/a/b/' f.txt",
"sed -ni 's/a/b/p' f.txt",
"git log |\n awk '{print $1}'",
"make 2>&1 |& sed -n 1,5p",
"awk 'BEGIN { print 1 }'",
"sed -n 1,5p",
])
def test_sed_and_awk_on_a_pipe_or_in_place_stay_quiet(cmd):
assert _bash_invokes_search(cmd) is False