@@ -4,6 +4,20 @@ const path = require('path');
44
55const root = path . resolve ( __dirname , '..' , '..' ) ;
66const outDir = path . join ( root , '_build' , 'parity' ) ;
7+ const upstreamDoc = path . join ( root , 'UPSTREAM.md' ) ;
8+
9+ function loadUpstreamReference ( ) {
10+ const text = fs . readFileSync ( upstreamDoc , 'utf8' ) ;
11+ const repoMatch = text . match ( / U p s t r e a m r e p o : \s + ( [ A - Z a - z 0 - 9 _ . - ] + \/ [ A - Z a - z 0 - 9 _ . - ] + ) / ) ;
12+ const commitMatch = text . match ( / P i n n e d c o m m i t .* : ` ( [ 0 - 9 a - f ] { 40 } ) ` / ) ;
13+ if ( ! repoMatch || ! commitMatch ) {
14+ throw new Error ( `failed to parse upstream reference from ${ upstreamDoc } ` ) ;
15+ }
16+ return {
17+ repo : repoMatch [ 1 ] ,
18+ commit : commitMatch [ 1 ] ,
19+ } ;
20+ }
721
822function runChecked ( name , command , args ) {
923 const result = spawnSync ( command , args , {
@@ -25,6 +39,52 @@ function runChecked(name, command, args) {
2539 return result . stdout . trim ( ) ;
2640}
2741
42+ function ensureReferenceCheckout ( ) {
43+ const { repo, commit } = loadUpstreamReference ( ) ;
44+ const referenceDir = path . join ( root , 'cpal-reference' ) ;
45+ const cargoManifest = path . join ( referenceDir , 'Cargo.toml' ) ;
46+ if ( fs . existsSync ( cargoManifest ) ) {
47+ const head = runChecked ( 'cpal-reference rev-parse' , 'git' , [
48+ '-C' ,
49+ referenceDir ,
50+ 'rev-parse' ,
51+ 'HEAD' ,
52+ ] ) ;
53+ if ( head === commit ) {
54+ return ;
55+ }
56+ }
57+
58+ if ( ! fs . existsSync ( referenceDir ) ) {
59+ runChecked ( 'clone cpal-reference' , 'git' , [
60+ 'clone' ,
61+ '--filter=blob:none' ,
62+ '--no-checkout' ,
63+ `https://github.com/${ repo } .git` ,
64+ referenceDir ,
65+ ] ) ;
66+ } else if ( ! fs . existsSync ( path . join ( referenceDir , '.git' ) ) ) {
67+ throw new Error ( `${ referenceDir } exists but is not a git checkout` ) ;
68+ }
69+
70+ runChecked ( 'fetch cpal-reference commit' , 'git' , [
71+ '-C' ,
72+ referenceDir ,
73+ 'fetch' ,
74+ '--depth' ,
75+ '1' ,
76+ 'origin' ,
77+ commit ,
78+ ] ) ;
79+ runChecked ( 'checkout cpal-reference commit' , 'git' , [
80+ '-C' ,
81+ referenceDir ,
82+ 'checkout' ,
83+ '--detach' ,
84+ commit ,
85+ ] ) ;
86+ }
87+
2888function sortStrings ( xs ) {
2989 return [ ...xs ] . sort ( ) ;
3090}
@@ -176,6 +236,8 @@ function writeArtifacts(rustRaw, moonRaw, rustProjected, moonProjected) {
176236}
177237
178238function main ( ) {
239+ ensureReferenceCheckout ( ) ;
240+
179241 const cargoArgs = [
180242 'run' ,
181243 '--quiet' ,
0 commit comments