runMergeSwitch

tcmts.cli.run.runMergeSwitch · #u4vbmv5f · term · runs on the server

uses tcmts.cli.main.execute, which uses tcmts.cli.help.usageError, which uses tcmts.cli.help.usageOf, which uses tcmts.cli.style.bold, which uses node:util

Runs tcm merge and tcm switch.

import { routeMerge, routeSwitch } from "#codebase/tcmts/cli/commands";
import { execute } from "#codebase/tcmts/cli/main";
import { abortMerge, landBranch, mergeApart } from "#codebase/tcmts/cli/merge";
import type { Context } from "#codebase/tcmts/cli/run";
import { choose } from "#codebase/tcmts/cli/ui";
import { parse } from "@bomb.sh/router";

export async function runMergeSwitch({ cb, rest, ...cx }: Context): Promise<number> {
  const cmd = cx.cmd as "merge" | "switch";
  const { model } = execute(
    cmd,
    parse({ merge: routeMerge, switch: routeSwitch }[cmd](), { argv: rest }),
  );
  if (cmd === "merge" && "abort" in model && model.abort) return abortMerge(cb);
  const others = cb.store.branches().filter((b) => b !== cb.store.branch().name);
  const branch =
    model.branch ??
    (await choose(
      `${cmd} <branch>`,
      cmd === "merge" ? "Merge which branch?" : "Switch to?",
      others.map((b) => ({ value: b, label: b })),
    ));
  if (cmd === "merge") {
    const into = cb.store.branch().name;
    const how = {
      noFastForward: "no-fast-forward" in model && !!model["no-fast-forward"],
      squash: "squash" in model && !!model.squash,
    };
    // (a remote's branch, `origin/main`, is what it has: merged as anything is, never landed)
    const remote =
      branch.includes("/") &&
      !!cb.store.getSetting(`remote.${branch.slice(0, branch.indexOf("/"))}`);
    // on main, or squashed (how sessions land), a branch lands; a merge into main is made only if nothing's left to
    // settle; one into another branch that isn't waits apart until it's settled
    if ((into === "main" || how.squash) && !remote && branch !== into)
      return landBranch(cb, branch, how);
    const r = mergeApart(cb, branch, { ...how, keep: into !== "main", scratch: cx.scratch });
    return r.left.conflicts.length || r.left.failed.length ? 1 : 0;
  }
  cb.store.switchBranch(branch);
  cb.refreshShim(true);
  console.log(`on branch ${branch}`);
  return 0;
}

uses

used by