Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
181 changes: 181 additions & 0 deletions packages/code/src/checkpoint-pack-fixture.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
import assert from 'node:assert/strict';
import { execFileSync, spawnSync } from 'node:child_process';
import { mkdir, mkdtemp, rm } from 'node:fs/promises';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { test } from 'node:test';

import { systemGitExecutable } from './linked-worktree-git-guard.js';

// Only synthetic repositories created here are ever passed to Git. This is
// evidence for the pack format, not a capture runner or a store isolation test.
for (const format of ['sha1', 'sha256']) {
test(`an explicit ${format} pack retains exactly the raw checkpoint closure offline`, async t => {
const executable = await systemGitExecutable();
const scratch = await mkdtemp(
join(tmpdir(), 'checkpoint-pack-fixture-')
);
t.after(() => rm(scratch, { recursive: true, force: true }));
const home = join(scratch, 'home');
const template = join(scratch, 'template');
await mkdir(home);
await mkdir(template);
const env = {
HOME: home,
XDG_CONFIG_HOME: home,
GIT_CONFIG_NOSYSTEM: '1',
GIT_CONFIG_GLOBAL: '/dev/null',
GIT_TERMINAL_PROMPT: '0',
GIT_ALLOW_PROTOCOL: '',
GIT_NO_LAZY_FETCH: '1',
GIT_AUTHOR_NAME: 'Fixture',
GIT_AUTHOR_EMAIL: 'fixture@example.invalid',
GIT_COMMITTER_NAME: 'Fixture',
GIT_COMMITTER_EMAIL: 'fixture@example.invalid',
GIT_AUTHOR_DATE: '2001-01-01T00:00:00Z',
GIT_COMMITTER_DATE: '2001-01-01T00:00:00Z',
};
const options = {
cwd: scratch,
env,
maxBuffer: 1024 * 1024,
timeout: 5000,
};
const git = (repo: string, args: string[], input?: string | Buffer) =>
execFileSync(executable, argv(repo, args), { ...options, input });
const argv = (repo: string, args: string[]) => [
'--no-replace-objects',
`--git-dir=${join(scratch, repo)}`,
...args,
];
const init = (repo: string) =>
git(repo, [
'init',
'--bare',
'--quiet',
`--template=${template}`,
`--object-format=${format}`,
]);
const source = 'source.git';
init(source);
const write = (args: string[], input: string | Buffer) =>
git(source, args, input).toString().trim();
const blob = (data: string | Buffer) =>
write(['hash-object', '-w', '--stdin'], data);
const tree = (...entries: string[]) =>
write(['mktree'], entries.join(''));
const commit = (root: string, label: string, ...parents: string[]) =>
write(
[
'commit-tree',
root,
...parents.flatMap(parent => ['-p', parent]),
],
`${label}\n`
);

const historical = Buffer.from([0, 255, 1]);
const deleted = blob(historical);
const original = tree(`100644 blob ${deleted}\tdeleted.bin\n`);
const base = commit(original, 'base');
const unrelated = commit(original, 'not in either parent chain');
const leftBlob = blob('left\n');
const rightBlob = blob('right\n');
const leftTree = tree(`100644 blob ${leftBlob}\tleft\n`);
const rightTree = tree(`100644 blob ${rightBlob}\tright\n`);
const left = commit(leftTree, 'left', base);
const right = commit(rightTree, 'right', base);
const merged = tree(
`040000 tree ${leftTree}\tnested\n`,
`100644 blob ${rightBlob}\tright\n`,
`160000 commit ${unrelated}\tsubmodule\n`
);
const head = commit(merged, 'merge', left, right);
const expected = [
base,
left,
right,
head,
original,
leftTree,
rightTree,
merged,
deleted,
leftBlob,
rightBlob,
].sort();
const closure = git(source, [
'rev-list',
'--objects',
'--no-object-names',
head,
])
.toString()
.trim()
.split('\n')
.sort();
assert.deepEqual(closure, expected);
assert.equal(git(source, ['for-each-ref']).length, 0);

const pack = (objects: string[]) =>
git(
source,
[
'pack-objects',
'--stdout',
'--window=0',
'--no-reuse-object',
'--no-reuse-delta',
],
`${objects.join('\n')}\n`
);
const complete = pack(closure);
const missingParent = pack(closure.filter(oid => oid !== base));
await rm(join(scratch, source), { recursive: true });

const imported = 'imported.git';
init(imported);
git(imported, ['index-pack', '--stdin', '--strict'], complete);
const inventory = git(imported, [
'cat-file',
'--batch-all-objects',
'--batch-check=%(objectname)',
])
.toString()
.trim()
.split('\n')
.sort();
assert.deepEqual(inventory, expected);
git(imported, ['fsck', '--strict', '--full', head]);
assert.equal(
git(imported, ['show', '-s', '--format=%P', head])
.toString()
.trim(),
`${left} ${right}`
);
assert.deepEqual(
git(imported, ['cat-file', 'blob', deleted]),
historical
);
assert.equal(git(imported, ['for-each-ref']).length, 0);

const incomplete = 'incomplete.git';
init(incomplete);
const rejected = spawnSync(
executable,
argv(incomplete, ['index-pack', '--stdin', '--strict']),
{
...options,
input: missingParent,
encoding: 'utf8',
}
);
assert.ifError(rejected.error);
assert.equal(rejected.signal, null);
assert.notEqual(rejected.status, 0);
assert.match(
rejected.stderr,
/did not receive expected object|missing/i
);
});
}
Loading