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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353/** Raw commit objects as exact bytes: parsing, the unsigned replay rewrite, and signed-replay inputs.
Replay keeps every byte the prepared commit carries except its `tree` and `parent` lines, so the message and headers are handled as bytes, never decoded text: a non-UTF-8 `encoding` commit round-trips unchanged.
@module */import { Buffer, } from 'node:buffer';
/** Byte ending each header line. */const NEWLINE_BYTE = 0x0A;
/** Byte opening a header continuation line. */const SPACE_BYTE = 0x20;
/** Headers a signed replay reproduces through `git commit-tree`; every other header is dropped there. */const REBUILT_HEADERS: ReadonlySet<string> = new Set([ 'tree', 'parent', 'author', 'committer', 'encoding', 'gpgsig', 'gpgsig-sha256',],);
/** Signature headers. */const SIGNATURE_HEADERS: ReadonlySet<string> = new Set([ 'gpgsig', 'gpgsig-sha256',],);
/** One header with its continuation lines. */export type RawCommitHeader = Readonly<{ /** Header name before the first space. */ name: string; /** Complete header bytes, continuation lines and final newline included. */ bytes: Uint8Array;}>;
/** Parsed raw commit. */export type RawCommit = Readonly<{ /** Headers in object order. */ headers: readonly RawCommitHeader[]; /** Message bytes after the blank line. */ message: Uint8Array; /** Tree header value. */ treeOid: string; /** `encoding` header value, absent for UTF-8. */ encoding?: string; /** Whether a `gpgsig` or `gpgsig-sha256` header is present. */ signed: boolean;}>;
/** Text value of a single-line header, without the name and trailing newline.
@param header - header
@returns value text */function headerValue(header: RawCommitHeader,): string { return Buffer.from(header.bytes,) .toString( 'utf8', header.name .length + 1, header.bytes .length - 1, );}
/** Splits header bytes into headers, joining continuation lines to the header they continue.
@param headerBytes - header section, each line ending in a newline
@returns headers in order */function splitHeaders(headerBytes: Uint8Array,): readonly RawCommitHeader[] { /** Start offset of every line. */ const lineStarts = [ 0, ...Array.from( headerBytes, function nextLineStart( byte, index, ): number { return (byte === NEWLINE_BYTE) && ((index + 1) < headerBytes.length) ? index + 1 : -1; }, ) .filter(function isLineStart(start,): boolean { return start !== (-1); },), ]; /** Starts of lines that open a header rather than continue one. */ const headerStarts = lineStarts.filter(function opensHeader(start,): boolean { return headerBytes[start] !== SPACE_BYTE; },); return headerStarts.map(function toHeader( start, index, ): RawCommitHeader { /** Header bytes up to the next header. */ const bytes = headerBytes.subarray( start, headerStarts[index + 1] ?? headerBytes.length, ); /** End of the header name. */ const nameEnd = bytes.indexOf(SPACE_BYTE,); return { name: Buffer.from(bytes.subarray( 0, nameEnd === (-1) ? bytes.length - 1 : nameEnd, ),) .toString('latin1',), bytes, }; },);}
/** Parses a raw commit object.
@param bytes - `git cat-file commit` output
@returns parsed commit
@throws TypeError when the object has no `tree` header
@example ```ts parseRawCommit(new TextEncoder().encode('tree abc\n\nmessage\n')); ``` */export function parseRawCommit(bytes: Uint8Array,): RawCommit { /** Header section end: the newline before the blank line. */ const blank = Buffer.from(bytes,) .indexOf('\n\n',); /** Header section, each line newline-terminated. */ const headerBytes = blank === (-1) ? bytes : bytes.subarray( 0, blank + 1, ); /** Parsed headers. */ const headers = splitHeaders(headerBytes,); /** Tree header. */ const tree = headers.find(function isTree(header,): boolean { return header.name === 'tree'; },); if (tree === undefined) throw new TypeError('Commit object has no tree header.',); /** Encoding header. */ const encoding = headers.find(function isEncoding(header,): boolean { return header.name === 'encoding'; },); return { headers, message: blank === (-1) ? new Uint8Array() : bytes.subarray(blank + 2,), treeOid: headerValue(tree,), ...(encoding === undefined ? {} : { encoding: headerValue(encoding,), }), signed: headers.some(function isSignature(header,): boolean { return SIGNATURE_HEADERS.has(header.name,); },), };}
/** Rewrites an unsigned commit onto a new tree and parent, keeping every other byte.
@param commit - parsed prepared commit
@param treeOid - replayed tree
@param parentOid - new parent
@returns raw commit bytes for `git hash-object -t commit -w --stdin`
@example ```ts rewriteCommitObject({ commit, treeOid, parentOid }); ``` */export function rewriteCommitObject({ commit, treeOid, parentOid,}: Readonly<{ commit: RawCommit; treeOid: string; parentOid: string;}>,): Uint8Array { return Buffer.concat([ ...commit.headers .flatMap(function rewritten(header,): readonly Uint8Array[] { if (header.name === 'tree') return [Buffer.from( `tree ${treeOid}\nparent ${parentOid}\n`, 'latin1', ),]; return header.name === 'parent' ? [] : [header.bytes,]; },), Buffer.from( '\n', 'latin1', ), commit.message, ],);}
/** Names of headers a signed replay drops, each once in object order.
@param commit - parsed prepared commit
@returns dropped header names
@example ```ts droppedReplayHeaders(commit); // ['x-custom'] ``` */export function droppedReplayHeaders(commit: RawCommit,): readonly string[] { return [ ...new Set(commit.headers .map(function headerName(header,): string { return header.name; },) .filter(function isCustom(name,): boolean { return !REBUILT_HEADERS.has(name,); },),), ];}
/** Git identity environment for one `author` or `committer` header, as `git commit-tree` reads it.
@param commit - parsed prepared commit
@param role - identity header
@returns `GIT_<ROLE>_NAME`, `GIT_<ROLE>_EMAIL`, and `GIT_<ROLE>_DATE`
@throws TypeError when the header is missing or malformed
@example ```ts identityEnvironment({ commit, role: 'author' }); // { GIT_AUTHOR_NAME: 'A', GIT_AUTHOR_EMAIL: 'a@example.com', GIT_AUTHOR_DATE: '@1700000000 +0000' } ``` */export function identityEnvironment({ commit, role,}: Readonly<{ commit: RawCommit; role: 'author' | 'committer';}>,): Readonly<Record<string, string>> { /** Identity header. */ const header = commit.headers .find(function isRole(candidate,): boolean { return candidate.name === role; },); if (header === undefined) throw new TypeError(`Commit object has no ${role} header.`,); /** `Name <email> <seconds> <zone>`. */ const value = headerValue(header,); /** Email delimiters. */ const open = value.lastIndexOf('<',); /** Email end. */ const close = value.indexOf( '>', open, ); if ((open === (-1)) || (close === (-1))) throw new TypeError(`Commit ${role} header is malformed: ${JSON.stringify(value,)}`,); /** Environment variable prefix. */ const prefix = `GIT_${role.toUpperCase()}`; return { [`${prefix}_NAME`]: value.slice( 0, open, ) .trimEnd(), [`${prefix}_EMAIL`]: value.slice( open + 1, close, ), [`${prefix}_DATE`]: `@${value.slice(close + 1,) .trim()}`, };}