Skip to content

fix(read): strip verbose diff from commit message#4770

Open
enaktes9-hub wants to merge 1 commit into
conventional-changelog:masterfrom
enaktes9-hub:fix/verbose-diff-stripping
Open

fix(read): strip verbose diff from commit message#4770
enaktes9-hub wants to merge 1 commit into
conventional-changelog:masterfrom
enaktes9-hub:fix/verbose-diff-stripping

Conversation

@enaktes9-hub
Copy link
Copy Markdown

Description

When git commit --verbose is used, git appends the diff to the commit message file (COMMIT_EDITMSG). Commitlint's --edit mode reads this file directly, causing body-max-line-length and footer-max-line-length rules to incorrectly fail on diff lines that exceed the configured limit.

Root Cause

getEditCommit() in @commitlint/read reads the raw commit message file without stripping the verbose diff. Git itself strips everything after the scissors line (# ------------------------ >8 ------------------------) before storing the commit, but commitlint sees the pre-stripped version when running as a commit-msg hook.

Fix

Strip everything after either:

  • The scissors line: # ------------------------ >8 ------------------------
  • The first diff --git marker

whichever comes first. This mirrors git's own stripping behavior without needing to parse core.commentChar.

Related

Fixes #437
Closes #437

When `git commit --verbose` is used, git appends the diff to the
commit message file (COMMIT_EDITMSG). Commitlint's --edit mode reads
this file directly, causing body/footer-max-line-length rules to
incorrectly fail on diff lines.

Strip everything after the scissors line or the first `diff --git`
marker, mirroring git's own stripping behavior.

Fixes conventional-changelog#437
@qodo-code-review
Copy link
Copy Markdown

Qodo reviews are paused for this user.

Troubleshooting steps vary by plan Learn more →

On a Teams plan?
Reviews resume once this user has a paid seat and their Git account is linked in Qodo.
Link Git account →

Using GitHub Enterprise Server, GitLab Self-Managed, or Bitbucket Data Center?
These require an Enterprise plan - Contact us
Contact us →

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Strips the verbose diff that git commit --verbose appends to COMMIT_EDITMSG so that body-max-line-length / footer-max-line-length rules don't fire on diff lines when commitlint is run as a commit-msg hook. Fixes #437.

Changes:

  • In getEditCommit, locate the earlier of the scissors line (# ------------------------ >8 ------------------------) or the first diff --git marker and slice the file contents up to that index before returning.
  • Preserves prior behavior (trailing newline appended, single-element array return) when neither marker is present.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +30 to +47
const raw = editFile.toString("utf-8");

// Strip the verbose diff added by `git commit --verbose`.
// Git appends the diff after a scissors line and/or a `diff --git`
// marker. The diff is never part of the final commit message so
// neither should commitlint see it (see #437).
const diffIndex = raw.indexOf("\ndiff --git ");
const scissorsIndex = raw.indexOf("\n# ------------------------ >8 ------------------------");
const cutIndex =
diffIndex !== -1 && scissorsIndex !== -1
? Math.min(diffIndex, scissorsIndex)
: diffIndex !== -1
? diffIndex
: scissorsIndex;

const cleaned = cutIndex !== -1 ? raw.slice(0, cutIndex) : raw;

return [`${cleaned}\n`];
Comment on lines +36 to +43
const diffIndex = raw.indexOf("\ndiff --git ");
const scissorsIndex = raw.indexOf("\n# ------------------------ >8 ------------------------");
const cutIndex =
diffIndex !== -1 && scissorsIndex !== -1
? Math.min(diffIndex, scissorsIndex)
: diffIndex !== -1
? diffIndex
: scissorsIndex;
@escapedcat
Copy link
Copy Markdown
Member

Thanks! Please have a look at the copilot feedback

This cam from Claude, some is duplication copilots feedback. Adding tests would be good. Even better if you:

  1. commit : add tests, fails on CI
  2. commit: add the fix, CI is passing

Code issues

  1. No tests — this is the biggest gap. @commitlint/read/src/read.test.ts:1 has the harness ready (git.bootstrap() + fs
    writes to .git/COMMIT_EDITMSG), and there are already commentChar fixtures in @commitlint/cli/src/cli.test.ts:410-442
    showing the exact pattern. The PR should add at least:
  • A test where COMMIT_EDITMSG contains header\n\nbody\n# ------------------------ >8 ------------------------\n# Do not
    modify…\ndiff --git a/foo b/foo\n+long line… → verifies the result stops at the scissors line.
  • A test where the scissors line is absent and only diff --git exists → verifies the fallback.
  • A test where neither marker exists → verifies normal messages are untouched (regression guard).
  1. core.commentChar is hardcoded to #. commitlint already reads git config core.commentChar at
    @commitlint/cli/src/cli.ts:309-319 and passes it to the parser. The PR's scissors match is hardcoded \n#
    ------------------------ >8 ------------------------. If a user has core.commentChar=$ (which has a test fixture in this
    repo), git will write $ ------------------------ >8 ------------------------ and the scissors branch won't match. The
    \ndiff --git fallback still saves the day for the actual diff content, so this is a partial issue, not a breakage — but
    the fix is incomplete for non-default commentChar. Suggested approach: match the ------------------------ >8
    ------------------------ substring without anchoring on #, or accept the commentChar via the function signature like the
    parser does.

ncaq added a commit to ncaq/dotfiles that referenced this pull request May 25, 2026
`verbose`で変更セットをバッファに入れておくことは、
GitHub Copilot Completeの精度を向上させます。

しかし現在commitlintがdiff部分に反応して行の長さをチェックしてしまうという問題と衝突します。
[{body,footer}-max-line-length may fail when git commit --verbose · Issue #437 · conventional-changelog/commitlint](conventional-changelog/commitlint#437)

少なくとも解決策の、
[fix(read): strip verbose diff from commit message by enaktes9-hub · Pull Request #4770 · conventional-changelog/commitlint](conventional-changelog/commitlint#4770)
がマージされるまでは、
`verbose`オプションを削除します。
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

{body,footer}-max-line-length may fail when git commit --verbose

3 participants