Repository navigation
Expand file tree
/
Copy pathversion-policy.mjs
More file actions
83 lines (75 loc) · 3.49 KB
/
Copy pathversion-policy.mjs
File metadata and controls
83 lines (75 loc) · 3.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import {execFileSync} from 'node:child_process';
import {readFileSync} from 'node:fs';
// package.json must match resolveVersion() — run scripts/sync-package-version.mjs after
// shipping code. The baseline is the newest first-parent commit that changed the
// "version" line of package.json. It is derived from history rather than stored as a
// SHA, so squash and rebase merges cannot orphan it (a stored branch SHA broke main
// after #176 and #191).
// Rules: feat/behavior → minor (reset patch); any other counted change → patch.
// Floor v0.4.0: western scale lock + performance arp/latch state (#130, #134).
function versionAt(rev){
try{
const text=execFileSync('git',['show',rev+':package.json'],{encoding:'utf8',stdio:['ignore','pipe','ignore']});
return JSON.parse(text).version??null;
}catch{return null}
}
export function versionBaseline(){
const candidates=execFileSync('git',['log','--first-parent','--format=%H','--','package.json'],{encoding:'utf8'})
.split('\n').filter(Boolean);
for(const sha of candidates){
if(versionAt(sha)!==versionAt(sha+'^'))return sha;
}
throw new Error('No commit changing the package.json version is in this checkout (shallow clone? fetch more history)');
}
const RELEASE_FLOOR=/^chore(?:\([^)]+\))?:\s*release\b/i;
const RELEASE_TAG=/^release v\d+\.\d+\.\d+/i;
const VERSION_SYNC=/^chore(?:\([^)]+\))?:\s*sync package\.json\b/i;
export function commitTitle(message){
const subject=message.split('\n')[0].trim();
if(!subject)return '';
if(subject.startsWith('Merge pull request')){
return message.split('\n').map(line=>line.trim()).filter(Boolean).at(-1)??subject;
}
return subject;
}
export function countsTowardVersion(message){
const subject=message.split('\n')[0].trim();
if(!subject)return false;
if(/^Merge branch/i.test(subject))return false;
// GitHub PR merges on main: count the PR title in the merge body, not the merge subject.
if(/^Merge [0-9a-f]{7,40} into/i.test(subject))return false;
const title=commitTitle(message);
if(RELEASE_FLOOR.test(title))return false;
if(RELEASE_TAG.test(title))return false;
if(VERSION_SYNC.test(title))return false;
return true;
}
export function calculateVersion(base,commits){
let [major,minor,patch]=base.split('.').map(Number);
for(const message of commits.slice().reverse()){
if(!countsTowardVersion(message))continue;
const title=commitTitle(message);
const breaking=/^[a-z]+(?:\([^)]+\))?!:/i.test(title)||/BREAKING CHANGE:/i.test(message);
if(breaking){
// Pre-1.0 development: even breaking changes advance minor only.
// A 1.0 release requires an explicit, separate decision.
if(major===0){minor++;patch=0}
else {major++;minor=0;patch=0}
}
else if(/^feat(?:\([^)]+\))?:/i.test(title)){minor++;patch=0}
else if(countsTowardVersion(message)){patch++}
}
return [major,minor,patch].join('.');
}
export function gitCommitsSinceBaseline(baseline=versionBaseline()){
// -z record separators; %B includes PR title line on merge commits (see commitTitle).
const raw=execFileSync('git',['log','--first-parent','-z','--format=%B',baseline+'..HEAD'],{encoding:'utf8'});
return raw.split('\0').filter(message=>message.trim());
}
export function packageBaseVersion(){
return JSON.parse(readFileSync('package.json','utf8')).version;
}
export function resolveVersion({baseVersion=packageBaseVersion(),commits,baseline}={}){
const list=commits??gitCommitsSinceBaseline(baseline??versionBaseline());
return calculateVersion(baseVersion,list);
}