fix(commit-validate): handle heredoc inside -m command substitution#66
fix(commit-validate): handle heredoc inside -m command substitution#66mouchar wants to merge 1 commit into
Conversation
📝 WalkthroughWalkthroughThe commit validator's extractMessage now recognizes bash-style heredocs (with optional quoted delimiters), captures the full multiline heredoc body, trims a leading blank line, and returns it as { msg, form: 'heredoc' } instead of truncating to the first line. ChangesHeredoc commit message extraction
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (1)
scripts/commit-validate.js (1)
18-18: ⚡ Quick winAdd explanatory comment for the complex regex pattern.
The heredoc regex pattern uses capture groups, backreferences, and multiline matching, making it difficult to understand at a glance. Consider adding a brief comment explaining the pattern structure.
📝 Suggested addition
+ // Match heredoc: <<[-]'?DELIM'?\n...content...\nDELIM (capturing delimiter and body) const heredocAny = command.match(/<<-?\s*'?([A-Za-z_][A-Za-z0-9_]*)'?\s*\n([\s\S]*?)\n\s*\1\s*$/m);🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@scripts/commit-validate.js` at line 18, Add a short inline comment above the heredoc regex assignment that explains what the pattern in the variable heredocAny matches (e.g., it detects bash/heredoc syntax: optional indented form <<- or <<, optional quoting of the delimiter, captures the delimiter name in group 1 and the body in group 2, uses a backreference (\1) to find the closing delimiter, and relies on multiline/singleline flags), referencing the heredocAny variable and the regex literal so future readers understand the capture groups, backreference, and flags.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@scripts/commit-validate.js`:
- Around line 18-22: The heredoc regex in scripts/commit-validate.js (the const
heredocAny assignment) doesn't match double-quoted delimiters; update the
pattern to capture an optional quote char (single or double) and the delimiter
name, ensure the same quote is used to close (so opening quote is captured in
group 1, delimiter in group 2), and then extract the body from the correct
capture group (body will move to group 3 instead of group 2); keep the rest of
the logic (trimming leading blank line and returning { msg: ..., form: 'heredoc'
}) the same but reference the new capture groups when computing body.
---
Nitpick comments:
In `@scripts/commit-validate.js`:
- Line 18: Add a short inline comment above the heredoc regex assignment that
explains what the pattern in the variable heredocAny matches (e.g., it detects
bash/heredoc syntax: optional indented form <<- or <<, optional quoting of the
delimiter, captures the delimiter name in group 1 and the body in group 2, uses
a backreference (\1) to find the closing delimiter, and relies on
multiline/singleline flags), referencing the heredocAny variable and the regex
literal so future readers understand the capture groups, backreference, and
flags.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 1c39a40c-fc04-4f21-a009-ee9990c13435
📒 Files selected for processing (1)
scripts/commit-validate.js
The -m flag regex matched first and captured the literal $(cat <<DELIM ... DELIM) string as the message, failing conventional commit validation. Reordered detection to check heredoc patterns before -m/--message flags so the heredoc body is used as the message when present. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> Signed-off-by: Robert Moucha <robert.moucha@gooddata.com>
There was a problem hiding this comment.
🧹 Nitpick comments (1)
scripts/commit-validate.js (1)
23-23: 💤 Low valueOptionally trim all leading blank lines, not just one.
The current pattern
/^\s*\n/removes only the first leading blank line. If the heredoc body starts with multiple consecutive blank lines, only the first is removed. For example:
- Input:
"\n\nfeat: test"→ Output:"\nfeat: test"(one blank line remains)This is likely fine for typical commit messages, but for robustness you could trim all leading blank lines:
♻️ Proposed refinement
- const body = heredocAny[3].replace(/^\s*\n/, ''); + const body = heredocAny[3].replace(/^(\s*\n)+/, '');🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@scripts/commit-validate.js` at line 23, The assignment to const body uses heredocAny[3].replace(/^\s*\n/, '') which only strips a single leading blank line; update that replace call in scripts/commit-validate.js (the const body = heredocAny[3].replace(...) statement) to remove all leading blank lines by matching one-or-more leading newline blocks (e.g., use a regex that repeats the leading blank-line pattern such as /^(?:\s*\r?\n)+/ or similar) so any number of initial blank lines are trimmed.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@scripts/commit-validate.js`:
- Line 23: The assignment to const body uses heredocAny[3].replace(/^\s*\n/, '')
which only strips a single leading blank line; update that replace call in
scripts/commit-validate.js (the const body = heredocAny[3].replace(...)
statement) to remove all leading blank lines by matching one-or-more leading
newline blocks (e.g., use a regex that repeats the leading blank-line pattern
such as /^(?:\s*\r?\n)+/ or similar) so any number of initial blank lines are
trimmed.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: efa1c4a0-3c86-413a-ab79-dfce5a3a911c
📒 Files selected for processing (1)
scripts/commit-validate.js
The -m flag regex matched first and captured the literal $(cat <<DELIM ... DELIM) string as the message, failing conventional commit validation. Reordered detection to check heredoc patterns before -m/--message flags so the heredoc body is used as the message when present.
When claude code tried to run idiomatic git commit:
then it failed with error:
This PR allows claude code to use multi-line
$(cat <<DELIM ... DELIM)passed as heredoc commit message.Summary by CodeRabbit