diff --git a/CHANGELOG.md b/CHANGELOG.md index 86777e11..c035cf5f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ ## Unreleased -- rule-hook now reads `git > out commit -m x` as one git commit, as the shell runs it. A redirect used to split a command in two, so such a commit escaped `commit-one-line`, `no-ai-attribution`, `round-title-counts` and `todo-rank-check`. +- rule-hook now reads `git > out commit -m x` as one git commit, as the shell runs it. A redirect used to split a command in two, so such a commit escaped `commit-one-line`, `round-title-counts` and `todo-rank-check`. - `larv-review needs-scoring-run` now calls a scoring run for the release `chunk-preflight` names as next, not for the earliest release in the plan. It also counts a change as unreleased the way `chunk-preflight` does: an entry under `CHANGELOG.md` → `## Unreleased`, or, where there is no such section, a commit after the newest `v*` tag (any commit when there is none). Before, a chunk that moved the next release's last items to a later release without shipping them called a scoring run, while `chunk-preflight` called that release unplanned. - README → Install now says that every machine taking this checkout must be a sandbox with no direct access to a production environment, since `settings.json` runs Claude Code in auto mode with the dangerous-mode prompt skipped. Before, it asked for a sandbox only for `loop-claude`. diff --git a/bin/rule_hook/test_rule_hook.py b/bin/rule_hook/test_rule_hook.py index 1d429325..d480c610 100644 --- a/bin/rule_hook/test_rule_hook.py +++ b/bin/rule_hook/test_rule_hook.py @@ -77,12 +77,18 @@ class Commits(unittest.TestCase): def test_a_redirect_keeps_its_command_s_words_together(self): self.assertEqual(segments("echo hi > f arg"), [["echo", "hi", "arg"]]) - self.assertEqual(committed("git > /tmp/x commit -m hi"), [Commit("/r", ["hi"], False)]) + for redirect in [">", ">>", "<", "<<<", "<>", ">&", "<&", ">|", "&>", "&>>"]: + with self.subTest(redirect): + self.assertEqual(committed(f"git {redirect} /tmp/x commit -m hi"), [Commit("/r", ["hi"], False)]) self.assertEqual(committed("git commit >&2 -m hi 2>&1"), [Commit("/r", ["hi"], False)]) self.assertEqual(committed("> out git commit -m hi"), [Commit("/r", ["hi"], False)]) - def test_a_command_after_a_redirect_and_a_separator_is_still_apart(self): + def test_a_separator_glued_to_a_redirect_still_ends_the_command(self): self.assertEqual(segments("echo a > f; echo b | cat &> g\nls"), [["echo", "a"], ["echo", "b"], ["cat"], ["ls"]]) + for command in ["echo a &f git commit -m hi", "tee >(true) git commit -m hi"]: + with self.subTest(command): + self.assertEqual(committed(command), [Commit("/r", ["hi"], False)]) + self.assertEqual(segments("diff <(a) b"), [["diff"], ["b"]]) class CommandBreaches(unittest.TestCase): diff --git a/bin/shell_command.py b/bin/shell_command.py index 1dd3711b..5e85780a 100644 --- a/bin/shell_command.py +++ b/bin/shell_command.py @@ -7,8 +7,8 @@ import shlex HEREDOC = re.compile(r"<<-?[ \t]*([\"']?)([A-Za-z_]\w*)\1[^\n]*\n.*?^[ \t]*\2[ \t]*$", re.DOTALL | re.MULTILINE) ASSIGNMENT = re.compile(r"[A-Za-z_][A-Za-z0-9_]*=") OPERATOR = set("\n();&|<>") -# A redirect operator on its own: a command goes on around it. -REDIRECT = re.compile(r"&?[<>]+[&|]?") +# A redirect operator on its own: a command goes on around it. `&<` is no redirect: it is a `&` that ends the command, then a `<`. +REDIRECT = re.compile(r"&>>?|[<>]+[&|]?") # A quoted string or an escape is taken whole, so a # inside one starts no comment; a comment starts a word. PIECE = re.compile(r"""'[^']*'|"(?:\\.|[^"\\])*"|\\.|(? list[list[str]]: - a quoted string is one word, so `bash -c "git commit"` and `echo "$(git commit)"` hide the inner command; - a backtick substitution stays inside its words, while `$(git commit)` outside quotes splits into the `$` and a command of its own; - a keyword or a brace opens its command's words, so `then git commit` reads as the command `then`; - - `<(...)` and `<<<` read as redirects, so the word after them is dropped as the target; + - `<(...)` and `>(...)` end the command, and the word after them is dropped as a redirect target, so `tee >(git commit)` hides the commit; + - `<<<` reads as a redirect, so the word after it is dropped as the target; - a file descriptor before a redirect, as in `2>&1`, stays a word; - a heredoc's body is dropped; - variables, aliases and functions are not expanded; @@ -54,6 +55,7 @@ def segments(command: str) -> list[list[str]]: # A redirect's target is not an operand. target = True elif set(token) <= OPERATOR and token: + # A separator, or a redirect glued to one as in `;>f`, `&" in token found.append([]) elif found[-1] or not ASSIGNMENT.match(token):