onBranch

tcmts.cli.contributions.onBranch · #tpigmpwo · term · runs anywhere

Run fn with branch current, then go back to the branch that was.

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

export function onBranch<T>(cb: Codebase, branch: string, fn: () => T): T {
  const was = cb.store.branch().name;
  if (was === branch) return fn();
  cb.store.switchBranch(branch);
  try {
    return fn();
  } finally {
    cb.store.switchBranch(was);
    cb.refreshShim(true);
  }
}

uses

used by