nameHistory

tcmts.share.nameHistory · #xy3jinng · term · runs anywhere

The history entries that changed what a name is: when it was added, each change to its code (or what it uses), and its removal, newest first; looking back scan entries along the branch's first parents, at most limit.

import type { Share } from "#codebase/tcmts/share";
import { entryTitle } from "#codebase/tcmts/store";
import type { Causal } from "#codebase/tcmts/store";

export function nameHistory(
  s: Pick<Share, "cb" | "cache">,
  name: string,
  branch?: string,
  scan = 300,
  limit = 8,
): { causal: string; command: string; ts: number; change: "added" | "changed" | "removed" }[] {
  const out: {
    causal: string;
    command: string;
    ts: number;
    change: "added" | "changed" | "removed";
  }[] = [];
  // (what it names in each entry, read from the entry's namespace node by node: a few nodes, not every name)
  const trees = s.cache.trees(s.cb);
  const named = (c: Causal | undefined) =>
    c ? trees.hashesAt(c.namespace, name).join(" ") || undefined : undefined;
  let c = s.cb.store.causal(s.cb.store.head(branch));
  for (let i = 0; c && i < scan && out.length < limit; i++) {
    const now = named(c);
    const parent = c.parents[0];
    const p = parent ? s.cb.store.causal(parent) : undefined;
    const before = named(p);
    if (now !== before)
      out.push({
        causal: c.hash,
        command: entryTitle(c),
        ts: c.ts,
        change: !before ? "added" : !now ? "removed" : "changed",
      });
    if (!parent || (!now && !before && out.length)) break;
    c = p;
  }
  return out;
}

uses

used by