fix(read): strip verbose diff from commit message#4770
Conversation
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 reviews are paused for this user.Troubleshooting steps vary by plan Learn more → On a Teams plan? Using GitHub Enterprise Server, GitLab Self-Managed, or Bitbucket Data Center? |
There was a problem hiding this comment.
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 firstdiff --gitmarker 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.
| 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`]; |
| 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; |
|
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:
|
`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`オプションを削除します。
Description
When
git commit --verboseis used, git appends the diff to the commit message file (COMMIT_EDITMSG). Commitlint's--editmode reads this file directly, causingbody-max-line-lengthandfooter-max-line-lengthrules to incorrectly fail on diff lines that exceed the configured limit.Root Cause
getEditCommit()in@commitlint/readreads 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:
# ------------------------ >8 ------------------------diff --gitmarkerwhichever comes first. This mirrors git's own stripping behavior without needing to parse
core.commentChar.Related
Fixes #437
Closes #437