包文件→ .shtukbase/packages/std-semver/checks/sv02_breaking.ts
checks/sv02_breaking.ts
29 行, 1.8 KB.
文件按包中的原样显示:代码及其注释不做翻译。
- // SV-R02: after the last release tag, a breaking change (`!` in a Conventional Commits header or a BREAKING CHANGE
- // footer) raises the major version (the minor one while the major is 0, setting `zeroMajor`); the project's version
- // is never below its last release. Git is only read (tag, log).
- import { compare, config, parse, PKG, projectVersion, releaseTags } from './semverlib.ts';
- import { finish, git, norm } from './lib/std.ts';
- const n = norm(PKG, 'SV-R02', 'observe');
- const out: string[] = [];
- const cfg = config();
- const tags = releaseTags(cfg).filter((t) => t.ver).sort((a, b) => compare(b.ver!, a.ver!));
- const cur = projectVersion(cfg);
- const curVer = cur ? parse(cur.version) : null;
- if (n.active && tags.length && cur && curVer) {
- const last = tags[0];
- const lv = last.ver!;
- if (compare(curVer, lv) < 0) out.push(`${cur.file}:${cur.line}: version ${cur.version} is below the last release ${last.tag}`);
- const log = git(['log', '--no-merges', '--format=%h%x1f%B%x1e', `${last.tag}..HEAD`]) ?? '';
- const breaking = log.split('\x1e').map((s) => s.trim()).filter(Boolean).filter((s) => {
- const msg = s.split('\x1f')[1] ?? '';
- return /^[A-Za-z][\w-]*(\([^()\n]*\))?!: /.test(msg) || /^BREAKING[ -]CHANGE: /m.test(msg);
- }).map((s) => s.split('\x1f')[0]);
- if (breaking.length) {
- const zero = lv.major === 0 && cfg.zeroMajor === 'minor';
- const ok = zero ? curVer.major > 0 || curVer.minor > lv.minor : curVer.major > lv.major;
- if (!ok)
- out.push(`${cur.file}:${cur.line}: after ${last.tag} there are breaking commits (${breaking.slice(0, 5).join(', ')}${breaking.length > 5 ? ', …' : ''}), but the version is ${cur.version} — ${zero ? `${lv.major}.${lv.minor + 1}.0 or higher is needed (while the major is 0, a breaking change raises the minor)` : `${lv.major + 1}.0.0 or higher is needed`}`);
- }
- }
- finish(n, out);