inPendingMerge

tcmts.cli.merge.inPendingMerge · #f4qky65k · term · runs on the server

uses tcmts.cli.style.yellow, which uses node:util

Run a command on a branch with a merge not finished (#yisipbwb): as merged, so what it changes settles the merge. Once nothing's left to settle, the branch moves to the merge (with every entry settling it); until then it goes back to where it was, and the merge waits on, as the command left it.

import type { Codebase } from "#codebase/tcmts/cli/codebase";
import { mergeLeft, pendingMerge } from "#codebase/tcmts/cli/merge";
import type { PendingMerge } from "#codebase/tcmts/cli/merge";
import { green, yellow } from "#codebase/tcmts/cli/style";

export async function inPendingMerge(
  cb: Codebase,
  run: () => number | Promise<number>,
): Promise<number> {
  const branch = cb.store.branch().name;
  const p = pendingMerge(cb, branch);
  if (!p) return run();
  if (cb.store.head() !== p.from) {
    console.log(
      yellow(
        `${branch} moved since merging ${p.theirs} into it began, so that merge is set aside: \`tcm merge --abort\` drops it (then merge again)`,
      ),
    );
    return run();
  }
  const own = cb.store.workOf(branch);
  cb.store.endBatch();
  cb.store.setHead(branch, p.head, `merge ${p.theirs}`, true);
  cb.store.setWorkOf(branch, p.work);
  cb.refreshShim(true);
  try {
    return await run();
  } finally {
    cb.store.endBatch();
    const left = mergeLeft(cb, p.before);
    if (!left.conflicts.length && !left.failed.length) {
      cb.store.deleteSetting(`merging ${branch}`);
      console.log(
        green(
          `\nmerged ${p.theirs} into ${branch}: nothing's left to settle, so ${branch} has the merge now`,
        ),
      );
    } else {
      const now: PendingMerge = { ...p, head: cb.store.head(), work: cb.store.workOf(branch) };
      cb.store.setHead(branch, p.from, `merge ${p.theirs} (kept apart)`, true);
      cb.store.setWorkOf(branch, own);
      cb.store.setSetting(`merging ${branch}`, JSON.stringify(now));
      cb.refreshShim(true);
    }
  }
}

uses

used by