namesElsewhere

tcmts.cli.merge.namesElsewhere · #7uw2udaj · term · runs anywhere

Names for definitions nothing on this branch names (the other side of a merge's, an old version), for showing code that uses them: the name of what replaced it here (an older x), or its name on another branch (main's first). Other branches' names are read once, when first needed.

import type { Codebase } from "#codebase/tcmts/cli/codebase";
import { shortHash } from "#codebase/tcmts/core/hash";

export function namesElsewhere(
  cb: Codebase,
): (hash: string) => { name: string; note: string } | undefined {
  let other: Map<string, { path: string; branch: string }> | undefined;
  return (h) => {
    const later = cb.store.patched(h);
    const now = later !== h ? cb.store.pathsOf(later)[0] : undefined;
    if (now) return { name: now, note: `an older ${now} (that's ${shortHash(later)} now)` };
    if (!other) {
      other = new Map();
      const here = cb.store.branch().name;
      const branches = cb.store.branches().filter((b) => b !== here);
      for (const b of [
        ...branches.filter((b) => b === "main"),
        ...branches.filter((b) => b !== "main"),
      ])
        for (const r of cb.store.names(b, true))
          if (!other.has(r.hash)) other.set(r.hash, { path: r.path, branch: b });
    }
    const o = other.get(h);
    return o && { name: o.path, note: `${o.path} on ${o.branch}` };
  };
}

uses

used by