| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- /** 原账本缺失时保存当前工作树与本轮证据;明确不是完整业务/容器迁移。 */
- import fs from 'node:fs';
- import path from 'node:path';
- import assert from 'node:assert/strict';
- import { fileURLToPath } from 'node:url';
- import { createHash, randomUUID } from 'node:crypto';
- import { spawnSync } from 'node:child_process';
- const root = fileURLToPath(new URL('..', import.meta.url));
- const evidence = 'test-results/login-refinement-20261130';
- const ledger = 'test-results/real-account-20261130/state.json';
- const sha = bytes => createHash('sha256').update(bytes).digest('hex');
- const git = args => {
- const result = spawnSync('git', args, { cwd: root, encoding: 'utf8', windowsHide: true, maxBuffer: 32 * 1024 * 1024 });
- assert.equal(result.status, 0, result.stderr || 'Git只读检查失败');
- return result.stdout;
- };
- for (const file of ['docs/00-tracking/接续检查点.md.lock', 'test-results/real-account-20261130/runner.lock']) {
- assert.ok(!fs.existsSync(path.join(root, file)), '发现锁,须先核实,不能创建可能不一致的备份:' + file);
- }
- const head = git(['rev-parse', 'HEAD']).trim();
- const unstaged = git(['diff', '--binary']);
- const staged = git(['diff', '--cached', '--binary']);
- const names = new Set((git(['diff', '--name-only', '-z', 'HEAD']) + git(['ls-files', '--others', '--exclude-standard', '-z'])).split('\0').filter(Boolean));
- for (const name of ['AGENTS.md', 'CLAUDE.md', 'docs/00-tracking/active-task.json', 'docs/00-tracking/接续检查点.md', 'docs/00-tracking/操作追踪.md', ledger]) names.add(name);
- const walk = relative => {
- for (const entry of fs.readdirSync(path.join(root, relative), { withFileTypes: true })) {
- if (entry.name === 'mock-build') continue;
- const name = path.posix.join(relative, entry.name);
- if (entry.isDirectory()) walk(name);
- else names.add(name);
- }
- };
- walk(evidence);
- const id = new Date().toISOString().replace(/[:.]/g, '-') + '-' + randomUUID().slice(0, 8);
- const relativeDirectory = '.local-backup/login-refinement-20261130/' + id;
- const directory = path.join(root, relativeDirectory);
- fs.mkdirSync(path.join(directory, 'files'), { recursive: true });
- const manifest = { version: 1, createdAt: new Date().toISOString(), head, scope: 'changed_and_untracked_sources_with_current_login_evidence', fullBusinessRecovery: false, dockerVolumesIncluded: false, originalLedgerIncluded: false, completePrivateConfigurationIncluded: false, files: [], deletions: [], metadata: [] };
- for (const name of [...names].sort()) {
- const source = path.resolve(root, name);
- const relative = path.relative(root, source);
- assert.ok(relative && !relative.startsWith('..' + path.sep) && !path.isAbsolute(relative), '源路径不能越过项目根目录');
- if (!fs.existsSync(source)) { manifest.deletions.push(name); continue; }
- assert.ok(fs.lstatSync(source).isFile() && !fs.lstatSync(source).isSymbolicLink(), '仅备份项目内普通文件:' + name);
- const bytes = fs.readFileSync(source);
- const target = path.join(directory, 'files', name);
- fs.mkdirSync(path.dirname(target), { recursive: true });
- fs.writeFileSync(target, bytes, { flag: 'wx' });
- manifest.files.push({ path: name, bytes: bytes.length, sha256: sha(bytes) });
- }
- const metadata = { 'worktree.patch': unstaged, 'staged.patch': staged, 'git-status.txt': git(['status', '--short']), 'README.txt': '仅含当前变更/未跟踪源码、规则、接续记录和本轮登录验收证据,恢复还需Git基线。未包含原业务账本、完整私有配置、Docker数据卷或Mock构建输出;不能当作完整跨机器业务恢复。先验证manifest再人工合并,不自动覆盖现有工作区。\n' };
- for (const [name, text] of Object.entries(metadata)) {
- fs.writeFileSync(path.join(directory, name), text, { flag: 'wx' });
- manifest.metadata.push({ path: name, sha256: sha(Buffer.from(text)) });
- }
- assert.equal(git(['rev-parse', 'HEAD']).trim(), head);
- assert.equal(git(['diff', '--binary']), unstaged, '备份期间工作树变化');
- assert.equal(git(['diff', '--cached', '--binary']), staged, '备份期间暂存区变化');
- for (const item of manifest.files) {
- assert.equal(sha(fs.readFileSync(path.join(root, item.path))), item.sha256, '源文件变化:' + item.path);
- assert.equal(sha(fs.readFileSync(path.join(directory, 'files', item.path))), item.sha256, '备份回读不一致:' + item.path);
- }
- for (const item of manifest.metadata) assert.equal(sha(fs.readFileSync(path.join(directory, item.path))), item.sha256);
- const manifestBytes = Buffer.from(JSON.stringify(manifest, null, 2) + '\n');
- fs.writeFileSync(path.join(directory, 'manifest.json'), manifestBytes, { flag: 'wx' });
- const receipt = { directory: relativeDirectory, files: manifest.files.length, verified: true, manifestSha256: sha(fs.readFileSync(path.join(directory, 'manifest.json'))), scope: manifest.scope, fullBusinessRecovery: false, dockerVolumesIncluded: false };
- fs.writeFileSync(path.join(directory, 'snapshot-receipt.json'), JSON.stringify(receipt, null, 2) + '\n', { flag: 'wx' });
- fs.writeFileSync(path.join(root, evidence, 'snapshot-receipt.json'), JSON.stringify(receipt, null, 2) + '\n');
- console.log(JSON.stringify(receipt, null, 2));
|