/** * 在 .tmp/ 底下開一個真的 git repo。 * git 操作不做 mock:在臨時 repo 上跑真的 git 比假的 git 可信,成本也低。 */ import { execFileSync } from 'node:child_process'; import { mkdtempSync, mkdirSync, writeFileSync, rmSync } from 'node:fs'; import { join } from 'node:path'; import { tmpRoot } from './run-script.js'; /** * @returns {{dir: string, cleanup: Function}} dir 為已有一顆 commit 的 git repo */ export function makeTempRepo() { mkdirSync(tmpRoot, { recursive: true }); const dir = mkdtempSync(join(tmpRoot, 'repo-')); const git = (...args) => execFileSync('git', args, { cwd: dir, encoding: 'utf8', env: { ...process.env, GIT_CONFIG_GLOBAL: '/dev/null', GIT_CONFIG_SYSTEM: '/dev/null' }, }); git('init', '-q', '-b', 'master'); git('config', 'user.name', 'tester'); git('config', 'user.email', 'tester@example.com'); writeFileSync(join(dir, 'README.md'), '# temp\n'); git('add', '-A'); git('commit', '-qm', 'initial'); return { dir, cleanup: () => rmSync(dir, { recursive: true, force: true }) }; }