@ibgib/vcs-gib 0.0.30 → 0.0.32

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 (151) hide show
  1. package/dist/AUTO-GENERATED-version.d.mts +1 -1
  2. package/dist/AUTO-GENERATED-version.mjs +1 -1
  3. package/dist/bootstrap/node-bootstrap.mjs +1 -1
  4. package/dist/bootstrap/node-bootstrap.mjs.map +1 -1
  5. package/dist/cli/commands/add-cmd.d.mts +2 -3
  6. package/dist/cli/commands/add-cmd.d.mts.map +1 -1
  7. package/dist/cli/commands/add-cmd.mjs +30 -283
  8. package/dist/cli/commands/add-cmd.mjs.map +1 -1
  9. package/dist/cli/commands/branch-cmd.d.mts +8 -8
  10. package/dist/cli/commands/branch-cmd.d.mts.map +1 -1
  11. package/dist/cli/commands/branch-cmd.mjs +45 -176
  12. package/dist/cli/commands/branch-cmd.mjs.map +1 -1
  13. package/dist/cli/commands/checkout-cmd.d.mts +2 -4
  14. package/dist/cli/commands/checkout-cmd.d.mts.map +1 -1
  15. package/dist/cli/commands/checkout-cmd.mjs +31 -110
  16. package/dist/cli/commands/checkout-cmd.mjs.map +1 -1
  17. package/dist/cli/commands/commit-cmd.d.mts +7 -8
  18. package/dist/cli/commands/commit-cmd.d.mts.map +1 -1
  19. package/dist/cli/commands/commit-cmd.mjs +40 -390
  20. package/dist/cli/commands/commit-cmd.mjs.map +1 -1
  21. package/dist/cli/commands/generate-comment-cmd.d.mts.map +1 -1
  22. package/dist/cli/commands/generate-comment-cmd.mjs +5 -2
  23. package/dist/cli/commands/generate-comment-cmd.mjs.map +1 -1
  24. package/dist/cli/commands/init-cmd.d.mts +2 -2
  25. package/dist/cli/commands/init-cmd.d.mts.map +1 -1
  26. package/dist/cli/commands/init-cmd.mjs +20 -70
  27. package/dist/cli/commands/init-cmd.mjs.map +1 -1
  28. package/dist/cli/commands/log-cmd.d.mts +2 -3
  29. package/dist/cli/commands/log-cmd.d.mts.map +1 -1
  30. package/dist/cli/commands/log-cmd.mjs +42 -271
  31. package/dist/cli/commands/log-cmd.mjs.map +1 -1
  32. package/dist/cli/commands/merge-cmd.d.mts +3 -17
  33. package/dist/cli/commands/merge-cmd.d.mts.map +1 -1
  34. package/dist/cli/commands/merge-cmd.mjs +32 -218
  35. package/dist/cli/commands/merge-cmd.mjs.map +1 -1
  36. package/dist/cli/commands/reset-cmd.d.mts +1 -1
  37. package/dist/cli/commands/reset-cmd.d.mts.map +1 -1
  38. package/dist/cli/commands/reset-cmd.mjs +17 -19
  39. package/dist/cli/commands/reset-cmd.mjs.map +1 -1
  40. package/dist/cli/commands/restore-cmd.d.mts +8 -7
  41. package/dist/cli/commands/restore-cmd.d.mts.map +1 -1
  42. package/dist/cli/commands/restore-cmd.mjs +44 -62
  43. package/dist/cli/commands/restore-cmd.mjs.map +1 -1
  44. package/dist/cli/commands/status-cmd.d.mts +2 -2
  45. package/dist/cli/commands/status-cmd.d.mts.map +1 -1
  46. package/dist/cli/commands/status-cmd.mjs +63 -62
  47. package/dist/cli/commands/status-cmd.mjs.map +1 -1
  48. package/dist/cli/vcs-projection-helper.d.mts +23 -0
  49. package/dist/cli/vcs-projection-helper.d.mts.map +1 -0
  50. package/dist/cli/vcs-projection-helper.mjs +91 -0
  51. package/dist/cli/vcs-projection-helper.mjs.map +1 -0
  52. package/dist/test/test-helpers.d.mts +2 -2
  53. package/dist/test/test-helpers.d.mts.map +1 -1
  54. package/package.json +3 -3
  55. package/src/AUTO-GENERATED-version.mts +1 -1
  56. package/src/bootstrap/node-bootstrap.mts +1 -1
  57. package/src/cli/cli-e2e.respec.mts +4 -4
  58. package/src/cli/commands/add-cmd.mts +32 -347
  59. package/src/cli/commands/branch-cmd.mts +56 -225
  60. package/src/cli/commands/checkout-cmd.mts +34 -138
  61. package/src/cli/commands/commit-cmd.mts +46 -473
  62. package/src/cli/commands/commit-log-cmd.respec.mts +11 -7
  63. package/src/cli/commands/generate-comment-cmd.mts +6 -2
  64. package/src/cli/commands/init-cmd.mts +20 -73
  65. package/src/cli/commands/log-cmd.mts +41 -331
  66. package/src/cli/commands/merge-cmd.mts +32 -252
  67. package/src/cli/commands/reset-cmd.mts +17 -19
  68. package/src/cli/commands/restore-cmd.mts +52 -72
  69. package/src/cli/commands/status-add-cmd.respec.mts +3 -3
  70. package/src/cli/commands/status-cmd.mts +68 -63
  71. package/src/cli/vcs-projection-helper.mts +112 -0
  72. package/src/engine/branch/branch-space.respec.mts +5 -5
  73. package/src/engine/merge/b2tfs-sync-comments.respec.mts +4 -4
  74. package/src/test/test-helpers.mts +2 -2
  75. package/src/vcs-gib.respec.mts +11 -8
  76. package/tsconfig.test.tsbuildinfo +1 -1
  77. package/tsconfig.tsbuildinfo +1 -1
  78. package/dist/engine/branch/branch-graph-helper.d.mts +0 -28
  79. package/dist/engine/branch/branch-graph-helper.d.mts.map +0 -1
  80. package/dist/engine/branch/branch-graph-helper.mjs +0 -81
  81. package/dist/engine/branch/branch-graph-helper.mjs.map +0 -1
  82. package/dist/engine/branch/branch-helper.d.mts +0 -40
  83. package/dist/engine/branch/branch-helper.d.mts.map +0 -1
  84. package/dist/engine/branch/branch-helper.mjs +0 -78
  85. package/dist/engine/branch/branch-helper.mjs.map +0 -1
  86. package/dist/engine/branch/branch-space-helper.d.mts +0 -23
  87. package/dist/engine/branch/branch-space-helper.d.mts.map +0 -1
  88. package/dist/engine/branch/branch-space-helper.mjs +0 -78
  89. package/dist/engine/branch/branch-space-helper.mjs.map +0 -1
  90. package/dist/engine/branch/branch-types.d.mts +0 -26
  91. package/dist/engine/branch/branch-types.d.mts.map +0 -1
  92. package/dist/engine/branch/branch-types.mjs +0 -7
  93. package/dist/engine/branch/branch-types.mjs.map +0 -1
  94. package/dist/engine/diff/line-diff-helper.d.mts +0 -76
  95. package/dist/engine/diff/line-diff-helper.d.mts.map +0 -1
  96. package/dist/engine/diff/line-diff-helper.mjs +0 -144
  97. package/dist/engine/diff/line-diff-helper.mjs.map +0 -1
  98. package/dist/engine/filter/glob-helper.d.mts +0 -51
  99. package/dist/engine/filter/glob-helper.d.mts.map +0 -1
  100. package/dist/engine/filter/glob-helper.mjs +0 -132
  101. package/dist/engine/filter/glob-helper.mjs.map +0 -1
  102. package/dist/engine/index/index-helper.d.mts +0 -113
  103. package/dist/engine/index/index-helper.d.mts.map +0 -1
  104. package/dist/engine/index/index-helper.mjs +0 -533
  105. package/dist/engine/index/index-helper.mjs.map +0 -1
  106. package/dist/engine/index/index-types.d.mts +0 -63
  107. package/dist/engine/index/index-types.d.mts.map +0 -1
  108. package/dist/engine/index/index-types.mjs +0 -7
  109. package/dist/engine/index/index-types.mjs.map +0 -1
  110. package/dist/engine/item/item-atom-helper.d.mts +0 -20
  111. package/dist/engine/item/item-atom-helper.d.mts.map +0 -1
  112. package/dist/engine/item/item-atom-helper.mjs +0 -30
  113. package/dist/engine/item/item-atom-helper.mjs.map +0 -1
  114. package/dist/engine/item/item-helper.d.mts +0 -94
  115. package/dist/engine/item/item-helper.d.mts.map +0 -1
  116. package/dist/engine/item/item-helper.mjs +0 -424
  117. package/dist/engine/item/item-helper.mjs.map +0 -1
  118. package/dist/engine/item/item-types.d.mts +0 -128
  119. package/dist/engine/item/item-types.d.mts.map +0 -1
  120. package/dist/engine/item/item-types.mjs +0 -16
  121. package/dist/engine/item/item-types.mjs.map +0 -1
  122. package/dist/engine/merge/b2tfs-3way-graft-strategy.d.mts +0 -74
  123. package/dist/engine/merge/b2tfs-3way-graft-strategy.d.mts.map +0 -1
  124. package/dist/engine/merge/b2tfs-3way-graft-strategy.mjs +0 -570
  125. package/dist/engine/merge/b2tfs-3way-graft-strategy.mjs.map +0 -1
  126. package/dist/engine/merge/b2tfs-3way-graft-types.d.mts +0 -34
  127. package/dist/engine/merge/b2tfs-3way-graft-types.d.mts.map +0 -1
  128. package/dist/engine/merge/b2tfs-3way-graft-types.mjs +0 -2
  129. package/dist/engine/merge/b2tfs-3way-graft-types.mjs.map +0 -1
  130. package/dist/engine/status/status-helper.d.mts +0 -90
  131. package/dist/engine/status/status-helper.d.mts.map +0 -1
  132. package/dist/engine/status/status-helper.mjs +0 -286
  133. package/dist/engine/status/status-helper.mjs.map +0 -1
  134. package/src/engine/branch/branch-graph-helper.mts +0 -117
  135. package/src/engine/branch/branch-helper.mts +0 -112
  136. package/src/engine/branch/branch-space-helper.mts +0 -110
  137. package/src/engine/branch/branch-types.mts +0 -29
  138. package/src/engine/diff/line-diff-helper.mts +0 -210
  139. package/src/engine/diff/line-diff-helper.respec.mts +0 -84
  140. package/src/engine/filter/glob-helper.mts +0 -165
  141. package/src/engine/filter/glob-helper.respec.mts +0 -94
  142. package/src/engine/index/index-helper.mts +0 -705
  143. package/src/engine/index/index-types.mts +0 -66
  144. package/src/engine/item/item-atom-helper.mts +0 -30
  145. package/src/engine/item/item-helper.mts +0 -544
  146. package/src/engine/item/item-types.mts +0 -137
  147. package/src/engine/item/item.respec.mts +0 -92
  148. package/src/engine/merge/b2tfs-3way-graft-strategy.mts +0 -630
  149. package/src/engine/merge/b2tfs-3way-graft-strategy.respec.mts +0 -346
  150. package/src/engine/merge/b2tfs-3way-graft-types.mts +0 -34
  151. package/src/engine/status/status-helper.mts +0 -383
@@ -1,705 +0,0 @@
1
- /**
2
- * @module index-helper
3
- *
4
- * B2tFS Index Helper functions: space-qualified address formatting and branch index tracking.
5
- */
6
-
7
- import path from 'node:path';
8
- import fs from 'node:fs/promises';
9
-
10
- import { getUUID, extractErrorMsg } from '@ibgib/helper-gib/dist/helpers/utils-helper.mjs';
11
- import { ROOT } from '@ibgib/ts-gib/dist/V1/constants.mjs';
12
- import { Factory_V1 } from '@ibgib/ts-gib/dist/V1/factory.mjs';
13
- import { getIbGibAddr } from '@ibgib/ts-gib/dist/helper.mjs';
14
- import { getIbGibsFromCache_fallbackToSpaces } from '@ibgib/core-gib/dist/common/other/ibgib-helper.mjs';
15
- import { MetaspaceService } from '@ibgib/core-gib/dist/witness/space/metaspace/metaspace-types.mjs';
16
- import { IbGibSpaceAny } from '@ibgib/core-gib/dist/witness/space/space-base-v1.mjs';
17
- import { putInSpace, persistTransformResult } from '@ibgib/core-gib/dist/witness/space/space-helper.mjs';
18
-
19
- import { GLOBAL_LOG_A_LOT } from '../../vcs-gib-constants.mjs';
20
- import { SpaceQualifiedIbGibAddr, B2tFSIndexIbGib_V1, B2tFSIndexData_V1, B2tFSIndexRel8ns_V1 } from './index-types.mjs';
21
- import { B2tFSBranchIbGib_V1 } from '../branch/branch-types.mjs';
22
- import { B2tFSItemIbGib_V1, B2TFS_CHILD_ITEM_REL8N_NAME } from '../item/item-types.mjs';
23
- import { getIbGibWithMetaspaceFallback, writeB2tFSItemToFS } from '../item/item-helper.mjs';
24
-
25
- const lcFile = `[index-helper]`;
26
- const logalot = GLOBAL_LOG_A_LOT;
27
-
28
- export const SOFT_ADDR_DATA_KEY_PREFIX = '@';
29
- export const SOFT_ADDR_DATA_KEY_PREFIX_SPACE_QUALIFIED = '$@';
30
-
31
- /**
32
- * Returns a space-qualified address string.
33
- * Format: `v:1^spaceId:<spaceId>^<ibGibAddr>`
34
- */
35
- export function getSpaceQualifiedIbGibAddr(
36
- optsOrSpaceId: { spaceId: string; ibGibAddr: string } | string,
37
- rawIbGibAddr?: string,
38
- ): SpaceQualifiedIbGibAddr {
39
- if (typeof optsOrSpaceId === 'object' && optsOrSpaceId !== null) {
40
- return `v:1^spaceId:${optsOrSpaceId.spaceId}^${optsOrSpaceId.ibGibAddr}`;
41
- }
42
- return `v:1^spaceId:${optsOrSpaceId}^${rawIbGibAddr}`;
43
- }
44
-
45
- /**
46
- * Parses a space-qualified address string into version, spaceId, ibGibAddr, ib, and gib.
47
- */
48
- export function parseSpaceQualifiedIbGibAddr(
49
- optsOrSqAddr: { spaceQualifiedAddr: string } | { sqAddr: string } | string,
50
- ): { version: number; spaceId: string; ibGibAddr: string; ib: string; gib: string } | null {
51
- let sqAddr: string | undefined;
52
- if (typeof optsOrSqAddr === 'string') {
53
- sqAddr = optsOrSqAddr;
54
- } else if (typeof optsOrSqAddr === 'object' && optsOrSqAddr !== null) {
55
- sqAddr = (optsOrSqAddr as any).spaceQualifiedAddr || (optsOrSqAddr as any).sqAddr;
56
- }
57
-
58
- if (!sqAddr || typeof sqAddr !== 'string') {
59
- return null;
60
- }
61
-
62
- const match = sqAddr.match(/^v:(\d+)\^spaceId:([^\^]+)\^(.+)$/);
63
- if (!match) {
64
- return null;
65
- }
66
-
67
- const version = parseInt(match[1], 10);
68
- const spaceId = match[2];
69
- const ibGibAddr = match[3];
70
-
71
- const lastCaretIndex = ibGibAddr.lastIndexOf('^');
72
- if (lastCaretIndex === -1) {
73
- return null;
74
- }
75
-
76
- const ib = ibGibAddr.substring(0, lastCaretIndex);
77
- const gib = ibGibAddr.substring(lastCaretIndex + 1);
78
-
79
- if (!ib || !gib) {
80
- return null;
81
- }
82
-
83
- return {
84
- version,
85
- spaceId,
86
- ibGibAddr,
87
- ib,
88
- gib,
89
- };
90
- }
91
-
92
- /**
93
- * Helper to extract branch name from space-qualified address or raw atom ib string.
94
- */
95
- export function extractBranchNameFromAddr(addr: string): string | null {
96
- if (!addr) {
97
- return null;
98
- }
99
- const sq = parseSpaceQualifiedIbGibAddr(addr);
100
- const ib = sq ? sq.ib : (addr.includes('^') ? addr.substring(0, addr.lastIndexOf('^')) : addr);
101
- if (ib && ib.startsWith('b2tfs_branch ')) {
102
- const parts = ib.split(' ');
103
- if (parts.length >= 2 && parts[1]) {
104
- return parts[1];
105
- }
106
- }
107
- return null;
108
- }
109
-
110
- /**
111
- * Deduplicates branch entries by branch name and merges new branch addresses into existing addresses
112
- * without dropping other registered branches.
113
- */
114
- export function mergeBranchAddrs(
115
- existingAddrs: string[] = [],
116
- newAddrs: string[] = [],
117
- ): string[] {
118
- const map = new Map<string, string>();
119
- for (const addr of existingAddrs) {
120
- if (!addr) {
121
- continue;
122
- }
123
- const bName = extractBranchNameFromAddr(addr) || addr;
124
- map.set(bName, addr);
125
- }
126
- for (const addr of newAddrs) {
127
- if (!addr) {
128
- continue;
129
- }
130
- const bName = extractBranchNameFromAddr(addr) || addr;
131
- map.set(bName, addr);
132
- }
133
- return Array.from(map.values());
134
- }
135
-
136
- /**
137
- * Creates a new B2tFSIndexIbGib special meta object for a space.
138
- */
139
- export async function createB2tFSIndexIbGib({
140
- spaceId,
141
- activeSpaceId,
142
- activeBranch,
143
- space,
144
- metaspace,
145
- branchAddr,
146
- }: {
147
- spaceId: string;
148
- activeSpaceId: string;
149
- activeBranch?: string;
150
- space: IbGibSpaceAny;
151
- metaspace: MetaspaceService;
152
- branchAddr?: string;
153
- }): Promise<B2tFSIndexIbGib_V1> {
154
- const lc = `${lcFile}[${createB2tFSIndexIbGib.name}]`;
155
- try {
156
- if (logalot) {
157
- console.log(`${lc} starting... (I: 3a4b5c6d7e8f9a0b1c2d3e4f5a6b7c8d)`);
158
- }
159
-
160
- const uuid = await getUUID();
161
- const data: B2tFSIndexData_V1 = {
162
- mySpaceId: spaceId,
163
- activeSpaceId,
164
- activeBranch,
165
- uuid,
166
- classname: 'B2tFSIndexIbGib_V1',
167
- '$@branch': [],
168
- stagedPaths: {},
169
- staged_item: [],
170
- };
171
-
172
- const rel8ns: B2tFSIndexRel8ns_V1 = {};
173
- if (branchAddr) {
174
- const sqBranchAddr = branchAddr.startsWith('v:1^spaceId:')
175
- ? branchAddr
176
- : getSpaceQualifiedIbGibAddr({
177
- spaceId: space.data?.spaceSubPath || space.data?.name || spaceId,
178
- ibGibAddr: branchAddr,
179
- });
180
- rel8ns.branch = [sqBranchAddr];
181
- rel8ns.branch_tjp = [sqBranchAddr];
182
- }
183
-
184
- const res = await Factory_V1.firstGen({
185
- parentIbGib: ROOT,
186
- ib: `meta special b2tfs_index ${uuid}`,
187
- data,
188
- rel8ns: Object.keys(rel8ns).length > 0 ? rel8ns : undefined,
189
- });
190
-
191
- const indexIbGib = res.newIbGib as B2tFSIndexIbGib_V1;
192
- await persistTransformResult({ resTransform: res, space });
193
- await metaspace.registerNewIbGib({ ibGib: indexIbGib });
194
-
195
- if (logalot) {
196
- console.log(`${lc} Created index IbGib: ${indexIbGib.ib} (I: 4b5c6d7e8f9a0b1c2d3e4f5a6b7c8d9e)`);
197
- }
198
-
199
- return indexIbGib;
200
- } catch (error) {
201
- console.error(`${lc} ${extractErrorMsg(error)} (E: 5c6d7e8f9a0b1c2d3e4f5a6b7c8d9e0f)`);
202
- throw error;
203
- } finally {
204
- if (logalot) {
205
- console.log(`${lc} complete. (I: 6d7e8f9a0b1c2d3e4f5a6b7c8d9e0f1a)`);
206
- }
207
- }
208
- }
209
-
210
- /**
211
- * Retrieves authoritative B2tFSIndexIbGib_V1 from space.
212
- */
213
- export async function getB2tFSIndexIbGib({
214
- space,
215
- vcsPath,
216
- }: {
217
- space: IbGibSpaceAny;
218
- vcsPath?: string;
219
- }): Promise<B2tFSIndexIbGib_V1 | undefined> {
220
- const lc = `${lcFile}[${getB2tFSIndexIbGib.name}]`;
221
- try {
222
- if (logalot) {
223
- console.log(`${lc} starting... (I: 7e8f9a0b1c2d3e4f5a6b7c8d9e0f1a2b)`);
224
- }
225
-
226
- if (!vcsPath) {
227
- return undefined;
228
- }
229
-
230
- const projPath = path.join(vcsPath, 'vcs_index_projection.json');
231
- let indexAddr: string | undefined;
232
- try {
233
- const raw = await fs.readFile(projPath, 'utf8');
234
- const parsed = JSON.parse(raw);
235
- indexAddr = parsed.indexAddr;
236
- } catch {
237
- return undefined;
238
- }
239
-
240
- if (!indexAddr) {
241
- return undefined;
242
- }
243
-
244
- const ibgibs = await getIbGibsFromCache_fallbackToSpaces({
245
- addrs: [indexAddr],
246
- space,
247
- });
248
-
249
- const indexIbGib = ibgibs?.at(0) as B2tFSIndexIbGib_V1;
250
- return indexIbGib;
251
- } catch (error) {
252
- console.error(`${lc} ${extractErrorMsg(error)} (E: 8f9a0b1c2d3e4f5a6b7c8d9e0f1a2b3c)`);
253
- throw error;
254
- } finally {
255
- if (logalot) {
256
- console.log(`${lc} complete. (I: 9a0b1c2d3e4f5a6b7c8d9e0f1a2b3c4d)`);
257
- }
258
- }
259
- }
260
-
261
- /**
262
- * Updates B2tFSIndexIbGib_V1 in space and updates local projection cache.
263
- */
264
- export async function updateB2tFSIndexIbGib({
265
- space,
266
- metaspace,
267
- indexIbGib,
268
- dataToUpdate,
269
- rel8nsToUpdate,
270
- vcsPath,
271
- }: {
272
- space: IbGibSpaceAny;
273
- metaspace: MetaspaceService;
274
- indexIbGib: B2tFSIndexIbGib_V1;
275
- dataToUpdate?: Partial<B2tFSIndexData_V1>;
276
- rel8nsToUpdate?: Partial<B2tFSIndexRel8ns_V1>;
277
- vcsPath?: string;
278
- }): Promise<B2tFSIndexIbGib_V1> {
279
- const lc = `${lcFile}[${updateB2tFSIndexIbGib.name}]`;
280
- try {
281
- if (logalot) {
282
- console.log(`${lc} starting... (I: 0b1c2d3e4f5a6b7c8d9e0f1a2b3c4d5e)`);
283
- }
284
-
285
- const mergedData: B2tFSIndexData_V1 = {
286
- ...(indexIbGib.data || {
287
- mySpaceId: space.data?.uuid || '',
288
- activeSpaceId: '',
289
- uuid: await getUUID(),
290
- }),
291
- ...dataToUpdate,
292
- };
293
-
294
- const mergedRel8ns: B2tFSIndexRel8ns_V1 = {
295
- ...(indexIbGib.rel8ns || {}),
296
- ...rel8nsToUpdate,
297
- };
298
-
299
- if (rel8nsToUpdate?.branch || indexIbGib.rel8ns?.branch) {
300
- mergedRel8ns.branch = mergeBranchAddrs(
301
- indexIbGib.rel8ns?.branch || [],
302
- rel8nsToUpdate?.branch || [],
303
- );
304
- }
305
-
306
- if (rel8nsToUpdate?.branch_tjp || indexIbGib.rel8ns?.branch_tjp) {
307
- mergedRel8ns.branch_tjp = mergeBranchAddrs(
308
- indexIbGib.rel8ns?.branch_tjp || [],
309
- rel8nsToUpdate?.branch_tjp || [],
310
- );
311
- }
312
-
313
- delete mergedRel8ns.past;
314
-
315
- const res = await Factory_V1.firstGen({
316
- parentIbGib: ROOT,
317
- ib: indexIbGib.ib,
318
- data: mergedData,
319
- rel8ns: mergedRel8ns,
320
- });
321
-
322
- const newIndexIbGib = res.newIbGib as B2tFSIndexIbGib_V1;
323
- await persistTransformResult({ resTransform: res, space });
324
- if (metaspace.zeroSpace && metaspace.zeroSpace !== space) {
325
- await persistTransformResult({ resTransform: res, space: metaspace.zeroSpace });
326
- }
327
- await metaspace.registerNewIbGib({ ibGib: newIndexIbGib });
328
-
329
- if (vcsPath) {
330
- await writeVcsIndexProjection({ vcsPath, indexIbGib: newIndexIbGib });
331
- }
332
-
333
- return newIndexIbGib;
334
- } catch (error) {
335
- console.error(`${lc} ${extractErrorMsg(error)} (E: 1c2d3e4f5a6b7c8d9e0f1a2b3c4d5e6f)`);
336
- throw error;
337
- } finally {
338
- if (logalot) {
339
- console.log(`${lc} complete. (I: 2d3e4f5a6b7c8d9e0f1a2b3c4d5e6f7a)`);
340
- }
341
- }
342
- }
343
-
344
- /**
345
- * Writes read-only JSON projection cache of repository index.
346
- */
347
- export async function writeVcsIndexProjection({
348
- vcsPath,
349
- indexIbGib,
350
- }: {
351
- vcsPath: string;
352
- indexIbGib: B2tFSIndexIbGib_V1;
353
- }): Promise<void> {
354
- const lc = `${lcFile}[${writeVcsIndexProjection.name}]`;
355
- try {
356
- if (logalot) {
357
- console.log(`${lc} starting... (I: 3e4f5a6b7c8d9e0f1a2b3c4d5e6f7a8b)`);
358
- }
359
-
360
- await fs.mkdir(vcsPath, { recursive: true });
361
-
362
- const indexAddr = getIbGibAddr({ ibGib: indexIbGib });
363
- const projectionObj = {
364
- _isReadOnlyProjection: true,
365
- _comment: 'STRICTLY READ-ONLY PROJECTION GENERATED FROM B2TFSINDEXIBGIB_V1 IN LOCALSPACE',
366
- indexAddr,
367
- spaceId: indexIbGib.data?.mySpaceId,
368
- activeBranch: indexIbGib.data?.activeBranch || 'main',
369
- stagedCount: (indexIbGib.data?.staged_item || []).length,
370
- stagedPaths: indexIbGib.data?.stagedPaths || {},
371
- updatedAt: new Date().toISOString(),
372
- };
373
-
374
- const projFile = path.join(vcsPath, 'vcs_index_projection.json');
375
- await fs.writeFile(projFile, JSON.stringify(projectionObj, null, 2), 'utf8');
376
- } catch (error) {
377
- console.error(`${lc} ${extractErrorMsg(error)} (E: 4f5a6b7c8d9e0f1a2b3c4d5e6f7a8b9c)`);
378
- throw error;
379
- } finally {
380
- if (logalot) {
381
- console.log(`${lc} complete. (I: 5a6b7c8d9e0f1a2b3c4d5e6f7a8b9c0d)`);
382
- }
383
- }
384
- }
385
-
386
- export interface UnstagePathsFromIndexOpts {
387
- indexIbGib: B2tFSIndexIbGib_V1;
388
- paths?: string[];
389
- targetDir?: string;
390
- activeWorktreeDir?: string;
391
- vcsPath?: string;
392
- space: IbGibSpaceAny;
393
- metaspace: MetaspaceService;
394
- all?: boolean;
395
- }
396
-
397
- export interface UnstagePathsFromIndexResult {
398
- updatedIndex: B2tFSIndexIbGib_V1;
399
- unstagedPaths: string[];
400
- }
401
-
402
- /**
403
- * Unstages specified paths (or all paths if `all` or empty `paths`) from `indexIbGib.data.stagedPaths`
404
- * and `indexIbGib.data.staged_item`.
405
- */
406
- export async function unstagePathsFromIndex({
407
- indexIbGib,
408
- paths = [],
409
- targetDir = process.cwd(),
410
- activeWorktreeDir,
411
- vcsPath,
412
- space,
413
- metaspace,
414
- all = false,
415
- }: UnstagePathsFromIndexOpts): Promise<UnstagePathsFromIndexResult> {
416
- const lc = `${lcFile}[${unstagePathsFromIndex.name}]`;
417
- try {
418
- if (logalot) {
419
- console.log(`${lc} starting... (I: 6a7b8c9d0e1f2a3b4c5d6e7f8a9b0c1d)`);
420
- }
421
-
422
- const currentStagedPaths = { ...(indexIbGib.data?.stagedPaths || {}) };
423
- let currentStagedItems = [...(indexIbGib.data?.staged_item || [])];
424
- const unstagedPaths: string[] = [];
425
-
426
- const worktreeDir = activeWorktreeDir || targetDir;
427
-
428
- if (all || paths.length === 0 || (paths.length === 1 && (paths[0] === '.' || paths[0] === ''))) {
429
- for (const relPath of Object.keys(currentStagedPaths)) {
430
- unstagedPaths.push(relPath);
431
- }
432
- const updatedIndex = await updateB2tFSIndexIbGib({
433
- space,
434
- metaspace,
435
- indexIbGib,
436
- dataToUpdate: {
437
- stagedPaths: {},
438
- staged_item: [],
439
- },
440
- vcsPath,
441
- });
442
- return { updatedIndex, unstagedPaths };
443
- }
444
-
445
- for (const p of paths) {
446
- let relPath = p.replace(/\\/g, '/');
447
- if (path.isAbsolute(p)) {
448
- relPath = path.relative(worktreeDir, p).replace(/\\/g, '/');
449
- } else if (targetDir !== worktreeDir) {
450
- const absP = path.resolve(targetDir, p);
451
- relPath = path.relative(worktreeDir, absP).replace(/\\/g, '/');
452
- }
453
-
454
- const targetKeys = Object.keys(currentStagedPaths).filter((k) => {
455
- return k === relPath || k.startsWith(relPath + '/');
456
- });
457
-
458
- for (const key of targetKeys) {
459
- const record = currentStagedPaths[key];
460
- if (record && record.itemAddr) {
461
- currentStagedItems = currentStagedItems.filter((addr) => addr !== record.itemAddr);
462
- }
463
- delete currentStagedPaths[key];
464
- if (!unstagedPaths.includes(key)) {
465
- unstagedPaths.push(key);
466
- }
467
- }
468
- }
469
-
470
- const updatedIndex = await updateB2tFSIndexIbGib({
471
- space,
472
- metaspace,
473
- indexIbGib,
474
- dataToUpdate: {
475
- stagedPaths: currentStagedPaths,
476
- staged_item: currentStagedItems,
477
- },
478
- vcsPath,
479
- });
480
-
481
- return { updatedIndex, unstagedPaths };
482
- } catch (error) {
483
- console.error(`${lc} ${extractErrorMsg(error)} (E: 7b8c9d0e1f2a3b4c5d6e7f8a9b0c1d2e)`);
484
- throw error;
485
- } finally {
486
- if (logalot) {
487
- console.log(`${lc} complete. (I: 8c9d0e1f2a3b4c5d6e7f8a9b0c1d2e3f)`);
488
- }
489
- }
490
- }
491
-
492
- export interface RestoreWorktreePathsOpts {
493
- paths?: string[];
494
- targetDir?: string;
495
- activeWorktreeDir: string;
496
- activeBranchSpace: IbGibSpaceAny;
497
- metaspace: MetaspaceService;
498
- indexIbGib?: B2tFSIndexIbGib_V1;
499
- vcsPath?: string;
500
- }
501
-
502
- export interface RestoreWorktreePathsResult {
503
- restoredPaths: string[];
504
- }
505
-
506
- /**
507
- * Restores working tree physical files from committed branch item snapshot.
508
- */
509
- export async function restoreWorktreePathsFromSnapshot({
510
- paths = [],
511
- targetDir = process.cwd(),
512
- activeWorktreeDir,
513
- activeBranchSpace,
514
- metaspace,
515
- indexIbGib,
516
- vcsPath,
517
- }: RestoreWorktreePathsOpts): Promise<RestoreWorktreePathsResult> {
518
- const lc = `${lcFile}[${restoreWorktreePathsFromSnapshot.name}]`;
519
- try {
520
- if (logalot) {
521
- console.log(`${lc} starting... (I: 9d0e1f2a3b4c5d6e7f8a9b0c1d2e3f4a)`);
522
- }
523
-
524
- let currIndex = indexIbGib;
525
- if (!currIndex) {
526
- currIndex = await getB2tFSIndexIbGib({ space: activeBranchSpace, vcsPath });
527
- }
528
- if (!currIndex) {
529
- throw new Error(`Repository index B2tFSIndexIbGib_V1 not found (E: 0e1f2a3b4c5d6e7f8a9b0c1d2e3f4a5b)`);
530
- }
531
-
532
- const activeBranch = currIndex.data?.activeBranch || 'main';
533
- const branchAddrs = [
534
- ...(currIndex.rel8ns?.branch || []),
535
- ...(currIndex.rel8ns?.branch_tjp || []),
536
- ];
537
-
538
- let branchIbGibs: B2tFSBranchIbGib_V1[] = [];
539
- if (branchAddrs.length > 0) {
540
- for (const bAddr of branchAddrs) {
541
- const bIbGib = await getIbGibWithMetaspaceFallback({
542
- addr: bAddr,
543
- space: activeBranchSpace,
544
- metaspace,
545
- });
546
- if (bIbGib) {
547
- branchIbGibs.push(bIbGib as B2tFSBranchIbGib_V1);
548
- }
549
- }
550
- }
551
-
552
- const activeBranchIbGib = branchIbGibs.find(
553
- (b) => b?.data?.name === activeBranch,
554
- ) || (branchIbGibs.at(0) as B2tFSBranchIbGib_V1);
555
-
556
- if (!activeBranchIbGib) {
557
- throw new Error(`Branch '${activeBranch}' not found (E: 1f2a3b4c5d6e7f8a9b0c1d2e3f4a5b6c)`);
558
- }
559
-
560
- const rootItemAddr = activeBranchIbGib.rel8ns?.b2tfs_item?.at(0);
561
- if (!rootItemAddr) {
562
- throw new Error(`Branch '${activeBranch}' has no root item (E: 2a3b4c5d6e7f8a9b0c1d2e3f4a5b6c7d)`);
563
- }
564
-
565
- const rootItem = await getIbGibWithMetaspaceFallback({
566
- addr: rootItemAddr,
567
- space: activeBranchSpace,
568
- metaspace,
569
- }) as B2tFSItemIbGib_V1;
570
-
571
- if (!rootItem) {
572
- throw new Error(`Root item node '${rootItemAddr}' could not be fetched (E: 3f7e6d5c4b3a2f1e0d9c8b7a6f5e4d3c)`);
573
- }
574
-
575
- const committedItemMap = new Map<string, { relPath: string; parentRelPath: string; item: B2tFSItemIbGib_V1 }>();
576
-
577
- await restoreCmd_collectCommittedItems({
578
- item: rootItem,
579
- parentRelPath: '',
580
- space: activeBranchSpace,
581
- metaspace,
582
- outMap: committedItemMap,
583
- });
584
-
585
- const restoredPaths: string[] = [];
586
-
587
- if (paths.length === 0 || (paths.length === 1 && (paths[0] === '.' || paths[0] === ''))) {
588
- for (const entry of committedItemMap.values()) {
589
- const destPath = path.join(activeWorktreeDir, entry.parentRelPath);
590
- await writeB2tFSItemToFS({
591
- item: entry.item,
592
- destPath,
593
- space: activeBranchSpace,
594
- metaspace,
595
- });
596
- restoredPaths.push(entry.relPath);
597
- }
598
- } else {
599
- for (const p of paths) {
600
- let relPath = p.replace(/\\/g, '/');
601
- if (path.isAbsolute(p)) {
602
- relPath = path.relative(activeWorktreeDir, p).replace(/\\/g, '/');
603
- } else if (targetDir !== activeWorktreeDir) {
604
- const absP = path.resolve(targetDir, p);
605
- relPath = path.relative(activeWorktreeDir, absP).replace(/\\/g, '/');
606
- }
607
-
608
- let matchedCount = 0;
609
- for (const entry of committedItemMap.values()) {
610
- if (entry.relPath === relPath || entry.relPath.startsWith(relPath + '/')) {
611
- const destPath = path.join(activeWorktreeDir, entry.parentRelPath);
612
- await writeB2tFSItemToFS({
613
- item: entry.item,
614
- destPath,
615
- space: activeBranchSpace,
616
- metaspace,
617
- });
618
- if (!restoredPaths.includes(entry.relPath)) {
619
- restoredPaths.push(entry.relPath);
620
- }
621
- matchedCount++;
622
- }
623
- }
624
-
625
- if (matchedCount === 0) {
626
- throw new Error(`pathspec '${p}' did not match any committed file(s) in snapshot (E: 4f5a6b7c8d9e0f1a2b3c4d5e6f7a8b9c)`);
627
- }
628
- }
629
- }
630
-
631
- return { restoredPaths };
632
- } catch (error) {
633
- console.error(`${lc} ${extractErrorMsg(error)} (E: 5e6f7a8b9c0d1e2f3a4b5c6d7e8f9a0b)`);
634
- throw error;
635
- } finally {
636
- if (logalot) {
637
- console.log(`${lc} complete. (I: 6e7f8a9b0c1d2e3f4a5b6c7d8e9f0a1b)`);
638
- }
639
- }
640
- }
641
-
642
- // #region restoreCmd_collectCommittedItems
643
- async function restoreCmd_collectCommittedItems({
644
- item,
645
- parentRelPath,
646
- space,
647
- metaspace,
648
- outMap,
649
- }: {
650
- item: B2tFSItemIbGib_V1;
651
- parentRelPath: string;
652
- space: IbGibSpaceAny;
653
- metaspace: MetaspaceService;
654
- outMap: Map<string, { relPath: string; parentRelPath: string; item: B2tFSItemIbGib_V1 }>;
655
- }): Promise<void> {
656
- if (!item || !item.data) { return; }
657
-
658
- const { fsType, name } = item.data;
659
-
660
- if (fsType === 'folder' && !parentRelPath) {
661
- const childAddrs = item.rel8ns?.[B2TFS_CHILD_ITEM_REL8N_NAME] || [];
662
- for (const childAddr of childAddrs) {
663
- const childItem = await getIbGibWithMetaspaceFallback({
664
- addr: childAddr,
665
- space,
666
- metaspace,
667
- }) as B2tFSItemIbGib_V1;
668
-
669
- if (childItem) {
670
- await restoreCmd_collectCommittedItems({
671
- item: childItem,
672
- parentRelPath: '',
673
- space,
674
- metaspace,
675
- outMap,
676
- });
677
- }
678
- }
679
- } else {
680
- const relPath = parentRelPath ? `${parentRelPath}/${name}` : name;
681
- outMap.set(relPath, { relPath, parentRelPath, item });
682
-
683
- if (fsType === 'folder') {
684
- const childAddrs = item.rel8ns?.[B2TFS_CHILD_ITEM_REL8N_NAME] || [];
685
- for (const childAddr of childAddrs) {
686
- const childItem = await getIbGibWithMetaspaceFallback({
687
- addr: childAddr,
688
- space,
689
- metaspace,
690
- }) as B2tFSItemIbGib_V1;
691
-
692
- if (childItem) {
693
- await restoreCmd_collectCommittedItems({
694
- item: childItem,
695
- parentRelPath: relPath,
696
- space,
697
- metaspace,
698
- outMap,
699
- });
700
- }
701
- }
702
- }
703
- }
704
- }
705
- // #endregion restoreCmd_collectCommittedItems