keepTexts

tcmts.cli.merge.keepTexts · #rkwj5h4h · term · runs on the server

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

A change one side of a merge made to a definition's comments, layout or doc where the other side changed its code (which mergeTexts can't keep: it's a text of the old code), merged as text into the code the merge keeps, three-way from the base's text: kept where that merges cleanly and leaves the code as it is (the same hash). Saved as an entry of command's. What's kept: the definitions whose text changed, and the names now showing it.

import type { Codebase } from "#codebase/tcmts/cli/codebase";
import { keyPath } from "#codebase/tcmts/cli/diff";
import type { NameState } from "#codebase/tcmts/cli/diff";
import { mergeShown, saveMerged, silently } from "#codebase/tcmts/cli/merge";

export function keepTexts(
  cb: Codebase,
  lost: string[],
  texts: {
    base: ReadonlyMap<string, string>;
    ours: ReadonlyMap<string, string>;
    theirs: ReadonlyMap<string, string>;
  },
  states: { ours: NameState; theirs: NameState },
  command: string,
): { hashes: string[]; paths: string[] } {
  const hashes = new Set<string>();
  const paths = new Set<string>();
  for (const h of lost)
    for (const side of ["ours", "theirs"] as const) {
      if (texts[side].get(h) === texts.base.get(h)) continue;
      const rec = cb.store.getDef(h);
      if (!rec || rec.kind === "file") continue;
      for (const [key, hs] of states[side]) {
        if (!hs.has(h)) continue;
        const path = keyPath(key);
        const now = cb.store.hashesAt(path, rec.kind);
        if (now.length !== 1 || now[0] === h) continue;
        const merged = cb.store.getDef(now[0]!)!;
        try {
          const r = silently(() =>
            mergeShown(
              cb,
              path,
              cb.store.defWithText(h, texts.base.get(h)),
              cb.store.defWithText(h, texts[side].get(h))!,
              merged,
            ),
          );
          if (
            !r.conflicts &&
            saveMerged(cb, path, merged, r.text, { jsx: !!merged.jsx, textOnly: true, command })
          ) {
            hashes.add(h);
            paths.add(path);
          }
        } catch {
          // (left as dropped: said so)
        }
      }
    }
  return { hashes: [...hashes], paths: [...paths] };
}

uses

used by