ShtukbaseStandard
包文件→ .shtukbase/packages/std-semver/checks/sv02_breaking.ts

checks/sv02_breaking.ts

29 行, 1.8 KB.

文件按包中的原样显示:代码及其注释不做翻译。

  1. // SV-R02: after the last release tag, a breaking change (`!` in a Conventional Commits header or a BREAKING CHANGE
  2. // footer) raises the major version (the minor one while the major is 0, setting `zeroMajor`); the project's version
  3. // is never below its last release. Git is only read (tag, log).
  4. import { compare, config, parse, PKG, projectVersion, releaseTags } from './semverlib.ts';
  5. import { finish, git, norm } from './lib/std.ts';
  6. const n = norm(PKG, 'SV-R02', 'observe');
  7. const out: string[] = [];
  8. const cfg = config();
  9. const tags = releaseTags(cfg).filter((t) => t.ver).sort((a, b) => compare(b.ver!, a.ver!));
  10. const cur = projectVersion(cfg);
  11. const curVer = cur ? parse(cur.version) : null;
  12. if (n.active && tags.length && cur && curVer) {
  13. const last = tags[0];
  14. const lv = last.ver!;
  15. if (compare(curVer, lv) < 0) out.push(`${cur.file}:${cur.line}: version ${cur.version} is below the last release ${last.tag}`);
  16. const log = git(['log', '--no-merges', '--format=%h%x1f%B%x1e', `${last.tag}..HEAD`]) ?? '';
  17. const breaking = log.split('\x1e').map((s) => s.trim()).filter(Boolean).filter((s) => {
  18. const msg = s.split('\x1f')[1] ?? '';
  19. return /^[A-Za-z][\w-]*(\([^()\n]*\))?!: /.test(msg) || /^BREAKING[ -]CHANGE: /m.test(msg);
  20. }).map((s) => s.split('\x1f')[0]);
  21. if (breaking.length) {
  22. const zero = lv.major === 0 && cfg.zeroMajor === 'minor';
  23. const ok = zero ? curVer.major > 0 || curVer.minor > lv.minor : curVer.major > lv.major;
  24. if (!ok)
  25. 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`}`);
  26. }
  27. }
  28. finish(n, out);