- MCP
- code on this site's /mcp
- CLI
- none: the JSON below is the answer
- JSON
- /christopher/abso-core/files/src/gitview/plain.ts?version=16&format=json
51 linesFirst import 100%Last change First import in version 0, 1 version changed it
You're reading version 16. The newest is 318, and no version since then changed this file.
1// Git-view helpers that need no git library, so the pre-receive hook (which
2// reads git through the git CLI) can use them without loading isomorphic-git.
3// tree.ts and ingest.ts re-export them.
5import { type AtomId, type AtomSet, canonicalText, type Graph, type Hunk, sortBySeq } from "../engine/index.ts";
7// fatal: invalid UTF-8 throws (so the file counts as binary); ignoreBOM: a BOM
8// stays in the text, so the file round-trips byte for byte.
9const UTF8 = new TextDecoder("utf-8", { fatal: true, ignoreBOM: true });
11/** A blob's text in canonical form, or why it isn't a text file. */
12export function decodeBlob(bytes: Uint8Array): { text: string } | { reason: "empty" | "binary" } {
13 if (bytes.length === 0) return { reason: "empty" };
14 if (bytes.subarray(0, 8000).includes(0)) return { reason: "binary" };
15 let text: string;
16 try {
17 text = UTF8.decode(bytes);
18 } catch {
19 return { reason: "binary" };
20 }
21 return { text: canonicalText(text) };
22}
24/** Maximal atoms of a closed set: those no atom in the set depends on. By seq. */
25export function headsOf(graph: Graph, set: AtomSet): AtomId[] {
26 const out: AtomId[] = [];
27 for (const id of set) if (!graph.dependentsOf(id).some((d) => set.has(d))) out.push(id);
28 return sortBySeq(graph, out);
29}
31/** "git-<short sha>", lengthened while another label has the name. */
32export function labelName(graph: Graph, sha: string): string {
33 for (let n = 7; n <= sha.length; n++) {
34 const name = `git-${sha.slice(0, n)}`;
35 if (!graph.label(name)) return name;
36 }
37 for (let i = 2; ; i++) {
38 const name = `git-${sha}-${i}`;
39 if (!graph.label(name)) return name;
40 }
41}
43/** Where a hunk is, the way git prints it: "path @@ -old,count +new,count @@" (1-based). */
44export function hunkLocation(path: string, hunk: Hunk, shift: number): string {
45 const oldCount = hunk.end - hunk.start;
46 const newCount = hunk.lines.length;
47 const newStart = hunk.start + shift;
48 const o = oldCount === 0 ? hunk.start : hunk.start + 1;
49 const n = newCount === 0 ? newStart : newStart + 1;
50 return `${path} @@ -${o},${oldCount} +${n},${newCount} @@`;
51}