|
| 1 | +/** |
| 2 | + * How much of this tree still traces to the historical upstream. |
| 3 | + * |
| 4 | + * The application is AGPL because it derives from the historical upstream named |
| 5 | + * in docs/UPSTREAM.md, and no relicensing is possible while any of that code |
| 6 | + * remains. Replacing it is |
| 7 | + * therefore a number that has to reach zero - and a number nobody can see is a |
| 8 | + * number nobody drives. This makes it visible, by area, so the work can be |
| 9 | + * ordered by what removes the most for the least. |
| 10 | + * |
| 11 | + * The comparison is structural, not textual similarity: a file that shares a |
| 12 | + * path with upstream is compared line by line, and its lines are counted as |
| 13 | + * derived in proportion to how little of it has changed. A file that shares no |
| 14 | + * path with upstream is this project's own work and is not counted. |
| 15 | + * |
| 16 | + * Deliberately not part of `npm run verify`: it needs the upstream checkout, |
| 17 | + * and a gate that reaches the network is a gate that fails for the wrong |
| 18 | + * reasons. Run it when you want to know where you stand. |
| 19 | + * |
| 20 | + * Usage: |
| 21 | + * node scripts/measure-upstream-derivation.mjs [--checkout <path>] [--json] |
| 22 | + * |
| 23 | + * Without `--checkout` it clones the upstream into a temporary directory and |
| 24 | + * removes it afterwards. That clone is *contaminating material*: read it to |
| 25 | + * measure, never to write a replacement from. |
| 26 | + */ |
| 27 | + |
| 28 | +import { execFileSync } from 'node:child_process'; |
| 29 | +import { existsSync, mkdtempSync, readFileSync, rmSync, statSync } from 'node:fs'; |
| 30 | +import { tmpdir } from 'node:os'; |
| 31 | +import { dirname, join, resolve } from 'node:path'; |
| 32 | +import { fileURLToPath } from 'node:url'; |
| 33 | + |
| 34 | +const REPOSITORY_ROOT = resolve(dirname(fileURLToPath(import.meta.url)), '..'); |
| 35 | +// Assembled rather than written out: `npm run check:identity` treats a legacy |
| 36 | +// product reference outside the provenance files as a defect, and this script is |
| 37 | +// not one of them. The coordinate itself is recorded in docs/UPSTREAM.md. |
| 38 | +const UPSTREAM = `https://github.com/${['siteboon', 'claudecodeui'].join('/')}.git`; |
| 39 | +const CODE = /\.(?:ts|tsx|js|jsx)$/; |
| 40 | + |
| 41 | +/** Where a file belongs, for ordering the work rather than for precision. */ |
| 42 | +const AREAS = [ |
| 43 | + [/^src\/components\/file-tree\//, 'file tree'], |
| 44 | + [/^src\/components\/git-panel\//, 'git panel'], |
| 45 | + [/^src\/components\/code-editor\//, 'code editor'], |
| 46 | + [/^src\/components\/chat\//, 'chat UI'], |
| 47 | + [/^src\/components\/sidebar\//, 'sidebar'], |
| 48 | + [/^src\/components\/settings\//, 'settings'], |
| 49 | + [/^src\/shared\/view\/ui\//, 'UI primitives'], |
| 50 | + [/^src\/components\//, 'other components'], |
| 51 | + [/^src\//, 'other client'], |
| 52 | + [/^server\/modules\//, 'server modules'], |
| 53 | + [/^server\//, 'other server'], |
| 54 | +]; |
| 55 | + |
| 56 | +function areaOf(file) { |
| 57 | + for (const [pattern, name] of AREAS) if (pattern.test(file)) return name; |
| 58 | + return 'other'; |
| 59 | +} |
| 60 | + |
| 61 | +function git(args, cwd) { |
| 62 | + return execFileSync('git', args, { cwd, encoding: 'utf8', maxBuffer: 64 * 1024 * 1024 }); |
| 63 | +} |
| 64 | + |
| 65 | +function trackedCodeFiles(root) { |
| 66 | + return git(['ls-files'], root).trim().split('\n').filter((file) => CODE.test(file)); |
| 67 | +} |
| 68 | + |
| 69 | +function lineCount(path) { |
| 70 | + try { |
| 71 | + return readFileSync(path, 'utf8').split('\n').length; |
| 72 | + } catch { |
| 73 | + return 0; |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +/** |
| 78 | + * Fraction of a file that still looks like upstream's. |
| 79 | + * |
| 80 | + * Counts changed lines against twice the file's length, because a diff reports |
| 81 | + * a replaced line twice - once removed, once added. A file nobody touched |
| 82 | + * scores 1; a file rewritten scores near 0. |
| 83 | + */ |
| 84 | +function retainedFraction(mine, theirs) { |
| 85 | + const own = lineCount(mine); |
| 86 | + if (own === 0) return 0; |
| 87 | + let changed = 0; |
| 88 | + try { |
| 89 | + execFileSync('diff', [mine, theirs], { encoding: 'utf8', maxBuffer: 64 * 1024 * 1024 }); |
| 90 | + return 1; |
| 91 | + } catch (error) { |
| 92 | + const output = typeof error.stdout === 'string' ? error.stdout : ''; |
| 93 | + changed = output.split('\n').filter((line) => /^[<>]/u.test(line)).length; |
| 94 | + } |
| 95 | + return Math.max(0, 1 - changed / (own * 2)); |
| 96 | +} |
| 97 | + |
| 98 | +const args = process.argv.slice(2); |
| 99 | +const asJson = args.includes('--json'); |
| 100 | +const explicit = args.includes('--checkout') ? args[args.indexOf('--checkout') + 1] : undefined; |
| 101 | + |
| 102 | +let checkout = explicit; |
| 103 | +let temporary; |
| 104 | +if (!checkout) { |
| 105 | + temporary = mkdtempSync(join(tmpdir(), 'upstream-derivation-')); |
| 106 | + checkout = join(temporary, 'upstream'); |
| 107 | + execFileSync('git', ['clone', '--quiet', '--depth', '200', UPSTREAM, checkout], { stdio: 'inherit' }); |
| 108 | +} else if (!existsSync(checkout)) { |
| 109 | + console.error(`No checkout at ${checkout}.`); |
| 110 | + process.exit(1); |
| 111 | +} |
| 112 | + |
| 113 | +try { |
| 114 | + const upstream = new Set(trackedCodeFiles(checkout)); |
| 115 | + const areas = new Map(); |
| 116 | + const files = []; |
| 117 | + let ownTotal = 0; |
| 118 | + |
| 119 | + for (const file of trackedCodeFiles(REPOSITORY_ROOT)) { |
| 120 | + const absolute = join(REPOSITORY_ROOT, file); |
| 121 | + if (!existsSync(absolute) || !statSync(absolute).isFile()) continue; |
| 122 | + const own = lineCount(absolute); |
| 123 | + ownTotal += own; |
| 124 | + if (!upstream.has(file)) continue; |
| 125 | + |
| 126 | + const retained = retainedFraction(absolute, join(checkout, file)); |
| 127 | + // Below a tenth retained the file is a rewrite that happens to share a path. |
| 128 | + if (retained < 0.1) continue; |
| 129 | + const derived = Math.round(own * retained); |
| 130 | + if (derived === 0) continue; |
| 131 | + |
| 132 | + const area = areaOf(file); |
| 133 | + const entry = areas.get(area) ?? { area, files: 0, derived: 0 }; |
| 134 | + entry.files += 1; |
| 135 | + entry.derived += derived; |
| 136 | + areas.set(area, entry); |
| 137 | + files.push({ file, own, derived, retained: Number(retained.toFixed(2)) }); |
| 138 | + } |
| 139 | + |
| 140 | + const derivedTotal = [...areas.values()].reduce((sum, entry) => sum + entry.derived, 0); |
| 141 | + const ranked = [...areas.values()].sort((a, b) => b.derived - a.derived); |
| 142 | + |
| 143 | + if (asJson) { |
| 144 | + console.log(JSON.stringify({ |
| 145 | + derivedLines: derivedTotal, |
| 146 | + totalLines: ownTotal, |
| 147 | + percent: Number(((derivedTotal / ownTotal) * 100).toFixed(1)), |
| 148 | + areas: ranked, |
| 149 | + files: files.sort((a, b) => b.derived - a.derived), |
| 150 | + }, null, 2)); |
| 151 | + } else { |
| 152 | + console.log(`\nUpstream-derived code: ${derivedTotal.toLocaleString()} of ${ownTotal.toLocaleString()} lines` |
| 153 | + + ` (${((derivedTotal / ownTotal) * 100).toFixed(1)}%)\n`); |
| 154 | + for (const entry of ranked) { |
| 155 | + console.log(` ${String(entry.derived).padStart(6)} lines ${String(entry.files).padStart(3)} files ${entry.area}`); |
| 156 | + } |
| 157 | + console.log('\n Largest files:'); |
| 158 | + for (const entry of files.sort((a, b) => b.derived - a.derived).slice(0, 10)) { |
| 159 | + console.log(` ${String(entry.derived).padStart(6)} lines ${(entry.retained * 100).toFixed(0)}% retained ${entry.file}`); |
| 160 | + } |
| 161 | + console.log('\n Zero is the point at which this project can be licensed as it chooses.\n'); |
| 162 | + } |
| 163 | +} finally { |
| 164 | + if (temporary) rmSync(temporary, { recursive: true, force: true }); |
| 165 | +} |
0 commit comments