#!/usr/bin/env node import { execFileSync } from "node:child_process"; import fs from "node:fs"; import path from "node:path"; import { fileURLToPath } from "node:url"; const SCRIPT_DIR = path.dirname(fileURLToPath(import.meta.url)); const REPO_ROOT = path.resolve(SCRIPT_DIR, "../.."); export const CODE_IMPORT_BASELINE = "1631a5d84e079f71ef377a7ca4f59bfad4d770b4"; const CODE_EXTENSIONS = new Set([ ".astro", ".bash", ".cjs", ".css", ".cts", ".dbml", ".gql", ".graphql", ".hcl", ".htm", ".html", ".java", ".js", ".jsx", ".kt", ".kts", ".less", ".mjs", ".mts", ".prisma", ".py", ".rb", ".rs", ".sass", ".scss", ".sh", ".sql", ".svelte", ".tf", ".ts", ".tsx", ".vue", ".zsh", ]); const CODE_FILENAMES = new Set(["Dockerfile", "Makefile"]); const ROOT_EXCLUDED_SEGMENTS = new Set([".planning", "build", "data", "docs", "guides"]); const EXCLUDED_SEGMENTS = new Set([ ".cache", ".next", ".react-router", ".tc", ".turbo", "__generated__", "coverage", "dist", "generated", "node_modules", "playwright-report", "test-results", "vendor", ]); const TEST_SEGMENTS = new Set(["__fixtures__", "__tests__", "e2e", "fixtures", "playwright", "test", "test-setup", "test-utils", "tests", "visual-baseline"]); const EXACT_EXCLUSIONS = new Set([ "apps/web/app/components/ui/icons/vendor.tsx", "packages/api-client-ts/openapi.json", "prisma/rls/tenant-policy-contract-isolation.sql", ]); const DEFAULTS = Object.freeze({ baseline: CODE_IMPORT_BASELINE, label: null, outputDir: "docs/metrics", ref: "origin/main", }); function printHelp() { console.log(`Usage: node scripts/metrics/loc-history.mjs [options] Options: --ref Mainline to inspect (default: ${DEFAULTS.ref}) --label Ref label rendered in outputs (default: value of --ref) --baseline Fork/code-import baseline (default: ${DEFAULTS.baseline}) --output-dir Output directory (default: ${DEFAULTS.outputDir}) --help Show this help Outputs: loc-history.csv Mainline snapshots and LOC values loc-history.svg Standalone chart LOC means non-empty physical lines in source-like files. Documentation, lockfiles, configuration/data formats, dependencies, build output, known generated source paths, and binary assets are excluded. Tests and database migrations are reported separately; committed migrations (including generated migration SQL) and packages/ui-subframe runtime source remain included.`); } export function parseArgs(argv) { const options = { ...DEFAULTS }; for (let index = 0; index < argv.length; index += 1) { const argument = argv[index]; if (argument === "--help") { printHelp(); process.exit(0); } if (argument === "--ref" || argument === "--label" || argument === "--baseline" || argument === "--output-dir") { const value = argv[index + 1]; if (!value || value.startsWith("--")) { throw new Error(`Missing value for ${argument}`); } if (argument === "--ref") options.ref = value; if (argument === "--label") options.label = value; if (argument === "--baseline") options.baseline = value; if (argument === "--output-dir") options.outputDir = value; index += 1; continue; } throw new Error(`Unknown argument: ${argument}`); } return { ...options, label: options.label ?? options.ref }; } function git(args, options = {}) { return execFileSync("git", args, { cwd: options.repoRoot ?? REPO_ROOT, encoding: options.encoding === "buffer" ? undefined : (options.encoding ?? "utf8"), input: options.input, maxBuffer: 512 * 1024 * 1024, stdio: options.stdio, }); } export function collectSnapshots(ref, { baseline = CODE_IMPORT_BASELINE, repoRoot = REPO_ROOT, runGit = git } = {}) { const resolvedRef = runGit(["rev-parse", "--verify", `${ref}^{commit}`], { repoRoot }).trim(); const resolvedBaseline = runGit(["rev-parse", "--verify", `${baseline}^{commit}`], { repoRoot }).trim(); try { runGit(["merge-base", "--is-ancestor", resolvedBaseline, resolvedRef], { repoRoot, stdio: "ignore" }); } catch { throw new Error(`LOC baseline ${resolvedBaseline} is not an ancestor of ${ref}`); } const log = runGit(["log", "--first-parent", "--reverse", "--date=short", "--format=%H%x09%P%x09%cs%x09%cI%x09%s", resolvedRef], { repoRoot }).trim(); if (!log) throw new Error(`No commits found for ${ref}`); const commits = log.split("\n").map((line, order) => { const [sha, rawParents, date, timestamp, ...subjectParts] = line.split("\t"); return { sha, parents: rawParents ? rawParents.split(" ") : [], date, order, timestamp, subject: subjectParts.join("\t"), }; }); const baselineCommit = commits.find((commit) => commit.sha === resolvedBaseline); if (!baselineCommit) { throw new Error(`LOC baseline ${resolvedBaseline} is not on the first-parent history of ${ref}`); } const baselineIndex = commits.indexOf(baselineCommit); const lastCommitPerDay = new Map(); for (const commit of commits.slice(baselineIndex)) lastCommitPerDay.set(commit.date, commit); const anchorBySha = new Map([[baselineCommit.sha, "code-import"]]); const snapshots = [baselineCommit, ...lastCommitPerDay.values()]; const uniqueSnapshots = new Map(snapshots.map((snapshot) => [snapshot.sha, { ...snapshot, anchor: anchorBySha.get(snapshot.sha) ?? "" }])); return [...uniqueSnapshots.values()].sort((left, right) => left.order - right.order); } export function classifyPath(filePath) { const normalizedPath = filePath.replaceAll("\\", "/"); const segments = normalizedPath.split("/"); const filename = segments.at(-1) ?? ""; const extension = path.posix.extname(filename).toLowerCase(); if (EXACT_EXCLUSIONS.has(normalizedPath)) return null; if (ROOT_EXCLUDED_SEGMENTS.has(segments[0])) return null; if (segments.some((segment) => EXCLUDED_SEGMENTS.has(segment))) return null; if (/\.generated\.[^.]+$/i.test(filename)) return null; if (/\.min\.(?:css|js)$/i.test(filename) || filename.endsWith(".map")) return null; const isCodeFile = CODE_EXTENSIONS.has(extension) || CODE_FILENAMES.has(filename) || filename.toLowerCase().endsWith(".dockerfile"); if (!isCodeFile) return null; if (normalizedPath.startsWith("prisma/migrations/") && extension === ".sql") { return "migration"; } const isTest = segments.some((segment) => TEST_SEGMENTS.has(segment.toLowerCase())) || /\.(?:test|spec|stories|visual-fixture)\.[^.]+$/i.test(filename) || /^__test(?:[._-]|$)/i.test(filename) || /^(?:playwright(?:-[^.]+)?\.config|test-(?:setup|utils?))\.[^.]+$/i.test(filename); return isTest ? "test" : "product"; } function listSnapshotFiles(snapshot, repoRoot) { const tree = git(["ls-tree", "-r", "-z", "--full-tree", snapshot.sha], { encoding: "buffer", repoRoot, }); const entries = tree.toString("utf8").split("\0").filter(Boolean); const files = []; for (const entry of entries) { const tabIndex = entry.indexOf("\t"); if (tabIndex === -1) continue; const metadata = entry.slice(0, tabIndex).split(" "); const filePath = entry.slice(tabIndex + 1); const [, objectType, blob] = metadata; if (objectType !== "blob") continue; const category = classifyPath(filePath); if (category) files.push({ blob, category }); } return files; } function findByte(buffer, byte, fromIndex) { for (let index = fromIndex; index < buffer.length; index += 1) { if (buffer[index] === byte) return index; } return -1; } export function countNonEmptyLines(buffer) { if (buffer.includes(0)) return 0; const content = buffer.toString("utf8"); if (!content) return 0; let count = 0; for (const line of content.split(/\r?\n/)) { if (/\S/.test(line)) count += 1; } return count; } function countBlobs(blobIds, repoRoot) { const counts = new Map(); const uniqueBlobIds = [...new Set(blobIds)].sort(); const batchSize = 250; for (let start = 0; start < uniqueBlobIds.length; start += batchSize) { const batch = uniqueBlobIds.slice(start, start + batchSize); const output = git(["cat-file", "--batch"], { encoding: "buffer", input: `${batch.join("\n")}\n`, repoRoot, }); let offset = 0; for (const requestedBlob of batch) { const headerEnd = findByte(output, 10, offset); if (headerEnd === -1) throw new Error(`Malformed cat-file response for ${requestedBlob}`); const header = output.subarray(offset, headerEnd).toString("utf8"); const [resolvedBlob, objectType, rawSize] = header.split(" "); if (objectType !== "blob") throw new Error(`Expected blob for ${requestedBlob}, got: ${header}`); const size = Number.parseInt(rawSize, 10); const contentStart = headerEnd + 1; const contentEnd = contentStart + size; counts.set(resolvedBlob, countNonEmptyLines(output.subarray(contentStart, contentEnd))); offset = contentEnd + 1; } } return counts; } function tallySnapshots(snapshots, repoRoot) { const filesBySnapshot = new Map(); const allBlobs = []; for (const snapshot of snapshots) { const files = listSnapshotFiles(snapshot, repoRoot); filesBySnapshot.set(snapshot.sha, files); for (const file of files) allBlobs.push(file.blob); } const lineCounts = countBlobs(allBlobs, repoRoot); return snapshots.map((snapshot) => { const loc = { product: 0, test: 0, migration: 0 }; const files = { product: 0, test: 0, migration: 0 }; for (const file of filesBySnapshot.get(snapshot.sha) ?? []) { loc[file.category] += lineCounts.get(file.blob) ?? 0; files[file.category] += 1; } return { ...snapshot, loc, files, total: loc.product + loc.test + loc.migration, }; }); } function csvCell(value) { const stringValue = String(value); return /[",\n]/.test(stringValue) ? `"${stringValue.replaceAll('"', '""')}"` : stringValue; } export function renderCsv(rows, ref) { const metadata = [ `# ref=${ref}`, `# resolved_tip=${rows.at(-1).sha}`, "# metric=non-empty physical source lines", "# sampling=fork/code-import baseline and last first-parent commit on each active calendar day", "# exclusions=docs, planning, configuration/data formats, dependencies, build output, known generated source paths, binary assets, lockfiles", "# included=packages/ui-subframe runtime source; committed migration SQL (reported separately)", ]; const header = [ "date", "timestamp", "commit", "anchor", "subject", "non_test_loc", "test_loc", "migration_loc", "total_loc", "non_test_files", "test_files", "migration_files", ]; const body = rows.map((row) => [ row.date, row.timestamp, row.sha, row.anchor, row.subject, row.loc.product, row.loc.test, row.loc.migration, row.total, row.files.product, row.files.test, row.files.migration, ] .map(csvCell) .join(",") ); return `${[...metadata, header.join(","), ...body].join("\n")}\n`; } function escapeXml(value) { return String(value).replaceAll("&", "&").replaceAll("<", "<").replaceAll(">", ">").replaceAll('"', """).replaceAll("'", "'"); } function formatNumber(value) { return new Intl.NumberFormat("de-DE").format(Math.round(value)); } function formatCompact(value) { return new Intl.NumberFormat("de-DE", { maximumFractionDigits: value >= 100_000 ? 0 : 1, notation: "compact", }).format(value); } function formatDate(timestamp, options = {}) { return new Intl.DateTimeFormat("de-DE", { day: "2-digit", month: "2-digit", timeZone: "Europe/Berlin", ...options, }).format(new Date(timestamp)); } function niceAxisMaximum(value) { if (value <= 0) return 1; const roughStep = value / 5; const magnitude = 10 ** Math.floor(Math.log10(roughStep)); const normalized = roughStep / magnitude; const niceNormalized = normalized <= 1 ? 1 : normalized <= 2 ? 2 : normalized <= 2.5 ? 2.5 : normalized <= 5 ? 5 : 10; const step = niceNormalized * magnitude; return Math.ceil(value / step) * step; } export function renderSvg(rows, ref) { const width = 1600; const height = 900; const plot = { left: 126, top: 282, right: 1518, bottom: 774 }; const plotWidth = plot.right - plot.left; const plotHeight = plot.bottom - plot.top; const minTime = Math.min(...rows.map((row) => Date.parse(row.timestamp))); const maxTime = Math.max(...rows.map((row) => Date.parse(row.timestamp))); const timeRange = Math.max(1, maxTime - minTime); const maxLoc = niceAxisMaximum(Math.max(...rows.map((row) => row.total)) * 1.04); const x = (row) => plot.left + ((Date.parse(row.timestamp) - minTime) / timeRange) * plotWidth; const y = (value) => plot.bottom - (value / maxLoc) * plotHeight; const point = (xValue, yValue) => `${xValue.toFixed(1)},${yValue.toFixed(1)}`; const stackedValue = (row, category) => { if (category === "product") return row.loc.product; if (category === "test") return row.loc.product + row.loc.test; return row.total; }; const lowerValue = (row, category) => { if (category === "product") return 0; if (category === "test") return row.loc.product; return row.loc.product + row.loc.test; }; const areaPath = (category) => { const upper = rows.map((row) => point(x(row), y(stackedValue(row, category)))); const lower = [...rows].reverse().map((row) => point(x(row), y(lowerValue(row, category)))); return `M ${upper.join(" L ")} L ${lower.join(" L ")} Z`; }; const totalPath = rows.map((row) => point(x(row), y(row.total))).join(" L "); const first = rows[0]; const latest = rows.at(-1); const colors = { product: "#0F766E", test: "#F59E0B", migration: "#8B5CF6", line: "#0F172A", }; const yTicks = 5; const xTicks = 7; const growthBaseline = rows.find((row) => row.anchor === "code-import") ?? first; const growth = latest.total - growthBaseline.total; const growthPercent = growthBaseline.total === 0 ? null : (growth / growthBaseline.total) * 100; const cards = [ { label: "Gesamt", value: formatNumber(latest.total), color: colors.line }, { label: "App + Tooling", value: formatNumber(latest.loc.product), color: colors.product }, { label: "Tests", value: formatNumber(latest.loc.test), color: colors.test }, { label: "Seit Code-Import", value: `${growth >= 0 ? "+" : ""}${formatCompact(growth)}${growthPercent === null ? "" : ` · ${growthPercent >= 0 ? "+" : ""}${growthPercent.toFixed(0)} %`}`, color: growth >= 0 ? colors.product : "#DC2626", }, ]; const svg = []; svg.push(``); svg.push( `` ); svg.push(`HiveGrid LOC-Verlauf auf ${escapeXml(ref)}`); svg.push( `Mainline-Snapshots mit Nicht-Test-Code, Tests und Datenbankmigrationen. Dokumentation, Konfigurations- und Datendateien, Abhängigkeiten, Build-Ausgaben sowie bekannte generierte Quellpfade sind ausgeschlossen. Committete Migrationen werden einschließlich generierter Migrationen separat gezählt.` ); svg.push(``); svg.push( `HiveGrid · LOC-Verlauf` ); svg.push( `${escapeXml(ref)} @ ${latest.sha.slice(0, 9)} · letzter Mainline-Commit je aktivem Tag · nicht-leere Quellcodezeilen` ); cards.forEach((card, index) => { const cardX = 78 + index * 366; svg.push(``); svg.push(``); svg.push( `${escapeXml(card.label.toUpperCase())}` ); svg.push( `${escapeXml(card.value)}` ); }); for (let tick = 0; tick <= yTicks; tick += 1) { const value = (maxLoc / yTicks) * tick; const tickY = y(value); svg.push(``); svg.push( `${escapeXml(formatCompact(value))}` ); } for (let tick = 0; tick < xTicks; tick += 1) { const ratio = tick / (xTicks - 1); const timestamp = minTime + ratio * timeRange; const tickX = plot.left + ratio * plotWidth; svg.push(``); svg.push( `${escapeXml(formatDate(timestamp))}` ); } svg.push(``); svg.push(``); svg.push(``); svg.push(``); const legend = [ ["App + Tooling", colors.product], ["Tests", colors.test], ["DB-Migrationen", colors.migration], ["Gesamt", colors.line], ]; legend.forEach(([label, color], index) => { const legendX = plot.left + index * 220; svg.push(``); svg.push( `${escapeXml(label)}` ); }); svg.push( `Bekannte Generated-Pfade, Docs, Konfig/Datendateien, Assets und Build-Ausgaben ausgeschlossen` ); svg.push(``); return `${svg.join("\n")}\n`; } function main() { const options = parseArgs(process.argv.slice(2)); const outputDir = path.resolve(REPO_ROOT, options.outputDir); const snapshots = collectSnapshots(options.ref, { baseline: options.baseline, repoRoot: REPO_ROOT }); const rows = tallySnapshots(snapshots, REPO_ROOT); fs.mkdirSync(outputDir, { recursive: true }); fs.writeFileSync(path.join(outputDir, "loc-history.csv"), renderCsv(rows, options.label)); fs.writeFileSync(path.join(outputDir, "loc-history.svg"), renderSvg(rows, options.label)); const first = rows[0]; const latest = rows.at(-1); console.log(`LOC history: ${rows.length} snapshots from ${first.date} to ${latest.date}`); console.log(`Latest ${options.ref} (${latest.sha.slice(0, 9)}): ${formatNumber(latest.total)} LOC`); console.log(` App + tooling: ${formatNumber(latest.loc.product)}`); console.log(` Tests: ${formatNumber(latest.loc.test)}`); console.log(` DB migrations: ${formatNumber(latest.loc.migration)}`); console.log(`Wrote ${path.relative(REPO_ROOT, outputDir)}/loc-history.{csv,svg}`); } const invokedDirectly = process.argv[1] && path.resolve(process.argv[1]) === fileURLToPath(import.meta.url); if (invokedDirectly) { try { main(); } catch (error) { console.error(error instanceof Error ? error.message : error); process.exit(1); } }