/** * @module cosmology/workspace/cosmology-workspace-service * * Unified Cosmology Workspace Service. * Serves as the single, cosmology-native API for initializing, verifying, * mutating, committing, and two-phase syncing data across Cosmoses, Galaxies, and Branch Spaces. */ import { extractErrorMsg, getUUID, hash } from '@ibgib/helper-gib/dist/helpers/utils-helper.mjs'; import { getIbGibAddr } from '@ibgib/ts-gib/dist/helper.mjs'; import { GLOBAL_LOG_A_LOT } from '../../core-constants.mjs'; import { MetaspaceService } from '../../witness/space/metaspace/metaspace-types.mjs'; import { IbGibSpaceAny } from '../../witness/space/space-base-v1.mjs'; import { lockSpace, unlockSpace, persistTransformResult } from '../../witness/space/space-helper.mjs'; import { getIbGibFromSpace, } from '../../common/other/ibgib-helper.mjs'; import { createBinIbGib } from '../../common/bin/bin-helpers.mjs'; import { CosmosIbGib_V1, CosmosGalaxyEntry, } from '../cosmos/cosmos-types.mjs'; import { createCosmosIbGib, getCosmosByName, getAllCosmoses, getGalaxyInfoFromCosmos, getGalaxiesFromCosmos, } from '../cosmos/cosmos-helpers.mjs'; import { executeCreateGalaxyInCosmos } from '../cosmos/cosmos-commands.mjs'; import { GalaxyPresetType } from '../galaxy/galaxy-configs.mjs'; import { B2tFSGalaxyIbGib_V1, B2tFSGalaxyData_V1 } from '../branching/b2tfs-galaxy/b2tfs-galaxy-types.mjs'; import { updateB2tFSGalaxyIbGib } from '../branching/b2tfs-galaxy/b2tfs-galaxy-helpers.mjs'; import { B2tFSBranchIbGib_V1 } from '../branching/branch/branch-types.mjs'; import { B2TFS_BRANCH_DEFAULT_NAME } from '../branching/branch/branch-constants.mjs'; import { getBranchInfoFromGalaxy, resolveContextualBranchName, ensureBranchSpace, } from '../branching/branch/branch-helpers.mjs'; import { createB2tFSItemIbGib, } from '../branching/item/item-helpers.mjs'; import { B2tFSItemIbGib_V1, BINARY_REL8N_NAME } from '../branching/item/item-types.mjs'; import { VCS_DEFAULT_FOLDER_NAME } from '../branching/branching-constants.mjs'; import { WorkspaceProjectionSpaceFactory, WorkspaceCmdOpts, WorkspaceCmdOptsSansIndex, WorkspaceCmdOptsSansIndexAndBranch, } from './cosmology-workspace-types.mjs'; import { VcsInitOpts, VcsInitResult, executeVcsInit, VcsAddOpts, VcsAddResult, executeVcsAdd, VcsCommitOpts, VcsCommitResult, executeVcsCommit, VcsBranchOpts, VcsBranchResult, executeVcsBranch, VcsCheckoutOpts, VcsCheckoutResult, executeVcsCheckout, VcsResetOpts, VcsResetResult, executeVcsReset, VcsRestoreOpts, VcsRestoreResult, executeVcsRestore, VcsMergeOpts, VcsMergeResult, executeVcsMerge, VcsLogOpts, VcsLogResult, executeVcsLog, VcsStatusOpts, VcsStatusResult, executeVcsStatus, VcsGenerateCommentResult, executeVcsGenerateComment, VcsPushResult, executeVcsPush, VcsCloneResult, executeVcsClone, VcsForkResult, executeVcsFork, } from '../branching/commands/index.mjs'; import { CosmologyWorkspaceServiceOptions, InitCosmosOpts, EnsureCosmosOpts, InitGalaxyOpts, InitGalaxyResult, EnsureGalaxyOpts, EnsureGalaxyResult, InitBranchOpts, InitBranchResult, EnsureBranchOpts, EnsureBranchResult, CommitItemsOpts, WorkspacePushOpts, WorkspaceCloneOpts, WorkspaceForkOpts, } from './cosmology-workspace-types.mjs'; const lcFile = `[CosmologyWorkspaceService]`; const logalot = GLOBAL_LOG_A_LOT; export class CosmologyWorkspaceService { protected lc: string = lcFile; public metaspace: MetaspaceService; public localSpace: IbGibSpaceAny; public defaultCosmosName?: string; public defaultGalaxyName?: string; public defaultGalaxyAddr?: string; public defaultBranchName?: string; public b2tfsGalaxyRoot: string; public cosmosRoot: string; public vcsFolderName: string; public workspaceSpace?: IbGibSpaceAny; public fnGetWorkspaceProjectionSpace?: WorkspaceProjectionSpaceFactory; public enableBranchLocking: boolean; constructor(options: CosmologyWorkspaceServiceOptions) { if (!options.metaspace) { throw new Error(`${this.lc} metaspace is required. (E: ed6cb5be45610b84d75e41e8541459a7)`); } if (!options.localSpace) { throw new Error(`${this.lc} localSpace is required. (E: 42b28dbbe61d95daf8b1aa30270eb689)`); } this.metaspace = options.metaspace; this.localSpace = options.localSpace; this.defaultCosmosName = options.cosmosName; this.defaultGalaxyName = options.galaxyName; this.defaultGalaxyAddr = options.galaxyAddr; this.defaultBranchName = options.branchName; this.b2tfsGalaxyRoot = options.b2tfsGalaxyRoot || ''; this.cosmosRoot = options.cosmosRoot || options.b2tfsGalaxyRoot || ''; this.vcsFolderName = options.vcsFolderName || VCS_DEFAULT_FOLDER_NAME; this.workspaceSpace = options.workspaceSpace; this.fnGetWorkspaceProjectionSpace = options.fnGetWorkspaceProjectionSpace; this.enableBranchLocking = options.enableBranchLocking ?? true; } // #region Cosmos Methods (Fail-Fast Lifecycle) /** * Initializes a brand-new Cosmos in localSpace. * Fails fast (throws) if a Cosmos with this name already exists. */ public async initCosmos(opts: InitCosmosOpts): Promise { const lc = `${this.lc}[${this.initCosmos.name}]`; const { cosmosName, description, ownerKeystoneTjp, initialGalaxies } = opts; const existing = await getCosmosByName({ metaspace: this.metaspace, space: this.localSpace, cosmosName, }); if (existing) { throw new Error(`${lc} Cosmos "${cosmosName}" is already initialized in localSpace. (E: 13a750fdf4c31cd679b5b8fcac45f731)`); } let cosmosIbGib = await createCosmosIbGib({ cosmosName, description, ownerKeystoneTjp, space: this.localSpace, metaspace: this.metaspace, }); if (initialGalaxies && initialGalaxies.length > 0) { for (const gal of initialGalaxies) { const galRes = await this.initGalaxy({ cosmosName, galaxyName: gal.galaxyName, discriminator: gal.discriminator, branchName: gal.branchName, rootFolderName: gal.rootFolderName, policy: gal.policy, }); cosmosIbGib = galRes.cosmosIbGib; } } if (logalot) { console.log(`${lc} Successfully initialized Cosmos "${cosmosName}"`); } return cosmosIbGib; } /** * Ensures an existing Cosmos in localSpace. * Fails fast (throws) if the Cosmos does not exist. Never initializes. */ public async ensureCosmos(opts?: EnsureCosmosOpts): Promise { const lc = `${this.lc}[${this.ensureCosmos.name}]`; const cosmosName = opts?.cosmosName || this.defaultCosmosName; if (!cosmosName) { throw new Error(`${lc} cosmosName is required. (E: 94467bc591832cf944f2992a8c5982c4)`); } const cosmos = await getCosmosByName({ metaspace: this.metaspace, space: this.localSpace, cosmosName, }); if (!cosmos) { throw new Error(`${lc} Cosmos "${cosmosName}" not found in localSpace (${this.localSpace.ib}). Expected an already-initialized Cosmos. (E: 8a8b8816ce0bb029742ebe5641cb92b5)`); } return cosmos; } /** * Lists all registered Cosmoses in localSpace. */ public async listCosmoses(): Promise { return await getAllCosmoses({ metaspace: this.metaspace, space: this.localSpace, }); } // #endregion // #region Galaxy Methods (Fail-Fast Lifecycle) /** * Initializes a new member Galaxy in a containing Cosmos. * Fails fast (throws) if the Galaxy already exists in the Cosmos. */ public async initGalaxy(opts: InitGalaxyOpts): Promise { const lc = `${this.lc}[${this.initGalaxy.name}]`; const cosmosName = opts.cosmosName || this.defaultCosmosName; if (!cosmosName) { throw new Error(`${lc} cosmosName is required. (E: 468e0e7ec35f14d290871747c5b5b154)`); } const cosmosIbGib = await this.ensureCosmos({ cosmosName }); if (cosmosIbGib.data.galaxies?.[opts.galaxyName]) { throw new Error(`${lc} Galaxy "${opts.galaxyName}" already exists in Cosmos "${cosmosName}". (E: 89c9bac2d2f957e0544762cca49baa00)`); } const branchName = opts.branchName || B2TFS_BRANCH_DEFAULT_NAME; const createRes = await executeCreateGalaxyInCosmos({ metaspace: this.metaspace, space: this.localSpace, cosmosIbGib, galaxyName: opts.galaxyName, discriminator: opts.discriminator || GalaxyPresetType.code, branchName, rootFolderName: opts.rootFolderName || 'root', policy: opts.policy, primaryIdentityTjpAddr: opts.primaryIdentityTjpAddr, deviceDelegate: opts.deviceDelegate, slice: opts.slice, icon: opts.icon, label: opts.label, }); const branchSpaceRes = await ensureBranchSpace({ metaspace: this.metaspace, branchName, galaxyName: opts.galaxyName, cosmosName, spaceId: createRes.initRes.spaceId, }); return { cosmosIbGib: createRes.cosmosIbGib, galaxyIbGib: createRes.galaxyIbGib, sqGalaxyAddr: createRes.sqGalaxyAddr, branchSpace: branchSpaceRes.space, branchIbGib: createRes.initRes.branchIbGib, initRes: createRes.initRes, }; } /** * Ensures an existing member Galaxy in a containing Cosmos. * Fails fast (throws) if the Galaxy or Cosmos does not exist. Never initializes. */ public async ensureGalaxy(opts?: EnsureGalaxyOpts): Promise { const lc = `${this.lc}[${this.ensureGalaxy.name}]`; const cosmosName = opts?.cosmosName || this.defaultCosmosName; let galaxyName = opts?.galaxyName || this.defaultGalaxyName; if (!cosmosName) { throw new Error(`${lc} cosmosName is required. (E: d207ce52200b5aa823f86ae728ba7217)`); } const cosmosIbGib = await this.ensureCosmos({ cosmosName }); if (!galaxyName && this.defaultGalaxyAddr) { const galaxies = cosmosIbGib.data.galaxies || {}; for (const [name, entry] of Object.entries(galaxies)) { if (entry.galaxyAddr === this.defaultGalaxyAddr) { galaxyName = name; break; } } } if (!galaxyName) { const galaxies = cosmosIbGib.data.galaxies || {}; const keys = Object.keys(galaxies); if (keys.length === 1) { galaxyName = keys[0]; } } if (!galaxyName) { throw new Error(`${lc} galaxyName is required. (E: 91df5144b36e66f4a2e56d73ee9f8080)`); } const entry = getGalaxyInfoFromCosmos(cosmosIbGib, galaxyName); if (!entry) { throw new Error(`${lc} Galaxy "${galaxyName}" not found in Cosmos "${cosmosName}". Expected an already-initialized Galaxy. (E: ea2bfdc3aa077e509f2d51455e786ed2)`); } if (!entry.galaxyAddr) { throw new Error(`${lc} Galaxy entry for "${galaxyName}" in Cosmos "${cosmosName}" is missing galaxyAddr. (E: fe88ebae766cf22d121ad6e8eed66a5d)`); } const latestGalaxyAddr = (await this.metaspace.getLatestAddr({ addr: entry.galaxyAddr, space: this.localSpace, })) || entry.galaxyAddr; const galaxyIbGib = await getIbGibFromSpace({ addr: latestGalaxyAddr, space: this.localSpace, }); if (!galaxyIbGib) { throw new Error(`${lc} Galaxy IbGib for "${galaxyName}" not found in localSpace (addr: ${latestGalaxyAddr}). (E: b376ac62a0b2ef67ee8666e84e88267d)`); } return { cosmosIbGib, galaxyEntry: entry, galaxyIbGib, }; } /** * Lists all member Galaxy manifest entries in a Cosmos. */ public async listGalaxies(opts?: { cosmosName?: string }): Promise { const cosmos = await this.ensureCosmos(opts); return getGalaxiesFromCosmos(cosmos); } // #endregion // #region Branch Methods (Fail-Fast Lifecycle) /** * Initializes a new branch in a Galaxy. * Fails fast (throws) if the branch already exists. */ public async initBranch(opts: InitBranchOpts): Promise { const lc = `${this.lc}[${this.initBranch.name}]`; const cosmosName = opts.cosmosName || this.defaultCosmosName; const galaxyName = opts.galaxyName || this.defaultGalaxyName; const { cosmosIbGib, galaxyIbGib } = await this.ensureGalaxy({ cosmosName, galaxyName: galaxyName!, }); const branchRes = await executeVcsBranch({ metaspace: this.metaspace, localSpace: this.localSpace, galaxyIbGib, branchName: opts.branchName, sourceBranchName: opts.fromBranchName, itemPath: opts.fromItemAddr, b2tfsGalaxyRoot: this.b2tfsGalaxyRoot, targetDir: opts.targetDir || this.b2tfsGalaxyRoot, vcsFolderName: this.vcsFolderName, }); const ensureRes = await this.ensureBranch({ cosmosName, galaxyName, branchName: opts.branchName, }); return { cosmosIbGib, galaxyIbGib: ensureRes.galaxyIbGib, branchInfo: ensureRes.branchInfo, branchSpace: ensureRes.branchSpace, branchIbGib: ensureRes.branchIbGib, branchRes, }; } /** * Ensures an existing branch in a Galaxy and resolves its dedicated branch space. * Fails fast (throws) if the branch or its dedicated branch space does not exist. Never initializes. */ public async ensureBranch(opts?: EnsureBranchOpts): Promise { const lc = `${this.lc}[${this.ensureBranch.name}]`; const cosmosName = opts?.cosmosName || this.defaultCosmosName; const galaxyName = opts?.galaxyName || this.defaultGalaxyName; const { cosmosIbGib, galaxyIbGib } = await this.ensureGalaxy({ cosmosName, galaxyName: galaxyName!, }); const targetBranchName = opts?.branchName || this.defaultBranchName || galaxyIbGib.data.policy?.defaultBranch || B2TFS_BRANCH_DEFAULT_NAME; let branchInfo: any; try { branchInfo = await getBranchInfoFromGalaxy({ galaxyIbGib, branchName: targetBranchName, metaspace: this.metaspace, }); } catch { throw new Error(`${lc} Branch "${targetBranchName}" not found in Galaxy "${galaxyName}" (Cosmos: "${cosmosName}"). (E: d18da7fb63f7ad176c158f2f4e88e419)`); } const { space: branchSpace } = await ensureBranchSpace({ metaspace: this.metaspace, branchName: targetBranchName, galaxyName, cosmosName, spaceId: branchInfo.branchId, }); const latestBranchAddr = await this.metaspace.getLatestAddr({ addr: branchInfo.branchAddr, space: branchSpace, }) || branchInfo.branchAddr; const branchIbGib = await getIbGibFromSpace({ addr: latestBranchAddr, space: branchSpace, }); if (!branchIbGib) { throw new Error(`${lc} Branch IbGib for "${targetBranchName}" not found in branch space. (E: 20e93c8dfbb067a7bc66b990ca2d2c74)`); } return { cosmosIbGib, galaxyIbGib, branchInfo, branchSpace, branchIbGib, }; } // #endregion // #region Direct Programmatic Commit (commitItems) /** * Executes a mutating branch operation wrapped with in-space lockSpace coordination. */ protected async withBranchLock(space: IbGibSpaceAny | undefined, fn: () => Promise): Promise { const lc = `${this.lc}[${this.withBranchLock.name}]`; const BRANCH_MUTATION_SCOPE = 'vcs_branch_mutation'; if (!this.enableBranchLocking || !space) { return await fn(); } let lockRes: any; try { lockRes = await lockSpace({ space, scope: BRANCH_MUTATION_SCOPE, secondsValid: 60, }); } catch (error) { if (logalot) { console.log(`${lc} error when locking space: ${extractErrorMsg(error)} (I: 903cc83c8a0a0d9762038989f53b7a26)`); } } try { return await fn(); } finally { if (lockRes && lockRes.data?.success) { try { await unlockSpace({ space, scope: BRANCH_MUTATION_SCOPE, }); } catch (error) { if (logalot) { console.log(`${lc} error when unlocking space: ${extractErrorMsg(error)} (I: a57948352efad2cf08b49d988f532826)`); } } } } } /** * Programmatically commits file items and optional companion comments directly into * a Galaxy branch without requiring a physical workspace projection on disk. */ public async commitItems(opts: CommitItemsOpts): Promise { const lc = `${this.lc}[${this.commitItems.name}]`; const { cosmosName = this.defaultCosmosName, galaxyName = this.defaultGalaxyName, branchName = this.defaultBranchName, items, message, author = 'vcs-user', primaryIdentityTjpAddr, deviceDelegate, } = opts; if (!items || items.length === 0) { throw new Error(`${lc} items array cannot be empty. (E: 9fac131149801bc4b2af845844aaca48)`); } const { cosmosIbGib, galaxyIbGib, branchInfo, branchSpace, branchIbGib, } = await this.ensureBranch({ cosmosName, galaxyName, branchName }); return await this.withBranchLock(branchSpace, async () => { // Stage items directly into galaxy stagedPaths const stagedPaths: Record = {}; for (const item of items) { const normPath = item.path.replace(/\\/g, '/').replace(/^\/+/, ''); if (item.action === 'delete') { stagedPaths[normPath] = { action: 'delete', fsType: 'file', relPath: normPath, stagedAt: new Date().toISOString(), isDeleted: true, }; } else { const contentBytes = typeof item.content === 'string' ? new TextEncoder().encode(item.content) : (item.content || new Uint8Array()); const contentHash = await hash({ s: typeof item.content === 'string' ? item.content : String(item.content) }); const binRes = await createBinIbGib({ data: contentBytes, space: branchSpace, metaspace: this.metaspace, }); const binIbGib = binRes.binIbGib; const binAddr = binRes.binAddr; // Create file item const filename = normPath.split('/').pop() || 'item'; const fileItemRes = await createB2tFSItemIbGib({ fsType: 'file', name: filename, data: { fileHash: contentHash, }, rel8ns: { [BINARY_REL8N_NAME]: [binAddr], }, }); const fileItem = fileItemRes.newIbGib as B2tFSItemIbGib_V1; await persistTransformResult({ resTransform: fileItemRes, space: branchSpace }); await this.metaspace.registerNewIbGib({ ibGib: fileItem, space: branchSpace }); const itemAddr = getIbGibAddr({ ibGib: fileItem }); stagedPaths[normPath] = { action: 'create', fsType: 'file', relPath: normPath, itemAddr, fileHash: contentHash, stagedAt: new Date().toISOString(), }; } } // Mutate galaxy with staged items const patchedGalaxy = await updateB2tFSGalaxyIbGib({ galaxyIbGib, space: this.localSpace, metaspace: this.metaspace, dataToUpdate: { stagedPaths, }, }); // Execute commit const commitRes = await executeVcsCommit({ metaspace: this.metaspace, localSpace: this.localSpace, branchName: branchInfo.branchName, activeBranchSpace: branchSpace, branchSpace, galaxyIbGib: patchedGalaxy, message, author, b2tfsGalaxyRoot: this.b2tfsGalaxyRoot || 'in-memory', targetDir: this.b2tfsGalaxyRoot || 'in-memory', primaryIdentityTjpAddr, deviceDelegate, }); return commitRes; }); } // #endregion // #region Worktree & Facade Operations public async getGalaxyIbGib(): Promise { const { galaxyIbGib } = await this.ensureGalaxy(); return galaxyIbGib; } public async getActiveBranchName(opts?: { galaxyIbGib?: B2tFSGalaxyIbGib_V1; targetDir?: string }): Promise { const galaxy = opts?.galaxyIbGib || await this.getGalaxyIbGib(); if (!galaxy) return undefined; if (!this.b2tfsGalaxyRoot) { return galaxy.data.policy?.defaultBranch || B2TFS_BRANCH_DEFAULT_NAME; } return resolveContextualBranchName({ targetDir: opts?.targetDir || this.b2tfsGalaxyRoot, b2tfsGalaxyRoot: this.b2tfsGalaxyRoot, galaxyIbGib: galaxy, }); } public async getActiveBranchSpace(opts?: B2tFSGalaxyIbGib_V1 | { galaxyIbGib?: B2tFSGalaxyIbGib_V1; targetDir?: string; branchName?: string }): Promise { let branchName: string | undefined; let targetDir: string | undefined; let galaxyIbGib: B2tFSGalaxyIbGib_V1 | undefined; if (opts && 'ib' in opts) { galaxyIbGib = opts as B2tFSGalaxyIbGib_V1; } else if (opts) { galaxyIbGib = (opts as any).galaxyIbGib; targetDir = (opts as any).targetDir; branchName = (opts as any).branchName; } if (!branchName) { branchName = await this.getActiveBranchName({ galaxyIbGib, targetDir }); } const { branchSpace } = await this.ensureBranch({ branchName }); return branchSpace; } public async getActiveWorkspaceProjectionSpace({ galaxyIbGib, targetDir, activeBranchSpace, }: { galaxyIbGib: B2tFSGalaxyIbGib_V1; targetDir?: string; activeBranchSpace?: IbGibSpaceAny; }): Promise { if (this.fnGetWorkspaceProjectionSpace) { const branchName = resolveContextualBranchName({ targetDir: targetDir || this.b2tfsGalaxyRoot, b2tfsGalaxyRoot: this.b2tfsGalaxyRoot, galaxyIbGib, }); const worktreeDir = branchName === (galaxyIbGib.data.policy?.defaultBranch || B2TFS_BRANCH_DEFAULT_NAME) ? this.b2tfsGalaxyRoot : `${this.b2tfsGalaxyRoot}/b/${branchName}`; const backingSpace = activeBranchSpace || await this.getActiveBranchSpace({ galaxyIbGib, targetDir }); return await this.fnGetWorkspaceProjectionSpace({ branchName, worktreeDir, backingSpace }); } return this.workspaceSpace; } public async status(opts?: Partial): Promise { const galaxyIbGib = opts?.galaxyIbGib || await this.getGalaxyIbGib(); const branchName = opts?.branchName || await this.getActiveBranchName({ galaxyIbGib, targetDir: opts?.targetDir }); const activeBranchSpace = await this.getActiveBranchSpace({ galaxyIbGib, branchName, targetDir: opts?.targetDir }); const workspaceSpace = opts?.workspaceSpace || await this.getActiveWorkspaceProjectionSpace({ galaxyIbGib, targetDir: opts?.targetDir, activeBranchSpace }); return await executeVcsStatus({ metaspace: this.metaspace, localSpace: this.localSpace, galaxyIbGib, activeBranchSpace, branchSpace: activeBranchSpace, workspaceSpace, b2tfsGalaxyRoot: this.b2tfsGalaxyRoot, targetDir: opts?.targetDir || this.b2tfsGalaxyRoot, vcsFolderName: this.vcsFolderName, ...opts, branchName, }); } public async add(paths: string[] = [], opts?: Partial): Promise { if (!this.b2tfsGalaxyRoot) { throw new Error(`this.b2tfsGalaxyRoot required for vcs add. Run init first. (E: 24dd25a7fdb80102c802a84faeb9b826)`); } const galaxyIbGib = opts?.galaxyIbGib || await this.getGalaxyIbGib(); const branchName = opts?.branchName || await this.getActiveBranchName({ galaxyIbGib, targetDir: opts?.targetDir }); const activeBranchSpace = await this.getActiveBranchSpace({ galaxyIbGib, branchName, targetDir: opts?.targetDir }); const workspaceSpace = opts?.workspaceSpace || await this.getActiveWorkspaceProjectionSpace({ galaxyIbGib, targetDir: opts?.targetDir, activeBranchSpace }); if (!workspaceSpace) { throw new Error(`workspaceSpace or fnGetWorkspaceProjectionSpace required. (E: 52d48219f9587517911dbcfed15c8826)`); } return await executeVcsAdd({ metaspace: this.metaspace, localSpace: this.localSpace, galaxyIbGib, branchName, activeBranchSpace, branchSpace: activeBranchSpace, workspaceSpace, paths, all: opts?.all, b2tfsGalaxyRoot: this.b2tfsGalaxyRoot, targetDir: opts?.targetDir || this.b2tfsGalaxyRoot, vcsFolderName: this.vcsFolderName, }); } public async commit(opts: WorkspaceCmdOptsSansIndexAndBranch): Promise { const galaxyIbGib = await this.getGalaxyIbGib(); const branchName = (opts as any)?.branchName || await this.getActiveBranchName({ galaxyIbGib, targetDir: opts?.targetDir }); const activeBranchSpace = await this.getActiveBranchSpace({ galaxyIbGib, branchName, targetDir: opts?.targetDir }); const workspaceSpace = (opts as any)?.workspaceSpace || await this.getActiveWorkspaceProjectionSpace({ galaxyIbGib, targetDir: opts?.targetDir, activeBranchSpace }); return await this.withBranchLock(activeBranchSpace, async () => { return await executeVcsCommit({ metaspace: this.metaspace, localSpace: this.localSpace, branchName, activeBranchSpace, branchSpace: activeBranchSpace, workspaceSpace, galaxyIbGib, message: opts.message, author: opts.author, b2tfsGalaxyRoot: this.b2tfsGalaxyRoot, targetDir: opts.targetDir || this.b2tfsGalaxyRoot, vcsFolderName: this.vcsFolderName, keepEmptyFolders: opts?.keepEmptyFolders, primaryIdentityTjpAddr: (opts as any)?.primaryIdentityTjpAddr, deviceDelegate: (opts as any)?.deviceDelegate, cosmosInfo: (opts as any)?.cosmosInfo, galaxyInfo: (opts as any)?.galaxyInfo, originSnapshotInfos: (opts as any)?.originSnapshotInfos, }); }); } public async branch(opts: WorkspaceCmdOptsSansIndexAndBranch): Promise { const galaxyIbGib = await this.getGalaxyIbGib(); const activeBranchSpace = await this.getActiveBranchSpace({ galaxyIbGib, branchName: opts?.sourceBranchName, targetDir: opts?.targetDir }); return await executeVcsBranch({ metaspace: this.metaspace, localSpace: this.localSpace, galaxyIbGib, activeBranchSpace, sourceBranchSpace: activeBranchSpace, branchName: opts?.branchName, sourceBranchName: opts?.sourceBranchName, currentBranchName: opts?.currentBranchName, itemPath: opts?.itemPath, b2tfsGalaxyRoot: this.b2tfsGalaxyRoot, targetDir: opts?.targetDir || this.b2tfsGalaxyRoot, vcsFolderName: this.vcsFolderName, cosmosInfo: { name: this.defaultCosmosName || (galaxyIbGib.data as any)?.cosmosName }, }); } public async checkout(opts: WorkspaceCmdOptsSansIndex): Promise { const trimmedBranchName = opts.branchName?.trim(); if (!trimmedBranchName) { throw new Error(`Branch name is required for checkout (E: e081280b32c8ffd8a8f4fd98e9f53826)`); } const galaxyIbGib = await this.getGalaxyIbGib(); let targetWorkspaceSpace = this.workspaceSpace; let effectiveTargetBranchSpace = opts.targetBranchSpace; if (this.fnGetWorkspaceProjectionSpace && this.b2tfsGalaxyRoot) { const targetWorktreeDir = trimmedBranchName === B2TFS_BRANCH_DEFAULT_NAME ? this.b2tfsGalaxyRoot : `${this.b2tfsGalaxyRoot}/b/${trimmedBranchName}`; if (!effectiveTargetBranchSpace) { const bInfo = await getBranchInfoFromGalaxy({ galaxyIbGib, branchName: trimmedBranchName, metaspace: this.metaspace, }); const { space: branchSpace } = await ensureBranchSpace({ metaspace: this.metaspace, branchName: trimmedBranchName, galaxyName: galaxyIbGib.data?.galaxyName, cosmosName: this.defaultCosmosName || galaxyIbGib.data?.cosmosName, spaceId: bInfo.branchId, }); effectiveTargetBranchSpace = branchSpace; } if (effectiveTargetBranchSpace) { targetWorkspaceSpace = await this.fnGetWorkspaceProjectionSpace({ branchName: trimmedBranchName, worktreeDir: targetWorktreeDir, backingSpace: effectiveTargetBranchSpace, }); } } return await this.withBranchLock(effectiveTargetBranchSpace || opts.targetBranchSpace, async () => { const res = await executeVcsCheckout({ metaspace: this.metaspace, localSpace: this.localSpace, galaxyIbGib, targetBranchSpace: effectiveTargetBranchSpace || opts.targetBranchSpace, workspaceSpace: targetWorkspaceSpace, branchName: trimmedBranchName, force: opts.force, b2tfsGalaxyRoot: this.b2tfsGalaxyRoot, targetDir: opts.targetDir || this.b2tfsGalaxyRoot, vcsFolderName: this.vcsFolderName, }); if (targetWorkspaceSpace) { this.workspaceSpace = targetWorkspaceSpace; } return res; }); } public async reset(opts: WorkspaceCmdOpts): Promise { const galaxyIbGib = await this.getGalaxyIbGib(); const activeBranchSpace = await this.getActiveBranchSpace({ galaxyIbGib, targetDir: opts?.targetDir }); return await executeVcsReset({ metaspace: this.metaspace, localSpace: this.localSpace, galaxyIbGib, activeBranchSpace, paths: opts?.paths, all: opts?.all, targetDir: opts?.targetDir, activeWorktreeDir: opts?.activeWorktreeDir, }); } public async restore(opts: Partial): Promise { const galaxyIbGib = opts?.galaxyIbGib || await this.getGalaxyIbGib(); const branchName = resolveContextualBranchName({ targetDir: opts?.targetDir || this.b2tfsGalaxyRoot, b2tfsGalaxyRoot: this.b2tfsGalaxyRoot, galaxyIbGib, }); const activeBranchSpace = await this.getActiveBranchSpace({ galaxyIbGib, targetDir: opts?.targetDir, branchName, }); const effectiveWorkspaceSpace = await this.getActiveWorkspaceProjectionSpace({ galaxyIbGib, targetDir: opts?.targetDir, activeBranchSpace, }); if (!effectiveWorkspaceSpace) { throw new Error(`workspaceSpace or fnGetWorkspaceProjectionSpace required. (E: 466ff89709f5e3fa485bec6df9375826)`); } return await executeVcsRestore({ metaspace: this.metaspace, localSpace: this.localSpace, workspaceSpace: effectiveWorkspaceSpace, galaxyIbGib, paths: opts?.paths, staged: opts?.staged, all: opts?.all, targetDir: opts?.targetDir, activeWorktreeDir: opts?.activeWorktreeDir, }); } public async merge(opts: WorkspaceCmdOptsSansIndexAndBranch): Promise { const galaxyIbGib = await this.getGalaxyIbGib(); const activeBranchSpace = await this.getActiveBranchSpace({ galaxyIbGib, targetDir: opts?.targetDir }); if (!opts.incomingBranchName) { throw new Error(`opts.incomingBranchName falsy (E: 5a83a806fd78f2edc27a26fd3e8cf826)`); } const effectiveWorkspaceSpace = await this.getActiveWorkspaceProjectionSpace({ galaxyIbGib, targetDir: opts?.targetDir, activeBranchSpace, }); return await this.withBranchLock(activeBranchSpace, async () => { return await executeVcsMerge({ metaspace: this.metaspace, localSpace: this.localSpace, galaxyIbGib, baseBranchSpace: activeBranchSpace, incomingBranchSpace: opts.incomingBranchSpace, workspaceSpace: effectiveWorkspaceSpace, incomingBranchName: opts.incomingBranchName, message: opts.message, b2tfsGalaxyRoot: this.b2tfsGalaxyRoot, targetDir: opts.targetDir || this.b2tfsGalaxyRoot, vcsFolderName: this.vcsFolderName, }); }); } public async log(opts: WorkspaceCmdOpts): Promise { const galaxyIbGib = await this.getGalaxyIbGib(); const branchName = opts?.branchName || await this.getActiveBranchName({ galaxyIbGib, targetDir: opts?.targetDir }); return await executeVcsLog({ metaspace: this.metaspace, galaxyIbGib, branchName, filePath: opts?.filePath, allComments: opts?.allComments, b2tfsGalaxyRoot: this.b2tfsGalaxyRoot, targetDir: opts?.targetDir || this.b2tfsGalaxyRoot, }); } public async generateComment(opts?: { paths?: string[]; folderTopic?: string; targetDir?: string; galaxyIbGib?: B2tFSGalaxyIbGib_V1; }): Promise { const galaxyIbGib = opts?.galaxyIbGib || await this.getGalaxyIbGib(); const targetDir = opts?.targetDir || this.b2tfsGalaxyRoot; const activeBranchSpace = await this.getActiveBranchSpace({ galaxyIbGib, targetDir }); const workspaceSpace = await this.getActiveWorkspaceProjectionSpace({ galaxyIbGib, targetDir, activeBranchSpace, }); if (!workspaceSpace) { throw new Error(`workspaceSpace is required to generate companion comments (E: b3f2081a91e54a4bb82a720db1e54a26)`); } return await executeVcsGenerateComment({ metaspace: this.metaspace, localSpace: this.localSpace, activeBranchSpace, workspaceSpace, galaxyIbGib, paths: opts?.paths, folderTopic: opts?.folderTopic, b2tfsGalaxyRoot: this.b2tfsGalaxyRoot, targetDir, vcsFolderName: this.vcsFolderName, }); } public async updateIndex(opts: { dataToUpdate?: Partial; targetSpace?: IbGibSpaceAny; }): Promise { const targetSpace = opts.targetSpace || this.localSpace; if (!targetSpace) { throw new Error(`localSpace required to update index (E: 5ff10877e06228e17fc1eb88d3073826)`); } const galaxyIbGib = await this.getGalaxyIbGib(); return await this.withBranchLock(targetSpace, async () => { return await updateB2tFSGalaxyIbGib({ space: targetSpace, metaspace: this.metaspace, galaxyIbGib, dataToUpdate: opts.dataToUpdate, }); }); } public async push(opts: WorkspacePushOpts): Promise { const { branchInfo, branchSpace, cosmosIbGib, galaxyIbGib } = await this.ensureBranch({ cosmosName: opts.cosmosName, galaxyName: opts.galaxyName, branchName: opts.branchName, }); let senderIdentityInfo: any; let fnSenderSecret: () => Promise; if (opts.senderIdentityInfo && opts.fnSenderSecret) { senderIdentityInfo = opts.senderIdentityInfo; fnSenderSecret = opts.fnSenderSecret; } else if (opts.deviceIdentity) { senderIdentityInfo = { senderIdentity: opts.deviceIdentity.syncDelegateKeystone, senderDomainAddr: opts.deviceIdentity.primaryTjpAddr || '', }; const secret = opts.deviceIdentity.syncDelegateSecret; fnSenderSecret = async () => secret; } else { throw new Error(`senderIdentityInfo/fnSenderSecret or deviceIdentity required for push (E: 52dd826a357bdb125b10756274e82d11)`); } return await executeVcsPush({ metaspace: this.metaspace, localSpace: this.localSpace, activeBranchSpace: branchSpace, galaxyIbGib, cosmosIbGib, branchName: branchInfo.branchName, remoteUrl: opts.remoteUrl, senderIdentityInfo, fnSenderSecret, conflictStrategy: opts.conflictStrategy, onProgress: opts.onProgress, }); } public async clone(opts: WorkspaceCloneOpts): Promise { let senderIdentityInfo: any = opts.senderIdentityInfo; let fnSenderSecret = opts.fnSenderSecret; if (!senderIdentityInfo && opts.deviceIdentity) { senderIdentityInfo = { senderIdentity: opts.deviceIdentity.syncDelegateKeystone, senderDomainAddr: opts.deviceIdentity.primaryTjpAddr || '', }; const secret = opts.deviceIdentity.syncDelegateSecret; fnSenderSecret = async () => secret; } return await executeVcsClone({ metaspace: this.metaspace, localSpace: this.localSpace, remoteUrl: opts.remoteUrl, targetCosmosName: opts.targetCosmosName, galaxy: opts.galaxy, vcsFolderName: opts.vcsFolderName, cwd: opts.cwd ?? this.cosmosRoot, senderIdentityInfo, fnSenderSecret, conflictStrategy: opts.conflictStrategy, onProgress: opts.onProgress, }); } public async fork(opts: WorkspaceForkOpts): Promise { let senderIdentityInfo: any = opts.senderIdentityInfo; let fnSenderSecret = opts.fnSenderSecret; if (!senderIdentityInfo && opts.deviceIdentity) { senderIdentityInfo = { senderIdentity: opts.deviceIdentity.syncDelegateKeystone, senderDomainAddr: opts.deviceIdentity.primaryTjpAddr || '', }; const secret = opts.deviceIdentity.syncDelegateSecret; fnSenderSecret = async () => secret; } return await executeVcsFork({ metaspace: this.metaspace, localSpace: this.localSpace, remoteUrl: opts.remoteUrl, galaxy: opts.galaxy, asName: opts.asName, vcsFolderName: opts.vcsFolderName, cwd: opts.cwd ?? this.cosmosRoot, senderIdentityInfo, fnSenderSecret, conflictStrategy: opts.conflictStrategy, onProgress: opts.onProgress, }); } // #endregion }