mergeText

tcmts.cli.merge.mergeText · #okxkkxhb · term · runs on the server

uses tcmts.cli.merge.silently, which uses process

Merge, as text, a definition both sides of a merge edited: its code as tcm view shows it (a page: its Markdown), the base's, ours and theirs, merged three-way (#r2tanrwe), then saved over both as tcm update saves it, if it typechecks, with nothing it says shown. The new hash, or undefined where it isn't merged (the edits overlap, it doesn't typecheck): then it stays a conflict.

import type { Codebase } from "#codebase/tcmts/cli/codebase";
import { isBytes } from "#codebase/tcmts/cli/files";
import { mergeShown, saveMerged, silently } from "#codebase/tcmts/cli/merge";
import type { DefRecord } from "#codebase/tcmts/core/types";

export function mergeText(
  cb: Codebase,
  name: string,
  base: string,
  ours: string,
  theirs: string,
): string | undefined {
  const recs = [base, ours, theirs].map((h) => cb.store.getDef(h));
  if (recs.some((r) => !r || r.kind !== recs[1]!.kind)) return undefined;
  if (recs[1]!.kind === "file" && recs.some((r) => isBytes(r!))) return undefined;
  const [b, o, t] = recs as DefRecord[];
  try {
    const merged = silently(() => mergeShown(cb, name, b, o!, t!));
    if (merged.conflicts) return undefined;
    return saveMerged(cb, name, o!, merged.text, { jsx: recs.some((r) => !!r!.jsx) });
  } catch {
    return undefined;
  }
}

uses

used by