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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333/** Paths both a prepared commit and the landed history changed since the preparation base, with the three tree entries replay's subsumption check compares.
Paths are Latin-1 decoded from `git diff-tree -z` output, so every byte survives and encodes back exactly.
@module */import { runShadowGit, } from '../shadow-repository/shadow-refs.ts';import { parseRawChanges, type RawChange,} from './commit-hook-changes.ts';import { decodeLatin1, } from './commit-replay-patch.ts';
/** Git's absent-entry mode in raw diff output. */const ABSENT_MODE = '000000';
/** Regular file modes. */const REGULAR_MODES: ReadonlySet<string> = new Set([ '100644', '100755',],);
/** A tree lacks the path. */export const ABSENT_ENTRY: unique symbol = Symbol('path absent from tree',);
/** One tree entry of a shared path. */export type TreeEntry = Readonly<{ /** Git mode. */ mode: string; /** Object ID. */ oid: string;}>;
/** One tree entry, or its absence. */export type SideEntry = TreeEntry | typeof ABSENT_ENTRY;
/** One path both sides changed. */export type SharedPath = Readonly<{ /** Repository path, Latin-1 decoded. */ path: string; /** Preparation base entry. */ base: SideEntry; /** Landed entry. */ landed: SideEntry; /** Prepared entry. */ prepared: SideEntry;}>;
/** Converts one side of a raw record into an entry.
@param mode - raw mode
@param oid - raw object ID
@returns entry, absent for the all-zero mode */function entryOf({ mode, oid,}: Readonly<{ mode: string; oid: string;}>,): SideEntry { return mode === ABSENT_MODE ? ABSENT_ENTRY : { mode, oid, };}
/** Lists the raw changes between two commits in the shadow.
@param gitPath - real Git executable
@param shadowPath - shadow repository
@param from - older commit
@param to - newer commit
@returns changes per path */async function rawChanges({ gitPath, shadowPath, from, to,}: Readonly<{ gitPath: string; shadowPath: string; from: string; to: string;}>,): Promise<ReadonlyMap<string, RawChange>> { return new Map(parseRawChanges(decodeLatin1((await runShadowGit({ gitPath, shadowPath, args: [ 'diff-tree', '-r', '-z', '--raw', '--no-renames', '--no-abbrev', from, to, ], },)).stdout,),) .map(function keyed(change,): readonly [ string, RawChange ] { return [ change.path, change, ]; },),);}
/** Whether two entries name the same mode and object, or are both absent.
@param left - first entry
@param right - second entry
@returns equality
@example ```ts sameEntry({ left: ABSENT_ENTRY, right: ABSENT_ENTRY }); // true ``` */export function sameEntry({ left, right,}: Readonly<{ left: SideEntry; right: SideEntry;}>,): boolean { if ((left === ABSENT_ENTRY) || (right === ABSENT_ENTRY)) return left === right; return (left.mode === right.mode) && (left.oid === right.oid);}
/** The three entries of a shared path.
@param shared - shared path
@returns base, landed, and prepared entries
@example ```ts sidesOf(shared).length; // 3 ``` */export function sidesOf(shared: SharedPath,): readonly SideEntry[] { return [ shared.base, shared.landed, shared.prepared, ];}
/** Whether all three entries of a shared path are regular files, so only their bytes can decide it.
@param shared - shared path
@returns whether the entries are regular files
@example ```ts isRegularTriple(shared); ``` */export function isRegularTriple(shared: SharedPath,): boolean { return sidesOf(shared,) .every(function regular(entry,): boolean { return (entry !== ABSENT_ENTRY) && REGULAR_MODES.has(entry.mode,); },);}
/** Whether the text check can decide a shared path: landed and prepared entries are regular files, and the base entry is one too or absent, an addition on both sides read against an empty base.
@param shared - shared path
@returns whether the text check applies
@example ```ts isTextCandidate(shared); ``` */export function isTextCandidate(shared: SharedPath,): boolean { /** Landed and prepared entries. */ const changed: readonly SideEntry[] = [ shared.landed, shared.prepared, ]; return changed.every(function regular(entry,): boolean { return (entry !== ABSENT_ENTRY) && REGULAR_MODES.has(entry.mode,); },) && ((shared.base === ABSENT_ENTRY) || REGULAR_MODES.has(shared.base .mode,));}
/** Object ID of a present entry.
@param entry - entry a caller already knows is present
@returns object ID
@throws {@link TypeError} for an absent entry
@example ```ts presentOid(shared.prepared); ``` */export function presentOid(entry: SideEntry,): string { if (entry === ABSENT_ENTRY) throw new TypeError('A regular-file entry of a shared replay path is absent.',); return entry.oid;}
/** Lists the paths both the landed history and the prepared commit changed since the merge base, in Git order.
@param gitPath - real Git executable
@param shadowPath - shadow repository
@param mergeBase - preparation base commit
@param current - target that won
@param prepared - prepared commit
@returns shared paths with their three entries
@example ```ts await listSharedPaths({ gitPath: '/usr/bin/git', shadowPath, mergeBase, current, prepared }); ``` */export async function listSharedPaths({ gitPath, shadowPath, mergeBase, current, prepared,}: Readonly<{ gitPath: string; shadowPath: string; mergeBase: string; current: string; prepared: string;}>,): Promise<readonly SharedPath[]> { /** Changes of the landed history and of the prepared commit since the base. */ const [landed, mine,] = await Promise.all([ rawChanges({ gitPath, shadowPath, from: mergeBase, to: current, },), rawChanges({ gitPath, shadowPath, from: mergeBase, to: prepared, },), ],); return [...landed.values(),].flatMap(function both(change,): readonly SharedPath[] { /** Prepared change of the same path. */ const own = mine.get(change.path,); return own === undefined ? [] : [{ path: change.path, base: entryOf({ mode: change.fromMode, oid: change.fromOid, },), landed: entryOf({ mode: change.toMode, oid: change.toOid, },), prepared: entryOf({ mode: own.toMode, oid: own.toOid, },), },]; },);}