Lines 2–17 at 318 (were 2–11 at 45)
22// A line's id names the atom that inserted it, so blame is a lookup per line: 33// that change's author and message, and the publish that brought it into 44// shared, with its message. Why adds the change's reasons, what it settled, 5−// and the reasons other changes give that point at it.
6−import type { AtomId } from "../../src/engine/index.ts";
5+// and the reasons other changes give that point at it, and the line's 6+// earlier changes (trail.ts). 7+import { type Atom, type AtomId, type LineId, type ReadView, seqOf, View } from "../../src/engine/index.ts"; 8+import { isClosedSet, markClosed, StableSet } from "../../src/engine/state-source.ts"; 9+import { blocksIn, isMarkerLine } from "./markers.ts"; 710import { pathKey } from "./pathkey.ts"; 811import type { Project } from "./project.ts"; 12+import { projectFileIn } from "./project-helpers.ts"; 13+import { type TrailStep, trail } from "./trail.ts"; 14+import { authorsInView } from "./version-files.ts"; 1016export interface BlameLine { Lines 72–78 at 318 (were 66–72 at 45)
6672 const { view, name } = viewOf(project, where); 6773 const file = project.spellingIn(view, asked); 6874 if (!view.hasFile(file)) throw badRequest(`${asked} isn't in ${name}`); 69− return { file, all: view.lines(file) };
75+ return { file, all: view.lines(file), view }; Lines 92–112 at 318 (were 86–96 at 45)
8692 const first = Math.max(1, from ?? 1); 8793 const last = Math.min(all.length, to ?? all.length); 8894 const graph = project.graph; 95+ // Each atom's author once (abso Capacity): on a stored writer, graph.atom reads and parses the whole atom, and 96+ // most lines share a few big atoms. 97+ const authors = new Map<AtomId, string>(); 98+ const authorOf = (id: AtomId): string => { 99+ const known = authors.get(id); 100+ if (known !== undefined) return known; 101+ const author = graph.atom(id).author; 102+ authors.set(id, author); 89105 const rows: BlameLine[] = all.slice(first - 1, last).map((l, i) => ({ 93− author: graph.atom(l.atom).author,
109+ author: authorOf(l.atom), 94110 version: project.versionOf(l.atom) ?? null, 95111 ...(project.publishOf(l.atom) === undefined ? { unpublished: true as const } : {}), Lines 129–216 at 318 (were 113–121 at 45)
133+ * The settle a line was kept through, when its own change settled nothing: 134+ * an edit that took out the markers of a block the line sat in (as yours or 135+ * theirs), as abso settle --yours, --theirs or --both makes. The earliest, 138+function keptThrough(project: Project, view: ReadView, file: string, line: LineId, author: Atom): Atom | undefined { 139+ const graph = project.graph; 140+ const set = view.set; 141+ // Only atoms that write or take out the file's lines can take out its markers (abso Capacity): every atom in the 142+ // history was read on a stored graph. 143+ for (const id of graph.eachAtomIn(file)) { 144+ if (seqOf(id) <= author.seq) continue; 145+ const s = graph.atom(id); 146+ if (s.resolves.length === 0 || !set.has(s.id)) continue; 147+ const marks = s.deletes.filter((id) => { 148+ const info = graph.line(id); 149+ return info !== undefined && info.file === file && isMarkerLine(info.text); 151+ if (marks.length === 0) continue; 152+ // The file just before the settle, markers and all: of a closed set, its atoms before the settle, which a 153+ // stored set answers one by one; otherwise listed. 154+ const before = new View( 157+ ? markClosed(new BeforeSet(set, s.seq)) 161+ .filter((a) => set.has(a.id)) 164+ if (!before.hasFile(file)) continue; 165+ const lines = before.lines(file); 166+ const at = lines.findIndex((l) => l.id === line); 167+ if (at < 0) continue; 168+ const block = blocksIn(lines.map((l) => l.text)).find((b) => at > b.start && at < b.end); 169+ if (block && marks.includes(lines[block.start]!.id)) return s; 174+/** A set's atoms numbered below a limit. Atoms depend only on earlier ones, so a closed set's prefix is closed. */ 175+class BeforeSet extends StableSet<AtomId> { 176+ readonly #set: ReadonlySet<AtomId>; 177+ readonly #limit: number; 178+ readonly retainedBytes = 32; 179+ constructor(set: ReadonlySet<AtomId>, limit: number) { 182+ this.#limit = limit; 184+ has(id: AtomId): boolean { 185+ const seq = seqOf(id); 186+ return seq >= 0 && seq < this.#limit && this.#set.has(id); 188+ *values(): SetIterator<AtomId> { 189+ for (const id of this.#set) { 190+ const seq = seqOf(id); 191+ if (seq >= 0 && seq < this.#limit) yield id; 196+/** The settler's push that was held for this collision, newest first: its message is that side's reason. */ 197+function heldPush(project: Project, settle: Atom, file: string, after: number) { 198+ const held = project 199+ .refusals({ agent: settle.author, limit: 1000 }) 202+ r.via === "publish" && 203+ r.message !== undefined && 204+ r.time <= settle.time && 206+ r.collisions.some((c) => c.file === file), 208+ return held ? { agent: held.agent, message: held.message!, time: held.time } : undefined; 116211/** One line: the change that wrote it, the publish behind it, its reasons, and what it settled. */ 117212export function why(project: Project, asked: string, n: number, where: BlameWhere = {}) { 118− const { file, all } = lines(project, asked, where);
213+ const { file, all, view } = lines(project, asked, where); 119214 const line = all[n - 1]; 121216 throw Object.assign(new Error(`${file} has ${all.length} lines; ask for one in 1-${all.length}`), { Lines 227–235 at 318 (were 132–138 at 45)
132227 toMessage: graph.atom(r.to).message, 134229 const reasonsIn: { from: AtomId; kind: string; text: string; fromAuthor: string; fromMessage: string }[] = []; 135− for (const other of graph.atoms()) {
230+ // The atoms that point at it, where the graph keeps them (abso Capacity): looking at every atom read each one. 231+ const pointing = graph.reasonsTo(atom.id); 232+ for (const other of pointing ? pointing.map((id) => graph.atom(id)) : graph.atoms()) { 136233 for (const r of other.reasons) { 137234 if (r.to === atom.id) Lines 260–284 at 318 (were 163–177 at 45)
163260 builtOn.sort((a, b) => 164261 (a.draft === undefined) !== (b.draft === undefined) ? (a.draft === undefined ? -1 : 1) : b.version - a.version, 166− const settled = atom.resolves.map((id) => {
263+ // What it settled: its own change, or the settle that kept it (both sides of a block it sat in). 264+ const kept = atom.resolves.length === 0 ? keptThrough(project, view, file, line.id, atom) : undefined; 265+ const settle = kept ?? atom; 266+ const settled = settle.resolves.map((id) => { 167267 const a = graph.atom(id); 168268 return { atom: id, author: a.author, message: a.message, publish: publishOf(project, id) }; 272+ ? heldPush(project, settle, file, Math.max(0, ...settled.map((s) => s.publish?.time ?? 0))) 274+ const gitIssues = gitIssuesOf(project.sharedView()); 278+ version: where.agent !== undefined ? null : (where.version ?? project.lastVersion(where.draft ?? null)), 281+ ...(gitIssues !== undefined ? { gitIssues } : {}), 177284 author: atom.author, Lines 291–388 at 318 (were 184–194 at 45)
294+ ...(kept ? { settledBy: { atom: kept.id, author: kept.author, publish: publishOf(project, kept.id) } } : {}), 295+ ...(held ? { held } : {}), 297+ ...lineTrail(project, view, file, line.id), 302+ * The line's changes, newest first (abso-core issue 20), its own change 303+ * first: each change that rewrote it, with the line's text after it, 304+ * whether it only changed formatting, and the file it moved from, if it did. Several changes in one push count as 305+ * one, formatting only if all of them were. 307+function lineTrail(project: Project, view: ReadView, file: string, line: LineId) { 308+ const graph = project.graph; 309+ // Changes pushed together: the same version of the same space, or both still in one agent's draft. 310+ const samePush = (a: Atom, b: Atom) => samePublish(publishOf(project, a.id), publishOf(project, b.id)); 311+ // What was pushed with a published atom is its version's atoms; an atom still in a draft goes with every other one, 312+ // so the trail looks near by as before. 313+ const pushmates = (a: Atom) => { 314+ const p = publishOf(project, a.id); 315+ return p ? project.versionAtoms(p.version, p.draft) : undefined; 317+ const { steps, more } = trail(graph, view, file, line, undefined, samePush, pushmates); 318+ const groups: { steps: TrailStep[]; publish: Publish | null }[] = []; 319+ for (const s of steps) { 320+ const publish = publishOf(project, s.atom); 321+ const prev = groups[groups.length - 1]; 324+ samePublish(prev.publish, publish) && 325+ (publish !== null || graph.atom(prev.steps[0]!.atom).author === graph.atom(s.atom).author) 328+ else groups.push({ steps: [s], publish }); 330+ const out = groups.map(({ steps: [newest, ...rest], publish }) => { 331+ const a = graph.atom(newest!.atom); 332+ const oldest = rest[rest.length - 1] ?? newest!; 333+ const moved = [newest!, ...rest].some((s) => s.maybeMoved) 334+ ? movedFrom(project, newest!.line, oldest.replaced, publish) 342+ formatting: [newest!, ...rest].every((s) => s.formatting), 343+ ...(moved !== undefined ? { movedFrom: moved } : {}), 347+ return { trail: out, ...(more ? { trailMore: true as const } : {}) }; 351+ * The file a push moved a line from: where the line it replaced was just 352+ * before the push, when that's not where the push left the line. 354+function movedFrom(project: Project, line: LineId, replaced: LineId | undefined, publish: Publish | null) { 355+ if (replaced === undefined || publish === null || publish.version < 1) return undefined; 356+ const where = publish.draft === null ? {} : { draft: publish.draft }; 357+ const fileIn = (version: number, id: LineId) => { 359+ return project.viewAt(version, where).fileOf(id); 364+ const before = fileIn(publish.version - 1, replaced); 365+ const after = fileIn(publish.version, line); 366+ return before !== undefined && after !== undefined && before !== after ? before : undefined; 369+/** Where "#N" in history imported from git points (abso.json's "gitIssues"), when shared's project file says. */ 370+function gitIssuesOf(view: ReadView): string | undefined { 371+ const file = projectFileIn(view); 372+ if (!file) return undefined; 374+ const url = (JSON.parse(file.text) as { gitIssues?: unknown }).gitIssues; 375+ return typeof url === "string" ? url : undefined; 381+function samePublish(a: Publish | null, b: Publish | null): boolean { 382+ return a === null || b === null ? a === b : a.draft === b.draft && a.version === b.version; 192386 * Who wrote a folder (#41, F33): every line of every file under it (the 193387 * whole project for ""), counted by the agent that wrote it, most first. 194388 * Binary files and files kept whole have no lines, so they count as files Lines 399–437 at 318 (were 205–224 at 45)
205399 const key = pathKey(want.join("/")); 206400 const graph = project.graph; 207401 const counts = new Map<string, number>(); 402+ // Each atom's author once (abso Capacity): on a stored writer, graph.atom reads and parses the whole atom, and 403+ // most lines share a few big atoms. 404+ const authors = new Map<AtomId, string>(); 405+ const authorOf = (id: AtomId): string => { 406+ const known = authors.get(id); 407+ if (known !== undefined) return known; 408+ const author = graph.atom(id).author; 409+ authors.set(id, author); 210414 let stored: string | undefined; 415+ // Each file's lines by author as kept when versions land (abso Capacity, version-files.ts), where they answer. 416+ const totalsOf = project.authorTotalsFor({ version, ...where }); 211417 for (const path of view.paths()) { 212418 const parts = path.split("/"); 213419 if (want.length > 0 && (parts.length <= want.length || pathKey(parts.slice(0, want.length).join("/")) !== key)) 215421 stored ??= parts.slice(0, want.length).join("/"); 217− for (const l of view.lines(path)) {
218− const author = graph.atom(l.atom).author;
219− counts.set(author, (counts.get(author) ?? 0) + 1);
220− total++;
423+ const totals = totalsOf?.(path); 425+ for (const [author, n] of totals.authors) { 426+ counts.set(author, (counts.get(author) ?? 0) + n); 431+ for (const [author, n] of authorsInView(view, path, graph, authorOf)) { 432+ counts.set(author, (counts.get(author) ?? 0) + n); 223436 if (want.length > 0 && stored === undefined) 224437 throw Object.assign(new Error(`there is no folder ${want.join("/")} in ${name}`), { code: "not-found" });