/** * @module restore-cmd * * Isomorphic execution engine for `vcs restore [path...]`. * Restores working tree paths from committed branch snapshot, or unstages paths from galaxy. */ import { extractErrorMsg } from '@ibgib/helper-gib/dist/helpers/utils-helper.mjs'; import { GLOBAL_LOG_A_LOT } from '../../../core-constants.mjs'; import { unstagePathsFromGalaxy, restoreWorktreePathsFromSnapshot, getB2tFSGalaxyIbGib } from '../b2tfs-galaxy/b2tfs-galaxy-helpers.mjs'; import { VcsRestoreOpts, VcsRestoreResult } from './command-types.mjs'; import { getBranchInfoFromGalaxy, resolveContextualBranchName } from '../branch/branch-helpers.mjs'; const lcFile = `[restore-cmd]`; const logalot = GLOBAL_LOG_A_LOT; export async function executeVcsRestore({ metaspace, localSpace, workspaceSpace, galaxyIbGib: givenGalaxyIbGib, branchName: explicitBranchName, paths = [], staged = false, all = false, targetDir = '', activeWorktreeDir = '', }: VcsRestoreOpts): Promise { const lc = `${lcFile}[${executeVcsRestore.name}]`; try { if (logalot) { console.log(`${lc} starting... (I: cee216893bed53ec4459e1eb5dacf326)`); } const galaxyIbGib = givenGalaxyIbGib || await getB2tFSGalaxyIbGib({ metaspace, space: localSpace }); if (!galaxyIbGib) { throw new Error(`couldn't get galaxyIbGib from space (${localSpace.ib}) via metaspace. (E: b18aa4c434cad43bc828c5641e298d26)`); } const branchName = explicitBranchName || resolveContextualBranchName({ targetDir: activeWorktreeDir || targetDir, b2tfsGalaxyRoot: targetDir, galaxyIbGib, }); if (staged) { // UNSTAGE MODE (--staged) const unstageRes = await unstagePathsFromGalaxy({ galaxyIbGib, paths, targetDir, activeWorktreeDir, space: localSpace, metaspace, all, }); return { restoredPaths: [], unstagedPaths: unstageRes.unstagedPaths, updatedGalaxy: unstageRes.updatedGalaxy, }; } else { // RESTORE WORKING TREE MODE const restoreRes = await restoreWorktreePathsFromSnapshot({ paths, targetDir, activeWorktreeDir, branchName, workspaceSpace, metaspace, galaxyIbGib, }); return { restoredPaths: restoreRes.restoredPaths, unstagedPaths: [], updatedGalaxy: galaxyIbGib, }; } } catch (error) { console.error(`${lc} ${extractErrorMsg(error)} (E: e9378836e2e8d5068b0b02c873ae6626)`); throw error; } finally { if (logalot) { console.log(`${lc} complete. (I: 6e00780e0d9a8c91280df4051e660826)`); } } }