diff --git a/scripts/hooks/pre-bash-commit-quality.js b/scripts/hooks/pre-bash-commit-quality.js index 278fd20c0..bfa1dd040 100644 --- a/scripts/hooks/pre-bash-commit-quality.js +++ b/scripts/hooks/pre-bash-commit-quality.js @@ -244,7 +244,10 @@ function readHeredocMessage(value, heredoc) { } }; } const after = /^[ \t\r\n]*\)((?:\\[\s\S]|[^"\\])*)"/.exec(value.slice(heredoc.end)); - if (!after || hasHeredocExpansion(after[1])) { + // In double quotes Bash drops a backslash-newline before it expands anything, so + // `$\HOME` is `$HOME`; other escapes stay for the expansion check to skip. + const continued = after && after[1].replace(/\\([\s\S])/g, (escape, next) => (next === '\n' ? '' : escape)); + if (!after || hasHeredocExpansion(continued)) { return { issue: { type: 'unverifiable', message: 'Cannot validate a commit message that adds commands or expansions to its here-document', diff --git a/tests/hooks/pre-bash-commit-quality.test.js b/tests/hooks/pre-bash-commit-quality.test.js index 30c78d6a6..060b2ae01 100644 --- a/tests/hooks/pre-bash-commit-quality.test.js +++ b/tests/hooks/pre-bash-commit-quality.test.js @@ -586,6 +586,15 @@ if (test('reads text after the substitution as part of the message', () => { hook.validateCommitMessage('git commit -m "$(cat <<\'EOF\'\nfix: a\nEOF\n)$HOME"').issues.map(i => i.type), ['unverifiable'] ); + // A line continuation inside it is dropped first: `$\HOME` is `$HOME`, `\$\HOME` is not. + assert.deepStrictEqual( + hook.validateCommitMessage('git commit -m "$(cat <<\'EOF\'\nfix: a\nEOF\n)$\\\nHOME"').issues.map(i => i.type), + ['unverifiable'] + ); + assert.strictEqual( + hook.validateCommitMessage('git commit -m "$(cat <<\'EOF\'\nfix: a\nEOF\n)\\$\\\nHOME"').message, + 'fix: a$HOME' + ); })) passed++; else failed++; if (process.platform !== 'win32') { @@ -601,6 +610,7 @@ if (process.platform !== 'win32') { ['"EOF"', 'fix: keep \\\\ and \\$HOME'], ["'EOF'", 'fix: update input', '.'], ["'EOF'", 'fix: update input\n\nbody line', ' and more'], + ["'EOF'", 'fix: a', '\\$\\\nHOME'], ]; inTempRepo(repoDir => { for (const [delimiter, body, suffix = ''] of forms) {