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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441/** Replay of a prepared commit onto a moved target, run in the shadow repository outside both landing locks.
The tree comes from `git merge-tree --write-tree --merge-base`; an unsigned commit is rebuilt by rewriting its raw object, and a signed one through `git commit-tree -S` under its original identities and dates. Every object replay writes stays in the shadow store until the next landing migrates it.
@module */import { join, } from 'node:path';import { tagged, } from '@monochromatic-dev/module-logger/ts';import { runShadowGit, } from '../shadow-repository/shadow-refs.ts';import { writePrivateFile, } from '../trust/registry-io.ts';import type { PreparationBase, } from './commit-transaction-capture.ts';import { CommitTransactionGitError, runTransactionGit,} from './commit-transaction-git.ts';import { droppedReplayHeaders, identityEnvironment, type RawCommit, rewriteCommitObject,} from './commit-replay-object.ts';
/** Module logger. */const l = tagged({ tag: 'cli-git', },);
/** Strict Git output decoder. */const DECODER = new TextDecoder( 'utf-8', { fatal: true, },);
/** UTF-8 encoder for synthesized objects. */const ENCODER = new TextEncoder();
/** `git merge-tree` exit status reporting conflicts; a tree ID still prints. */const MERGE_CONFLICT_EXIT = 1;
/** Keeps pathspec magic active, so `:(top,literal)` names concrete paths even when the caller disabled magic. */const MAGIC_PATHSPECS = { GIT_LITERAL_PATHSPECS: '0', } as const;
/** Outcome of merging the prepared change onto the current target. */export type ReplayMerge = | Readonly<{ /** The change applies cleanly. */ kind: 'merged'; /** Merged tree, in the shadow store. */ treeOid: string; }> | Readonly<{ /** The change conflicts with the target. */ kind: 'conflict'; /** Conflicting paths in Git path byte order. */ paths: readonly string[]; }>;
/** Commit to use as `--merge-base`: the preparation base, or for an unborn base a root commit of the empty tree, because Git 2.40 accepts only a commit there.
@param gitPath - real Git executable
@param shadowPath - shadow repository
@param base - preparation base
@param emptyTreeOid - empty tree of the repository's object format
@returns merge-base commit
@example ```ts await replayMergeBase({ gitPath: '/usr/bin/git', shadowPath, base, emptyTreeOid }); ``` */export async function replayMergeBase({ gitPath, shadowPath, base, emptyTreeOid,}: Readonly<{ gitPath: string; shadowPath: string; base: PreparationBase; emptyTreeOid: string;}>,): Promise<string> { if (base.kind === 'commit') return base.oid; return DECODER.decode((await runShadowGit({ gitPath, shadowPath, args: [ 'hash-object', '-t', 'commit', '-w', '--stdin', ], input: ENCODER.encode(`tree ${emptyTreeOid}\nauthor cli-git <cli-git@localhost> 0 +0000\ncommitter cli-git <cli-git@localhost> 0 +0000\n\ncli-git replay base for an unborn branch\n`,), },)).stdout,) .trim();}
/** Sorts paths in Git path byte order.
@param paths - paths
@returns sorted copy */function byteOrdered(paths: readonly string[],): readonly string[] { return paths.toSorted(function byteOrder( left, right, ): number { return Buffer.compare( Buffer.from( left, 'utf8', ), Buffer.from( right, 'utf8', ), ); },);}
/** Merges the prepared change onto the current target in the shadow.
@param gitPath - real Git executable
@param shadowPath - shadow repository
@param mergeBase - preparation base commit, see {@link replayMergeBase}
@param current - current target commit
@param prepared - prepared commit
@param worktreeRoot - worktree root, where Git reports conflicted paths from the repository root
@returns merged tree or conflicting paths
@throws {@link CommitTransactionGitError} when `git merge-tree` fails for another reason than conflicts, such as a Git without `--merge-base`
@example ```ts await mergeReplayTree({ gitPath: '/usr/bin/git', shadowPath, mergeBase, current, prepared }); ``` */export async function mergeReplayTree({ gitPath, shadowPath, mergeBase, current, prepared, worktreeRoot,}: Readonly<{ gitPath: string; shadowPath: string; mergeBase: string; current: string; prepared: string; worktreeRoot: string;}>,): Promise<ReplayMerge> { /** Tagged merge logger. */ const rl = tagged({ tag: mergeReplayTree.name, l, },); /** Merge outcome: `<tree>NUL`, then on conflict each conflicted path NUL-terminated, an empty record, and messages. */ const result = await runShadowGit({ gitPath, shadowPath, args: [ 'merge-tree', '--write-tree', '--name-only', '-z', `--merge-base=${mergeBase}`, current, prepared, ], allowFailure: true, cwd: worktreeRoot, },); if ((result.exitCode !== 0) && (result.exitCode !== MERGE_CONFLICT_EXIT)) throw new CommitTransactionGitError(`git merge-tree could not replay ${prepared} onto ${current} (exit ${String(result.exitCode,)}): ${result.stderr .trim()}`,); /** NUL-separated records. */ const [treeOid = '', ...rest] = DECODER.decode(result.stdout,) .split('\0',); if (result.exitCode === 0) { rl.debug(`replayed ${prepared} onto ${current} as tree ${treeOid}`,); return { kind: 'merged', treeOid, }; } /** Conflicted paths before the empty record that opens the messages. */ const end = rest.indexOf('',); /** Unique conflicted paths. */ const paths = byteOrdered([ ...new Set(end === (-1) ? rest : rest.slice( 0, end, ),), ],); rl.debug(`replay of ${prepared} onto ${current} conflicts in ${paths.join(', ',)}`,); return { kind: 'conflict', paths, };}
/** Earliest commit after the preparation base that touches any of the paths, the commit a conflict lost to.
@param gitPath - real Git executable
@param cwd - owning worktree directory
@param base - preparation base
@param current - current target
@param paths - conflicting paths
@returns winning commit, the current target when no commit touches the paths
@example ```ts await firstCommitTouching({ gitPath: '/usr/bin/git', cwd: '/repo', base, current, paths: ['a.txt'] }); ``` */export async function firstCommitTouching({ gitPath, cwd, base, current, paths,}: Readonly<{ gitPath: string; cwd: string; base: PreparationBase; current: string; paths: readonly string[];}>,): Promise<string> { /** Commits after the base touching the paths, oldest first. */ const listed = DECODER.decode((await runTransactionGit({ gitPath, cwd, args: [ 'rev-list', '--reverse', current, ...(base.kind === 'commit' ? [`^${base.oid}`,] : []), '--', ...paths.map(function fromRoot(path,): string { return `:(top,literal)${path}`; },), ], environment: MAGIC_PATHSPECS, },)).stdout,) .split('\n',) .find(function nonempty(line,): boolean { return line.length > 0; },); return listed ?? current;}
/** A replayed commit written to the shadow store. */export type ReplayedCommit = Readonly<{ /** Replayed commit. */ oid: string; /** Custom headers a signed rebuild dropped, empty for an unsigned rewrite. */ droppedHeaders: readonly string[];}>;
/** Writes the prepared commit onto a new tree and parent in the shadow store.
@param gitPath - real Git executable
@param shadowPath - shadow repository
@param directory - transaction directory receiving the message file of a signed rebuild
@param attempt - landing attempt number naming the message file
@param commit - parsed prepared commit
@param treeOid - new tree
@param parentOid - new parent
@param signingKey - key ID the invocation passed to `-S`; absent signs with the configured key
@param globalArgs - caller's kept global options, which may configure signing
@returns replayed commit
@example ```ts await writeReplayedCommit({ gitPath: '/usr/bin/git', shadowPath, directory, attempt: 1, commit, treeOid, parentOid, globalArgs: [] }); ``` */export async function writeReplayedCommit({ gitPath, shadowPath, directory, attempt, commit, treeOid, parentOid, signingKey, globalArgs,}: Readonly<{ gitPath: string; shadowPath: string; directory: string; attempt: number; commit: RawCommit; treeOid: string; parentOid: string; signingKey?: string; globalArgs: readonly string[];}>,): Promise<ReplayedCommit> { if (!commit.signed) return { oid: DECODER.decode((await runShadowGit({ gitPath, shadowPath, args: [ 'hash-object', '-t', 'commit', '-w', '--stdin', ], input: rewriteCommitObject({ commit, treeOid, parentOid, },), },)).stdout,) .trim(), droppedHeaders: [], }; /** Exact message bytes for `-F`. */ const messagePath = join( directory, `replay-message-${String(attempt,)}`, ); await writePrivateFile({ path: messagePath, bytes: commit.message, },); /** Re-signed commit under the prepared commit's identities and dates. */ const signed = await runShadowGit({ gitPath, shadowPath, args: [ ...globalArgs, '-c', `i18n.commitEncoding=${commit.encoding ?? 'utf8'}`, 'commit-tree', treeOid, '-p', parentOid, `-S${signingKey ?? ''}`, '-F', messagePath, ], environment: { ...identityEnvironment({ commit, role: 'author', },), ...identityEnvironment({ commit, role: 'committer', },), }, },); return { oid: DECODER.decode(signed.stdout,) .trim(), droppedHeaders: droppedReplayHeaders(commit,), };}