Repository navigation
fix(hook): keep backslash-newline inside quotes and comments - #5117
Open
seragAhmed wants to merge 2 commits into
Open
seragAhmed wants to merge 2 commits into
seragAhmed wants to merge 2 commits into
Conversation
`collapse_line_continuations` replaced every backslash-newline with a space, including inside single quotes, `$'...'` and comments, where bash keeps it. A single-quoted commit message lost its line break, and a comment ending in a backslash swallowed the next line, which bash runs as its own command. - The collapse is now a scanner with quote and comment state: it joins only outside single quotes, `$'...'` and comments; inside double quotes it drops the backslash and newline and keeps the string's own whitespace, as bash does. - A comment that ends in a backslash-newline passes the command through. The lexer has no comment state and would join the next line into the comment's segment, so the hook would otherwise allow a rewrite whose second line no deny or ask rule was checked against. - The wrapper permission check keeps joining every continuation (`join_all_line_continuations`): the script of `bash -c 'git \<NL>push'` is parsed again by the inner shell, where that is a continuation. Fixes rtk-ai#5090 Co-Authored-By: Claude Opus 5.5 <[email protected]>
📊 Automated PR Analysis
SummaryRewrites the shell line-continuation collapser as a quote/comment-aware scanner instead of a blind regex, so backslash-newlines inside single quotes, $'...' strings, and comments are preserved rather than collapsed, matching bash's actual behavior. It also detects comments that end in a backslash-newline and defers to the agent's own permission handling instead of letting the rewrite silently misinterpret the following line, while keeping the separate wrapper-check join function unchanged for i… Review Checklist
Linked issues: #5090 Analyzed automatically by wshm · This is an automated analysis, not a human review. |
The wrapper check joins every continuation; the rewrite joins only those bash joins (rtk-ai#5090). Co-Authored-By: Claude Opus 5.5 <[email protected]>
3 tasks done
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #5090
collapse_line_continuationsis now a small scanner with quote and comment state instead of a regex. It joins backslash-newline (with the blanks around it) into one space only outside single quotes,$'...'and comments. Inside double quotes it drops just the backslash and the newline and keeps the string's own whitespace, which is what bash does. Sogit commit -m 'one \⏎two' && lskeeps its message, andgit commit -m "a \⏎ b"becomes"a b"rather than"a b".ls # note \⏎ls -la) now passes the whole command through. Keeping the newline alone is not enough here: the lexer has no comment state, reads that backslash as escaping the newline, and puts the next line in the comment's segment. With the newline kept, bash would run that line while the hook allows a rewrite that no deny or ask rule was checked against (withgit push:*denied and*allowed,ls # note \⏎git push --forcegets an Allow verdict). Before this change the rewrite happened to defuse it, because the collapse turnedgit pushinto comment text, which also meant it silently changed what ran. Passing through hands the command to the agent's own permission handling.join_all_line_continuations, the old regex). The script ofbash -c 'git \⏎push'is parsed again by the inner shell, where that backslash-newline is a continuation, so a deny rule must still seegit push(test_line_continuation_is_joined_before_the_wrapper_check).Test plan
cargo fmt --all && cargo clippy --all-targets && cargo test --all --no-fail-fast. Clippy is clean.find_cmd::run_from_args_propagates_find_exit_status,stream::test_read_lines_lossy_preserves_lines_after_invalid_utf8), and they fail on unmodifieddeveloptoo.rewrite_shell_wrapper_testandgo_test_pipe_testeach had one failing test per run here. It was a different test on each run, and it happens ondeveloptoo, so these are Windows-specific and flaky.'...',$'...'and comments (LF and CRLF). Double quotes drop only\⏎. Escaped\',\#and\$'open nothing, and#inside a word is not a comment.git log \⏎ --onelinethat still joins.ls # note \⏎git push --forceunder an Allow verdict isDefer, notAllowRewrite.rtk rewriteon Windows (Git Bash for the inputs):git commit -m 'one \⏎two' && lsrtk git commit -m 'one \⏎two' && rtk lsls # note \⏎ls -lagit log \⏎ --oneline -n 3rtk git log --oneline -n 3git commit -m "a \⏎ b" && git statusrtk git commit -m "a b" && rtk git statusecho $'x \⏎y' && git statusecho $'x \⏎y' && rtk git statusI couldn't run the issue's bash script itself (it needs stub binaries on a POSIX
PATH), so the bash side of the table is from bash's quoting rules, not from a run.🤖 Generated with Claude Code