Loslegen
Git Commit Writer — Draft conventional commits

Git Commit Writer — Draft conventional commits

Reads your staged git changes and writes a Conventional Commit message: type, scope, imperative subject, a file-by-file body, and a BREAKING CHANGE footer when a public function or option was removed. Warns when one commit mixes unrelated folders, and lints any message as a commit-msg hook. Deterministic; changes nothing unless you ask.
#Ingenieurwesen
Rating
Weitere Bewertungen erforderlich
Sold
0
How to use
Herunterladen

The problem

Commit messages are written last, when you are tired of the change. The result is update stuff, fix, or a header that says feat for a bug fix. Nobody can scan the history, and tools that read Conventional Commits (changelog generators, release bots) cannot use it. On a team that stages into one working tree, a second problem hides inside the first: files from someone else's task slip into your commit and nothing says so.

How it works

  1. Reads the staged diff with git diff --cached (or a diff you pass in with --diff-file).
  2. Parses every file: status (added, deleted, renamed, modified), lines added and removed, and which function each changed line sits in.
  3. Decides the type from what changed:
    • only tests, docs, CI files, build files or config changed: test, docs, ci, build, chore
    • a new file or a new function appears: feat
    • added lines mention a fix, bug, crash or typo: fix
    • added lines mention caching or speed: perf
    • only comments were added: docs; only whitespace moved: style
    • existing code changed inside a function: refactor; values changed outside any function: chore
  4. Decides the scope from the folder every file shares (src/, lib/ and app/ are skipped), or from the file name when one root file changed.
  5. Writes the subject from what it found: add slugify in words, remove legacy_trim and --verbose, rename a.py to b.py. The header is cut at a word boundary to fit --max-subject (default 72).
  6. Detects breaking changes: a public function that disappeared (a name that is gone from the diff, not merely moved), a removed add_argument("--flag"), or a changed file containing BREAKING CHANGE:. It adds ! to the header and one footer line per reason.
  7. Checks the staging: three or more top-level folders, more than 30 files, three or more kinds of change (source, build, config) mixed together, or files outside the --only patterns you expect.

The result is deterministic: the same diff always gives the same message.

Usage

# print a message for what is staged right now
python3 scripts/commit_writer.py

# read a diff file (or - for stdin) instead of git
python3 scripts/commit_writer.py --diff-file changes.patch

# commit with the generated message
python3 scripts/commit_writer.py --commit

# lint a message, or a message file (this is what the hook runs)
python3 scripts/commit_writer.py --lint "feat(api): add retry"
python3 scripts/commit_writer.py --lint-file .git/COMMIT_EDITMSG

# install a commit-msg hook that rejects bad messages
python3 scripts/commit_writer.py --install-hook
option meaning
--repo PATH the git repository (default: current folder)
--diff-file PATH read a unified diff from a file, or - for stdin
--type T force the type (feat, fix, docs, style, refactor, perf, test, build, ci, chore, revert)
--scope S force the scope
--ref ID add a Refs: ID footer, such as an issue number
--suffix ID append (ID) to the header, such as a task id
--breaking mark the commit as breaking even if nothing was detected
--only PATTERN expected path pattern (repeat it); other staged files raise a warning
--max-subject N header length limit (default 72)
--json print the full analysis: type, scope, reason, files, breaking reasons, warnings

Limits (read these)

  • The type is a heuristic, not a judgment. fix is chosen when the added lines mention a fix, bug or crash; a bug fix with no such word is reported as refactor or chore. The reason field in --json says why a type was chosen. Pass --type when you know better.
  • It names what changed, not why. The subject is built from file and function names. Edit it, or add a body that explains the reason.
  • Breaking detection reads names, not behaviour. A removed public function or option is found; a changed return value is not. Functions starting with _ are treated as private.
  • Function names come from def, function, func, fn, class and export lines. Other languages still work for files, scope and type, with fewer symbols in the subject.
  • It never touches your repository unless you pass --commit or --install-hook, makes no network calls, and sends nothing anywhere.