Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/Aquaticat/Monochromatic. TypeScript and Rust monorepo: Claude Code and Pi coding-agent plugins, a m
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380import { mkdir, realpath, } from 'node:fs/promises';import { join, } from 'node:path';
import { describe, expect, it,} from '@monochromatic-dev/module-test/ts';
import { resolveGitWorktreeIdentity, } from './git-worktree-identity.ts';import { resolveRealGit as resolveGit, } from '@monochromatic-dev/git-executable/ts';import { createTempDirectory, initializeMainRepository, initializeRepository, runRealGit,} from './worktree-copy-fixture.unit.test.ts';
/** Absolute real-Git executable for identity fixtures. */const gitPath = await resolveGit();
/** Temporarily overrides one process environment entry. @param key - environment key to replace @param value - temporary environment value @returns disposable environment restoration @mutates process.env @example ```ts using override = overrideEnvironment({ key: 'GIT_DIR', value: '/repo/.git' }); ``` */function overrideEnvironment({ key, value,}: Readonly<{ key: string; value: string;}>,): Disposable { /** Whether environment originally owned key. */ const hadOriginal = Object.hasOwn(process.env, key,); /** Original value retained only when key was present. */ const original = process.env[key] ?? ''; process.env[key] = value; return { [Symbol.dispose](): void { if (hadOriginal) { process.env[key] = original; return; } if (!Reflect.deleteProperty(process.env, key,)) throw new Error(`Could not restore absent environment key ${JSON.stringify(key,)}.`,); }, };}
await describe({ name: resolveGitWorktreeIdentity.name, concurrency: 1, children: [ it({ name: 'classifies directory outside repository', fn: async () => { await using fixture = await createTempDirectory();
/** Identity resolved outside repository. */ const identity = await resolveGitWorktreeIdentity({ args: [ '-C', fixture.path, 'status', ], gitPath, },);
expect(identity,).toEqual({ kind: 'outside-worktree', effectiveCwd: fixture.path, },); }, },),
it({ name: 'returns canonical main worktree identity', fn: async () => { await using fixture = await createTempDirectory(); /** Main repository root under disposable fixture. */ const mainRoot = join(fixture.path, 'main',); await initializeMainRepository(mainRoot,);
/** Canonical main repository identity. */ const identity = await resolveGitWorktreeIdentity({ args: [ '-C', mainRoot, 'status', ], gitPath, },); /** Canonical main Git directory expected in identity. */ const expectedGitDir = await realpath(join(mainRoot, '.git',),);
expect(identity,).toEqual({ kind: 'main-worktree', commonDir: expectedGitDir, effectiveCwd: mainRoot, gitDir: expectedGitDir, worktreeRoot: await realpath(mainRoot,), },); }, },),
it({ name: 'returns canonical linked worktree identity', fn: async () => { await using fixture = await createTempDirectory(); /** Linked source root created with sibling main repository. */ const linkedRoot = join(fixture.path, 'linked',); await initializeRepository(linkedRoot,);
/** Canonical linked repository identity. */ const identity = await resolveGitWorktreeIdentity({ args: [ '-C', linkedRoot, 'status', ], gitPath, },);
expect(identity.kind,).toBe('linked-worktree',); if (identity.kind !== 'linked-worktree') throw new Error('Expected linked worktree identity.',); expect(identity.gitDir,).not.toBe(identity.commonDir,); expect(identity.worktreeRoot,).toBe(await realpath(linkedRoot,),); }, },),
it({ name: 'returns canonical bare repository identity', fn: async () => { await using fixture = await createTempDirectory(); /** Bare repository selected through explicit Git directory. */ const bareRoot = join(fixture.path, 'bare.git',); await runRealGit({ cwd: fixture.path, args: [ 'init', '--bare', bareRoot, ], },);
/** Canonical bare repository identity. */ const identity = await resolveGitWorktreeIdentity({ args: [ '--git-dir', bareRoot, 'status', ], gitPath, },); /** Canonical bare Git directory expected for both administrative fields. */ const expectedGitDir = await realpath(bareRoot,);
expect(identity,).toEqual({ kind: 'bare-repository', commonDir: expectedGitDir, effectiveCwd: process.cwd(), gitDir: expectedGitDir, },); }, },),
it({ name: 'honors pre-subcommand chdir selection', fn: async () => { await using fixture = await createTempDirectory(); /** Main repository selected from unrelated invocation cwd. */ const mainRoot = join(fixture.path, 'main',); await initializeMainRepository(mainRoot,);
/** Main identity selected through pre-subcommand chdir. */ const identity = await resolveGitWorktreeIdentity({ args: [ '-C', mainRoot, 'status', ], gitPath, },);
expect(identity.kind,).toBe('main-worktree',); }, },),
it({ name: 'honors explicit git-dir and work-tree selection', fn: async () => { await using fixture = await createTempDirectory(); /** Unrelated cwd for explicit repository-selection options. */ const invocationRoot = join(fixture.path, 'invocation',); /** Main repository selected explicitly from unrelated cwd. */ const mainRoot = join(fixture.path, 'main',); await Promise.all([ mkdir(invocationRoot, { recursive: true, },), initializeMainRepository(mainRoot,), ],);
/** Main identity selected through explicit administrative options. */ const identity = await resolveGitWorktreeIdentity({ args: [ '-C', invocationRoot, '--git-dir', join(mainRoot, '.git',), '--work-tree', mainRoot, 'status', ], gitPath, },);
expect(identity.kind,).toBe('main-worktree',); }, },),
it({ name: 'honors chained relative chdir selection', fn: async () => { await using fixture = await createTempDirectory(); /** Main repository reached through relative second chdir. */ const mainRoot = join(fixture.path, 'main',); await initializeMainRepository(mainRoot,);
/** Identity selected after ordered absolute and relative chdir options. */ const identity = await resolveGitWorktreeIdentity({ args: [ '-C', fixture.path, '-C', 'main', 'status', ], gitPath, },);
expect(identity.kind,).toBe('main-worktree',); expect(identity.effectiveCwd,).toBe(mainRoot,); }, },),
it({ name: 'honors glued git-dir and work-tree selection', fn: async () => { await using fixture = await createTempDirectory(); /** Main repository selected by glued global options. */ const mainRoot = join(fixture.path, 'main',); await initializeMainRepository(mainRoot,);
/** Main identity selected through glued administrative options. */ const identity = await resolveGitWorktreeIdentity({ args: [ `--git-dir=${join(mainRoot, '.git',)}`, `--work-tree=${mainRoot}`, 'status', ], gitPath, },);
expect(identity.kind,).toBe('main-worktree',); }, },),
it({ name: 'honors inherited git-dir and work-tree selection', fn: async () => { await using fixture = await createTempDirectory(); /** Main repository selected by inherited Git environment. */ const mainRoot = join(fixture.path, 'main',); await initializeMainRepository(mainRoot,); /** Temporary inherited Git-directory selection. */ using gitDirOverride = overrideEnvironment({ key: 'GIT_DIR', value: join(mainRoot, '.git',), },); /** Temporary inherited worktree selection. */ using workTreeOverride = overrideEnvironment({ key: 'GIT_WORK_TREE', value: mainRoot, },);
/** Main identity selected through inherited environment. */ const identity = await resolveGitWorktreeIdentity({ args: ['status',], gitPath, },);
expect(identity.kind,).toBe('main-worktree',); }, },),
it({ name: 'maps invalid explicit git-dir to outside worktree', fn: async () => { await using fixture = await createTempDirectory();
/** Outside identity produced after real Git rejects explicit missing directory. */ const identity = await resolveGitWorktreeIdentity({ args: [ '--git-dir', join(fixture.path, 'missing.git',), 'status', ], gitPath, },);
expect(identity.kind,).toBe('outside-worktree',); }, },), ],},);