From 7695b357c7e74bba9aa1add4250252abeafa1cc5 Mon Sep 17 00:00:00 2001 From: L4XB Date: Fri, 23 Oct 2026 02:39:07 +0200 Subject: [PATCH 01/10] fix(hooks): check the subject line of multi-line and here-document commit messages The commit-message checks read the whole -m value. A multi-line message was measured against the 72-character subject limit as a whole, and a body ending in a period got the trailing-period warning. A message written as -m "$(cat <<'EOF' ... EOF)", the form Claude Code uses, was checked as the literal $(cat <<'EOF' text, so every such commit got the format and length warnings. The here-document body is now read as the message, and the format, length, capitalization and period checks apply to its subject line, the first line git keeps. --- scripts/hooks/pre-bash-commit-quality.js | 25 +++++++---- tests/hooks/pre-bash-commit-quality.test.js | 48 +++++++++++++++++++++ 2 files changed, 64 insertions(+), 9 deletions(-) diff --git a/scripts/hooks/pre-bash-commit-quality.js b/scripts/hooks/pre-bash-commit-quality.js index b4ec15368..917fe03b0 100644 --- a/scripts/hooks/pre-bash-commit-quality.js +++ b/scripts/hooks/pre-bash-commit-quality.js @@ -175,16 +175,23 @@ function validateCommitMessage(command) { // Extract commit message from command (quote-aware: when quoted, capture to the // matching closing quote, consuming escaped chars (\") so an embedded escaped // quote does not truncate the subject, and allowing the OTHER quote char inside - // the body; when unquoted, capture the full remaining tail, not just the first token) - const messageMatch = command.match(/(?:-m|--message)[=\s]+(?:"((?:\\.|[^"\\])*)"|'((?:\\.|[^'\\])*)'|([^"']+?)\s*$)/); + // the body; when unquoted, capture the full remaining tail, not just the first token). + // A message written as -m "$(cat <<'EOF' ... EOF)", as Claude Code writes them, is + // the body of that here-document. + const messageMatch = command.match(/(?:-m|--message)[=\s]+(?:"\$\(\s*cat\s+<<(?-?)\s*(?['"]?)(?[A-Za-z_][\w.-]*)\k[ \t]*\r?\n(?[\s\S]*?)\r?\n[ \t]*\k[ \t]*\r?(?:\n|$)|"(?(?:\\.|[^"\\])*)"|'(?(?:\\.|[^'\\])*)'|(?[^"']+?)\s*$)/); if (!messageMatch) return null; - const message = messageMatch[1] ?? messageMatch[2] ?? messageMatch[3]; + const { stripTabs, heredoc, double, single, bare } = messageMatch.groups; + // <<- strips the leading tabs of each line of the here-document + const message = stripTabs ? heredoc.replace(/^\t+/gm, '') : heredoc ?? double ?? single ?? bare; + // The format rules apply to the subject: git drops leading blank lines and trailing + // whitespace, and the first line left is the subject. + const subject = (message.split('\n').find(line => line.trim()) || '').trimEnd(); const issues = []; // Check conventional commit format const conventionalCommit = /^(feat|fix|docs|style|refactor|test|chore|build|ci|perf|revert)(\(.+\))?:\s*.+/; - if (!conventionalCommit.test(message)) { + if (!conventionalCommit.test(subject)) { issues.push({ type: 'format', message: 'Commit message does not follow conventional commit format', @@ -193,17 +200,17 @@ function validateCommitMessage(command) { } // Check message length - if (message.length > 72) { + if (subject.length > 72) { issues.push({ type: 'length', - message: `Commit message too long (${message.length} chars, max 72)`, + message: `Commit message too long (${subject.length} chars, max 72)`, suggestion: 'Keep the first line under 72 characters' }); } // Check for lowercase first letter (conventional) - if (conventionalCommit.test(message)) { - const afterColon = message.split(':')[1]; + if (conventionalCommit.test(subject)) { + const afterColon = subject.split(':')[1]; if (afterColon && /^[A-Z]/.test(afterColon.trim())) { issues.push({ type: 'capitalization', @@ -214,7 +221,7 @@ function validateCommitMessage(command) { } // Check for trailing period - if (message.endsWith('.')) { + if (subject.endsWith('.')) { issues.push({ type: 'punctuation', message: 'Commit message should not end with a period', diff --git a/tests/hooks/pre-bash-commit-quality.test.js b/tests/hooks/pre-bash-commit-quality.test.js index 85a76ee9f..65a10e649 100644 --- a/tests/hooks/pre-bash-commit-quality.test.js +++ b/tests/hooks/pre-bash-commit-quality.test.js @@ -504,6 +504,54 @@ if (test('measures length of the full message past an apostrophe (not the trunca assert.strictEqual(res.message, subject); assert.ok(res.issues.some(i => i.type === 'length'), 'full (>72) message should trigger a length issue'); })) passed++; else failed++; +// --- Subject line and here-document messages --- + +if (test('checks only the subject line of a multi-line message', () => { + const res = hook.validateCommitMessage( + 'git commit -m "fix: handle empty input\n\nThe parser crashed on an empty string. It now returns an empty list."' + ); + assert.deepStrictEqual(res.issues, []); +})) passed++; else failed++; + +if (test('reads the here-document message that Claude Code writes', () => { + const body = 'feat(hooks): add a check\n\nA body line that says why, and quotes "the case" it covers.'; + const res = hook.validateCommitMessage(`git commit -m "$(cat <<'EOF'\n${body}\nEOF\n)"`); + assert.strictEqual(res.message, body); + assert.deepStrictEqual(res.issues, []); +})) passed++; else failed++; + +if (test('still reports a bad subject inside a here-document', () => { + const res = hook.validateCommitMessage('git commit -m "$(cat < i.type), ['format', 'punctuation']); +})) passed++; else failed++; + +if (test('reads a <<- here-document without its leading tabs', () => { + const res = hook.validateCommitMessage('git commit -m "$(cat <<-"END"\n\tfix: tabbed subject\n\tEND\n)"'); + assert.strictEqual(res.message, 'fix: tabbed subject'); + assert.deepStrictEqual(res.issues, []); +})) passed++; else failed++; + +if (test('needs the description on the subject line itself', () => { + const res = hook.validateCommitMessage('git commit -m "feat:\nadd a check"'); + assert.ok(res.issues.some(i => i.type === 'format'), 'a subject with nothing after the colon is not conventional'); +})) passed++; else failed++; + +if (test('passes a valid here-document commit message end to end', () => { + inTempRepo(repoDir => { + writeAndStage(repoDir, 'index.js', 'const clean = true;\n'); + const input = JSON.stringify({ + tool_input: { + command: `git commit -m "$(cat <<'EOF'\nfix(hooks): read the message body\n\nA longer explanation that runs well past seventy-two characters in total.\nEOF\n)"` + } + }); + const { result, stderr } = captureConsoleError(() => hook.evaluate(input)); + + assert.strictEqual(result.exitCode, 0); + assert.ok(!stderr.includes('Commit Message Issues'), `expected no message warnings, got: ${stderr}`); + assert.ok(stderr.includes('All checks passed'), `expected a pass, got: ${stderr}`); + }); +})) passed++; else failed++; + if (test('handles hash-prefixed python comments and string false-positives', () => { inTempRepo(repoDir => { writeAndStage(repoDir, 'script.py', [ From 51fa8c3088ebd4e77cf90be5701609d19fa508b4 Mon Sep 17 00:00:00 2001 From: L4XB Date: Fri, 23 Oct 2026 03:04:13 +0200 Subject: [PATCH 02/10] fix(hooks): end a here-document commit message only at its exact delimiter line The closing pattern accepted spaces and tabs around the delimiter for both forms, so a body line such as ' EOF' ended the message early. Bash ends < { + const text = line.replace(/\r$/, ''); + return stripTabs ? text.replace(/^\t+/, '') : text; + }); + const end = lines.indexOf(delimiter); + return end === -1 ? null : lines.slice(0, end).join('\n'); +} + /** * Validate commit message format * @param {string} command @@ -175,15 +194,16 @@ function validateCommitMessage(command) { // Extract commit message from command (quote-aware: when quoted, capture to the // matching closing quote, consuming escaped chars (\") so an embedded escaped // quote does not truncate the subject, and allowing the OTHER quote char inside - // the body; when unquoted, capture the full remaining tail, not just the first token). - // A message written as -m "$(cat <<'EOF' ... EOF)", as Claude Code writes them, is - // the body of that here-document. - const messageMatch = command.match(/(?:-m|--message)[=\s]+(?:"\$\(\s*cat\s+<<(?-?)\s*(?['"]?)(?[A-Za-z_][\w.-]*)\k[ \t]*\r?\n(?[\s\S]*?)\r?\n[ \t]*\k[ \t]*\r?(?:\n|$)|"(?(?:\\.|[^"\\])*)"|'(?(?:\\.|[^'\\])*)'|(?[^"']+?)\s*$)/); + // the body; when unquoted, capture the full remaining tail, not just the first token) + const messageMatch = command.match(/(?:-m|--message)[=\s]+(?:"((?:\\.|[^"\\])*)"|'((?:\\.|[^'\\])*)'|([^"']+?)\s*$)/); if (!messageMatch) return null; - const { stripTabs, heredoc, double, single, bare } = messageMatch.groups; - // <<- strips the leading tabs of each line of the here-document - const message = stripTabs ? heredoc.replace(/^\t+/gm, '') : heredoc ?? double ?? single ?? bare; + // A message written as -m "$(cat <<'EOF' ... EOF)", as Claude Code writes them, is the + // body of that here-document. It is read from the command itself, since it may hold quotes. + const heredoc = messageMatch[1]?.startsWith('$(') + ? heredocBody(command.slice(messageMatch.index + messageMatch[0].indexOf('"') + 1)) + : null; + const message = heredoc ?? messageMatch[1] ?? messageMatch[2] ?? messageMatch[3]; // The format rules apply to the subject: git drops leading blank lines and trailing // whitespace, and the first line left is the subject. const subject = (message.split('\n').find(line => line.trim()) || '').trimEnd(); diff --git a/tests/hooks/pre-bash-commit-quality.test.js b/tests/hooks/pre-bash-commit-quality.test.js index 65a10e649..4987dbedf 100644 --- a/tests/hooks/pre-bash-commit-quality.test.js +++ b/tests/hooks/pre-bash-commit-quality.test.js @@ -525,6 +525,13 @@ if (test('still reports a bad subject inside a here-document', () => { assert.deepStrictEqual(res.issues.map(i => i.type), ['format', 'punctuation']); })) passed++; else failed++; +if (test('ends a here-document only at a line that holds just the delimiter', () => { + const res = hook.validateCommitMessage( + 'git commit -m "$(cat <<\'EOF\'\nfix: keep reading\n\n EOF\nEOF \nmore body\nEOF\n)"' + ); + assert.strictEqual(res.message, 'fix: keep reading\n\n EOF\nEOF \nmore body'); +})) passed++; else failed++; + if (test('reads a <<- here-document without its leading tabs', () => { const res = hook.validateCommitMessage('git commit -m "$(cat <<-"END"\n\tfix: tabbed subject\n\tEND\n)"'); assert.strictEqual(res.message, 'fix: tabbed subject'); From 1410071d3cc4dac8e6ee9be1b1e131310f64ce93 Mon Sep 17 00:00:00 2001 From: L4XB Date: Fri, 23 Oct 2026 03:20:18 +0200 Subject: [PATCH 03/10] fix(hooks): let a double-quoted commit message hold a backslash before a newline The quoted-message pattern took an escape as a backslash and one character other than a newline. A here-document body with a line ending in a backslash, such as a Windows path, therefore matched no -m at all, and the message was not checked. A backslash may now escape a newline too. --- scripts/hooks/pre-bash-commit-quality.js | 9 +++++---- tests/hooks/pre-bash-commit-quality.test.js | 7 +++++++ 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/scripts/hooks/pre-bash-commit-quality.js b/scripts/hooks/pre-bash-commit-quality.js index c60d8782f..763f6c05d 100644 --- a/scripts/hooks/pre-bash-commit-quality.js +++ b/scripts/hooks/pre-bash-commit-quality.js @@ -192,10 +192,11 @@ function heredocBody(value) { */ function validateCommitMessage(command) { // Extract commit message from command (quote-aware: when quoted, capture to the - // matching closing quote, consuming escaped chars (\") so an embedded escaped - // quote does not truncate the subject, and allowing the OTHER quote char inside - // the body; when unquoted, capture the full remaining tail, not just the first token) - const messageMatch = command.match(/(?:-m|--message)[=\s]+(?:"((?:\\.|[^"\\])*)"|'((?:\\.|[^'\\])*)'|([^"']+?)\s*$)/); + // matching closing quote, consuming escaped chars (\", and a backslash before a + // newline) so an embedded escaped quote does not truncate the subject, and allowing + // the OTHER quote char inside the body; when unquoted, capture the full remaining + // tail, not just the first token) + const messageMatch = command.match(/(?:-m|--message)[=\s]+(?:"((?:\\[\s\S]|[^"\\])*)"|'((?:\\.|[^'\\])*)'|([^"']+?)\s*$)/); if (!messageMatch) return null; // A message written as -m "$(cat <<'EOF' ... EOF)", as Claude Code writes them, is the diff --git a/tests/hooks/pre-bash-commit-quality.test.js b/tests/hooks/pre-bash-commit-quality.test.js index 4987dbedf..8c437b00b 100644 --- a/tests/hooks/pre-bash-commit-quality.test.js +++ b/tests/hooks/pre-bash-commit-quality.test.js @@ -525,6 +525,13 @@ if (test('still reports a bad subject inside a here-document', () => { assert.deepStrictEqual(res.issues.map(i => i.type), ['format', 'punctuation']); })) passed++; else failed++; +if (test('reads a here-document whose body has a backslash at the end of a line', () => { + const res = hook.validateCommitMessage('git commit -m "$(cat <<\'EOF\'\nUpdate stuff.\n\nC:\\build\\\nEOF\n)"'); + assert.ok(res, 'expected a validation result'); + assert.strictEqual(res.message, 'Update stuff.\n\nC:\\build\\'); + assert.deepStrictEqual(res.issues.map(i => i.type), ['format', 'punctuation']); +})) passed++; else failed++; + if (test('ends a here-document only at a line that holds just the delimiter', () => { const res = hook.validateCommitMessage( 'git commit -m "$(cat <<\'EOF\'\nfix: keep reading\n\n EOF\nEOF \nmore body\nEOF\n)"' From c6c346056b8e563e09d1626ff17ae7a032574edf Mon Sep 17 00:00:00 2001 From: L4XB Date: Fri, 23 Oct 2026 03:43:02 +0200 Subject: [PATCH 04/10] fix(hooks): drop line continuations from a double-quoted commit message Bash removes a backslash-newline inside double quotes, so git never sees it, but the check kept both characters. A message that starts with one had a subject of just the backslash and got a false format warning. Line continuations are now dropped from a double-quoted message before its subject is read. An escaped backslash before a newline stays, and a here-document body is read as written. --- scripts/hooks/pre-bash-commit-quality.js | 4 +++- tests/hooks/pre-bash-commit-quality.test.js | 8 ++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/scripts/hooks/pre-bash-commit-quality.js b/scripts/hooks/pre-bash-commit-quality.js index 763f6c05d..90c281609 100644 --- a/scripts/hooks/pre-bash-commit-quality.js +++ b/scripts/hooks/pre-bash-commit-quality.js @@ -204,7 +204,9 @@ function validateCommitMessage(command) { const heredoc = messageMatch[1]?.startsWith('$(') ? heredocBody(command.slice(messageMatch.index + messageMatch[0].indexOf('"') + 1)) : null; - const message = heredoc ?? messageMatch[1] ?? messageMatch[2] ?? messageMatch[3]; + // In double quotes, Bash drops a backslash-newline, a line continuation + const doubleQuoted = messageMatch[1]?.replace(/\\(\r?\n|[\s\S])/g, (escape, next) => (next.endsWith('\n') ? '' : escape)); + const message = heredoc ?? doubleQuoted ?? messageMatch[2] ?? messageMatch[3]; // The format rules apply to the subject: git drops leading blank lines and trailing // whitespace, and the first line left is the subject. const subject = (message.split('\n').find(line => line.trim()) || '').trimEnd(); diff --git a/tests/hooks/pre-bash-commit-quality.test.js b/tests/hooks/pre-bash-commit-quality.test.js index 8c437b00b..695c19a0c 100644 --- a/tests/hooks/pre-bash-commit-quality.test.js +++ b/tests/hooks/pre-bash-commit-quality.test.js @@ -525,6 +525,14 @@ if (test('still reports a bad subject inside a here-document', () => { assert.deepStrictEqual(res.issues.map(i => i.type), ['format', 'punctuation']); })) passed++; else failed++; +if (test('drops a line continuation from a double-quoted message, as Bash does', () => { + const res = hook.validateCommitMessage('git commit -m "\\\nfix: handle empty input"'); + assert.strictEqual(res.message, 'fix: handle empty input'); + assert.deepStrictEqual(res.issues, []); + // An escaped backslash before a newline is a backslash, and the newline stays + assert.strictEqual(hook.validateCommitMessage('git commit -m "fix: path C:\\\\\nbody"').message, 'fix: path C:\\\\\nbody'); +})) passed++; else failed++; + if (test('reads a here-document whose body has a backslash at the end of a line', () => { const res = hook.validateCommitMessage('git commit -m "$(cat <<\'EOF\'\nUpdate stuff.\n\nC:\\build\\\nEOF\n)"'); assert.ok(res, 'expected a validation result'); From 71cf7045e8d3d3a2eda0894784a61044ca3166ed Mon Sep 17 00:00:00 2001 From: L4XB Date: Fri, 23 Oct 2026 03:59:10 +0200 Subject: [PATCH 05/10] fix(hooks): decode the escapes that Bash removes in a double-quoted commit message In double quotes, Bash drops the backslash before $, `, ", \ and a newline, so git receives fix: say "hello" for -m "fix: say \"hello\"". The check kept the backslashes, so an escaped backslash before a newline counted twice. These escapes are now decoded, and a backslash before any other character stays. The test that pinned the escaped quotes now expects the message git records. --- scripts/hooks/pre-bash-commit-quality.js | 5 +++-- tests/hooks/pre-bash-commit-quality.test.js | 8 +++++--- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/scripts/hooks/pre-bash-commit-quality.js b/scripts/hooks/pre-bash-commit-quality.js index 90c281609..ac06203d1 100644 --- a/scripts/hooks/pre-bash-commit-quality.js +++ b/scripts/hooks/pre-bash-commit-quality.js @@ -204,8 +204,9 @@ function validateCommitMessage(command) { const heredoc = messageMatch[1]?.startsWith('$(') ? heredocBody(command.slice(messageMatch.index + messageMatch[0].indexOf('"') + 1)) : null; - // In double quotes, Bash drops a backslash-newline, a line continuation - const doubleQuoted = messageMatch[1]?.replace(/\\(\r?\n|[\s\S])/g, (escape, next) => (next.endsWith('\n') ? '' : escape)); + // In double quotes, Bash drops the backslash before $ ` " \ and drops a backslash-newline, + // a line continuation, altogether. Before any other character the backslash stays. + const doubleQuoted = messageMatch[1]?.replace(/\\(\r?\n|[$`"\\])/g, (escape, next) => (next.endsWith('\n') ? '' : next)); const message = heredoc ?? doubleQuoted ?? messageMatch[2] ?? messageMatch[3]; // The format rules apply to the subject: git drops leading blank lines and trailing // whitespace, and the first line left is the subject. diff --git a/tests/hooks/pre-bash-commit-quality.test.js b/tests/hooks/pre-bash-commit-quality.test.js index 695c19a0c..c5653e57d 100644 --- a/tests/hooks/pre-bash-commit-quality.test.js +++ b/tests/hooks/pre-bash-commit-quality.test.js @@ -495,7 +495,7 @@ if (test('captures full single-quoted -m message containing a double quote', () if (test('captures full double-quoted -m message with escaped inner quotes (not truncated)', () => { const res = hook.validateCommitMessage('git commit -m "fix: say \\"hello\\" to the user"'); assert.ok(res, 'expected a validation result'); - assert.strictEqual(res.message, 'fix: say \\"hello\\" to the user'); + assert.strictEqual(res.message, 'fix: say "hello" to the user'); })) passed++; else failed++; if (test('measures length of the full message past an apostrophe (not the truncated prefix)', () => { @@ -529,8 +529,10 @@ if (test('drops a line continuation from a double-quoted message, as Bash does', const res = hook.validateCommitMessage('git commit -m "\\\nfix: handle empty input"'); assert.strictEqual(res.message, 'fix: handle empty input'); assert.deepStrictEqual(res.issues, []); - // An escaped backslash before a newline is a backslash, and the newline stays - assert.strictEqual(hook.validateCommitMessage('git commit -m "fix: path C:\\\\\nbody"').message, 'fix: path C:\\\\\nbody'); + // An escaped backslash before a newline is one backslash, and the newline stays + assert.strictEqual(hook.validateCommitMessage('git commit -m "fix: path C:\\\\\nbody"').message, 'fix: path C:\\\nbody'); + // Bash removes the backslash only before $ ` " \ and a newline + assert.strictEqual(hook.validateCommitMessage('git commit -m "fix: cost \\$5 in \\d+ steps"').message, 'fix: cost $5 in \\d+ steps'); })) passed++; else failed++; if (test('reads a here-document whose body has a backslash at the end of a line', () => { From 69cdff28e0ff91e2a340ecb300145462182eab9b Mon Sep 17 00:00:00 2001 From: L4XB Date: Fri, 23 Oct 2026 04:13:06 +0200 Subject: [PATCH 06/10] fix(hooks): treat only backslash-LF as a line continuation in a commit message Bash removes a backslash before a newline, but not one before a carriage return. Removing backslash-CRLF joined two lines of the message, and their text was then read as the subject. --- scripts/hooks/pre-bash-commit-quality.js | 2 +- tests/hooks/pre-bash-commit-quality.test.js | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/scripts/hooks/pre-bash-commit-quality.js b/scripts/hooks/pre-bash-commit-quality.js index ac06203d1..7869a6993 100644 --- a/scripts/hooks/pre-bash-commit-quality.js +++ b/scripts/hooks/pre-bash-commit-quality.js @@ -206,7 +206,7 @@ function validateCommitMessage(command) { : null; // In double quotes, Bash drops the backslash before $ ` " \ and drops a backslash-newline, // a line continuation, altogether. Before any other character the backslash stays. - const doubleQuoted = messageMatch[1]?.replace(/\\(\r?\n|[$`"\\])/g, (escape, next) => (next.endsWith('\n') ? '' : next)); + const doubleQuoted = messageMatch[1]?.replace(/\\([\n$`"\\])/g, (escape, next) => (next === '\n' ? '' : next)); const message = heredoc ?? doubleQuoted ?? messageMatch[2] ?? messageMatch[3]; // The format rules apply to the subject: git drops leading blank lines and trailing // whitespace, and the first line left is the subject. diff --git a/tests/hooks/pre-bash-commit-quality.test.js b/tests/hooks/pre-bash-commit-quality.test.js index c5653e57d..faa14c932 100644 --- a/tests/hooks/pre-bash-commit-quality.test.js +++ b/tests/hooks/pre-bash-commit-quality.test.js @@ -531,7 +531,9 @@ if (test('drops a line continuation from a double-quoted message, as Bash does', assert.deepStrictEqual(res.issues, []); // An escaped backslash before a newline is one backslash, and the newline stays assert.strictEqual(hook.validateCommitMessage('git commit -m "fix: path C:\\\\\nbody"').message, 'fix: path C:\\\nbody'); - // Bash removes the backslash only before $ ` " \ and a newline + // Bash removes the backslash only before $ ` " \ and a newline; before a CR it stays + assert.strictEqual(hook.validateCommitMessage('git commit -m "fix: keep path\\\r\nbody."').message, 'fix: keep path\\\r\nbody.'); + assert.deepStrictEqual(hook.validateCommitMessage('git commit -m "fix: keep path\\\r\nbody."').issues, []); assert.strictEqual(hook.validateCommitMessage('git commit -m "fix: cost \\$5 in \\d+ steps"').message, 'fix: cost $5 in \\d+ steps'); })) passed++; else failed++; From 950f8ce0f32aa3cf88aced8aa71c56ea54d12bf1 Mon Sep 17 00:00:00 2001 From: haelyra <49814733+haelyra@users.noreply.github.com> Date: Thu, 5 Nov 2026 10:43:53 +0100 Subject: [PATCH 07/10] fix(hooks): validate Bash commit here-document substitutions The commit-quality half of 7632669d from #3735: a single-quoted -m message keeps its backslashes, and an unquoted here-document whose body Bash would expand yields an advisory that the subject cannot be validated from the literal command. (split from commit 7632669d287118f412298d0c2b600723d114e084) --- scripts/hooks/pre-bash-commit-quality.js | 35 +++++++++++++++++---- tests/hooks/pre-bash-commit-quality.test.js | 22 +++++++++++++ 2 files changed, 51 insertions(+), 6 deletions(-) diff --git a/scripts/hooks/pre-bash-commit-quality.js b/scripts/hooks/pre-bash-commit-quality.js index 7869a6993..bfd705f42 100644 --- a/scripts/hooks/pre-bash-commit-quality.js +++ b/scripts/hooks/pre-bash-commit-quality.js @@ -167,22 +167,35 @@ function findFileIssues(filePath) { } /** - * The body of the here-document that `value` starts with, as in `$(cat <<'EOF'`, or null. + * The body and expansion mode of the here-document that `value` starts with, + * as in `$(cat <<'EOF'`, or null. * It ends at the line that holds only the delimiter, after leading tabs for <<-, which also * strips them from the body. * @param {string} value - * @returns {string|null} + * @returns {{body: string, expands: boolean}|null} */ function heredocBody(value) { const opener = /^\$\(\s*cat\s+<<(-?)\s*(['"]?)([A-Za-z_][\w.-]*)\2[ \t]*\r?\n/.exec(value); if (!opener) return null; - const [, stripTabs, , delimiter] = opener; + const [, stripTabs, quote, delimiter] = opener; const lines = value.slice(opener[0].length).split('\n').map(line => { const text = line.replace(/\r$/, ''); return stripTabs ? text.replace(/^\t+/, '') : text; }); const end = lines.indexOf(delimiter); - return end === -1 ? null : lines.slice(0, end).join('\n'); + return end === -1 ? null : { body: lines.slice(0, end).join('\n'), expands: quote === '' }; +} + +// Bash expands parameters, commands and arithmetic in an unquoted here-document. +// Only backslashes before $, `, \\ and newline suppress the following character. +function hasHeredocExpansion(body) { + for (let i = 0; i < body.length; i++) { + const ch = body[i]; + const next = body[i + 1] || ''; + if (ch === '\\' && next && '$`\\\n'.includes(next)) { i++; continue; } + if (ch === '`' || (ch === '$' && next && (/[A-Za-z0-9_]/.test(next) || '$@*#?!-({['.includes(next)))) return true; + } + return false; } /** @@ -196,7 +209,7 @@ function validateCommitMessage(command) { // newline) so an embedded escaped quote does not truncate the subject, and allowing // the OTHER quote char inside the body; when unquoted, capture the full remaining // tail, not just the first token) - const messageMatch = command.match(/(?:-m|--message)[=\s]+(?:"((?:\\[\s\S]|[^"\\])*)"|'((?:\\.|[^'\\])*)'|([^"']+?)\s*$)/); + const messageMatch = command.match(/(?:-m|--message)[=\s]+(?:"((?:\\[\s\S]|[^"\\])*)"|'([^']*)'|([^"']+?)\s*$)/); if (!messageMatch) return null; // A message written as -m "$(cat <<'EOF' ... EOF)", as Claude Code writes them, is the @@ -204,10 +217,20 @@ function validateCommitMessage(command) { const heredoc = messageMatch[1]?.startsWith('$(') ? heredocBody(command.slice(messageMatch.index + messageMatch[0].indexOf('"') + 1)) : null; + if (heredoc?.expands && hasHeredocExpansion(heredoc.body)) { + return { + message: null, + issues: [{ + type: 'unverifiable', + message: 'Cannot validate a commit message with unquoted here-document expansions', + suggestion: 'Quote the here-document delimiter to keep the message literal' + }] + }; + } // In double quotes, Bash drops the backslash before $ ` " \ and drops a backslash-newline, // a line continuation, altogether. Before any other character the backslash stays. const doubleQuoted = messageMatch[1]?.replace(/\\([\n$`"\\])/g, (escape, next) => (next === '\n' ? '' : next)); - const message = heredoc ?? doubleQuoted ?? messageMatch[2] ?? messageMatch[3]; + const message = heredoc?.body ?? doubleQuoted ?? messageMatch[2] ?? messageMatch[3]; // The format rules apply to the subject: git drops leading blank lines and trailing // whitespace, and the first line left is the subject. const subject = (message.split('\n').find(line => line.trim()) || '').trimEnd(); diff --git a/tests/hooks/pre-bash-commit-quality.test.js b/tests/hooks/pre-bash-commit-quality.test.js index faa14c932..68f5bfced 100644 --- a/tests/hooks/pre-bash-commit-quality.test.js +++ b/tests/hooks/pre-bash-commit-quality.test.js @@ -525,6 +525,28 @@ if (test('still reports a bad subject inside a here-document', () => { assert.deepStrictEqual(res.issues.map(i => i.type), ['format', 'punctuation']); })) passed++; else failed++; +if (test('warns when an unquoted here-document can expand the commit message', () => { + const command = 'git commit -m "$(cat < i.type), ['unverifiable']); + assert.deepStrictEqual( + hook.validateCommitMessage('git commit -m "$(cat <<\'EOF\'\nfix: ${EMPTY}\nEOF\n)"').issues, + [] + ); + assert.deepStrictEqual( + hook.validateCommitMessage('git commit -m "$(cat < { + const res = hook.validateCommitMessage(String.raw`git commit -m 'Update path\'`); + assert.ok(res, 'expected a validation result for valid Bash quoting'); + assert.strictEqual(res.message, 'Update path' + String.fromCharCode(92)); + assert.ok(res.issues.some(issue => issue.type === 'format')); +})) passed++; else failed++; + if (test('drops a line continuation from a double-quoted message, as Bash does', () => { const res = hook.validateCommitMessage('git commit -m "\\\nfix: handle empty input"'); assert.strictEqual(res.message, 'fix: handle empty input'); From 8c67067582f035754cf4f9663a0642acc3efae35 Mon Sep 17 00:00:00 2001 From: L4XB Date: Thu, 5 Nov 2026 10:49:40 +0100 Subject: [PATCH 08/10] fix(hooks): join continued lines of a commit here-document as bash does In an unquoted here-document, Bash removes a backslash-newline before Git receives the message, so `fix: \` followed by `Update input.` commits `fix: Update input.`, and a delimiter line right after a continued line does not end the body. Read the body that way, keep `<<-` from stripping the tabs of a continued line, and drop the backslash before $, ` and \. A quoted body stays literal, as Bash 4 and later and zsh read it. Inside "$(...)", Bash 3.2 joins its lines too, so the hook now notes when that would change the subject. --- scripts/hooks/pre-bash-commit-quality.js | 58 ++++++++++++++++----- tests/hooks/pre-bash-commit-quality.test.js | 55 +++++++++++++++++++ 2 files changed, 101 insertions(+), 12 deletions(-) diff --git a/scripts/hooks/pre-bash-commit-quality.js b/scripts/hooks/pre-bash-commit-quality.js index bfd705f42..25903e6a5 100644 --- a/scripts/hooks/pre-bash-commit-quality.js +++ b/scripts/hooks/pre-bash-commit-quality.js @@ -170,20 +170,45 @@ function findFileIssues(filePath) { * The body and expansion mode of the here-document that `value` starts with, * as in `$(cat <<'EOF'`, or null. * It ends at the line that holds only the delimiter, after leading tabs for <<-, which also - * strips them from the body. + * strips them from the body. In an unquoted here-document, a line that ends in an unescaped + * backslash is joined with the next one before the delimiter is looked for, so a delimiter + * line right after it does not end the body. A quoted body stays literal, as Bash 4 and later + * and zsh read it; `bash32Body` is how Bash 3.2 reads it inside "$(...)", with those lines + * joined too. * @param {string} value - * @returns {{body: string, expands: boolean}|null} + * @returns {{body: string, expands: boolean, bash32Body: string|null}|null} */ function heredocBody(value) { const opener = /^\$\(\s*cat\s+<<(-?)\s*(['"]?)([A-Za-z_][\w.-]*)\2[ \t]*\r?\n/.exec(value); if (!opener) return null; const [, stripTabs, quote, delimiter] = opener; - const lines = value.slice(opener[0].length).split('\n').map(line => { - const text = line.replace(/\r$/, ''); - return stripTabs ? text.replace(/^\t+/, '') : text; - }); - const end = lines.indexOf(delimiter); - return end === -1 ? null : { body: lines.slice(0, end).join('\n'), expands: quote === '' }; + const lines = value.slice(opener[0].length).split('\n').map(line => line.replace(/\r$/, '')); + const read = joinLines => { + const body = []; + let joined = ''; + for (const line of lines) { + // <<- strips leading tabs while the line read so far is empty, so not from a line + // that a backslash continues after some text + const text = joined + (stripTabs && !joined ? line.replace(/^\t+/, '') : line); + // An odd number of trailing backslashes escapes the newline + if (joinLines && /(?:^|[^\\])(?:\\\\)*\\$/.test(text)) { + joined = text.slice(0, -1); + continue; + } + joined = ''; + if (text === delimiter) return body.join('\n'); + body.push(text); + } + return null; + }; + const expands = quote === ''; + const body = read(expands); + return body === null ? null : { body, expands, bash32Body: expands ? null : read(true) }; +} + +// Git drops leading blank lines and trailing whitespace, and the first line left is the subject. +function subjectOf(message) { + return (message.split('\n').find(line => line.trim()) || '').trimEnd(); } // Bash expands parameters, commands and arithmetic in an unquoted here-document. @@ -230,11 +255,20 @@ function validateCommitMessage(command) { // In double quotes, Bash drops the backslash before $ ` " \ and drops a backslash-newline, // a line continuation, altogether. Before any other character the backslash stays. const doubleQuoted = messageMatch[1]?.replace(/\\([\n$`"\\])/g, (escape, next) => (next === '\n' ? '' : next)); - const message = heredoc?.body ?? doubleQuoted ?? messageMatch[2] ?? messageMatch[3]; - // The format rules apply to the subject: git drops leading blank lines and trailing - // whitespace, and the first line left is the subject. - const subject = (message.split('\n').find(line => line.trim()) || '').trimEnd(); + // An unquoted here-document also loses the backslash before $, ` and \. + const heredocMessage = heredoc && (heredoc.expands ? heredoc.body.replace(/\\([$`\\])/g, '$1') : heredoc.body); + const message = heredocMessage ?? doubleQuoted ?? messageMatch[2] ?? messageMatch[3]; + // The format rules apply to the subject. + const subject = subjectOf(message); const issues = []; + + if (heredoc?.bash32Body != null && subjectOf(heredoc.bash32Body) !== subject) { + issues.push({ + type: 'shell-dependent', + message: 'Bash 3.2 joins a subject line that ends in a backslash with the next line, even with a quoted delimiter', + suggestion: 'Do not end the subject line with a backslash' + }); + } // Check conventional commit format const conventionalCommit = /^(feat|fix|docs|style|refactor|test|chore|build|ci|perf|revert)(\(.+\))?:\s*.+/; diff --git a/tests/hooks/pre-bash-commit-quality.test.js b/tests/hooks/pre-bash-commit-quality.test.js index 68f5bfced..c82b78969 100644 --- a/tests/hooks/pre-bash-commit-quality.test.js +++ b/tests/hooks/pre-bash-commit-quality.test.js @@ -547,6 +547,61 @@ if (test('treats a backslash as literal inside a single-quoted commit message', assert.ok(res.issues.some(issue => issue.type === 'format')); })) passed++; else failed++; +if (test('joins a continued line of an unquoted here-document, as Bash does', () => { + const res = hook.validateCommitMessage('git commit -m "$(cat < i.type), ['capitalization', 'punctuation']); + // A delimiter line right after a continued line belongs to the body + assert.strictEqual( + hook.validateCommitMessage('git commit -m "$(cat < { + const res = hook.validateCommitMessage('git commit -m "$(cat <<\'EOF\'\nfix: \\\nUpdate input.\nEOF\n)"'); + assert.strictEqual(res.message, 'fix: \\\nUpdate input.'); + assert.deepStrictEqual(res.issues.map(i => i.type), ['shell-dependent']); + assert.deepStrictEqual( + hook.validateCommitMessage('git commit -m "$(cat <<\'EOF\'\nfix: add a check\n\nA body line \\\nEOF\n)"').issues, + [] + ); +})) passed++; else failed++; + +if (process.platform !== 'win32') { + if (test('reads the message that Bash and Git record for a here-document', () => { + const bashMajor = Number(spawnSync('/bin/bash', ['-c', 'echo "${BASH_VERSINFO[0]}"'], { encoding: 'utf8' }).stdout); + const forms = [ + ['EOF', 'fix: \\\nUpdate input.'], + ['EOF', 'fix: a \\\nEOF\nb'], + ['EOF', 'fix: a \\\\\nb \\` c'], + ['-EOF', '\tfix: \\\n\tUpdate input.'], + ['-EOF', '\tfix: a\n\t\\\n\tb'], + ["'EOF'", 'fix: \\\nUpdate input.'], + ['"EOF"', 'fix: keep \\\\ and \\$HOME'], + ]; + inTempRepo(repoDir => { + for (const [delimiter, body] of forms) { + const command = `git commit -q --allow-empty -m "$(cat <<${delimiter}\n${body}\nEOF\n)"`; + const run = spawnSync('/bin/bash', ['-c', command], { cwd: repoDir, encoding: 'utf8' }); + assert.strictEqual(run.status, 0, run.stderr); + const recorded = spawnSync('git', ['log', '-1', '--format=%B'], { cwd: repoDir, encoding: 'utf8' }).stdout.trimEnd(); + const res = hook.validateCommitMessage(command); + if (bashMajor < 4 && res.issues.some(i => i.type === 'shell-dependent')) { + assert.notStrictEqual(recorded, res.message, `Bash 3.2 joins the quoted body of ${JSON.stringify(command)}`); + } else { + assert.strictEqual(res.message, recorded, `message of ${JSON.stringify(command)}`); + } + } + }); + })) passed++; else failed++; +} + if (test('drops a line continuation from a double-quoted message, as Bash does', () => { const res = hook.validateCommitMessage('git commit -m "\\\nfix: handle empty input"'); assert.strictEqual(res.message, 'fix: handle empty input'); From 2ff19fafa0166e3dcd3ec7ff2adad3c6a88dd662 Mon Sep 17 00:00:00 2001 From: L4XB Date: Thu, 5 Nov 2026 13:22:57 +0100 Subject: [PATCH 09/10] fix(hooks): read text after a commit here-document's substitution MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit In `-m "$(cat <<'EOF' … EOF\n)."` Bash drops the body's trailing newlines and adds the period, so Git records `fix: update input.`, but the hook checked only the here-document and missed the trailing-period warning. Read the rest of the quoted value after the substitution's `)` and add it to the message as double-quoted text. Commands or an expansion after the here-document make the message unverifiable, as an expansion in an unquoted body already does. The here-document reading moves into readHeredocMessage, which also returns the Bash 3.2 note. --- scripts/hooks/pre-bash-commit-quality.js | 81 ++++++++++++++------- tests/hooks/pre-bash-commit-quality.test.js | 21 +++++- 2 files changed, 73 insertions(+), 29 deletions(-) diff --git a/scripts/hooks/pre-bash-commit-quality.js b/scripts/hooks/pre-bash-commit-quality.js index 25903e6a5..278fd20c0 100644 --- a/scripts/hooks/pre-bash-commit-quality.js +++ b/scripts/hooks/pre-bash-commit-quality.js @@ -182,11 +182,16 @@ function heredocBody(value) { const opener = /^\$\(\s*cat\s+<<(-?)\s*(['"]?)([A-Za-z_][\w.-]*)\2[ \t]*\r?\n/.exec(value); if (!opener) return null; const [, stripTabs, quote, delimiter] = opener; - const lines = value.slice(opener[0].length).split('\n').map(line => line.replace(/\r$/, '')); + const raw = value.slice(opener[0].length).split('\n'); + const lines = raw.map(line => line.replace(/\r$/, '')); + // Where the delimiter line ends in `value`, for the text after the substitution. + let end = -1; const read = joinLines => { const body = []; let joined = ''; - for (const line of lines) { + let offset = opener[0].length; + for (const [index, line] of lines.entries()) { + offset += raw[index].length + 1; // <<- strips leading tabs while the line read so far is empty, so not from a line // that a backslash continues after some text const text = joined + (stripTabs && !joined ? line.replace(/^\t+/, '') : line); @@ -196,14 +201,17 @@ function heredocBody(value) { continue; } joined = ''; - if (text === delimiter) return body.join('\n'); + if (text === delimiter) { + if (end === -1) end = Math.min(offset, value.length); + return body.join('\n'); + } body.push(text); } return null; }; const expands = quote === ''; const body = read(expands); - return body === null ? null : { body, expands, bash32Body: expands ? null : read(true) }; + return body === null ? null : { body, expands, bash32Body: expands ? null : read(true), end }; } // Git drops leading blank lines and trailing whitespace, and the first line left is the subject. @@ -223,6 +231,42 @@ function hasHeredocExpansion(body) { return false; } +// The message a `-m "$(cat <<…)"` value gives Git, or an `issue` when the literal command +// does not show it: an expansion in an unquoted body, or commands or expansions after the +// here-document. Text after the substitution's `)` is part of the message: `$(…)."` adds a +// period to the body, whose trailing newlines the substitution drops first. +function readHeredocMessage(value, heredoc) { + if (heredoc.expands && hasHeredocExpansion(heredoc.body)) { + return { issue: { + type: 'unverifiable', + message: 'Cannot validate a commit message with unquoted here-document expansions', + suggestion: 'Quote the here-document delimiter to keep the message literal' + } }; + } + const after = /^[ \t\r\n]*\)((?:\\[\s\S]|[^"\\])*)"/.exec(value.slice(heredoc.end)); + if (!after || hasHeredocExpansion(after[1])) { + return { issue: { + type: 'unverifiable', + message: 'Cannot validate a commit message that adds commands or expansions to its here-document', + suggestion: 'Write the whole message inside the here-document' + } }; + } + const tail = after[1].replace(/\\([\n$`"\\])/g, (escape, next) => (next === '\n' ? '' : next)); + const withTail = body => (tail ? body.replace(/\n+$/, '') + tail : body); + // An unquoted here-document also loses the backslash before $, ` and \. + const body = heredoc.expands ? heredoc.body.replace(/\\([$`\\])/g, '$1') : heredoc.body; + const message = withTail(body); + const bash32Message = heredoc.bash32Body === null ? null : withTail(heredoc.bash32Body); + const note = bash32Message !== null && subjectOf(bash32Message) !== subjectOf(message) + ? { + type: 'shell-dependent', + message: 'Bash 3.2 joins a subject line that ends in a backslash with the next line, even with a quoted delimiter', + suggestion: 'Do not end the subject line with a backslash' + } + : null; + return { message, note }; +} + /** * Validate commit message format * @param {string} command @@ -239,36 +283,19 @@ function validateCommitMessage(command) { // A message written as -m "$(cat <<'EOF' ... EOF)", as Claude Code writes them, is the // body of that here-document. It is read from the command itself, since it may hold quotes. - const heredoc = messageMatch[1]?.startsWith('$(') - ? heredocBody(command.slice(messageMatch.index + messageMatch[0].indexOf('"') + 1)) - : null; - if (heredoc?.expands && hasHeredocExpansion(heredoc.body)) { - return { - message: null, - issues: [{ - type: 'unverifiable', - message: 'Cannot validate a commit message with unquoted here-document expansions', - suggestion: 'Quote the here-document delimiter to keep the message literal' - }] - }; - } + const value = command.slice(messageMatch.index + messageMatch[0].indexOf('"') + 1); + const heredoc = messageMatch[1]?.startsWith('$(') ? heredocBody(value) : null; + const fromHeredoc = heredoc && readHeredocMessage(value, heredoc); + if (fromHeredoc?.issue) return { message: null, issues: [fromHeredoc.issue] }; // In double quotes, Bash drops the backslash before $ ` " \ and drops a backslash-newline, // a line continuation, altogether. Before any other character the backslash stays. const doubleQuoted = messageMatch[1]?.replace(/\\([\n$`"\\])/g, (escape, next) => (next === '\n' ? '' : next)); - // An unquoted here-document also loses the backslash before $, ` and \. - const heredocMessage = heredoc && (heredoc.expands ? heredoc.body.replace(/\\([$`\\])/g, '$1') : heredoc.body); - const message = heredocMessage ?? doubleQuoted ?? messageMatch[2] ?? messageMatch[3]; + const message = fromHeredoc?.message ?? doubleQuoted ?? messageMatch[2] ?? messageMatch[3]; // The format rules apply to the subject. const subject = subjectOf(message); const issues = []; - if (heredoc?.bash32Body != null && subjectOf(heredoc.bash32Body) !== subject) { - issues.push({ - type: 'shell-dependent', - message: 'Bash 3.2 joins a subject line that ends in a backslash with the next line, even with a quoted delimiter', - suggestion: 'Do not end the subject line with a backslash' - }); - } + if (fromHeredoc?.note) issues.push(fromHeredoc.note); // Check conventional commit format const conventionalCommit = /^(feat|fix|docs|style|refactor|test|chore|build|ci|perf|revert)(\(.+\))?:\s*.+/; diff --git a/tests/hooks/pre-bash-commit-quality.test.js b/tests/hooks/pre-bash-commit-quality.test.js index c82b78969..30c78d6a6 100644 --- a/tests/hooks/pre-bash-commit-quality.test.js +++ b/tests/hooks/pre-bash-commit-quality.test.js @@ -573,6 +573,21 @@ if (test('keeps a quoted here-document literal and notes that Bash 3.2 joins it' ); })) passed++; else failed++; +if (test('reads text after the substitution as part of the message', () => { + const res = hook.validateCommitMessage('git commit -m "$(cat <<\'EOF\'\nfix: update input\nEOF\n)."'); + assert.strictEqual(res.message, 'fix: update input.'); + assert.deepStrictEqual(res.issues.map(i => i.type), ['punctuation']); + assert.strictEqual( + hook.validateCommitMessage('git commit -m "$(cat <<\'EOF\'\nfix: a\nEOF\n)\\$HOME"').message, + 'fix: a$HOME' + ); + // An expansion after the substitution is not literal text. + assert.deepStrictEqual( + hook.validateCommitMessage('git commit -m "$(cat <<\'EOF\'\nfix: a\nEOF\n)$HOME"').issues.map(i => i.type), + ['unverifiable'] + ); +})) passed++; else failed++; + if (process.platform !== 'win32') { if (test('reads the message that Bash and Git record for a here-document', () => { const bashMajor = Number(spawnSync('/bin/bash', ['-c', 'echo "${BASH_VERSINFO[0]}"'], { encoding: 'utf8' }).stdout); @@ -584,10 +599,12 @@ if (process.platform !== 'win32') { ['-EOF', '\tfix: a\n\t\\\n\tb'], ["'EOF'", 'fix: \\\nUpdate input.'], ['"EOF"', 'fix: keep \\\\ and \\$HOME'], + ["'EOF'", 'fix: update input', '.'], + ["'EOF'", 'fix: update input\n\nbody line', ' and more'], ]; inTempRepo(repoDir => { - for (const [delimiter, body] of forms) { - const command = `git commit -q --allow-empty -m "$(cat <<${delimiter}\n${body}\nEOF\n)"`; + for (const [delimiter, body, suffix = ''] of forms) { + const command = `git commit -q --allow-empty -m "$(cat <<${delimiter}\n${body}\nEOF\n)${suffix}"`; const run = spawnSync('/bin/bash', ['-c', command], { cwd: repoDir, encoding: 'utf8' }); assert.strictEqual(run.status, 0, run.stderr); const recorded = spawnSync('git', ['log', '-1', '--format=%B'], { cwd: repoDir, encoding: 'utf8' }).stdout.trimEnd(); From fee3d086669bab9e757f4e578163a09ad45e253f Mon Sep 17 00:00:00 2001 From: L4XB Date: Thu, 5 Nov 2026 14:36:26 +0100 Subject: [PATCH 10/10] fix(hooks): drop line continuations before checking text after a commit here-document In double quotes Bash drops a backslash-newline before it expands anything, so a tail of `$\HOME` after the substitution's `)` is `$HOME` and expands. The expansion check read the tail with the continuation still in it and saw an escaped newline, so the hook validated `fix: a$HOME` with no warning. Continuations are now dropped first, while other escapes stay for the check: `\$\HOME` is still the literal `$HOME`. --- scripts/hooks/pre-bash-commit-quality.js | 5 ++++- tests/hooks/pre-bash-commit-quality.test.js | 10 ++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) 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) {