|
| 1 | +import { execFileSync } from 'node:child_process'; |
| 2 | +import { mkdtempSync, mkdirSync, readFileSync, readdirSync, rmSync, lstatSync, symlinkSync, writeFileSync } from 'node:fs'; |
| 3 | +import { tmpdir } from 'node:os'; |
| 4 | +import { join } from 'node:path'; |
| 5 | +import { afterEach, describe, expect, it } from 'vitest'; |
| 6 | +import { createTaskClone } from '../git/clone.ts'; |
| 7 | + |
| 8 | +const roots: string[] = []; |
| 9 | +const git = (cwd: string, ...args: string[]) => execFileSync('git', ['-c', 'core.hooksPath=/dev/null', ...args], |
| 10 | + { cwd, encoding: 'utf8', stdio: ['ignore', 'pipe', 'pipe'] }).trim(); |
| 11 | +function fixture() { |
| 12 | + const root = mkdtempSync(join(tmpdir(), 'clone-test-')); roots.push(root); |
| 13 | + const source = join(root, 'source'), parent = join(root, 'tasks'); |
| 14 | + mkdirSync(source); mkdirSync(parent); |
| 15 | + git(source, 'init'); git(source, 'config', 'user.name', 'Test'); git(source, 'config', 'user.email', 'test@example.com'); |
| 16 | + writeFileSync(join(source, 'file.txt'), 'trusted\n'); git(source, 'add', '.'); git(source, 'commit', '-m', 'baseline'); |
| 17 | + return { source, parent, head: git(source, 'rev-parse', 'HEAD'), taskId: 'task-1' }; |
| 18 | +} |
| 19 | +afterEach(() => { for (const root of roots.splice(0)) rmSync(root, { recursive: true, force: true }); }); |
| 20 | +describe('isolated staging clone', () => { |
| 21 | + it('copies objects, ignores dirty source changes, and has no origin or shared metadata', () => { |
| 22 | + const input = fixture(); |
| 23 | + writeFileSync(join(input.source, 'file.txt'), 'uncommitted'); |
| 24 | + const clone = createTaskClone(input); |
| 25 | + expect(readFileSync(join(clone.directory, 'file.txt'), 'utf8')).toBe('trusted\n'); |
| 26 | + expect(lstatSync(join(clone.directory, '.git')).isDirectory()).toBe(true); |
| 27 | + expect(git(clone.directory, 'status', '--porcelain')).toBe(''); |
| 28 | + expect(git(clone.directory, 'remote')).toBe(''); |
| 29 | + const hash = git(input.source, 'rev-parse', 'HEAD:file.txt'); |
| 30 | + const path = join('objects', hash.slice(0, 2), hash.slice(2)); |
| 31 | + const sourceObject = join(input.source, '.git', path), cloneObject = join(clone.directory, '.git', path); |
| 32 | + const before = readFileSync(sourceObject); |
| 33 | + expect(lstatSync(cloneObject).nlink).toBe(1); |
| 34 | + expect(lstatSync(cloneObject).ino).not.toBe(lstatSync(sourceObject).ino); |
| 35 | + writeFileSync(cloneObject, 'corrupted disposable task object'); |
| 36 | + expect(readFileSync(sourceObject)).toEqual(before); |
| 37 | + expect(git(input.source, 'cat-file', '-p', hash)).toBe('trusted'); |
| 38 | + }); |
| 39 | + it('supports linked-worktree sources but produces standalone metadata', () => { |
| 40 | + const input = fixture(), linked = join(input.parent, 'linked'); |
| 41 | + git(input.source, 'worktree', 'add', '--detach', linked, input.head); |
| 42 | + const clone = createTaskClone({ ...input, source: linked }); |
| 43 | + expect(lstatSync(join(clone.directory, '.git')).isDirectory()).toBe(true); |
| 44 | + expect(git(clone.directory, 'rev-parse', 'HEAD')).toBe(input.head); |
| 45 | + }); |
| 46 | + it.each(['objects/info/alternates', 'objects/info/http-alternates', 'info/grafts', 'shallow'])('rejects %s before allocating a clone', name => { |
| 47 | + const input = fixture(); |
| 48 | + writeFileSync(join(input.source, '.git', name), ''); |
| 49 | + expect(() => createTaskClone(input)).toThrow('Unsupported Git storage'); |
| 50 | + expect(readdirSync(input.parent)).toEqual([]); |
| 51 | + }); |
| 52 | + it('rejects object symlinks and replacements', () => { |
| 53 | + const input = fixture(); |
| 54 | + symlinkSync('/tmp', join(input.source, '.git/objects/linked')); |
| 55 | + expect(() => createTaskClone(input)).toThrow('object entry'); |
| 56 | + rmSync(join(input.source, '.git/objects/linked')); |
| 57 | + git(input.source, 'update-ref', `refs/replace/${input.head}`, input.head); |
| 58 | + expect(() => createTaskClone(input)).toThrow('Replacement'); |
| 59 | + }); |
| 60 | + it('rejects nested storage, including paths through symlinks', () => { |
| 61 | + const input = fixture(), nested = join(input.source, 'tasks'), alias = join(input.parent, 'alias'); |
| 62 | + mkdirSync(nested); symlinkSync(nested, alias); |
| 63 | + expect(() => createTaskClone({ ...input, parent: alias })).toThrow('outside'); |
| 64 | + }); |
| 65 | + it('requires a full existing commit and finite time budget', () => { |
| 66 | + const input = fixture(); |
| 67 | + expect(() => createTaskClone({ ...input, head: 'HEAD' })).toThrow('full committed'); |
| 68 | + expect(() => createTaskClone({ ...input, head: 'f'.repeat(40) })).toThrow(); |
| 69 | + expect(() => createTaskClone({ ...input, timeoutMs: Infinity })).toThrow('deadline'); |
| 70 | + expect(readdirSync(input.parent)).toEqual([]); |
| 71 | + }); |
| 72 | + it('does not inherit Git directory, index, configuration or object overrides', () => { |
| 73 | + const input = fixture(); |
| 74 | + const keys = ['GIT_DIR', 'GIT_INDEX_FILE', 'GIT_CONFIG_COUNT', 'GIT_CONFIG_KEY_0', 'GIT_CONFIG_VALUE_0']; |
| 75 | + const old = keys.map(key => process.env[key]); |
| 76 | + try { |
| 77 | + Object.assign(process.env, { GIT_DIR: '/missing', GIT_INDEX_FILE: '/missing', GIT_CONFIG_COUNT: '1', |
| 78 | + GIT_CONFIG_KEY_0: 'core.bare', GIT_CONFIG_VALUE_0: 'true' }); |
| 79 | + const clone = createTaskClone(input); |
| 80 | + expect(readFileSync(join(clone.directory, 'file.txt'), 'utf8')).toBe('trusted\n'); |
| 81 | + } finally { |
| 82 | + keys.forEach((key, i) => { if (old[i] === undefined) delete process.env[key]; else process.env[key] = old[i]; }); |
| 83 | + } |
| 84 | + }); |
| 85 | +}); |
0 commit comments