Skip to content

feat(commits): add Conventional Commits message normalizer - #218

Open
iamlasse wants to merge 1 commit into
marcus:mainfrom
iamlasse:feat/commit-normalize-restore
Open

feat(commits): add Conventional Commits message normalizer#218
iamlasse wants to merge 1 commit into
marcus:mainfrom
iamlasse:feat/commit-normalize-restore

Conversation

@iamlasse

Copy link
Copy Markdown

Summary

Standardizes commit message format with a Conventional Commits normalizer, restored from the unmerged commit c26c5fd and reviewed against main with three fixes applied.

Format rules (internal/commits)

  • Header: <type>(<scope>): <subject> per Conventional Commits 1.0.0
  • Allowed types: feat, fix, docs, style, refactor, test, chore, perf, build, ci
  • Type auto-lowercased; capitalized subject is a hard error
  • Subject max 72 chars, no trailing period, whitespace trimmed
  • Body wrapped at 72 columns, paragraphs preserved, git # comments stripped
  • Normalization is idempotent; sentinel errors (ErrEmptyMessage, ErrMissingType, ErrUnknownType, ErrMissingSubject, ErrSubjectTooLong, ErrSubjectLowercase) matched via errors.Is

CLI

nightshift commit normalize "feat: add login screen"   # positional
nightshift commit normalize --file .git/COMMIT_EDITMSG  # hook usage
git log -1 --pretty=%B | nightshift commit normalize     # stdin
nightshift commit normalize --check "feat: add login"   # validate only, non-zero exit on invalid

Hook

make install-hooks now installs both pre-commit and commit-msg. The commit-msg hook normalizes the message file in place before the commit is created and blocks messages it cannot fix (missing/unknown type, capitalized or overlong subject). Bypass with git commit --no-verify.

Docs: docs/commit-messages.md.

Fixes applied on top of c26c5fd

  • scripts/commit-msg.sh: set -e aborted the script before the friendly error could print — capture inside if ! and use a mktemp error file instead of a fixed /tmp path
  • commit normalize --check: no longer prints the rewritten message (validate-only)
  • CLI errors print once without the usage block spam
  • Makefile install-hooks actually links the commit-msg hook (docs referenced it but it wasn't installed)

Testing

  • go build ./...
  • go vet ./...
  • gofmt -l . clean ✅
  • go test ./... all packages pass ✅ (table-driven tests in internal/commits/normalizer_test.go: happy path, auto-fix cases, every sentinel error, idempotency, allowed-type set)
  • Manually verified: CLI positional/--file/stdin/--check paths, and the hook rewriting a message file in place + rejecting an invalid one

🤖 Generated with Claude Code

Add a pure internal/commits package that validates and rewrites commit
messages into canonical Conventional Commits form:

- <type>(<scope>): <subject> header with allowed type set
- lowercase type, non-capitalized subject, max 72-char subject
- whitespace/trailing-period cleanup and 72-column body wrapping
- git comment stripping and idempotent normalization
- sentinel errors (ErrEmptyMessage, ErrMissingType, ErrUnknownType,
  ErrMissingSubject, ErrSubjectTooLong, ErrSubjectLowercase) matched
  via errors.Is

Wire a 'nightshift commit normalize' cobra subcommand (positional arg,
--file, stdin; --check validate-only mode) and ship an installable
scripts/commit-msg.sh hook that normalizes the message file in place and
blocks invalid messages. make install-hooks now installs both hooks.
Rules and usage are documented in docs/commit-messages.md.

Restored from unmerged commit c26c5fd with fixes: hook now reports its
friendly error instead of being silenced by set -e, --check no longer
prints the rewritten message, CLI errors print once without usage spam,
and install-hooks links the commit-msg hook.

Nightshift-Task: commit-normalize
Nightshift-Ref: https://github.com/marcus/nightshift

Co-Authored-By: Claude <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant