#!/usr/bin/env node /** Differential check of real commits: the incumbent TypeScript wrapper (run from source) and the native wrapper commit the same change in two identical disposable repositories, and must agree on the exit code, the policy events on standard error, the commit that results, the index, and the worktree. Object names, timestamps and fixture paths differ between two repositories, so they are replaced by placeholders before the texts are compared; the trees compared are by content, so equal content has equal blob names. Usage: node bin/differential-commit.mjs [--native ] [--case ] */ // The bin scripts sit outside the package tsconfig's include list, so type-aware lint checks them in an inferred // program that loads no ambient Node types; this reference loads them, so `process` and `node:` imports stay typed // in this file on its own instead of depending on a sibling module that carries the same reference. /// import { access, chmod, mkdir, mkdtemp, readFile, rm, symlink, writeFile, } from 'node:fs/promises'; import { tmpdir } from 'node:os'; import { dirname, join, resolve, } from 'node:path'; import { parseArgs } from 'node:util'; import { DifferentialError, hermeticEnvironment, observe, REAL_GIT, setupGit, writeFiles, } from './differential-config-process.mjs'; /** The package directory, `package/git-policy/cli`. */ const packageDirectory = resolve( import.meta.dirname, '..', ); /** One commit case: a baseline, the changes made before the commit, an optional hook, and the wrapped commands. @typedef {{ name: string, baseline: Record, changes: Record, stage?: readonly string[], hook?: string, race?: { slow: readonly string[], fast: readonly string[], fastFiles: Record }, commands: readonly (readonly string[])[], paths: readonly string[], }} Case */ /** The cases: the commit shapes the plan names. */ const cases = /** @type {readonly Case[]} */ ([ { name: 'final-newline-correction', baseline: { 'a.txt': 'one\n' }, changes: { 'a.txt': 'one\ntwo' }, commands: [ [ 'commit', '--message=second', 'a.txt', ], ], paths: ['a.txt'], }, { name: 'pre-commit-restage', baseline: { 'a.txt': 'one\n' }, changes: { 'a.txt': 'one\ntwo\n' }, hook: '#!/bin/sh\nprintf "hooked\\n" >> a.txt\n/usr/bin/git add -- a.txt\n', commands: [ [ 'commit', '--message=second', 'a.txt', ], ], paths: ['a.txt'], }, { name: 'no-change-normalization', baseline: { 'a.txt': 'one\n' }, changes: { 'a.txt': 'one' }, commands: [ [ 'commit', '--message=second', 'a.txt', ], ], paths: ['a.txt'], }, { name: 'amend', baseline: { 'a.txt': 'one\n' }, changes: { 'a.txt': 'one\ntwo\n' }, commands: [ [ 'commit', '--amend', '--message=amended', 'a.txt', ], ], paths: ['a.txt'], }, { name: 'all-tracked-changes', baseline: { 'a.txt': 'one\n', 'b.txt': 'b\n', }, changes: { 'a.txt': 'one\ntwo\n', 'b.txt': 'b\nmore\n', }, commands: [ [ 'commit', '-a', '--no-only', '--message=all', ], ], paths: [ 'a.txt', 'b.txt', ], }, { name: 'include-flag', baseline: { 'a.txt': 'one\n', 'b.txt': 'b\n', }, changes: { 'a.txt': 'one\ntwo', 'b.txt': 'b\nmore\n', }, stage: ['b.txt'], commands: [ [ 'commit', '--include', '--message=include', 'a.txt', ], ], paths: [ 'a.txt', 'b.txt', ], }, { name: 'pathspec-from-file', baseline: { 'a.txt': 'one\n', 'b.txt': 'b\n', 'list.txt': 'a.txt\n', }, changes: { 'a.txt': 'one\ntwo\n', 'b.txt': 'b\nmore\n', }, commands: [ [ 'commit', '--message=listed', '--pathspec-from-file=list.txt', ], ], paths: [ 'a.txt', 'b.txt', ], }, { name: 'allow-empty', baseline: { 'a.txt': 'one\n' }, changes: {}, commands: [ [ 'commit', '--allow-empty', '--no-only', '--message=empty', ], ], paths: ['a.txt'], }, { name: 'concurrent-commit-replay', baseline: { 'a.txt': 'one\n', 'b.txt': 'b\n', }, changes: { 'a.txt': 'one\ntwo\n' }, race: { slow: [ 'commit', '--message=slow', 'a.txt', ], fast: [ 'commit', '--message=fast', 'b.txt', ], fastFiles: { 'b.txt': 'b\nfast\n' }, }, commands: [], paths: [ 'a.txt', 'b.txt', ], }, { name: 'blocked-by-commit-only', baseline: { 'a.txt': 'one\n' }, changes: { 'a.txt': 'one\ntwo\n' }, stage: ['a.txt'], commands: [ [ 'commit', '--message=second', ], ], paths: ['a.txt'], }, ]); /** A wrapper under comparison. @typedef {{ name: string, run: (args: readonly string[], extraEnvironment?: Record) => ReturnType, }} Runner */ /** One disposable repository with a wrapper ready to run in it. @typedef {{ root: string, home: string, run: Runner['run'] }} Fixture */ /** Build a repository with the case's baseline commit, hook, staged paths and uncommitted changes. @param {{ context: string, name: string, kase: Case, wrapper: 'incumbent' | 'native', native: string }} request - parent directory, label, case, wrapper kind and the native executable @returns {Promise} the ready fixture */ async function createFixture({ context, name, kase, wrapper, native, }) { const root = join( context, name, ); const home = join( context, `${name}-home`, ); await mkdir( root, { recursive: true }, ); await mkdir(home); await setupGit({ root, home, args: [ 'init', '--quiet', '--initial-branch=main', ], }); await writeFiles({ root, files: kase.baseline, }); await setupGit({ root, home, args: [ 'add', '--all', ], }); await setupGit({ root, home, args: [ 'commit', '--quiet', '--message=baseline', ], }); await writeFiles({ root, files: kase.changes, }); if (kase.stage !== undefined) await setupGit({ root, home, args: [ 'add', '--', ...kase.stage, ], }); if (kase.hook !== undefined) { const hook = join( root, '.git/hooks/pre-commit', ); await mkdir( dirname(hook), { recursive: true }, ); await writeFile( hook, kase.hook, ); await chmod( hook, 0o755, ); } const bin = join( context, `${name}-bin`, ); await mkdir(bin); await symlink( native, join( bin, 'git', ), ); const path = wrapper === 'native' ? `${bin}:/usr/bin:/bin` : `/usr/bin:/bin:${dirname(process.execPath)}`; const environment = hermeticEnvironment({ home, path, }); return { root, home, run: function runWrapper(args, extra = {}) { return wrapper === 'native' ? observe({ command: join( bin, 'git', ), args, cwd: root, env: { ...environment, ...extra, }, }) : observe({ command: process.execPath, args: [ join( packageDirectory, 'src/index.ts', ), ...args, ], cwd: root, env: { ...environment, ...extra, }, }); }, }; } /** Replace what two repositories legitimately differ in: object names, timestamps and the fixture paths. @param {string} text - captured text @param {readonly string[]} roots - fixture roots and homes to mask @returns {string} comparable text */ function normalize( text, roots, ) { const masked = roots.reduce( function maskRoot( current, root, ) { return current.replaceAll( root, '', ); }, text, ); // Diagnostic log lines (`[warn] ...`) are not part of the wrapper's JSONL contract and word their tags differently. return masked .split('\n') .filter(function isNotDiagnostic(line) { return !line.startsWith('[warn]') && !line.startsWith('[debug]'); }) .join('\n') .replaceAll( /\b[0-9a-f]{40}\b/gu, '', ) .replaceAll( /\b[0-9a-f]{7,12}\b/gu, '', ) .replaceAll( /\d{4}-\d\d-\d\dT[\d:.]+Z/gu, '