@teambit/merge-lanes 0.0.0-012f21d51d884d0035b43584f4bd41f1410e77b9

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.
@@ -0,0 +1,792 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = exports.MergeLanesMain = void 0;
7
+ function _bitError() {
8
+ const data = require("@teambit/bit-error");
9
+ _bitError = function () {
10
+ return data;
11
+ };
12
+ return data;
13
+ }
14
+ function _path() {
15
+ const data = _interopRequireDefault(require("path"));
16
+ _path = function () {
17
+ return data;
18
+ };
19
+ return data;
20
+ }
21
+ function _cli() {
22
+ const data = require("@teambit/cli");
23
+ _cli = function () {
24
+ return data;
25
+ };
26
+ return data;
27
+ }
28
+ function _importer() {
29
+ const data = require("@teambit/importer");
30
+ _importer = function () {
31
+ return data;
32
+ };
33
+ return data;
34
+ }
35
+ function _lanes() {
36
+ const data = require("@teambit/lanes");
37
+ _lanes = function () {
38
+ return data;
39
+ };
40
+ return data;
41
+ }
42
+ function _merging() {
43
+ const data = require("@teambit/merging");
44
+ _merging = function () {
45
+ return data;
46
+ };
47
+ return data;
48
+ }
49
+ function _componentModules() {
50
+ const data = require("@teambit/component.modules.merge-helper");
51
+ _componentModules = function () {
52
+ return data;
53
+ };
54
+ return data;
55
+ }
56
+ function _workspace() {
57
+ const data = require("@teambit/workspace");
58
+ _workspace = function () {
59
+ return data;
60
+ };
61
+ return data;
62
+ }
63
+ function _configStore() {
64
+ const data = require("@teambit/config-store");
65
+ _configStore = function () {
66
+ return data;
67
+ };
68
+ return data;
69
+ }
70
+ function _harmonyModules() {
71
+ const data = require("@teambit/harmony.modules.get-basic-log");
72
+ _harmonyModules = function () {
73
+ return data;
74
+ };
75
+ return data;
76
+ }
77
+ function _componentId() {
78
+ const data = require("@teambit/component-id");
79
+ _componentId = function () {
80
+ return data;
81
+ };
82
+ return data;
83
+ }
84
+ function _pMapSeries() {
85
+ const data = _interopRequireDefault(require("p-map-series"));
86
+ _pMapSeries = function () {
87
+ return data;
88
+ };
89
+ return data;
90
+ }
91
+ function _scope() {
92
+ const data = require("@teambit/scope");
93
+ _scope = function () {
94
+ return data;
95
+ };
96
+ return data;
97
+ }
98
+ function _laneId() {
99
+ const data = require("@teambit/lane-id");
100
+ _laneId = function () {
101
+ return data;
102
+ };
103
+ return data;
104
+ }
105
+ function _logger() {
106
+ const data = require("@teambit/logger");
107
+ _logger = function () {
108
+ return data;
109
+ };
110
+ return data;
111
+ }
112
+ function _checkout() {
113
+ const data = require("@teambit/checkout");
114
+ _checkout = function () {
115
+ return data;
116
+ };
117
+ return data;
118
+ }
119
+ function _remove() {
120
+ const data = require("@teambit/remove");
121
+ _remove = function () {
122
+ return data;
123
+ };
124
+ return data;
125
+ }
126
+ function _lodash() {
127
+ const data = require("lodash");
128
+ _lodash = function () {
129
+ return data;
130
+ };
131
+ return data;
132
+ }
133
+ function _export() {
134
+ const data = require("@teambit/export");
135
+ _export = function () {
136
+ return data;
137
+ };
138
+ return data;
139
+ }
140
+ function _mergeLanes() {
141
+ const data = require("./merge-lanes.aspect");
142
+ _mergeLanes = function () {
143
+ return data;
144
+ };
145
+ return data;
146
+ }
147
+ function _mergeLane() {
148
+ const data = require("./merge-lane.cmd");
149
+ _mergeLane = function () {
150
+ return data;
151
+ };
152
+ return data;
153
+ }
154
+ function _missingCompsToMerge() {
155
+ const data = require("./exceptions/missing-comps-to-merge");
156
+ _missingCompsToMerge = function () {
157
+ return data;
158
+ };
159
+ return data;
160
+ }
161
+ function _mergeAbort() {
162
+ const data = require("./merge-abort.cmd");
163
+ _mergeAbort = function () {
164
+ return data;
165
+ };
166
+ return data;
167
+ }
168
+ function _lastMerged() {
169
+ const data = require("./last-merged");
170
+ _lastMerged = function () {
171
+ return data;
172
+ };
173
+ return data;
174
+ }
175
+ function _mergeMove() {
176
+ const data = require("./merge-move.cmd");
177
+ _mergeMove = function () {
178
+ return data;
179
+ };
180
+ return data;
181
+ }
182
+ function _express() {
183
+ const data = require("@teambit/express");
184
+ _express = function () {
185
+ return data;
186
+ };
187
+ return data;
188
+ }
189
+ function _lanesCheckConflicts() {
190
+ const data = require("./lanes-check-conflicts.route");
191
+ _lanesCheckConflicts = function () {
192
+ return data;
193
+ };
194
+ return data;
195
+ }
196
+ function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
197
+ function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
198
+ function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
199
+ function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
200
+ function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
201
+ function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
202
+ class MergeLanesMain {
203
+ constructor(workspace, merging, lanes, logger, remove, scope, exporter, importer, checkout) {
204
+ this.workspace = workspace;
205
+ this.merging = merging;
206
+ this.lanes = lanes;
207
+ this.logger = logger;
208
+ this.remove = remove;
209
+ this.scope = scope;
210
+ this.exporter = exporter;
211
+ this.importer = importer;
212
+ this.checkout = checkout;
213
+ }
214
+ async mergeLaneByCLI(laneName, options) {
215
+ if (!this.workspace) {
216
+ throw new (_bitError().BitError)(`unable to merge a lane outside of Bit workspace`);
217
+ }
218
+ const currentLaneId = this.workspace.consumer.getCurrentLaneId();
219
+ const otherLaneId = await this.workspace.consumer.getParsedLaneId(laneName);
220
+ return this.mergeLane(otherLaneId, currentLaneId, options);
221
+ }
222
+
223
+ /**
224
+ * merge otherLaneId into currentLaneId
225
+ */
226
+ async mergeLane(otherLaneId, currentLaneId, options) {
227
+ this.validateMergeFlags(otherLaneId, currentLaneId, options);
228
+ const {
229
+ currentLane,
230
+ otherLane,
231
+ laneToFetchArtifactsFrom,
232
+ idsToMerge
233
+ } = await this.resolveMergeContext(otherLaneId, currentLaneId, options);
234
+ const {
235
+ mergeStrategy,
236
+ noAutoSnap,
237
+ noSnap,
238
+ tag,
239
+ snapMessage,
240
+ existingOnWorkspaceOnly,
241
+ build,
242
+ loose,
243
+ keepReadme,
244
+ squash,
245
+ noSquash,
246
+ pattern,
247
+ includeDeps,
248
+ skipDependencyInstallation,
249
+ resolveUnrelated,
250
+ ignoreConfigChanges,
251
+ throwIfNotUpToDate,
252
+ detachHead
253
+ } = options;
254
+ const legacyScope = this.scope.legacyScope;
255
+ const consumer = this.workspace?.consumer;
256
+ if (throwIfNotUpToDate) await this.throwIfNotUpToDate(otherLaneId, currentLaneId);
257
+ this.logger.debug(`merging the following ids: ${idsToMerge.toString()}`);
258
+ const shouldSquash = squash || currentLaneId.isDefault() && !noSquash;
259
+ let allComponentsStatus = await this.merging.getMergeStatus(idsToMerge, {
260
+ resolveUnrelated,
261
+ ignoreConfigChanges,
262
+ shouldSquash,
263
+ mergeStrategy,
264
+ handleTargetAheadAsDiverged: noSnap,
265
+ detachHead,
266
+ shouldMergeAspectsData: true
267
+ }, currentLane, otherLane);
268
+ if (pattern) {
269
+ const componentIds = idsToMerge;
270
+ const compIdsFromPattern = await (this.workspace || this.scope).filterIdsFromPoolIdsByPattern(pattern, componentIds);
271
+ allComponentsStatus = await filterComponentsStatus(allComponentsStatus, compIdsFromPattern, idsToMerge, legacyScope, includeDeps, otherLane, shouldSquash);
272
+ idsToMerge.forEach(bitId => {
273
+ if (!allComponentsStatus.find(c => c.id.isEqualWithoutVersion(bitId))) {
274
+ allComponentsStatus.push({
275
+ id: bitId,
276
+ unchangedLegitimately: true,
277
+ unchangedMessage: `excluded by pattern`
278
+ });
279
+ }
280
+ });
281
+ }
282
+ if (existingOnWorkspaceOnly && this.workspace) {
283
+ const workspaceIds = this.workspace.listIds();
284
+ const compIdsFromPattern = workspaceIds.filter(id => allComponentsStatus.find(c => c.id.isEqualWithoutVersion(id)));
285
+ allComponentsStatus = await filterComponentsStatus(allComponentsStatus, compIdsFromPattern, idsToMerge, legacyScope, includeDeps, otherLane, shouldSquash);
286
+ idsToMerge.forEach(bitId => {
287
+ if (!allComponentsStatus.find(c => c.id.isEqualWithoutVersion(bitId))) {
288
+ allComponentsStatus.push({
289
+ id: bitId,
290
+ unchangedLegitimately: true,
291
+ unchangedMessage: `not in the workspace`
292
+ });
293
+ }
294
+ });
295
+ }
296
+ (0, _checkout().throwForFailures)(allComponentsStatus);
297
+ const succeededComponents = allComponentsStatus.filter(c => !c.unchangedMessage);
298
+ if (shouldSquash) {
299
+ await squashSnaps(succeededComponents, currentLaneId, otherLaneId, legacyScope, {
300
+ messageTitle: options.snapMessage,
301
+ detachHead
302
+ });
303
+ }
304
+ if (laneToFetchArtifactsFrom) {
305
+ const ids = allComponentsStatus.map(c => c.id);
306
+ await this.importer.importHeadArtifactsFromLane(laneToFetchArtifactsFrom, ids, true);
307
+ }
308
+ const lastMerged = consumer ? new (_lastMerged().LastMerged)(this.scope, consumer, this.logger) : undefined;
309
+ const snapshot = await lastMerged?.takeSnapshot(currentLane);
310
+ const mergeResults = await this.merging.mergeSnaps({
311
+ mergeStrategy,
312
+ allComponentsStatus,
313
+ otherLaneId,
314
+ currentLane,
315
+ noAutoSnap,
316
+ noSnap,
317
+ tag,
318
+ snapMessage,
319
+ build,
320
+ skipDependencyInstallation,
321
+ detachHead,
322
+ loose
323
+ });
324
+ if (snapshot) await lastMerged?.persistSnapshot(snapshot);
325
+ const mergedSuccessfully = !mergeResults.failedComponents || mergeResults.failedComponents.length === 0 || mergeResults.failedComponents.every(failedComponent => failedComponent.unchangedLegitimately);
326
+ let deleteResults = {};
327
+ if (!keepReadme && otherLane && otherLane.readmeComponent && mergedSuccessfully) {
328
+ const readmeComponentId = otherLane.readmeComponent.id.changeVersion(otherLane.readmeComponent?.head?.hash);
329
+ deleteResults = await this.remove.removeLocallyByIds([readmeComponentId], {
330
+ reasonForRemoval: 'lane-merge'
331
+ });
332
+ } else if (otherLane && !otherLane.readmeComponent) {
333
+ deleteResults = {
334
+ readmeResult: ''
335
+ };
336
+ }
337
+ const configMergeResults = (0, _lodash().compact)(allComponentsStatus.map(c => c.configMergeResult));
338
+ const mergedSnapIds = _componentId().ComponentIdList.fromArray(mergeResults.mergeSnapResults?.snappedComponents.map(c => c.id) || []);
339
+ const componentsWithConfigConflicts = configMergeResults.filter(c => c.hasConflicts()).map(c => c.compIdStr);
340
+ const conflicts = [];
341
+ const mergedSuccessfullyIds = [];
342
+ mergeResults.components?.forEach(c => {
343
+ const files = Object.keys(c.filesStatus).filter(f => c.filesStatus[f] === _componentModules().FileStatus.manual || c.filesStatus[f] === _componentModules().FileStatus.binaryConflict);
344
+ const config = componentsWithConfigConflicts.includes(c.id.toStringWithoutVersion());
345
+ if (files.length || config) {
346
+ conflicts.push({
347
+ id: c.id,
348
+ files,
349
+ config
350
+ });
351
+ } else {
352
+ const snappedId = mergedSnapIds.searchWithoutVersion(c.id);
353
+ mergedSuccessfullyIds.push(snappedId || c.id);
354
+ }
355
+ });
356
+ await this.workspace?.consumer.onDestroy(`lane-merge (${otherLaneId.name})`);
357
+ return {
358
+ mergeResults,
359
+ deleteResults,
360
+ configMergeResults,
361
+ mergedSuccessfullyIds,
362
+ conflicts
363
+ };
364
+ }
365
+ validateMergeFlags(otherLaneId, currentLaneId, options) {
366
+ const {
367
+ tag,
368
+ resolveUnrelated,
369
+ detachHead
370
+ } = options;
371
+ if (tag && !currentLaneId.isDefault()) {
372
+ throw new (_bitError().BitError)(`--tag only possible when on main. currently checked out to ${currentLaneId.toString()}`);
373
+ }
374
+ if (otherLaneId.isEqual(currentLaneId)) {
375
+ throw new (_bitError().BitError)(`unable to merge lane "${otherLaneId.toString()}", you're already at this lane. to get updates, simply run "bit checkout head"`);
376
+ }
377
+ if (resolveUnrelated && currentLaneId.isDefault()) {
378
+ throw new (_bitError().BitError)(`unable to resolve unrelated when on main. switch to ${otherLaneId.toString()} and run "bit lane merge main --resolve-unrelated"`);
379
+ }
380
+ if (detachHead && !currentLaneId.isDefault()) {
381
+ throw new (_bitError().BitError)(`unable to detach head. the current lane is not main`);
382
+ }
383
+ }
384
+ async resolveMergeContext(otherLaneId, currentLaneId, options) {
385
+ const {
386
+ skipFetch,
387
+ excludeNonLaneComps,
388
+ shouldIncludeUpdateDependents,
389
+ fetchCurrent
390
+ } = options;
391
+ const legacyScope = this.scope.legacyScope;
392
+ if (fetchCurrent && !currentLaneId.isDefault()) {
393
+ // if current is default, it'll be fetch later on
394
+ await this.lanes.fetchLaneWithItsComponents(currentLaneId);
395
+ }
396
+ const currentLane = currentLaneId.isDefault() ? undefined : await legacyScope.loadLane(currentLaneId);
397
+ const isDefaultLane = otherLaneId.isDefault();
398
+ if (isDefaultLane) {
399
+ if (!skipFetch) {
400
+ const ids = await this.getMainIdsToMerge(currentLane, !excludeNonLaneComps);
401
+ const compIdList = _componentId().ComponentIdList.fromArray(ids).toVersionLatest();
402
+ await this.importer.importObjectsFromMainIfExist(compIdList);
403
+ }
404
+ }
405
+ let laneToFetchArtifactsFrom;
406
+ const getOtherLane = async () => {
407
+ let lane = await legacyScope.loadLane(otherLaneId);
408
+ const shouldFetch = !lane || !skipFetch && !lane.isNew;
409
+ if (shouldFetch) {
410
+ // don't assign `lane` to the result of this command. otherwise, if you have local snaps, it'll ignore them and use the remote-lane.
411
+ const otherLane = await this.lanes.fetchLaneWithItsComponents(otherLaneId, shouldIncludeUpdateDependents);
412
+ laneToFetchArtifactsFrom = otherLane;
413
+ lane = await legacyScope.loadLane(otherLaneId);
414
+ }
415
+ return lane;
416
+ };
417
+ const otherLane = isDefaultLane ? undefined : await getOtherLane();
418
+ if (fetchCurrent && otherLane && currentLaneId.isDefault()) {
419
+ const ids = await this.getMainIdsToMerge(otherLane, false, shouldIncludeUpdateDependents);
420
+ const compIdList = _componentId().ComponentIdList.fromArray(ids).toVersionLatest();
421
+ await this.importer.importObjectsFromMainIfExist(compIdList);
422
+ }
423
+ const getBitIds = async () => {
424
+ if (isDefaultLane) {
425
+ const ids = await this.getMainIdsToMerge(currentLane, !excludeNonLaneComps, shouldIncludeUpdateDependents);
426
+ const modelComponents = await Promise.all(ids.map(id => this.scope.legacyScope.getModelComponent(id)));
427
+ return (0, _lodash().compact)(modelComponents.map(c => {
428
+ if (!c.head) return null; // probably the component was never merged to main
429
+ return c.toComponentId().changeVersion(c.head.toString());
430
+ }));
431
+ }
432
+ if (!otherLane) throw new Error(`lane must be defined for non-default`);
433
+ return shouldIncludeUpdateDependents ? otherLane.toComponentIdsIncludeUpdateDependents() : otherLane.toComponentIds();
434
+ };
435
+ const idsToMerge = await getBitIds();
436
+ return {
437
+ currentLane,
438
+ otherLane,
439
+ laneToFetchArtifactsFrom,
440
+ idsToMerge
441
+ };
442
+ }
443
+
444
+ /**
445
+ * check conflicts in case of merging sourceLaneId into targetLaneId
446
+ */
447
+ async checkLaneForConflicts(sourceLaneIdStr, targetLaneIdStr, options) {
448
+ const legacyScope = this.scope.legacyScope;
449
+ const otherLaneId = await legacyScope.lanes.parseLaneIdFromString(sourceLaneIdStr);
450
+ const currentLaneId = await legacyScope.lanes.parseLaneIdFromString(targetLaneIdStr);
451
+ options.excludeNonLaneComps = true;
452
+ const {
453
+ currentLane,
454
+ otherLane,
455
+ idsToMerge
456
+ } = await this.resolveMergeContext(otherLaneId, currentLaneId, options);
457
+ const allComponentsStatus = await this.merging.getMergeStatus(idsToMerge, {
458
+ mergeStrategy: 'manual'
459
+ }, currentLane, otherLane);
460
+ (0, _checkout().throwForFailures)(allComponentsStatus);
461
+ const configMergeResults = (0, _lodash().compact)(allComponentsStatus.map(c => c.configMergeResult));
462
+ const componentsWithConfigConflicts = configMergeResults.filter(c => c.hasConflicts()).map(c => c.compIdStr);
463
+ const conflicts = [];
464
+ allComponentsStatus.forEach(c => {
465
+ const files = c.mergeResults?.modifiedFiles.filter(f => f.conflict || f.isBinaryConflict) || [];
466
+ const config = componentsWithConfigConflicts.includes(c.id.toStringWithoutVersion());
467
+ if (files.length || config) {
468
+ const configData = configMergeResults.find(co => co.compIdStr === c.id.toStringWithoutVersion());
469
+ const configConflict = configData?.generateMergeConflictFile() || undefined;
470
+ conflicts.push({
471
+ id: c.id,
472
+ files: files.map(f => f.filePath),
473
+ config,
474
+ configConflict
475
+ });
476
+ }
477
+ });
478
+ return {
479
+ conflicts
480
+ };
481
+ }
482
+ async mergeMove(newLaneName, options) {
483
+ if (!this.workspace) throw new (_workspace().OutsideWorkspaceError)();
484
+ const lastMerge = new (_lastMerged().LastMerged)(this.scope, this.workspace.consumer, this.logger);
485
+ await lastMerge.restoreLaneObjectFromLastMerged();
486
+ const newLaneResult = await this.lanes.createLane(newLaneName, {
487
+ scope: options.scope
488
+ });
489
+ return newLaneResult;
490
+ }
491
+ async abortLaneMerge(checkoutProps, mergeAbortOpts) {
492
+ if (!this.workspace) throw new (_workspace().OutsideWorkspaceError)();
493
+ const lastMerge = new (_lastMerged().LastMerged)(this.scope, this.workspace.consumer, this.logger);
494
+ const currentLane = await this.lanes.getCurrentLane();
495
+ const {
496
+ compDirsToRemove
497
+ } = await lastMerge.restoreFromLastMerged(mergeAbortOpts, currentLane);
498
+ await this.workspace._reloadConsumer();
499
+ this.workspace.consumer.scope.objects.unmergedComponents.removeAllComponents();
500
+ await this.workspace.consumer.scope.objects.unmergedComponents.write();
501
+ const configMergeFile = this.workspace.getConflictMergeFile();
502
+ await configMergeFile.delete();
503
+ let checkoutResults;
504
+ let checkoutError;
505
+ checkoutProps.ids = this.workspace.listIds();
506
+ checkoutProps.restoreMissingComponents = true;
507
+ try {
508
+ checkoutResults = await this.checkout.checkout(checkoutProps);
509
+ } catch (err) {
510
+ this.logger.error(`merge-abort got an error during the checkout stage`, err);
511
+ checkoutError = err;
512
+ }
513
+ const restoredItems = [`${_path().default.basename(this.workspace.consumer.bitMap.mapPath)} file`, `${_path().default.basename(this.workspace.consumer.config.path)} file`];
514
+ if (compDirsToRemove.length) {
515
+ restoredItems.push(`deleted components directories: ${compDirsToRemove.join(', ')}`);
516
+ }
517
+ if (currentLane) {
518
+ restoredItems.push(`${currentLane.id()} lane object`);
519
+ }
520
+ return {
521
+ checkoutResults,
522
+ restoredItems,
523
+ checkoutError
524
+ };
525
+ }
526
+ async getMainIdsToMerge(lane, includeNonLaneComps = true, includeUpdateDependents = false) {
527
+ if (!lane) throw new Error(`unable to merge ${_laneId().DEFAULT_LANE}, the current lane was not found`);
528
+ const laneIds = includeUpdateDependents ? lane.toComponentIdsIncludeUpdateDependents() : lane.toComponentIds();
529
+ const ids = laneIds.filter(id => this.scope.isExported(id));
530
+ if (includeNonLaneComps) {
531
+ if (!this.workspace) {
532
+ throw new (_bitError().BitError)(`getMainIdsToMerge needs workspace`);
533
+ }
534
+ const workspaceIds = this.workspace.listIds();
535
+ const mainNotOnLane = workspaceIds.filter(id => !laneIds.find(laneId => laneId.isEqualWithoutVersion(id)) && this.scope.isExported(id));
536
+ ids.push(...mainNotOnLane);
537
+ }
538
+ return ids;
539
+ }
540
+ async throwIfNotUpToDate(fromLaneId, toLaneId) {
541
+ const status = await this.lanes.diffStatus(fromLaneId, toLaneId, {
542
+ skipChanges: true
543
+ });
544
+ const compsNotUpToDate = status.componentsStatus.filter(s => !s.upToDate);
545
+ if (compsNotUpToDate.length) {
546
+ throw new Error(`unable to merge, the following components are not up-to-date:
547
+ ${compsNotUpToDate.map(s => s.componentId.toString()).join('\n')}`);
548
+ }
549
+ }
550
+ static async provider([lanes, cli, workspace, merging, loggerMain, remove, scope, exporter, importer, checkout, configStore, express]) {
551
+ const logger = loggerMain.createLogger(_mergeLanes().MergeLanesAspect.id);
552
+ const lanesCommand = cli.getCommand('lane');
553
+ const mergeLanesMain = new MergeLanesMain(workspace, merging, lanes, logger, remove, scope, exporter, importer, checkout);
554
+ lanesCommand?.commands?.push(new (_mergeLane().MergeLaneCmd)(mergeLanesMain, configStore));
555
+ lanesCommand?.commands?.push(new (_mergeAbort().MergeAbortLaneCmd)(mergeLanesMain));
556
+ lanesCommand?.commands?.push(new (_mergeMove().MergeMoveLaneCmd)(mergeLanesMain));
557
+ express.register([new (_lanesCheckConflicts().LanesCheckConflictsRoute)(mergeLanesMain, logger)]);
558
+ return mergeLanesMain;
559
+ }
560
+ }
561
+ exports.MergeLanesMain = MergeLanesMain;
562
+ _defineProperty(MergeLanesMain, "slots", []);
563
+ _defineProperty(MergeLanesMain, "dependencies", [_lanes().LanesAspect, _cli().CLIAspect, _workspace().WorkspaceAspect, _merging().MergingAspect, _logger().LoggerAspect, _remove().RemoveAspect, _scope().ScopeAspect, _export().ExportAspect, _importer().ImporterAspect, _checkout().CheckoutAspect, _configStore().ConfigStoreAspect, _express().ExpressAspect]);
564
+ _defineProperty(MergeLanesMain, "runtime", _cli().MainRuntime);
565
+ async function filterComponentsStatus(allComponentsStatus, compIdsToKeep, allBitIds, legacyScope, includeDeps = false, otherLane,
566
+ // lane that gets merged into the current lane. if not provided, it's main that gets merged into the current lane
567
+ shouldSquash) {
568
+ const bitIdsFromPattern = _componentId().ComponentIdList.fromArray(compIdsToKeep);
569
+ const bitIdsNotFromPattern = allBitIds.filter(bitId => !bitIdsFromPattern.hasWithoutVersion(bitId));
570
+ const filteredComponentStatus = [];
571
+ const depsToAdd = [];
572
+ const missingDepsFromHead = {};
573
+ const missingDepsFromHistory = [];
574
+ const versionsToCheckPerId = await (0, _pMapSeries().default)(compIdsToKeep, async compId => {
575
+ const fromStatus = allComponentsStatus.find(c => c.id.isEqualWithoutVersion(compId));
576
+ if (!fromStatus) {
577
+ throw new Error(`filterComponentsStatus: unable to find ${compId.toString()} in component-status`);
578
+ }
579
+ filteredComponentStatus.push(fromStatus);
580
+ if (fromStatus.unchangedMessage) {
581
+ return undefined;
582
+ }
583
+ if (!otherLane) {
584
+ // if merging main, no need to check whether the deps are included in the pattern.
585
+ return undefined;
586
+ }
587
+ const {
588
+ divergeData
589
+ } = fromStatus;
590
+ if (!divergeData) {
591
+ throw new Error(`filterComponentsStatus: unable to find divergeData for ${compId.toString()}`);
592
+ }
593
+ let targetVersions = divergeData.snapsOnTargetOnly;
594
+ if (!targetVersions.length) {
595
+ return undefined;
596
+ }
597
+ const modelComponent = await legacyScope.getModelComponent(compId);
598
+ if (shouldSquash) {
599
+ // no need to check all versions, we merge only the head
600
+ const headOnTarget = otherLane ? otherLane.getCompHeadIncludeUpdateDependents(compId) : modelComponent.head;
601
+ if (!headOnTarget) {
602
+ throw new Error(`filterComponentsStatus: unable to find head for ${compId.toString()}`);
603
+ }
604
+ targetVersions = [headOnTarget];
605
+ }
606
+ return {
607
+ compId,
608
+ targetVersions,
609
+ modelComponent
610
+ };
611
+ });
612
+
613
+ // all these versions needs to be imported to load them later and check whether they have dependencies on the target lane
614
+ const toImport = (0, _lodash().compact)(versionsToCheckPerId).map(c => c.targetVersions.map(v => c.compId.changeVersion(v.toString()))).flat();
615
+ await legacyScope.scopeImporter.importWithoutDeps(_componentId().ComponentIdList.fromArray(toImport), {
616
+ lane: otherLane,
617
+ cache: true,
618
+ includeVersionHistory: false,
619
+ reason: 'import all history of given patterns components to check whether they have dependencies on the lane'
620
+ });
621
+ await (0, _pMapSeries().default)((0, _lodash().compact)(versionsToCheckPerId), async ({
622
+ compId,
623
+ targetVersions,
624
+ modelComponent
625
+ }) => {
626
+ await (0, _pMapSeries().default)(targetVersions, async remoteVersion => {
627
+ const versionObj = await modelComponent.loadVersion(remoteVersion.toString(), legacyScope.objects);
628
+ const flattenedDeps = versionObj.getAllFlattenedDependencies();
629
+ const depsNotIncludeInPattern = flattenedDeps.filter(id => bitIdsNotFromPattern.find(bitId => bitId.isEqualWithoutVersion(id)));
630
+ if (!depsNotIncludeInPattern.length) {
631
+ return;
632
+ }
633
+ const depsOnLane = [];
634
+ await Promise.all(depsNotIncludeInPattern.map(async dep => {
635
+ const isOnLane = await legacyScope.isIdOnLane(dep, otherLane);
636
+ if (isOnLane) {
637
+ depsOnLane.push(dep);
638
+ }
639
+ }));
640
+ if (!depsOnLane.length) {
641
+ return;
642
+ }
643
+ if (includeDeps) {
644
+ depsToAdd.push(...depsOnLane);
645
+ } else {
646
+ const headOnTarget = otherLane ? otherLane.getCompHeadIncludeUpdateDependents(compId) : modelComponent.head;
647
+ const depsOnLaneStr = depsOnLane.map(dep => dep.toStringWithoutVersion());
648
+ if (headOnTarget?.isEqual(remoteVersion)) {
649
+ depsOnLaneStr.forEach(dep => {
650
+ (missingDepsFromHead[dep] ||= []).push(compId.toStringWithoutVersion());
651
+ });
652
+ } else {
653
+ missingDepsFromHistory.push(...depsOnLaneStr);
654
+ }
655
+ }
656
+ });
657
+ });
658
+ if (Object.keys(missingDepsFromHead).length || missingDepsFromHistory.length) {
659
+ throw new (_missingCompsToMerge().MissingCompsToMerge)(missingDepsFromHead, (0, _lodash().uniq)(missingDepsFromHistory));
660
+ }
661
+ if (depsToAdd.length) {
662
+ // remove the version, otherwise, the uniq gives duplicate components with different versions.
663
+ const depsWithoutVersion = depsToAdd.map(d => d.changeVersion(undefined));
664
+ const depsUniqWithoutVersion = _componentId().ComponentIdList.uniqFromArray(depsWithoutVersion);
665
+ depsUniqWithoutVersion.forEach(id => {
666
+ const fromStatus = allComponentsStatus.find(c => c.id.isEqualWithoutVersion(id));
667
+ if (!fromStatus) {
668
+ throw new Error(`filterComponentsStatus: unable to find ${id.toString()} in component-status`);
669
+ }
670
+ filteredComponentStatus.push(fromStatus);
671
+ });
672
+ }
673
+ return filteredComponentStatus;
674
+ }
675
+ async function getLogForSquash(otherLaneId) {
676
+ const basicLog = await (0, _harmonyModules().getBasicLog)();
677
+ const log = _objectSpread(_objectSpread({}, basicLog), {}, {
678
+ message: `squashed during merge from ${otherLaneId.toString()}`
679
+ });
680
+ return log;
681
+ }
682
+ async function squashSnaps(succeededComponents, currentLaneId, otherLaneId, scope, opts = {}) {
683
+ const currentLaneName = currentLaneId.name;
684
+ const log = await getLogForSquash(otherLaneId);
685
+ await Promise.all(succeededComponents.map(async ({
686
+ id,
687
+ divergeData,
688
+ componentFromModel
689
+ }) => {
690
+ if (!divergeData) {
691
+ throw new Error(`unable to squash. divergeData is missing from ${id.toString()}`);
692
+ }
693
+ const modifiedComp = await squashOneComp(currentLaneName, otherLaneId, id, divergeData, log, scope, componentFromModel, opts);
694
+ if (modifiedComp) {
695
+ scope.objects.add(modifiedComp);
696
+ const modelComponent = await scope.getModelComponent(id);
697
+ const versionHistory = await modelComponent.updateRebasedVersionHistory(scope.objects, [modifiedComp]);
698
+ if (versionHistory) scope.objects.add(versionHistory);
699
+ }
700
+ }));
701
+ }
702
+
703
+ /**
704
+ * returns Version object if it was modified. otherwise, returns undefined
705
+ */
706
+ async function squashOneComp(currentLaneName, otherLaneId, id, divergeData, log, scope, componentFromModel, opts = {}) {
707
+ const {
708
+ messageTitle,
709
+ detachHead
710
+ } = opts;
711
+ const shouldSquash = () => {
712
+ if (divergeData.isDiverged()) {
713
+ if (detachHead) {
714
+ // for detach head, it's ok to have it as diverged. as long as the target is ahead, we want to squash.
715
+ return true;
716
+ }
717
+ throw new (_bitError().BitError)(`unable to squash because ${id.toString()} is diverged in history.
718
+ consider switching to "${otherLaneId.name}" first, merging "${currentLaneName}", then switching back to "${currentLaneName}" and merging "${otherLaneId.name}"
719
+ alternatively, use "--no-squash" flag to keep the entire history of "${otherLaneId.name}"`);
720
+ }
721
+ if (divergeData.isSourceAhead()) {
722
+ // nothing to do. current is ahead, nothing to merge. (it was probably filtered out already as a "failedComponent")
723
+ return false;
724
+ }
725
+ if (!divergeData.isTargetAhead()) {
726
+ // nothing to do. current and remote are the same, nothing to merge. (it was probably filtered out already as a "failedComponent")
727
+ return false;
728
+ }
729
+ return true;
730
+ };
731
+ if (!shouldSquash()) {
732
+ return undefined;
733
+ }
734
+
735
+ // remote is ahead and was not diverge.
736
+ const remoteSnaps = divergeData.snapsOnTargetOnly;
737
+ if (remoteSnaps.length === 0) {
738
+ throw new Error(`remote is ahead but it has no snaps. it's impossible`);
739
+ }
740
+ const getAllMessages = async () => {
741
+ if (!messageTitle) return [];
742
+ await scope.scopeImporter.importManyObjects({
743
+ [otherLaneId.scope]: remoteSnaps.map(s => s.toString())
744
+ });
745
+ const versionObjects = await Promise.all(remoteSnaps.map(s => scope.objects.load(s)));
746
+ return (0, _lodash().compact)(versionObjects).map(v => v.log.message);
747
+ };
748
+ const getFinalMessage = async () => {
749
+ if (!messageTitle) return undefined;
750
+ const allMessage = await getAllMessages();
751
+ const allMessageStr = (0, _lodash().compact)(allMessage).map(m => `[*] ${m}`).join('\n');
752
+ return `${messageTitle}\n${allMessageStr}`;
753
+ };
754
+ if (!componentFromModel) {
755
+ throw new Error('unable to squash, the componentFromModel is missing');
756
+ }
757
+ const currentParents = componentFromModel.parents;
758
+
759
+ // if the remote has only one snap, there is nothing to squash.
760
+ // other checks here is to make sure `componentFromModel.addAsOnlyParent` call is not needed.
761
+ if (remoteSnaps.length === 1 && divergeData.commonSnapBeforeDiverge && currentParents.length === 1) {
762
+ return undefined;
763
+ }
764
+ const doSquash = async () => {
765
+ if (divergeData.commonSnapBeforeDiverge) {
766
+ componentFromModel.addAsOnlyParent(divergeData.commonSnapBeforeDiverge);
767
+ return;
768
+ }
769
+ if (currentLaneName !== _laneId().DEFAULT_LANE) {
770
+ // when squashing into lane, we have to take main into account
771
+ const modelComponent = await scope.getModelComponentIfExist(id);
772
+ if (!modelComponent) throw new Error(`missing ModelComponent for ${id.toString()}`);
773
+ if (modelComponent.head) {
774
+ componentFromModel.addAsOnlyParent(modelComponent.head);
775
+ return;
776
+ }
777
+ }
778
+ // there is no commonSnapBeforeDiverge. the local has no snaps, all are remote, no need for parents. keep only head.
779
+ componentFromModel.parents.forEach(ref => componentFromModel.removeParent(ref));
780
+ };
781
+ await doSquash();
782
+ const finalMessage = await getFinalMessage();
783
+ componentFromModel.setSquashed({
784
+ previousParents: currentParents,
785
+ laneId: otherLaneId
786
+ }, log, finalMessage);
787
+ return componentFromModel;
788
+ }
789
+ _mergeLanes().MergeLanesAspect.addRuntime(MergeLanesMain);
790
+ var _default = exports.default = MergeLanesMain;
791
+
792
+ //# sourceMappingURL=merge-lanes.main.runtime.js.map