Skip to content

commit-validate.js false-positives on any heredoc or -m flag, unrelated to git commit #82

Description

@slava07437352641-ops

Bug

scripts/commit-validate.js's extractMessage() misidentifies non-git commit Bash commands as commit messages, causing the PreToolUse hook to block unrelated work.

Root cause

The -m/--message/heredoc/-F/--file sub-patterns in extractMessage() run unconditionally on the full command string, with no upfront check that the command actually invokes git commit. Two concrete false positives:

  1. Any heredoc in a Bash command matches the heredocAny regex (line ~30) and its first content line gets validated as a commit subject — even for something totally unrelated, e.g.:

    cat > file.py << 'EOF'
    import json
    EOF

    This blocks with "Commit message must follow conventional commits..." even though there's no git invocation at all.

  2. Any -m "..." flag on any command (not just git commit -m) gets captured as a "commit message" by the shortFlag regex, e.g. tools that happen to take a -m flag for unrelated purposes.

Fix

Add a guard at the top of extractMessage() that returns early unless the command actually matches a real git commit invocation, before running any of the sub-pattern checks:

function extractMessage(command) {
  if (!command) return { msg: null, form: 'empty' };

  if (!/\bgit\s+(?:-[^\s]+\s+)*commit\b/.test(command)) {
    return { msg: null, form: 'not-a-commit' };
  }

  const shortFlag = command.match(/(?:^|\s)-m\s+(?:"((?:[^"\\]|\\.)*)"|'((?:[^'\\]|\\.)*)'|(\S+))/);
  // ... rest unchanged

Verified locally: heredoc/non-commit commands now pass through silently (exit 0), a real invalid commit message still blocks correctly (exit 2), and a valid conventional commit still passes (exit 0).

Happy to open a PR with this exact change if useful — flagging as an issue first in case there's a preferred approach or existing WIP on this.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions