/** * @module checkout-cmd * * Isomorphic execution engine for `vcs checkout `. * Switches active branch pointer in B2tFSGalaxyIbGib_V1 and projects target branch snapshot onto workspace. */ import { extractErrorMsg } from '@ibgib/helper-gib/dist/helpers/utils-helper.mjs'; import { joinPaths } from '@ibgib/helper-gib/dist/helpers/path-helper.mjs'; import { GLOBAL_LOG_A_LOT } from '../../../core-constants.mjs'; import { updateB2tFSGalaxyIbGib } from '../b2tfs-galaxy/b2tfs-galaxy-helpers.mjs'; import { VcsCheckoutOpts, VcsCheckoutResult } from './command-types.mjs'; import { getBranchAndRootItemInfosAndIbGibsAndSpaceFromGalaxyIbGib } from '../item/item-helpers.mjs'; import { B2TFS_BRANCH_DEFAULT_NAME } from '../branch/branch-constants.mjs'; import { getSpaceIdFromIb } from '../../../witness/space/metaspace/metaspace-innerspace/metaspace-innerspace-helper.mjs'; import { CMD_MODIFIER_WORKSPACE_SCAN } from '../../../witness/space/workspace-projection-space/workspace-projection-space-constants.mjs'; import { WorkspaceScanResultData } from '../../../witness/space/workspace-projection-space/workspace-projection-space-types.mjs'; const lcFile = `[checkout-cmd]`; const logalot = GLOBAL_LOG_A_LOT; export async function executeVcsCheckout({ metaspace, localSpace, targetBranchSpace, workspaceSpace, galaxyIbGib, branchName, force = false, b2tfsGalaxyRoot = '', }: VcsCheckoutOpts): Promise { const lc = `${lcFile}[${executeVcsCheckout.name}]`; try { if (logalot) { console.log(`${lc} starting... (I: 28b508922ce8249958263628c96e2726)`); } const trimmedBranchName = branchName?.trim(); if (!trimmedBranchName) { throw new Error(`Branch name is required for checkout (E: c896086f2d6917f73831f4d8676ff826)`); } const previousBranch = galaxyIbGib.data.policy?.defaultBranch || B2TFS_BRANCH_DEFAULT_NAME; // 1. Resolve branch, root item, and branch space from galaxy const { branchIbGib: latestTargetBranchIbGib, branchInfo: targetBranchInfo, branchSpace: resolvedTargetBranchSpace, rootItemAddr_latest: rootItemAddr, rootItemIbGib_latest: rootItem, } = await getBranchAndRootItemInfosAndIbGibsAndSpaceFromGalaxyIbGib({ galaxyIbGib, branchName: trimmedBranchName, metaspace, }); const effectiveTargetBranchSpace = targetBranchSpace || resolvedTargetBranchSpace; if (!rootItem) { throw new Error(`Root item not found for address '${rootItemAddr}' (E: ec0a28ab5a2e13f1889e3f016e64ca26)`); } // 2. Project branch snapshot onto workspace space if provided and worktree is fresh/empty (or forced) if (workspaceSpace) { let shouldProject = true; if (!force) { try { const scanRes = await workspaceSpace.witness({ data: { cmd: 'get', cmdModifiers: [CMD_MODIFIER_WORKSPACE_SCAN], }, }); const scanData = (scanRes?.data as any)?.scanResult as WorkspaceScanResultData; const fileCount = scanData?.files ? (scanData.files instanceof Map ? scanData.files.size : Object.keys(scanData.files).length) : 0; // If files already exist in this branch's worktree and not forced, do NOT overwrite working files on context switch if (fileCount > 0) { shouldProject = false; } } catch { shouldProject = true; } } if (shouldProject) { await workspaceSpace.witness({ data: { cmd: 'put', ibGibAddrs: [rootItemAddr], }, ibGibs: [rootItem], }); } } const worktreePath = trimmedBranchName === B2TFS_BRANCH_DEFAULT_NAME ? b2tfsGalaxyRoot : (b2tfsGalaxyRoot ? joinPaths(b2tfsGalaxyRoot, 'b', trimmedBranchName) : joinPaths('b', trimmedBranchName)); // Checkout does not mutate the shared galaxy repository DAG const updatedGalaxy = galaxyIbGib; return { previousBranch, currentBranch: trimmedBranchName, worktreePath, updatedGalaxy, }; } catch (error) { console.error(`${lc} ${extractErrorMsg(error)} (E: bfd24a20afeb3d0ee15bf52f427ba826)`); throw error; } finally { if (logalot) { console.log(`${lc} complete. (I: 0b4068603a0fba6058e8ca7a080df826)`); } } }