@ibgib/vcs-gib 0.0.6 → 0.0.18

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (185) hide show
  1. package/README.md +24 -2
  2. package/VCS_CLI_USER_GUIDE.md +2 -2
  3. package/dist/AUTO-GENERATED-version.d.mts +1 -1
  4. package/dist/AUTO-GENERATED-version.d.mts.map +1 -1
  5. package/dist/AUTO-GENERATED-version.mjs +1 -1
  6. package/dist/AUTO-GENERATED-version.mjs.map +1 -1
  7. package/dist/bootstrap/node-bootstrap.d.mts.map +1 -1
  8. package/dist/bootstrap/node-bootstrap.mjs +9 -0
  9. package/dist/bootstrap/node-bootstrap.mjs.map +1 -1
  10. package/dist/bootstrap/{node-bootstrap.respec.node.d.mts → node-bootstrap.respec.d.mts} +1 -1
  11. package/dist/bootstrap/node-bootstrap.respec.d.mts.map +1 -0
  12. package/dist/bootstrap/{node-bootstrap.respec.node.mjs → node-bootstrap.respec.mjs} +1 -1
  13. package/dist/bootstrap/node-bootstrap.respec.mjs.map +1 -0
  14. package/dist/cli/cli-arg-parser.d.mts +3 -0
  15. package/dist/cli/cli-arg-parser.d.mts.map +1 -1
  16. package/dist/cli/cli-arg-parser.mjs +13 -1
  17. package/dist/cli/cli-arg-parser.mjs.map +1 -1
  18. package/dist/cli/{cli-arg-parser.respec.node.d.mts → cli-arg-parser.respec.d.mts} +1 -1
  19. package/dist/cli/cli-arg-parser.respec.d.mts.map +1 -0
  20. package/dist/cli/{cli-arg-parser.respec.node.mjs → cli-arg-parser.respec.mjs} +1 -1
  21. package/dist/cli/cli-arg-parser.respec.mjs.map +1 -0
  22. package/dist/cli/cli-constants.d.mts +6 -0
  23. package/dist/cli/cli-constants.d.mts.map +1 -1
  24. package/dist/cli/cli-constants.mjs +24 -0
  25. package/dist/cli/cli-constants.mjs.map +1 -1
  26. package/dist/cli/{cli-e2e.respec.node.d.mts → cli-e2e.respec.d.mts} +1 -1
  27. package/dist/cli/cli-e2e.respec.d.mts.map +1 -0
  28. package/dist/cli/{cli-e2e.respec.node.mjs → cli-e2e.respec.mjs} +2 -2
  29. package/dist/cli/cli-e2e.respec.mjs.map +1 -0
  30. package/dist/cli/commands/add-cmd.d.mts.map +1 -1
  31. package/dist/cli/commands/add-cmd.mjs +35 -30
  32. package/dist/cli/commands/add-cmd.mjs.map +1 -1
  33. package/dist/cli/commands/{branch-checkout-cmd.respec.node.d.mts → branch-checkout-cmd.respec.d.mts} +1 -1
  34. package/dist/cli/commands/branch-checkout-cmd.respec.d.mts.map +1 -0
  35. package/dist/cli/commands/{branch-checkout-cmd.respec.node.mjs → branch-checkout-cmd.respec.mjs} +172 -8
  36. package/dist/cli/commands/branch-checkout-cmd.respec.mjs.map +1 -0
  37. package/dist/cli/commands/branch-cmd.d.mts +2 -1
  38. package/dist/cli/commands/branch-cmd.d.mts.map +1 -1
  39. package/dist/cli/commands/branch-cmd.mjs +59 -8
  40. package/dist/cli/commands/branch-cmd.mjs.map +1 -1
  41. package/dist/cli/commands/checkout-cmd.d.mts.map +1 -1
  42. package/dist/cli/commands/checkout-cmd.mjs +38 -18
  43. package/dist/cli/commands/checkout-cmd.mjs.map +1 -1
  44. package/dist/cli/commands/commit-cmd.d.mts.map +1 -1
  45. package/dist/cli/commands/commit-cmd.mjs +26 -23
  46. package/dist/cli/commands/commit-cmd.mjs.map +1 -1
  47. package/dist/cli/commands/{commit-log-cmd.respec.node.d.mts → commit-log-cmd.respec.d.mts} +1 -1
  48. package/dist/cli/commands/commit-log-cmd.respec.d.mts.map +1 -0
  49. package/dist/cli/commands/{commit-log-cmd.respec.node.mjs → commit-log-cmd.respec.mjs} +1 -1
  50. package/dist/cli/commands/commit-log-cmd.respec.mjs.map +1 -0
  51. package/dist/cli/commands/{init-cmd.respec.node.d.mts → init-cmd.respec.d.mts} +1 -1
  52. package/dist/cli/commands/init-cmd.respec.d.mts.map +1 -0
  53. package/dist/cli/commands/{init-cmd.respec.node.mjs → init-cmd.respec.mjs} +1 -1
  54. package/dist/cli/commands/init-cmd.respec.mjs.map +1 -0
  55. package/dist/cli/commands/merge-cmd.d.mts +23 -0
  56. package/dist/cli/commands/merge-cmd.d.mts.map +1 -0
  57. package/dist/cli/commands/merge-cmd.mjs +224 -0
  58. package/dist/cli/commands/merge-cmd.mjs.map +1 -0
  59. package/dist/cli/commands/reset-cmd.d.mts +21 -0
  60. package/dist/cli/commands/reset-cmd.d.mts.map +1 -0
  61. package/dist/cli/commands/reset-cmd.mjs +93 -0
  62. package/dist/cli/commands/reset-cmd.mjs.map +1 -0
  63. package/dist/cli/commands/reset-restore-cmd.respec.d.mts +7 -0
  64. package/dist/cli/commands/reset-restore-cmd.respec.d.mts.map +1 -0
  65. package/dist/cli/commands/reset-restore-cmd.respec.mjs +185 -0
  66. package/dist/cli/commands/reset-restore-cmd.respec.mjs.map +1 -0
  67. package/dist/cli/commands/restore-cmd.d.mts +22 -0
  68. package/dist/cli/commands/restore-cmd.d.mts.map +1 -0
  69. package/dist/cli/commands/restore-cmd.mjs +115 -0
  70. package/dist/cli/commands/restore-cmd.mjs.map +1 -0
  71. package/dist/cli/commands/{status-add-cmd.respec.node.d.mts → status-add-cmd.respec.d.mts} +1 -1
  72. package/dist/cli/commands/status-add-cmd.respec.d.mts.map +1 -0
  73. package/dist/cli/commands/{status-add-cmd.respec.node.mjs → status-add-cmd.respec.mjs} +25 -1
  74. package/dist/cli/commands/status-add-cmd.respec.mjs.map +1 -0
  75. package/dist/cli/vcs-cli-main.d.mts.map +1 -1
  76. package/dist/cli/vcs-cli-main.mjs +36 -3
  77. package/dist/cli/vcs-cli-main.mjs.map +1 -1
  78. package/dist/engine/branch/branch-graph-helper.d.mts +12 -3
  79. package/dist/engine/branch/branch-graph-helper.d.mts.map +1 -1
  80. package/dist/engine/branch/branch-graph-helper.mjs +50 -18
  81. package/dist/engine/branch/branch-graph-helper.mjs.map +1 -1
  82. package/dist/engine/branch/branch-space-helper.d.mts +1 -0
  83. package/dist/engine/branch/branch-space-helper.d.mts.map +1 -1
  84. package/dist/engine/branch/branch-space-helper.mjs +1 -0
  85. package/dist/engine/branch/branch-space-helper.mjs.map +1 -1
  86. package/dist/engine/branch/{branch-space.respec.node.d.mts → branch-space.respec.d.mts} +1 -1
  87. package/dist/engine/branch/branch-space.respec.d.mts.map +1 -0
  88. package/dist/engine/branch/{branch-space.respec.node.mjs → branch-space.respec.mjs} +12 -7
  89. package/dist/engine/branch/branch-space.respec.mjs.map +1 -0
  90. package/dist/engine/diff/{line-diff-helper.respec.node.d.mts → line-diff-helper.respec.d.mts} +1 -1
  91. package/dist/engine/diff/line-diff-helper.respec.d.mts.map +1 -0
  92. package/dist/engine/diff/{line-diff-helper.respec.node.mjs → line-diff-helper.respec.mjs} +1 -1
  93. package/dist/engine/diff/line-diff-helper.respec.mjs.map +1 -0
  94. package/dist/engine/filter/{glob-helper.respec.node.d.mts → glob-helper.respec.d.mts} +1 -1
  95. package/dist/engine/filter/glob-helper.respec.d.mts.map +1 -0
  96. package/dist/engine/filter/{glob-helper.respec.node.mjs → glob-helper.respec.mjs} +1 -1
  97. package/dist/engine/filter/glob-helper.respec.mjs.map +1 -0
  98. package/dist/engine/index/index-helper.d.mts +35 -0
  99. package/dist/engine/index/index-helper.d.mts.map +1 -1
  100. package/dist/engine/index/index-helper.mjs +241 -0
  101. package/dist/engine/index/index-helper.mjs.map +1 -1
  102. package/dist/engine/item/item-helper.d.mts +29 -3
  103. package/dist/engine/item/item-helper.d.mts.map +1 -1
  104. package/dist/engine/item/item-helper.mjs +234 -56
  105. package/dist/engine/item/item-helper.mjs.map +1 -1
  106. package/dist/engine/item/item.respec.d.mts +7 -0
  107. package/dist/engine/item/item.respec.d.mts.map +1 -0
  108. package/dist/engine/item/item.respec.mjs +75 -0
  109. package/dist/engine/item/item.respec.mjs.map +1 -0
  110. package/dist/engine/merge/b2tfs-3way-merge-strategy.d.mts +38 -0
  111. package/dist/engine/merge/b2tfs-3way-merge-strategy.d.mts.map +1 -0
  112. package/dist/engine/merge/b2tfs-3way-merge-strategy.mjs +593 -0
  113. package/dist/engine/merge/b2tfs-3way-merge-strategy.mjs.map +1 -0
  114. package/dist/engine/merge/b2tfs-merge-strategy.respec.d.mts +7 -0
  115. package/dist/engine/merge/b2tfs-merge-strategy.respec.d.mts.map +1 -0
  116. package/dist/engine/merge/b2tfs-merge-strategy.respec.mjs +335 -0
  117. package/dist/engine/merge/b2tfs-merge-strategy.respec.mjs.map +1 -0
  118. package/dist/respec-gib.node.mjs +1 -1
  119. package/dist/respec-gib.node.mjs.map +1 -1
  120. package/dist/{vcs-gib.respec.node.d.mts → vcs-gib.respec.d.mts} +1 -1
  121. package/dist/vcs-gib.respec.d.mts.map +1 -0
  122. package/dist/{vcs-gib.respec.node.mjs → vcs-gib.respec.mjs} +1 -1
  123. package/dist/vcs-gib.respec.mjs.map +1 -0
  124. package/package.json +3 -3
  125. package/src/AUTO-GENERATED-version.mts +1 -1
  126. package/src/bootstrap/node-bootstrap.mts +6 -0
  127. package/src/cli/cli-arg-parser.mts +15 -0
  128. package/src/cli/cli-constants.mts +27 -0
  129. package/src/cli/{cli-e2e.respec.node.mts → cli-e2e.respec.mts} +2 -2
  130. package/src/cli/commands/add-cmd.mts +41 -29
  131. package/src/cli/commands/{branch-checkout-cmd.respec.node.mts → branch-checkout-cmd.respec.mts} +190 -8
  132. package/src/cli/commands/branch-cmd.mts +78 -8
  133. package/src/cli/commands/checkout-cmd.mts +48 -23
  134. package/src/cli/commands/commit-cmd.mts +35 -26
  135. package/src/cli/commands/merge-cmd.mts +289 -0
  136. package/src/cli/commands/reset-cmd.mts +128 -0
  137. package/src/cli/commands/reset-restore-cmd.respec.mts +222 -0
  138. package/src/cli/commands/restore-cmd.mts +155 -0
  139. package/src/cli/commands/{status-add-cmd.respec.node.mts → status-add-cmd.respec.mts} +27 -0
  140. package/src/cli/vcs-cli-main.mts +36 -3
  141. package/src/engine/branch/branch-graph-helper.mts +66 -18
  142. package/src/engine/branch/branch-space-helper.mts +6 -0
  143. package/src/engine/branch/{branch-space.respec.node.mts → branch-space.respec.mts} +12 -6
  144. package/src/engine/index/index-helper.mts +324 -0
  145. package/src/engine/item/item-helper.mts +284 -58
  146. package/src/engine/item/item.respec.mts +92 -0
  147. package/src/engine/merge/b2tfs-3way-merge-strategy.mts +725 -0
  148. package/src/engine/merge/b2tfs-merge-strategy.respec.mts +438 -0
  149. package/src/respec-gib.node.mts +2 -2
  150. package/tsconfig.tsbuildinfo +1 -1
  151. package/dist/bootstrap/node-bootstrap.respec.node.d.mts.map +0 -1
  152. package/dist/bootstrap/node-bootstrap.respec.node.mjs.map +0 -1
  153. package/dist/cli/cli-arg-parser.respec.node.d.mts.map +0 -1
  154. package/dist/cli/cli-arg-parser.respec.node.mjs.map +0 -1
  155. package/dist/cli/cli-e2e.respec.node.d.mts.map +0 -1
  156. package/dist/cli/cli-e2e.respec.node.mjs.map +0 -1
  157. package/dist/cli/commands/branch-checkout-cmd.respec.node.d.mts.map +0 -1
  158. package/dist/cli/commands/branch-checkout-cmd.respec.node.mjs.map +0 -1
  159. package/dist/cli/commands/commit-log-cmd.respec.node.d.mts.map +0 -1
  160. package/dist/cli/commands/commit-log-cmd.respec.node.mjs.map +0 -1
  161. package/dist/cli/commands/init-cmd.respec.node.d.mts.map +0 -1
  162. package/dist/cli/commands/init-cmd.respec.node.mjs.map +0 -1
  163. package/dist/cli/commands/status-add-cmd.respec.node.d.mts.map +0 -1
  164. package/dist/cli/commands/status-add-cmd.respec.node.mjs.map +0 -1
  165. package/dist/engine/branch/branch-space.respec.node.d.mts.map +0 -1
  166. package/dist/engine/branch/branch-space.respec.node.mjs.map +0 -1
  167. package/dist/engine/diff/line-diff-helper.respec.node.d.mts.map +0 -1
  168. package/dist/engine/diff/line-diff-helper.respec.node.mjs.map +0 -1
  169. package/dist/engine/filter/glob-helper.respec.node.d.mts.map +0 -1
  170. package/dist/engine/filter/glob-helper.respec.node.mjs.map +0 -1
  171. package/dist/vcs-gib.respec.node.d.mts.map +0 -1
  172. package/dist/vcs-gib.respec.node.mjs.map +0 -1
  173. package/test_runs/001-hello-node/20260728_090100_0001-flow-core-main.md +0 -39
  174. package/test_runs/001-hello-node/20260728_090200_0003-flow-branching-worktree.md +0 -54
  175. package/test_runs/001-hello-node/20260728_090300_0002-flow-selective-add.md +0 -45
  176. package/test_runs/001-hello-node/20260728_142100_0007-flow-edge-cases.md +0 -44
  177. package/test_runs/001-hello-node/20260728_142400_0005-flow-hardcoded-ignores.md +0 -36
  178. package/tools/pack-for-test.mjs +0 -73
  179. /package/src/bootstrap/{node-bootstrap.respec.node.mts → node-bootstrap.respec.mts} +0 -0
  180. /package/src/cli/{cli-arg-parser.respec.node.mts → cli-arg-parser.respec.mts} +0 -0
  181. /package/src/cli/commands/{commit-log-cmd.respec.node.mts → commit-log-cmd.respec.mts} +0 -0
  182. /package/src/cli/commands/{init-cmd.respec.node.mts → init-cmd.respec.mts} +0 -0
  183. /package/src/engine/diff/{line-diff-helper.respec.node.mts → line-diff-helper.respec.mts} +0 -0
  184. /package/src/engine/filter/{glob-helper.respec.node.mts → glob-helper.respec.mts} +0 -0
  185. /package/src/{vcs-gib.respec.node.mts → vcs-gib.respec.mts} +0 -0
@@ -24,11 +24,13 @@ import { bootstrapVcsMetaspace } from '../../bootstrap/node-bootstrap.mjs';
24
24
  import { getOrCreateBranchSpace } from '../../engine/branch/branch-space-helper.mjs';
25
25
  import { getB2tFSItemIb } from '../../engine/item/item-atom-helper.mjs';
26
26
  import { B2tFSItemIbGib_V1, B2TFS_CHILD_ITEM_REL8N_NAME } from '../../engine/item/item-types.mjs';
27
- import { getIbGibWithMetaspaceFallback } from '../../engine/item/item-helper.mjs';
27
+ import { getIbGibWithMetaspaceFallback, createB2tFSItemIbGib, evolveB2tFSItemIbGib } from '../../engine/item/item-helper.mjs';
28
28
  import { getB2tFSBranchSpaceName } from '../../engine/branch/branch-helper.mjs';
29
29
  import { B2tFSBranchIbGib_V1 } from '../../engine/branch/branch-types.mjs';
30
30
  import { getB2tFSIndexIbGib, updateB2tFSIndexIbGib, getSpaceQualifiedIbGibAddr } from '../../engine/index/index-helper.mjs';
31
31
  import { B2tFSStagedItemRecord } from '../../engine/index/index-types.mjs';
32
+ import { TransformResult } from '@ibgib/ts-gib/dist/types.mjs';
33
+ import { IbGib_V1 } from '@ibgib/ts-gib/dist/V1/types.mjs';
32
34
  import { GLOBAL_LOG_A_LOT } from '../../vcs-gib-constants.mjs';
33
35
 
34
36
  const lcFile = `[commit-cmd]`;
@@ -127,16 +129,18 @@ export async function executeCommitCmd({
127
129
  }
128
130
  }
129
131
 
130
- currentBranchIbGib = branchIbGibs.find(
132
+ currentBranchIbGib = branchIbGibs.findLast(
131
133
  (b) => (b as B2tFSBranchIbGib_V1)?.data?.name === branchName,
132
- ) as B2tFSBranchIbGib_V1 || (branchIbGibs.at(0) as B2tFSBranchIbGib_V1);
134
+ ) as B2tFSBranchIbGib_V1 || (branchIbGibs.at(-1) as B2tFSBranchIbGib_V1);
135
+
133
136
 
134
137
  if (currentBranchIbGib) {
135
138
  currentBranchAddr = getIbGibAddr({ ibGib: currentBranchIbGib });
136
139
  }
137
140
  }
138
141
 
139
- const currentRootAddr = currentBranchIbGib?.rel8ns?.b2tfs_item?.at(0);
142
+ const currentRootAddr = currentBranchIbGib?.rel8ns?.b2tfs_item?.at(-1);
143
+
140
144
 
141
145
  // 1. Create CommentIbGib_V1 commit comment node in activeSpace
142
146
  const commentRes = await createCommentIbGib({
@@ -244,7 +248,7 @@ async function commitCmd_buildEvolvedRootItem({
244
248
  metaspace: any;
245
249
  repoName: string;
246
250
  }): Promise<{ newRootItem: B2tFSItemIbGib_V1; newRootAddr: string }> {
247
- const itemsByRelPath = new Map<string, { itemAddr: string; fsType: 'file' | 'folder' }>();
251
+ const itemsByRelPath = new Map<string, { itemAddr: string; fsType: 'file' | 'folder'; item?: B2tFSItemIbGib_V1 }>();
248
252
 
249
253
  if (currentRootAddr) {
250
254
  const currentRootItem = await getIbGibWithMetaspaceFallback({
@@ -328,27 +332,34 @@ async function commitCmd_buildEvolvedRootItem({
328
332
  }
329
333
 
330
334
  const folderName = folderRelPath ? path.basename(folderRelPath) : repoName;
331
- const diffTxId = await getUUID();
332
335
  const nameHash = crypto.createHash('sha256').update(folderName).digest('hex');
333
336
 
334
- const rel8ns: Record<string, string[]> = {};
335
- if (childAddrs.length > 0) {
336
- rel8ns[B2TFS_CHILD_ITEM_REL8N_NAME] = childAddrs;
337
- }
337
+ const existingFolderRec = itemsByRelPath.get(folderRelPath);
338
338
 
339
- const folderRes = await Factory_V1.firstGen({
340
- parentIbGib: ROOT,
341
- ib: getB2tFSItemIb('folder', folderName, nameHash),
342
- data: {
343
- name: folderName,
344
- nameHash,
339
+ let folderRes: TransformResult<IbGib_V1>;
340
+
341
+ if (existingFolderRec && existingFolderRec.item) {
342
+ const oldChildAddrs = existingFolderRec.item.rel8ns?.[B2TFS_CHILD_ITEM_REL8N_NAME] || [];
343
+ const toRemoveChildAddrs = oldChildAddrs.filter(a => !childAddrs.includes(a));
344
+
345
+ folderRes = await evolveB2tFSItemIbGib({
346
+ existingItem: existingFolderRec.item,
347
+ dataToUpdate: {
348
+ nameHash,
349
+ },
350
+ rel8nsToUpdate: childAddrs.length > 0 ? { [B2TFS_CHILD_ITEM_REL8N_NAME]: childAddrs } : undefined,
351
+ rel8nsToRemove: toRemoveChildAddrs.length > 0 ? { [B2TFS_CHILD_ITEM_REL8N_NAME]: toRemoveChildAddrs } : undefined,
352
+ });
353
+ } else {
354
+ folderRes = await createB2tFSItemIbGib({
345
355
  fsType: 'folder',
346
- diffTransactionId: diffTxId,
347
- uuid: await getUUID(),
348
- classname: 'B2tFSItemIbGib_V1',
349
- },
350
- rel8ns,
351
- });
356
+ name: folderName,
357
+ data: {
358
+ nameHash,
359
+ },
360
+ rel8ns: childAddrs.length > 0 ? { [B2TFS_CHILD_ITEM_REL8N_NAME]: childAddrs } : undefined,
361
+ });
362
+ }
352
363
 
353
364
  const folderItem = folderRes.newIbGib as B2tFSItemIbGib_V1;
354
365
  const folderAddr = getIbGibAddr({ ibGib: folderItem });
@@ -383,16 +394,14 @@ async function commitCmd_walkBranchGraph({
383
394
  space: any;
384
395
  metaspace?: any;
385
396
  currentRelPath: string;
386
- outMap: Map<string, { itemAddr: string; fsType: 'file' | 'folder' }>;
397
+ outMap: Map<string, { itemAddr: string; fsType: 'file' | 'folder'; item?: B2tFSItemIbGib_V1 }>;
387
398
  }): Promise<void> {
388
399
  if (!item || !item.data) { return; }
389
400
 
390
401
  const { fsType } = item.data;
391
402
  const itemAddr = getIbGibAddr({ ibGib: item });
392
403
 
393
- if (currentRelPath) {
394
- outMap.set(currentRelPath, { itemAddr, fsType });
395
- }
404
+ outMap.set(currentRelPath, { itemAddr, fsType, item });
396
405
 
397
406
  if (fsType === 'folder') {
398
407
  const childAddrs = item.rel8ns?.[B2TFS_CHILD_ITEM_REL8N_NAME] || [];
@@ -0,0 +1,289 @@
1
+ /**
2
+ * @module merge-cmd
3
+ *
4
+ * Command handler for `vcs merge <branchName>`.
5
+ * Merges target branch into active branch using SyncSagaCoordinator sync and 3-way graft merge strategy.
6
+ * Projects merged branch snapshot onto active branch physical worktree path.
7
+ */
8
+
9
+ import path from 'node:path';
10
+ import fs from 'node:fs/promises';
11
+
12
+ import { clone, extractErrorMsg } from '@ibgib/helper-gib/dist/helpers/utils-helper.mjs';
13
+ import { getIbGibAddr } from '@ibgib/ts-gib/dist/helper.mjs';
14
+ import { rel8 } from '@ibgib/ts-gib/dist/V1/transforms/rel8.mjs';
15
+ import { putInSpace, getLatestAddrs, getFromSpace, registerNewIbGib } from '@ibgib/core-gib/dist/witness/space/space-helper.mjs';
16
+ import { SyncSagaCoordinator } from '@ibgib/core-gib/dist/sync/sync-saga-coordinator.mjs';
17
+ import { SyncPeerInnerspace_V1 } from '@ibgib/core-gib/dist/sync/sync-peer/sync-peer-innerspace/sync-peer-innerspace-v1.mjs';
18
+ import { SYNC_PEER_INNERSPACE_DEFAULT_DATA_V1 } from '@ibgib/core-gib/dist/sync/sync-peer/sync-peer-innerspace/sync-peer-innerspace-constants.mjs';
19
+ import { SyncConflictStrategy } from '@ibgib/core-gib/dist/sync/sync-constants.mjs';
20
+ import { GraftPreference } from '@ibgib/core-gib/dist/sync/graft-info/graft-info-types.mjs';
21
+
22
+
23
+ import { bootstrapVcsMetaspace } from '../../bootstrap/node-bootstrap.mjs';
24
+ import { B2tFSBranchIbGib_V1 } from '../../engine/branch/branch-types.mjs';
25
+ import { getOrCreateBranchSpace } from '../../engine/branch/branch-space-helper.mjs';
26
+ import { B2tFSItemIbGib_V1, B2TFS_CHILD_ITEM_REL8N_NAME } from '../../engine/item/item-types.mjs';
27
+ import { writeB2tFSItemToFS, getIbGibWithMetaspaceFallback } from '../../engine/item/item-helper.mjs';
28
+ import { getB2tFSIndexIbGib, updateB2tFSIndexIbGib } from '../../engine/index/index-helper.mjs';
29
+ import { GLOBAL_LOG_A_LOT } from '../../vcs-gib-constants.mjs';
30
+
31
+ const lcFile = `[merge-cmd]`;
32
+ const logalot = GLOBAL_LOG_A_LOT;
33
+
34
+ export interface MergeCmdOpts {
35
+ targetBranchName?: string;
36
+ targetDir?: string;
37
+ vcsFolderName?: string;
38
+ quiet?: boolean;
39
+ }
40
+
41
+ export interface MergeCmdResult {
42
+ activeBranch: string;
43
+ targetBranch: string;
44
+ activeSpaceId: string;
45
+ targetSpaceId: string;
46
+ mergedBranchAddr?: string;
47
+ worktreePath: string;
48
+ }
49
+
50
+ export async function executeMergeCmd({
51
+ targetBranchName,
52
+ targetDir = process.cwd(),
53
+ vcsFolderName = '.vcsgib',
54
+ quiet = false,
55
+ }: MergeCmdOpts = {}): Promise<MergeCmdResult> {
56
+ const lc = `${lcFile}[${executeMergeCmd.name}]`;
57
+ try {
58
+ if (logalot) {
59
+ console.log(`${lc} starting... (I: 4c5d6e7f8a9b0c1d2e3f4a5b6c7d8e9f)`);
60
+ }
61
+
62
+ const trimmedTargetBranch = targetBranchName?.trim();
63
+ if (!trimmedTargetBranch) {
64
+ throw new Error(`Branch name is required for merge (E: 5d6e7f8a9b0c1d2e3f4a5b6c7d8e9f0a)`);
65
+ }
66
+
67
+ const absTargetDir = path.resolve(targetDir);
68
+
69
+ const { metaspace, localSpace, repoRoot, vcsPath } = await bootstrapVcsMetaspace({
70
+ repoPath: absTargetDir,
71
+ vcsFolderName,
72
+ });
73
+
74
+ // Verify repo exists
75
+ try {
76
+ await fs.access(vcsPath);
77
+ } catch {
78
+ throw new Error(`Not an ibgib repository (directory ${vcsPath} does not exist) (E: 6e7f8a9b0c1d2e3f4a5b6c7d8e9f0a1b)`);
79
+ }
80
+
81
+ // 1. Fetch authoritative repository index from localSpace
82
+ const indexIbGib = await getB2tFSIndexIbGib({ space: localSpace, vcsPath });
83
+ if (!indexIbGib) {
84
+ throw new Error(`Repository index B2tFSIndexIbGib_V1 not found in localSpace (E: 7f8a9b0c1d2e3f4a5b6c7d8e9f0a1b2c)`);
85
+ }
86
+
87
+ const activeBranchName = indexIbGib.data?.activeBranch || 'main';
88
+
89
+ if (activeBranchName === trimmedTargetBranch) {
90
+ throw new Error(`Cannot merge branch '${trimmedTargetBranch}' into itself (E: 8a9b0c1d2e3f4a5b6c7d8e9f0a1b2c3d)`);
91
+ }
92
+
93
+ // 2. Fetch all registered branch ibgibs
94
+ const branchAddrs = [
95
+ ...(indexIbGib.rel8ns?.branch || []),
96
+ ...(indexIbGib.rel8ns?.branch_tjp || []),
97
+ ];
98
+
99
+ let branchIbGibs: B2tFSBranchIbGib_V1[] = [];
100
+ if (branchAddrs.length > 0) {
101
+ for (const bAddr of branchAddrs) {
102
+ const bIbGib = await getIbGibWithMetaspaceFallback({
103
+ addr: bAddr,
104
+ space: localSpace,
105
+ metaspace,
106
+ });
107
+ if (bIbGib) {
108
+ branchIbGibs.push(bIbGib as B2tFSBranchIbGib_V1);
109
+ }
110
+ }
111
+ }
112
+
113
+ // Resolve active branch node & target branch node
114
+ const activeBranchIbGib = branchIbGibs.findLast(
115
+ (b) => b?.data?.name === activeBranchName,
116
+ );
117
+ if (!activeBranchIbGib) {
118
+ throw new Error(`Active branch '${activeBranchName}' not found in repository (E: 9b0c1d2e3f4a5b6c7d8e9f0a1b2c3d4e)`);
119
+ }
120
+
121
+ const targetBranchIbGib = branchIbGibs.findLast(
122
+ (b) => b?.data?.name === trimmedTargetBranch,
123
+ );
124
+
125
+ if (!targetBranchIbGib) {
126
+ throw new Error(`Branch '${trimmedTargetBranch}' does not exist (E: 0c1d2e3f4a5b6c7d8e9f0a1b2c3d4e5f)`);
127
+ }
128
+
129
+ // 3. Resolve active branch space & target branch space
130
+ const activeSpaceRes = await getOrCreateBranchSpace({
131
+ metaspace,
132
+ vcsPath,
133
+ branchName: activeBranchName,
134
+ });
135
+ const activeSpace = activeSpaceRes.space;
136
+
137
+ const targetSpaceRes = await getOrCreateBranchSpace({
138
+ metaspace,
139
+ vcsPath,
140
+ branchName: trimmedTargetBranch,
141
+ });
142
+ const targetSpace = targetSpaceRes.space;
143
+
144
+ await putInSpace({ space: activeSpace, ibGib: activeBranchIbGib });
145
+ await putInSpace({ space: targetSpace, ibGib: targetBranchIbGib });
146
+
147
+ // Resolve latest branch tip inside targetSpace (in case commits occurred on target branch)
148
+ const targetBranchTjpAddr = targetBranchIbGib.rel8ns?.tjp?.at(0) || getIbGibAddr({ ibGib: targetBranchIbGib });
149
+ const latestTargetBranchRes = await getLatestAddrs({ space: targetSpace, addrs: [targetBranchTjpAddr] });
150
+ const latestTargetBranchAddr = (latestTargetBranchRes?.data as any)?.latestAddrs?.[0] || getIbGibAddr({ ibGib: targetBranchIbGib });
151
+
152
+ const latestTargetBranchIbGib = (await getIbGibWithMetaspaceFallback({
153
+ addr: latestTargetBranchAddr,
154
+ space: targetSpace,
155
+ metaspace,
156
+ })) as B2tFSBranchIbGib_V1 || targetBranchIbGib;
157
+
158
+ const targetRootAddr = latestTargetBranchIbGib.rel8ns?.b2tfs_item?.at(-1);
159
+
160
+ if (!targetRootAddr) {
161
+ throw new Error(`Target branch '${trimmedTargetBranch}' has no valid root item address (E: 3b4c5d6e7f8a9b0c1d2e3f4a5b6c7d8e)`);
162
+ }
163
+ const targetRootItem = await getIbGibWithMetaspaceFallback({
164
+ addr: targetRootAddr,
165
+ space: targetSpace,
166
+ metaspace,
167
+ });
168
+ if (!targetRootItem) {
169
+ throw new Error(`Target root item '${targetRootAddr}' not found in target space (E: 4c5d6e7f8a9b0c1d2e3f4a5b6c7d8e9f)`);
170
+ }
171
+
172
+ // 4. Setup SyncPeerInnerspace and SyncSagaCoordinator syncing target branch space into active branch space
173
+ const peer = new SyncPeerInnerspace_V1(clone(SYNC_PEER_INNERSPACE_DEFAULT_DATA_V1));
174
+ await peer.initialized;
175
+ await peer.initializeOpts({
176
+ localMetaspace: metaspace,
177
+ localSpace: targetSpace,
178
+ receiverSpace: activeSpace,
179
+ receiverCoordinator: new SyncSagaCoordinator(),
180
+ receiverMetaspace: metaspace,
181
+ });
182
+
183
+ const senderCoordinator = new SyncSagaCoordinator();
184
+ const syncSaga = await senderCoordinator.sync({
185
+ peer,
186
+ localSpace: targetSpace,
187
+ metaspace,
188
+ domainIbGibs: [targetRootItem],
189
+ conflictStrategy: SyncConflictStrategy.optimisticWithLCS,
190
+ graftPreference: GraftPreference.receiver,
191
+ });
192
+
193
+
194
+
195
+
196
+ await syncSaga.done;
197
+
198
+ // 5. Query activeSpace for updated active branch node & root item evolved natively by sync graft strategy
199
+ const activeBranchTjpAddr = activeBranchIbGib.rel8ns?.tjp?.at(0) || getIbGibAddr({ ibGib: activeBranchIbGib });
200
+ const latestBranchRes = await getLatestAddrs({ space: activeSpace, addrs: [activeBranchTjpAddr] });
201
+ const mergedBranchAddr = (latestBranchRes?.data as any)?.latestAddrs?.[0] || getIbGibAddr({ ibGib: activeBranchIbGib });
202
+
203
+ const mergedBranchIbGib = (await getIbGibWithMetaspaceFallback({
204
+ addr: mergedBranchAddr,
205
+ space: activeSpace,
206
+ metaspace,
207
+ })) as B2tFSBranchIbGib_V1 || activeBranchIbGib;
208
+
209
+ const mergedRootItemAddr = mergedBranchIbGib.rel8ns?.b2tfs_item?.at(-1);
210
+
211
+ // Update indexIbGib to reference new merged branch node
212
+ await updateB2tFSIndexIbGib({
213
+ space: activeSpace,
214
+ metaspace,
215
+ indexIbGib,
216
+ rel8nsToUpdate: {
217
+ branch: [mergedBranchAddr],
218
+ },
219
+ vcsPath,
220
+ });
221
+
222
+
223
+ // 6. Project merged branch snapshot files onto physical worktree disk
224
+ const isDefaultBranch = activeBranchName === 'main';
225
+ const worktreePath = isDefaultBranch
226
+ ? repoRoot
227
+ : path.join(repoRoot, 'b', activeBranchName);
228
+
229
+ await fs.mkdir(worktreePath, { recursive: true });
230
+
231
+ if (mergedRootItemAddr) {
232
+ const mergedRootItem = await getIbGibWithMetaspaceFallback({
233
+ addr: mergedRootItemAddr,
234
+ space: activeSpace,
235
+ metaspace,
236
+ }) as B2tFSItemIbGib_V1;
237
+
238
+ if (mergedRootItem) {
239
+ if (mergedRootItem.data?.fsType === 'file') {
240
+ await writeB2tFSItemToFS({
241
+ item: mergedRootItem,
242
+ destPath: worktreePath,
243
+ space: activeSpace,
244
+ metaspace,
245
+ });
246
+ } else {
247
+ const childAddrs = mergedRootItem.rel8ns?.[B2TFS_CHILD_ITEM_REL8N_NAME] || [];
248
+ for (const childAddr of childAddrs) {
249
+ const childItem = await getIbGibWithMetaspaceFallback({
250
+ addr: childAddr,
251
+ space: activeSpace,
252
+ metaspace,
253
+ }) as B2tFSItemIbGib_V1;
254
+
255
+ if (childItem) {
256
+ await writeB2tFSItemToFS({
257
+ item: childItem,
258
+ destPath: worktreePath,
259
+ space: activeSpace,
260
+ metaspace,
261
+ });
262
+ }
263
+ }
264
+ }
265
+ }
266
+ }
267
+
268
+ if (!quiet) {
269
+ console.log(`Successfully merged branch '${trimmedTargetBranch}' into '${activeBranchName}' (I: 1d2e3f4a5b6c7d8e9f0a1b2c3d4e5f6a)`);
270
+ console.log(` Worktree projection: ${worktreePath}`);
271
+ }
272
+
273
+ return {
274
+ activeBranch: activeBranchName,
275
+ targetBranch: trimmedTargetBranch,
276
+ activeSpaceId: activeSpaceRes.spaceId,
277
+ targetSpaceId: targetSpaceRes.spaceId,
278
+ mergedBranchAddr: getIbGibAddr({ ibGib: mergedBranchIbGib }),
279
+ worktreePath,
280
+ };
281
+ } catch (error) {
282
+ console.error(`${lc} ${extractErrorMsg(error)} (E: 2e3f4a5b6c7d8e9f0a1b2c3d4e5f6a7b)`);
283
+ throw error;
284
+ } finally {
285
+ if (logalot) {
286
+ console.log(`${lc} complete. (I: 3f4a5b6c7d8e9f0a1b2c3d4e5f6a7b8c)`);
287
+ }
288
+ }
289
+ }
@@ -0,0 +1,128 @@
1
+ /**
2
+ * @module reset-cmd
3
+ *
4
+ * Command handler for `vcs reset [path...]`.
5
+ * Unstages specified files (or all files) from active branch index in localSpace.
6
+ */
7
+
8
+ import path from 'node:path';
9
+ import fs from 'node:fs/promises';
10
+
11
+ import { extractErrorMsg } from '@ibgib/helper-gib/dist/helpers/utils-helper.mjs';
12
+ import { promptForConfirm } from '@ibgib/helper-gib/dist/helpers/node-helper.mjs';
13
+ import { putInSpace } from '@ibgib/core-gib/dist/witness/space/space-helper.mjs';
14
+
15
+ import { bootstrapVcsMetaspace } from '../../bootstrap/node-bootstrap.mjs';
16
+ import { getOrCreateBranchSpace } from '../../engine/branch/branch-space-helper.mjs';
17
+ import { getB2tFSIndexIbGib, unstagePathsFromIndex } from '../../engine/index/index-helper.mjs';
18
+ import { GLOBAL_LOG_A_LOT } from '../../vcs-gib-constants.mjs';
19
+
20
+ const lcFile = `[reset-cmd]`;
21
+ const logalot = GLOBAL_LOG_A_LOT;
22
+
23
+ export interface ResetCmdOpts {
24
+ paths?: string[];
25
+ targetDir?: string;
26
+ vcsFolderName?: string;
27
+ all?: boolean;
28
+ yes?: boolean;
29
+ force?: boolean;
30
+ quiet?: boolean;
31
+ }
32
+
33
+ export interface ResetCmdResult {
34
+ unstagedPaths: string[];
35
+ stagedCount: number;
36
+ }
37
+
38
+ export async function executeResetCmd({
39
+ paths = [],
40
+ targetDir = process.cwd(),
41
+ vcsFolderName = '.vcsgib',
42
+ all = false,
43
+ yes = false,
44
+ force = false,
45
+ quiet = false,
46
+ }: ResetCmdOpts = {}): Promise<ResetCmdResult> {
47
+ const lc = `${lcFile}[${executeResetCmd.name}]`;
48
+ try {
49
+ if (logalot) {
50
+ console.log(`${lc} starting... (I: 1a2b3c4d5e6f7a8b9c0d1e2f3a4b5c6d)`);
51
+ }
52
+
53
+ const absTargetDir = path.resolve(targetDir);
54
+
55
+ const { metaspace, localSpace, vcsPath, repoRoot } = await bootstrapVcsMetaspace({
56
+ repoPath: absTargetDir,
57
+ vcsFolderName,
58
+ });
59
+
60
+ // Verify repo exists
61
+ try {
62
+ await fs.access(vcsPath);
63
+ } catch {
64
+ throw new Error(`Not an ibgib repository (directory ${vcsPath} does not exist) (E: 2e3f4a5b6c7d8e9f0a1b2c3d4e5f6a7b)`);
65
+ }
66
+
67
+ // Retrieve authoritative B2tFSIndexIbGib_V1 from space
68
+ const indexIbGib = await getB2tFSIndexIbGib({ space: localSpace, vcsPath });
69
+ if (!indexIbGib) {
70
+ throw new Error(`Repository not initialized. Run 'vcs init' first. (E: 3e4f5a6b7c8d9e0f1a2b3c4d5e6f7a8b)`);
71
+ }
72
+
73
+ const activeBranch = indexIbGib.data?.activeBranch || 'main';
74
+ const activeWorktreeDir = activeBranch === 'main' ? repoRoot : path.join(repoRoot, 'b', activeBranch);
75
+
76
+ const { space: activeSpace } = await getOrCreateBranchSpace({
77
+ metaspace,
78
+ vcsPath,
79
+ branchName: activeBranch,
80
+ });
81
+
82
+ const skipPrompt = yes || force || quiet || !process.stdin.isTTY;
83
+ if (!skipPrompt) {
84
+ const confirmed = await promptForConfirm({ msg: 'Unstage specified files from index?' });
85
+ if (!confirmed) {
86
+ if (!quiet) {
87
+ console.log('Operation cancelled.');
88
+ }
89
+ const stagedCount = Object.keys(indexIbGib.data?.stagedPaths || {}).length;
90
+ return { unstagedPaths: [], stagedCount };
91
+ }
92
+ }
93
+
94
+ const unstageRes = await unstagePathsFromIndex({
95
+ indexIbGib,
96
+ paths,
97
+ targetDir,
98
+ activeWorktreeDir,
99
+ vcsPath,
100
+ space: activeSpace,
101
+ metaspace,
102
+ all,
103
+ });
104
+
105
+ if (localSpace !== activeSpace) {
106
+ await putInSpace({ space: localSpace, ibGib: unstageRes.updatedIndex });
107
+ }
108
+
109
+ if (!quiet) {
110
+ for (const unstagedPath of unstageRes.unstagedPaths) {
111
+ console.log(`unstaged: ${unstagedPath}`);
112
+ }
113
+ }
114
+
115
+ const finalStagedCount = Object.keys(unstageRes.updatedIndex.data?.stagedPaths || {}).length;
116
+ return {
117
+ unstagedPaths: unstageRes.unstagedPaths,
118
+ stagedCount: finalStagedCount,
119
+ };
120
+ } catch (error) {
121
+ console.error(`${lc} ${extractErrorMsg(error)} (E: 4e5f6a7b8c9d0e1f2a3b4c5d6e7f8a9b)`);
122
+ throw error;
123
+ } finally {
124
+ if (logalot) {
125
+ console.log(`${lc} complete. (I: 5f6a7b8c9d0e1f2a3b4c5d6e7f8a9b0c)`);
126
+ }
127
+ }
128
+ }