@virling/commitcheck 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +18 -0
- package/LICENSE +75 -0
- package/README.md +148 -2
- package/bin/commitcheck.js +77 -0
- package/package.json +41 -4
- package/src/config.js +48 -0
- package/src/git.js +34 -0
- package/src/rules.js +38 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to this project are documented here. The format follows [Keep a Changelog](https://keepachangelog.com/en/1.1.0/) and the project uses [Semantic Versioning](https://semver.org/).
|
|
4
|
+
|
|
5
|
+
## [Unreleased]
|
|
6
|
+
|
|
7
|
+
## [0.1.0] - 2026-11-30
|
|
8
|
+
|
|
9
|
+
### Added
|
|
10
|
+
|
|
11
|
+
- `commitcheck [<range>]`: checks every non-merge commit in a git range, by default `@{upstream}..HEAD`, and prints each failing commit with its failures.
|
|
12
|
+
- `commitcheck --file <path>`: checks one message file for the commit-msg hook, ignoring `#` comment lines and the diff below the scissors line.
|
|
13
|
+
- `commitcheck install`: writes a `.git/hooks/commit-msg` hook, refuses to replace a hook it did not write, and prints the line to add when `core.hooksPath` is set.
|
|
14
|
+
- Rules from `.commitcheck.json` or the `"commitcheck"` key in package.json: `maxSubject`, `maxLines`, `noTrailers`, `noAttribution` (AI co-author trailers, "Generated with" lines and the robot emoji, on by default), `noEmoji`, `noEmDash` and `bannedPatterns`. Unknown keys and wrong types are errors.
|
|
15
|
+
- `--json` output.
|
|
16
|
+
|
|
17
|
+
[Unreleased]: https://github.com/virling/commitcheck/compare/v0.1.0...HEAD
|
|
18
|
+
[0.1.0]: https://github.com/virling/commitcheck/releases/tag/v0.1.0
|
package/LICENSE
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
# PolyForm Noncommercial License 1.0.0
|
|
2
|
+
|
|
3
|
+
<https://polyformproject.org/licenses/noncommercial/1.0.0>
|
|
4
|
+
|
|
5
|
+
## Acceptance
|
|
6
|
+
|
|
7
|
+
In order to get any license under these terms, you must agree to them as both strict obligations and conditions to all your licenses.
|
|
8
|
+
|
|
9
|
+
## Copyright License
|
|
10
|
+
|
|
11
|
+
The licensor grants you a copyright license for the software to do everything you might do with the software that would otherwise infringe the licensor's copyright in it for any permitted purpose. However, you may only distribute the software according to [Distribution License](#distribution-license) and make changes or new works based on the software according to [Changes and New Works License](#changes-and-new-works-license).
|
|
12
|
+
|
|
13
|
+
## Distribution License
|
|
14
|
+
|
|
15
|
+
The licensor grants you an additional copyright license to distribute copies of the software. Your license to distribute covers distributing the software with changes and new works permitted by [Changes and New Works License](#changes-and-new-works-license).
|
|
16
|
+
|
|
17
|
+
## Notices
|
|
18
|
+
|
|
19
|
+
You must ensure that anyone who gets a copy of any part of the software from you also gets a copy of these terms or the URL for them above, as well as copies of any plain-text lines beginning with `Required Notice:` that the licensor provided with the software. For example:
|
|
20
|
+
|
|
21
|
+
> Required Notice: Copyright Yoyodyne, Inc. (http://example.com)
|
|
22
|
+
|
|
23
|
+
## Changes and New Works License
|
|
24
|
+
|
|
25
|
+
The licensor grants you an additional copyright license to make changes and new works based on the software for any permitted purpose.
|
|
26
|
+
|
|
27
|
+
## Patent License
|
|
28
|
+
|
|
29
|
+
The licensor grants you a patent license for the software that covers patent claims the licensor can license, or becomes able to license, that you would infringe by using the software.
|
|
30
|
+
|
|
31
|
+
## Noncommercial Purposes
|
|
32
|
+
|
|
33
|
+
Any noncommercial purpose is a permitted purpose.
|
|
34
|
+
|
|
35
|
+
## Personal Uses
|
|
36
|
+
|
|
37
|
+
Personal use for research, experiment, and testing for the benefit of public knowledge, personal study, private entertainment, hobby projects, amateur pursuits, or religious observance, without any anticipated commercial application, is use for a permitted purpose.
|
|
38
|
+
|
|
39
|
+
## Noncommercial Organizations
|
|
40
|
+
|
|
41
|
+
Use by any charitable organization, educational institution, public research organization, public safety or health organization, environmental protection organization, or government institution is use for a permitted purpose regardless of the source of funding or obligations resulting from the funding.
|
|
42
|
+
|
|
43
|
+
## Fair Use
|
|
44
|
+
|
|
45
|
+
You may have "fair use" rights for the software under the law. These terms do not limit them.
|
|
46
|
+
|
|
47
|
+
## No Other Rights
|
|
48
|
+
|
|
49
|
+
These terms do not allow you to sublicense or transfer any of your licenses to anyone else, or prevent the licensor from granting licenses to anyone else. These terms do not imply any other licenses.
|
|
50
|
+
|
|
51
|
+
## Patent Defense
|
|
52
|
+
|
|
53
|
+
If you make any written claim that the software infringes or contributes to infringement of any patent, your patent license for the software granted under these terms ends immediately. If your company makes such a claim, your patent license ends immediately for work on behalf of your company.
|
|
54
|
+
|
|
55
|
+
## Violations
|
|
56
|
+
|
|
57
|
+
The first time you are notified in writing that you have violated any of these terms, or done anything with the software not covered by your licenses, your licenses can nonetheless continue if you come into full compliance with these terms, and take practical steps to correct past violations, within 32 days of receiving notice. Otherwise, all your licenses end immediately.
|
|
58
|
+
|
|
59
|
+
## No Liability
|
|
60
|
+
|
|
61
|
+
***As far as the law allows, the software comes as is, without any warranty or condition, and the licensor will not be liable to you for any damages arising out of these terms or the use or nature of the software, under any kind of legal claim.***
|
|
62
|
+
|
|
63
|
+
## Definitions
|
|
64
|
+
|
|
65
|
+
The **licensor** is the individual or entity offering these terms, and the **software** is the software the licensor makes available under these terms.
|
|
66
|
+
|
|
67
|
+
**You** refers to the individual or entity agreeing to these terms.
|
|
68
|
+
|
|
69
|
+
**Your company** is any legal entity, sole proprietorship, or other kind of organization that you work for, plus all organizations that have control over, are under the control of, or are under common control with that organization. **Control** means ownership of substantially all the assets of an entity, or the power to direct its management and policies by vote, contract, or otherwise. Control can be direct or indirect.
|
|
70
|
+
|
|
71
|
+
**Your licenses** are all the licenses granted to you for the software under these terms.
|
|
72
|
+
|
|
73
|
+
**Use** means anything you do with the software requiring one of your licenses.
|
|
74
|
+
|
|
75
|
+
Required Notice: Copyright 2026 Virling (https://github.com/virling)
|
package/README.md
CHANGED
|
@@ -1,3 +1,149 @@
|
|
|
1
|
-
#
|
|
1
|
+
# commitcheck
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Check git commit messages against a few simple rules, in a commit-msg hook and in CI. Zero dependencies.
|
|
4
|
+
|
|
5
|
+
- **Catches AI tool attribution.** `Co-Authored-By` trailers naming Claude, Copilot, Cursor, Codex and other AI tools, `Generated with ...` lines, and the robot emoji fail by default.
|
|
6
|
+
- **Small, explicit rules.** Subject length, line count, banned trailers, emoji, em dashes and your own regexes. Nothing else.
|
|
7
|
+
- **Checks a whole branch.** `commitcheck` checks every commit on your branch that is not on its upstream, or any range you pass. Merge commits are skipped.
|
|
8
|
+
- **Works as a hook.** `commitcheck install` writes a `commit-msg` hook that rejects a bad message before the commit is made.
|
|
9
|
+
|
|
10
|
+
## Requirements
|
|
11
|
+
|
|
12
|
+
- Node.js 20 or newer
|
|
13
|
+
- git
|
|
14
|
+
|
|
15
|
+
## Usage
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
npx @virling/commitcheck # check commits in @{upstream}..HEAD
|
|
19
|
+
npx @virling/commitcheck origin/main..HEAD # check a range
|
|
20
|
+
npx @virling/commitcheck --file .git/COMMIT_EDITMSG
|
|
21
|
+
npx @virling/commitcheck install # write .git/hooks/commit-msg
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
| Flag | Meaning |
|
|
25
|
+
|---|---|
|
|
26
|
+
| `--file <path>` | Check one message file instead of a range. Lines starting with `#` and everything below git's scissors line (`# ---- >8 ----`) are ignored, as git does. |
|
|
27
|
+
| `--json` | Machine-readable output. |
|
|
28
|
+
| `--help` | Print usage. |
|
|
29
|
+
|
|
30
|
+
Each failing commit is printed as `<short sha> <subject>` followed by its failures. A real run on three commits of [virling/virling](https://github.com/virling/virling), with the rules from [this repo's `.commitcheck.json`](.commitcheck.json):
|
|
31
|
+
|
|
32
|
+
```
|
|
33
|
+
$ npx @virling/commitcheck 4a319d2~2..f6b3109
|
|
34
|
+
f6b3109 Add post: which blog posts bring paying customers (#682)
|
|
35
|
+
AI co-author: Co-authored-by: Claude Sonnet 5.5 <noreply@anthropic.com>
|
|
36
|
+
4a319d2 Add post: why Direct is the biggest traffic source (#680)
|
|
37
|
+
AI co-author: Co-authored-by: Claude Sonnet 5.5 <noreply@anthropic.com>
|
|
38
|
+
2ab8306 Flag browsers that render WebGL in software as automated (#681)
|
|
39
|
+
subject is 63 characters (max 59)
|
|
40
|
+
3 commits checked in 4a319d2~2..f6b3109, 3 failed
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
As a commit-msg hook it checks the message before the commit is made:
|
|
44
|
+
|
|
45
|
+
```
|
|
46
|
+
$ git commit -m "Add the export button to the report page" -m "Generated with Claude Code"
|
|
47
|
+
commitcheck: commit message rejected
|
|
48
|
+
Add the export button to the report page
|
|
49
|
+
generated-by line: Generated with Claude Code
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
It exits 1 if any message fails or git or the config gives an error, and 2 for an unknown flag or too many arguments. With no range and no upstream on the current branch, it asks you to pass one, such as `origin/main..HEAD`.
|
|
53
|
+
|
|
54
|
+
## Rules
|
|
55
|
+
|
|
56
|
+
Rules come from `.commitcheck.json` in the repo root, or the `"commitcheck"` key in `package.json` when there is no `.commitcheck.json`. They are merged over the defaults. An unknown key or a wrong type is an error.
|
|
57
|
+
|
|
58
|
+
| Rule | Default | Fails when |
|
|
59
|
+
|---|---|---|
|
|
60
|
+
| `maxSubject` | `72` | the first line is longer than this many characters |
|
|
61
|
+
| `maxLines` | `null` | the message has more than this many non-empty lines (`null` means no limit) |
|
|
62
|
+
| `noTrailers` | `[]` | a line is a trailer with one of these keys, for example `["Co-Authored-By"]` (case does not matter) |
|
|
63
|
+
| `noAttribution` | `true` | a `Co-Authored-By` trailer names an AI tool, a line starts with `Generated with` or `Generated by`, or the message has the robot emoji (U+1F916) |
|
|
64
|
+
| `noEmoji` | `false` | the message has any emoji |
|
|
65
|
+
| `noEmDash` | `false` | the message has an em dash (U+2014) |
|
|
66
|
+
| `bannedPatterns` | `[]` | the message matches one of these regexes (case-sensitive; `^` and `$` match at each line) |
|
|
67
|
+
|
|
68
|
+
`noAttribution` looks for these names in the `Co-Authored-By` name or email, as whole words, ignoring case: Claude, Anthropic, Copilot (and GitHub Copilot), Cursor, Codex, OpenAI, ChatGPT, Gemini, Devin, Aider, Windsurf. Co-authors who are people still pass unless you also ban the trailer with `noTrailers`.
|
|
69
|
+
|
|
70
|
+
Example `.commitcheck.json` for short, plain commits: at most 2 lines, a subject under 60 characters, no co-author trailers, no attribution, no emoji, no em dashes, and no conventional-commit prefixes:
|
|
71
|
+
|
|
72
|
+
```json
|
|
73
|
+
{
|
|
74
|
+
"maxSubject": 59,
|
|
75
|
+
"maxLines": 2,
|
|
76
|
+
"noTrailers": ["Co-Authored-By"],
|
|
77
|
+
"noAttribution": true,
|
|
78
|
+
"noEmoji": true,
|
|
79
|
+
"noEmDash": true,
|
|
80
|
+
"bannedPatterns": ["^(feat|fix|chore|docs|refactor|test|ci|build|perf|style)(\\(.+\\))?!?: "]
|
|
81
|
+
}
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
Leave out `bannedPatterns` if your repo uses conventional commits. Imperative mood is not checked; no simple rule gets it right.
|
|
85
|
+
|
|
86
|
+
## The commit-msg hook
|
|
87
|
+
|
|
88
|
+
`commitcheck install` writes `.git/hooks/commit-msg`:
|
|
89
|
+
|
|
90
|
+
```sh
|
|
91
|
+
#!/bin/sh
|
|
92
|
+
# written by commitcheck install
|
|
93
|
+
exec npx --no-install @virling/commitcheck --file "$1"
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
`npx --no-install` uses the copy in the project's `node_modules` (add it with `npm install -D @virling/commitcheck`) or one npx already has cached, and never downloads. Running `install` again rewrites a hook it wrote, but it refuses to replace any other `commit-msg` hook and prints the line to add to it instead.
|
|
97
|
+
|
|
98
|
+
If `git config core.hooksPath` is set, git ignores `.git/hooks`, so `install` writes nothing, prints the line to add to the `commit-msg` hook in that folder, and exits 1:
|
|
99
|
+
|
|
100
|
+
```sh
|
|
101
|
+
npx --no-install @virling/commitcheck --file "$1" || exit 1
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
## In CI
|
|
105
|
+
|
|
106
|
+
Check every commit in a pull request:
|
|
107
|
+
|
|
108
|
+
```yaml
|
|
109
|
+
name: commitcheck
|
|
110
|
+
|
|
111
|
+
on: pull_request
|
|
112
|
+
|
|
113
|
+
permissions:
|
|
114
|
+
contents: read
|
|
115
|
+
|
|
116
|
+
jobs:
|
|
117
|
+
commitcheck:
|
|
118
|
+
runs-on: ubuntu-latest
|
|
119
|
+
steps:
|
|
120
|
+
- uses: actions/checkout@v7
|
|
121
|
+
with:
|
|
122
|
+
fetch-depth: 0
|
|
123
|
+
- uses: actions/setup-node@v7
|
|
124
|
+
with:
|
|
125
|
+
node-version: 24
|
|
126
|
+
- run: npx @virling/commitcheck "origin/$BASE_REF..HEAD"
|
|
127
|
+
env:
|
|
128
|
+
BASE_REF: ${{ github.base_ref }}
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
`fetch-depth: 0` fetches the base branch so the range resolves. The base ref goes through `env:` so a branch name is never pasted into the shell script.
|
|
132
|
+
|
|
133
|
+
## Compared with commitlint
|
|
134
|
+
|
|
135
|
+
commitlint does much more: plugins, shareable configs and a full Conventional Commits parser. commitcheck has a handful of fixed rules, no dependencies, and catches AI tool attribution out of the box. If you want Conventional Commits enforced, use commitlint; if you want short, plain messages without AI trailers, this is less to set up.
|
|
136
|
+
|
|
137
|
+
## More tools from Virling
|
|
138
|
+
|
|
139
|
+
- [agentmem](https://github.com/virling/agentmem): project memory for AI coding agents, shared across Claude Code, Cursor, Codex and other MCP clients.
|
|
140
|
+
- [ccdoctor](https://github.com/virling/ccdoctor): checks your Claude Code setup: hooks, MCP servers and plugins that are registered but not working.
|
|
141
|
+
- [ciwhy](https://github.com/virling/ciwhy): finds the real error in a failed GitHub Actions run and tells you if it is flaky.
|
|
142
|
+
- [npmship](https://github.com/virling/npmship): releases an npm package and verifies it landed on npm, in CI and on GitHub.
|
|
143
|
+
- [reposync](https://github.com/virling/reposync): keeps shared files such as CI workflows identical across sibling repos.
|
|
144
|
+
- [rulecheck](https://github.com/virling/rulecheck): lints CLAUDE.md, AGENTS.md and other agent rule files for stale paths, missing scripts and duplicates.
|
|
145
|
+
- [subagents](https://github.com/virling/subagents): reviews what Claude Code subagents did in a session and flags repeated or wasted work.
|
|
146
|
+
|
|
147
|
+
## License
|
|
148
|
+
|
|
149
|
+
[PolyForm Noncommercial 1.0.0](LICENSE).
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import fs from "node:fs";
|
|
3
|
+
import { parseArgs } from "node:util";
|
|
4
|
+
import { loadConfig } from "../src/config.js";
|
|
5
|
+
import { commits, defaultRange, install, root } from "../src/git.js";
|
|
6
|
+
import { check } from "../src/rules.js";
|
|
7
|
+
|
|
8
|
+
const HELP = `commitcheck: check git commit messages against simple rules
|
|
9
|
+
|
|
10
|
+
commitcheck [<range>] check each commit in a git range (default: @{upstream}..HEAD)
|
|
11
|
+
commitcheck --file <path> check one message file (for the commit-msg hook)
|
|
12
|
+
commitcheck install write .git/hooks/commit-msg that runs --file
|
|
13
|
+
--json machine-readable output
|
|
14
|
+
--help
|
|
15
|
+
|
|
16
|
+
Rules come from .commitcheck.json or the "commitcheck" key in package.json.
|
|
17
|
+
Merge commits are skipped. Exits 1 if any message fails, 2 on bad usage.`;
|
|
18
|
+
|
|
19
|
+
const format = (problems) => problems.map((p) => ` ${p.message}`).join("\n");
|
|
20
|
+
|
|
21
|
+
function runRange(cwd, range, json) {
|
|
22
|
+
const config = loadConfig(root(cwd));
|
|
23
|
+
const r = range || defaultRange(cwd);
|
|
24
|
+
const results = commits(cwd, r).map((c) => ({ sha: c.sha, ...check(c.message, config) }));
|
|
25
|
+
const failed = results.filter((c) => c.problems.length);
|
|
26
|
+
if (json) console.log(JSON.stringify({ ok: !failed.length, range: r, checked: results.length, failed }, null, 2));
|
|
27
|
+
else {
|
|
28
|
+
for (const c of failed) console.log(`${c.sha.slice(0, 7)} ${c.subject}\n${format(c.problems)}`);
|
|
29
|
+
console.log(`${results.length} commit${results.length === 1 ? "" : "s"} checked in ${r}, ${failed.length} failed`);
|
|
30
|
+
}
|
|
31
|
+
return failed.length ? 1 : 0;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function runFile(cwd, file, json) {
|
|
35
|
+
const { subject, problems } = check(fs.readFileSync(file, "utf8"), loadConfig(root(cwd)));
|
|
36
|
+
if (json) console.log(JSON.stringify({ ok: !problems.length, file, subject, problems }, null, 2));
|
|
37
|
+
else if (problems.length) console.log(`commitcheck: commit message rejected\n ${subject}\n${format(problems)}`);
|
|
38
|
+
return problems.length ? 1 : 0;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function runInstall(cwd, json) {
|
|
42
|
+
const r = install(cwd);
|
|
43
|
+
if (json) console.log(JSON.stringify(r, null, 2));
|
|
44
|
+
else if (r.file) console.log(`Wrote ${r.file}`);
|
|
45
|
+
else console.log(`core.hooksPath is set to ${r.hooksPath}, so nothing was written. Add this line to its commit-msg hook:\n ${r.line}`);
|
|
46
|
+
return r.file ? 0 : 1;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function main() {
|
|
50
|
+
let args;
|
|
51
|
+
try {
|
|
52
|
+
args = parseArgs({ options: { file: { type: "string" }, json: { type: "boolean" }, help: { type: "boolean" } }, allowPositionals: true });
|
|
53
|
+
} catch (e) {
|
|
54
|
+
console.error(`commitcheck: ${e.message}\n\n${HELP}`);
|
|
55
|
+
return 2;
|
|
56
|
+
}
|
|
57
|
+
const { values, positionals } = args;
|
|
58
|
+
if (values.help) {
|
|
59
|
+
console.log(HELP);
|
|
60
|
+
return 0;
|
|
61
|
+
}
|
|
62
|
+
if (positionals.length > 1 || (values.file !== undefined && positionals.length)) {
|
|
63
|
+
console.error(`commitcheck: too many arguments\n\n${HELP}`);
|
|
64
|
+
return 2;
|
|
65
|
+
}
|
|
66
|
+
const cwd = process.cwd();
|
|
67
|
+
if (values.file !== undefined) return runFile(cwd, values.file, values.json);
|
|
68
|
+
if (positionals[0] === "install") return runInstall(cwd, values.json);
|
|
69
|
+
return runRange(cwd, positionals[0], values.json);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
try {
|
|
73
|
+
process.exitCode = main();
|
|
74
|
+
} catch (e) {
|
|
75
|
+
console.error(`commitcheck: ${e.stderr?.trim() || e.message}`);
|
|
76
|
+
process.exitCode = 1;
|
|
77
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,43 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@virling/commitcheck",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Check git commit messages against simple rules, including AI tool attribution",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"bin": {
|
|
7
|
+
"commitcheck": "bin/commitcheck.js"
|
|
8
|
+
},
|
|
9
|
+
"scripts": {
|
|
10
|
+
"test": "node --test"
|
|
11
|
+
},
|
|
12
|
+
"engines": {
|
|
13
|
+
"node": ">=20"
|
|
14
|
+
},
|
|
15
|
+
"license": "PolyForm-Noncommercial-1.0.0",
|
|
16
|
+
"keywords": [
|
|
17
|
+
"git",
|
|
18
|
+
"commit",
|
|
19
|
+
"commit-msg",
|
|
20
|
+
"lint",
|
|
21
|
+
"hook",
|
|
22
|
+
"co-authored-by",
|
|
23
|
+
"attribution"
|
|
24
|
+
],
|
|
25
|
+
"repository": {
|
|
26
|
+
"type": "git",
|
|
27
|
+
"url": "git+https://github.com/virling/commitcheck.git"
|
|
28
|
+
},
|
|
29
|
+
"files": [
|
|
30
|
+
"bin",
|
|
31
|
+
"src",
|
|
32
|
+
"README.md",
|
|
33
|
+
"LICENSE",
|
|
34
|
+
"CHANGELOG.md"
|
|
35
|
+
],
|
|
36
|
+
"homepage": "https://github.com/virling/commitcheck#readme",
|
|
37
|
+
"bugs": {
|
|
38
|
+
"url": "https://github.com/virling/commitcheck/issues"
|
|
39
|
+
},
|
|
40
|
+
"publishConfig": {
|
|
41
|
+
"access": "public"
|
|
42
|
+
}
|
|
43
|
+
}
|
package/src/config.js
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
|
|
4
|
+
export const DEFAULTS = { maxSubject: 72, maxLines: null, noTrailers: [], noAttribution: true, noEmoji: false, noEmDash: false, bannedPatterns: [] };
|
|
5
|
+
|
|
6
|
+
const isCount = (v) => Number.isInteger(v) && v > 0;
|
|
7
|
+
const isStrings = (v) => Array.isArray(v) && v.every((x) => typeof x === "string");
|
|
8
|
+
const TYPES = {
|
|
9
|
+
maxSubject: [isCount, "a positive integer"],
|
|
10
|
+
maxLines: [(v) => v === null || isCount(v), "a positive integer or null"],
|
|
11
|
+
noTrailers: [isStrings, "an array of strings"],
|
|
12
|
+
noAttribution: [(v) => typeof v === "boolean", "true or false"],
|
|
13
|
+
noEmoji: [(v) => typeof v === "boolean", "true or false"],
|
|
14
|
+
noEmDash: [(v) => typeof v === "boolean", "true or false"],
|
|
15
|
+
bannedPatterns: [isStrings, "an array of regex strings"],
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
function parse(file) {
|
|
19
|
+
try {
|
|
20
|
+
return JSON.parse(fs.readFileSync(file, "utf8"));
|
|
21
|
+
} catch (e) {
|
|
22
|
+
throw new Error(`${path.basename(file)}: ${e.message}`);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function validate(rules, source) {
|
|
27
|
+
if (!rules || typeof rules !== "object" || Array.isArray(rules)) throw new Error(`${source}: expected an object of rules`);
|
|
28
|
+
for (const [key, value] of Object.entries(rules)) {
|
|
29
|
+
if (!TYPES[key]) throw new Error(`${source}: unknown key "${key}" (known: ${Object.keys(TYPES).join(", ")})`);
|
|
30
|
+
if (!TYPES[key][0](value)) throw new Error(`${source}: "${key}" must be ${TYPES[key][1]}, got ${JSON.stringify(value)}`);
|
|
31
|
+
}
|
|
32
|
+
for (const p of rules.bannedPatterns || []) {
|
|
33
|
+
try {
|
|
34
|
+
new RegExp(p);
|
|
35
|
+
} catch (e) {
|
|
36
|
+
throw new Error(`${source}: bannedPatterns: ${e.message}`);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
return { ...DEFAULTS, ...rules };
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export function loadConfig(root) {
|
|
43
|
+
const own = path.join(root, ".commitcheck.json");
|
|
44
|
+
if (fs.existsSync(own)) return validate(parse(own), ".commitcheck.json");
|
|
45
|
+
const pkg = path.join(root, "package.json");
|
|
46
|
+
const rules = fs.existsSync(pkg) ? parse(pkg).commitcheck : undefined;
|
|
47
|
+
return rules === undefined ? { ...DEFAULTS } : validate(rules, 'package.json "commitcheck"');
|
|
48
|
+
}
|
package/src/git.js
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { execFileSync } from "node:child_process";
|
|
2
|
+
import fs from "node:fs";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
|
|
5
|
+
export const git = (cwd, ...args) => execFileSync("git", args, { cwd, encoding: "utf8", stdio: ["ignore", "pipe", "pipe"], maxBuffer: 256e6 }).trim();
|
|
6
|
+
|
|
7
|
+
export const root = (cwd) => git(cwd, "rev-parse", "--show-toplevel");
|
|
8
|
+
|
|
9
|
+
export function defaultRange(cwd) {
|
|
10
|
+
const upstream = git(cwd, "for-each-ref", "--format=%(upstream:short)", git(cwd, "rev-parse", "--symbolic-full-name", "HEAD"));
|
|
11
|
+
if (!upstream) throw new Error("the current branch has no upstream; pass a range such as origin/main..HEAD");
|
|
12
|
+
return `${upstream}..HEAD`;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function commits(cwd, range) {
|
|
16
|
+
const fields = git(cwd, "log", "--no-merges", "--format=%H%x00%B%x00", "--end-of-options", range, "--").split("\0");
|
|
17
|
+
const list = [];
|
|
18
|
+
for (let i = 0; i + 1 < fields.length; i += 2) list.push({ sha: fields[i].trim(), message: fields[i + 1] });
|
|
19
|
+
return list;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const MARKER = "# written by commitcheck install";
|
|
23
|
+
const HOOK_LINE = 'npx --no-install @virling/commitcheck --file "$1"';
|
|
24
|
+
|
|
25
|
+
export function install(cwd) {
|
|
26
|
+
const hooksPath = git(cwd, "config", "--default", "", "--get", "core.hooksPath");
|
|
27
|
+
if (hooksPath) return { hooksPath, line: `${HOOK_LINE} || exit 1` };
|
|
28
|
+
const file = path.resolve(cwd, git(cwd, "rev-parse", "--git-path", "hooks/commit-msg"));
|
|
29
|
+
if (fs.existsSync(file) && !fs.readFileSync(file, "utf8").includes(MARKER)) throw new Error(`${file} already exists and was not written by commitcheck; add this line to it instead:\n ${HOOK_LINE} || exit 1`);
|
|
30
|
+
fs.mkdirSync(path.dirname(file), { recursive: true });
|
|
31
|
+
fs.writeFileSync(file, `#!/bin/sh\n${MARKER}\nexec ${HOOK_LINE}\n`);
|
|
32
|
+
fs.chmodSync(file, 0o755);
|
|
33
|
+
return { file };
|
|
34
|
+
}
|
package/src/rules.js
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
const AI_TOOLS = /\b(claude|anthropic|copilot|cursor|codex|openai|chatgpt|gemini|devin|aider|windsurf)\b/i;
|
|
2
|
+
const ROBOT = "\u{1F916}";
|
|
3
|
+
const EMOJI = /\p{Emoji_Presentation}|\p{Extended_Pictographic}\uFE0F/u;
|
|
4
|
+
const SCISSORS = /^# -+ >8 -+$/;
|
|
5
|
+
|
|
6
|
+
export function clean(text) {
|
|
7
|
+
const lines = text.replace(/\r\n/g, "\n").split("\n");
|
|
8
|
+
const cut = lines.findIndex((l) => SCISSORS.test(l));
|
|
9
|
+
return (cut < 0 ? lines : lines.slice(0, cut)).filter((l) => !l.startsWith("#")).join("\n").trim();
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
const trailer = (line) => line.match(/^([A-Za-z0-9-]+):\s*(.*)$/);
|
|
13
|
+
|
|
14
|
+
export function check(message, config) {
|
|
15
|
+
const text = clean(message);
|
|
16
|
+
const lines = text.split("\n");
|
|
17
|
+
const subject = lines[0];
|
|
18
|
+
const problems = [];
|
|
19
|
+
const fail = (rule, msg) => problems.push({ rule, message: msg });
|
|
20
|
+
if (!text) fail("empty", "message is empty");
|
|
21
|
+
const length = [...subject].length;
|
|
22
|
+
if (length > config.maxSubject) fail("maxSubject", `subject is ${length} characters (max ${config.maxSubject})`);
|
|
23
|
+
const count = lines.filter((l) => l.trim()).length;
|
|
24
|
+
if (config.maxLines !== null && count > config.maxLines) fail("maxLines", `${count} lines (max ${config.maxLines})`);
|
|
25
|
+
const banned = config.noTrailers.map((k) => k.toLowerCase());
|
|
26
|
+
for (const line of lines) {
|
|
27
|
+
const t = trailer(line);
|
|
28
|
+
const ai = config.noAttribution && t && t[1].toLowerCase() === "co-authored-by" && AI_TOOLS.test(t[2]);
|
|
29
|
+
if (ai) fail("noAttribution", `AI co-author: ${line}`);
|
|
30
|
+
else if (t && banned.includes(t[1].toLowerCase())) fail("noTrailers", `${t[1]} trailer is not allowed: ${line}`);
|
|
31
|
+
if (config.noAttribution && /^\W*generated (with|by)\b/i.test(line)) fail("noAttribution", `generated-by line: ${line}`);
|
|
32
|
+
}
|
|
33
|
+
if (config.noAttribution && text.includes(ROBOT)) fail("noAttribution", "robot emoji");
|
|
34
|
+
if (config.noEmoji && EMOJI.test(text)) fail("noEmoji", "emoji");
|
|
35
|
+
if (config.noEmDash && text.includes("\u2014")) fail("noEmDash", "em dash");
|
|
36
|
+
for (const p of config.bannedPatterns) if (new RegExp(p, "m").test(text)) fail("bannedPatterns", `matches banned pattern /${p}/`);
|
|
37
|
+
return { subject, problems };
|
|
38
|
+
}
|