@teambit/merging 1.0.107 → 1.0.108

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,826 @@
1
+ import { CLIAspect, CLIMain, MainRuntime } from '@teambit/cli';
2
+ import semver from 'semver';
3
+ import WorkspaceAspect, { OutsideWorkspaceError, Workspace } from '@teambit/workspace';
4
+ import { Consumer } from '@teambit/legacy/dist/consumer';
5
+ import ComponentsList from '@teambit/legacy/dist/consumer/component/components-list';
6
+ import {
7
+ MergeStrategy,
8
+ FileStatus,
9
+ ApplyVersionResult,
10
+ getMergeStrategyInteractive,
11
+ MergeOptions,
12
+ } from '@teambit/legacy/dist/consumer/versions-ops/merge-version';
13
+ import SnappingAspect, { SnapResults, SnappingMain, TagResults } from '@teambit/snapping';
14
+ import hasWildcard from '@teambit/legacy/dist/utils/string/has-wildcard';
15
+ import mapSeries from 'p-map-series';
16
+ import { ComponentID, ComponentIdList } from '@teambit/component-id';
17
+ import { BitError } from '@teambit/bit-error';
18
+ import GeneralError from '@teambit/legacy/dist/error/general-error';
19
+ import { LaneId } from '@teambit/lane-id';
20
+ import { AutoTagResult } from '@teambit/legacy/dist/scope/component-ops/auto-tag';
21
+ import { UnmergedComponent } from '@teambit/legacy/dist/scope/lanes/unmerged-components';
22
+ import { Lane, ModelComponent } from '@teambit/legacy/dist/scope/models';
23
+ import { Ref } from '@teambit/legacy/dist/scope/objects';
24
+ import chalk from 'chalk';
25
+ import { ConfigAspect, ConfigMain } from '@teambit/config';
26
+ import RemoveAspect, { RemoveMain } from '@teambit/remove';
27
+ import { pathNormalizeToLinux } from '@teambit/legacy/dist/utils';
28
+ import { ComponentWriterAspect, ComponentWriterMain } from '@teambit/component-writer';
29
+ import ConsumerComponent from '@teambit/legacy/dist/consumer/component/consumer-component';
30
+ import ImporterAspect, { ImporterMain } from '@teambit/importer';
31
+ import { Logger, LoggerAspect, LoggerMain } from '@teambit/logger';
32
+ import GlobalConfigAspect, { GlobalConfigMain } from '@teambit/global-config';
33
+ import { compact, isEmpty } from 'lodash';
34
+ import { MergeResultsThreeWay } from '@teambit/legacy/dist/consumer/versions-ops/merge-version/three-way-merge';
35
+ import { DependencyResolverAspect, WorkspacePolicyConfigKeysNames } from '@teambit/dependency-resolver';
36
+ import {
37
+ ApplyVersionWithComps,
38
+ CheckoutAspect,
39
+ CheckoutMain,
40
+ ComponentStatusBase,
41
+ applyModifiedVersion,
42
+ removeFilesIfNeeded,
43
+ updateFileStatus,
44
+ } from '@teambit/checkout';
45
+ import { DEPENDENCIES_FIELDS } from '@teambit/legacy/dist/constants';
46
+ import deleteComponentsFiles from '@teambit/legacy/dist/consumer/component-ops/delete-component-files';
47
+ import { SnapsDistance } from '@teambit/legacy/dist/scope/component-ops/snaps-distance';
48
+ import { InstallMain, InstallAspect } from '@teambit/install';
49
+ import { MergeCmd } from './merge-cmd';
50
+ import { MergingAspect } from './merging.aspect';
51
+ import { ConfigMergeResult } from './config-merge-result';
52
+ import { MergeStatusProvider, MergeStatusProviderOptions } from './merge-status-provider';
53
+
54
+ type ResolveUnrelatedData = {
55
+ strategy: MergeStrategy;
56
+ headOnCurrentLane: Ref;
57
+ unrelatedHead: Ref;
58
+ unrelatedLaneId: LaneId;
59
+ };
60
+ type PkgEntry = { name: string; version: string; force: boolean };
61
+
62
+ export type WorkspaceDepsUpdates = { [pkgName: string]: [string, string] }; // from => to
63
+ export type WorkspaceDepsConflicts = Record<WorkspacePolicyConfigKeysNames, Array<{ name: string; version: string }>>; // the pkg value is in a format of CONFLICT::OURS::THEIRS
64
+
65
+ export type ComponentMergeStatus = ComponentStatusBase & {
66
+ mergeResults?: MergeResultsThreeWay | null;
67
+ divergeData?: SnapsDistance;
68
+ resolvedUnrelated?: ResolveUnrelatedData;
69
+ configMergeResult?: ConfigMergeResult;
70
+ };
71
+
72
+ export type ComponentMergeStatusBeforeMergeAttempt = ComponentStatusBase & {
73
+ divergeData?: SnapsDistance;
74
+ resolvedUnrelated?: ResolveUnrelatedData;
75
+ mergeProps?: {
76
+ otherLaneHead: Ref;
77
+ currentId: ComponentID;
78
+ modelComponent: ModelComponent;
79
+ };
80
+ };
81
+
82
+ export type FailedComponents = { id: ComponentID; unchangedMessage: string; unchangedLegitimately?: boolean };
83
+
84
+ export type ApplyVersionResults = {
85
+ components?: ApplyVersionResult[];
86
+ version?: string;
87
+ failedComponents?: FailedComponents[];
88
+ removedComponents?: ComponentID[];
89
+ addedComponents?: ComponentID[]; // relevant when restoreMissingComponents is true (e.g. bit lane merge-abort)
90
+ resolvedComponents?: ConsumerComponent[]; // relevant for bit merge --resolve
91
+ abortedComponents?: ApplyVersionResult[]; // relevant for bit merge --abort
92
+ mergeSnapResults?: {
93
+ snappedComponents: ConsumerComponent[];
94
+ autoSnappedResults: AutoTagResult[];
95
+ removedComponents?: ComponentIdList;
96
+ } | null;
97
+ mergeSnapError?: Error;
98
+ leftUnresolvedConflicts?: boolean;
99
+ verbose?: boolean;
100
+ newFromLane?: string[];
101
+ newFromLaneAdded?: boolean;
102
+ installationError?: Error; // in case the package manager failed, it won't throw, instead, it'll return error here
103
+ compilationError?: Error; // in case the compiler failed, it won't throw, instead, it'll return error here
104
+ workspaceDepsUpdates?: WorkspaceDepsUpdates; // in case workspace.jsonc has been updated with dependencies versions
105
+ };
106
+
107
+ export class MergingMain {
108
+ constructor(
109
+ private workspace: Workspace,
110
+ private install: InstallMain,
111
+ private snapping: SnappingMain,
112
+ private checkout: CheckoutMain,
113
+ private logger: Logger,
114
+ private componentWriter: ComponentWriterMain,
115
+ private importer: ImporterMain,
116
+ private config: ConfigMain,
117
+ private remove: RemoveMain
118
+ ) {}
119
+
120
+ async merge(
121
+ ids: string[],
122
+ mergeStrategy: MergeStrategy,
123
+ abort: boolean,
124
+ resolve: boolean,
125
+ noSnap: boolean,
126
+ message: string,
127
+ build: boolean,
128
+ skipDependencyInstallation: boolean
129
+ ): Promise<ApplyVersionResults> {
130
+ if (!this.workspace) throw new OutsideWorkspaceError();
131
+ const consumer: Consumer = this.workspace.consumer;
132
+ let mergeResults;
133
+ if (resolve) {
134
+ mergeResults = await this.resolveMerge(ids, message, build);
135
+ } else if (abort) {
136
+ mergeResults = await this.abortMerge(ids);
137
+ } else {
138
+ const bitIds = await this.getComponentsToMerge(consumer, ids);
139
+ mergeResults = await this.mergeComponentsFromRemote(
140
+ consumer,
141
+ bitIds,
142
+ mergeStrategy,
143
+ noSnap,
144
+ message,
145
+ build,
146
+ skipDependencyInstallation
147
+ );
148
+ }
149
+ await consumer.onDestroy('merge');
150
+ return mergeResults;
151
+ }
152
+
153
+ /**
154
+ * when user is on main, it merges the remote main components into local.
155
+ * when user is on a lane, it merges the remote lane components into the local lane.
156
+ */
157
+ async mergeComponentsFromRemote(
158
+ consumer: Consumer,
159
+ bitIds: ComponentID[],
160
+ mergeStrategy: MergeStrategy,
161
+ noSnap: boolean,
162
+ snapMessage: string,
163
+ build: boolean,
164
+ skipDependencyInstallation: boolean
165
+ ): Promise<ApplyVersionResults> {
166
+ const currentLaneId = consumer.getCurrentLaneId();
167
+ const currentLaneObject = await consumer.getCurrentLaneObject();
168
+ const allComponentsStatus = await this.getAllComponentsStatus(bitIds, currentLaneId, currentLaneObject);
169
+ const failedComponents = allComponentsStatus.filter((c) => c.unchangedMessage && !c.unchangedLegitimately);
170
+ if (failedComponents.length) {
171
+ const failureMsgs = failedComponents
172
+ .map(
173
+ (failedComponent) =>
174
+ `${chalk.bold(failedComponent.id.toString())} - ${chalk.red(failedComponent.unchangedMessage as string)}`
175
+ )
176
+ .join('\n');
177
+ throw new BitError(`unable to merge due to the following failures:\n${failureMsgs}`);
178
+ }
179
+
180
+ return this.mergeSnaps({
181
+ mergeStrategy,
182
+ allComponentsStatus,
183
+ laneId: currentLaneId,
184
+ localLane: currentLaneObject,
185
+ noSnap,
186
+ snapMessage,
187
+ build,
188
+ skipDependencyInstallation,
189
+ });
190
+ }
191
+
192
+ /**
193
+ * merge multiple components according to the "allComponentsStatus".
194
+ */
195
+ async mergeSnaps({
196
+ mergeStrategy,
197
+ allComponentsStatus,
198
+ laneId,
199
+ localLane,
200
+ noSnap,
201
+ tag,
202
+ snapMessage,
203
+ build,
204
+ skipDependencyInstallation,
205
+ }: {
206
+ mergeStrategy: MergeStrategy;
207
+ allComponentsStatus: ComponentMergeStatus[];
208
+ laneId: LaneId;
209
+ localLane: Lane | null;
210
+ noSnap: boolean;
211
+ tag?: boolean;
212
+ snapMessage: string;
213
+ build: boolean;
214
+ skipDependencyInstallation?: boolean;
215
+ }): Promise<ApplyVersionResults> {
216
+ const consumer = this.workspace.consumer;
217
+ const componentWithConflict = allComponentsStatus.find(
218
+ (component) => component.mergeResults && component.mergeResults.hasConflicts
219
+ );
220
+ if (componentWithConflict && !mergeStrategy) {
221
+ mergeStrategy = await getMergeStrategyInteractive();
222
+ }
223
+ const failedComponents: FailedComponents[] = allComponentsStatus
224
+ .filter((componentStatus) => componentStatus.unchangedMessage)
225
+ .filter((componentStatus) => !componentStatus.shouldBeRemoved)
226
+ .map((componentStatus) => ({
227
+ id: componentStatus.id,
228
+ unchangedMessage: componentStatus.unchangedMessage as string,
229
+ unchangedLegitimately: componentStatus.unchangedLegitimately,
230
+ }));
231
+
232
+ const componentIdsToRemove = allComponentsStatus
233
+ .filter((componentStatus) => componentStatus.shouldBeRemoved)
234
+ .map((c) => c.id.changeVersion(undefined));
235
+
236
+ const succeededComponents = allComponentsStatus.filter((componentStatus) => !componentStatus.unchangedMessage);
237
+
238
+ const componentsResults = await this.applyVersionMultiple(succeededComponents, laneId, mergeStrategy, localLane);
239
+
240
+ const allConfigMerge = compact(succeededComponents.map((c) => c.configMergeResult));
241
+
242
+ const { workspaceDepsUpdates, workspaceDepsConflicts } = await this.updateWorkspaceJsoncWithDepsIfNeeded(
243
+ allConfigMerge
244
+ );
245
+
246
+ await this.generateConfigMergeConflictFileForAll(allConfigMerge, workspaceDepsConflicts);
247
+
248
+ if (localLane) consumer.scope.objects.add(localLane);
249
+
250
+ await consumer.scope.objects.persist(); // persist anyway, if localLane is null it should save all main heads
251
+
252
+ await consumer.scope.objects.unmergedComponents.write();
253
+
254
+ await consumer.writeBitMap(`merge ${laneId.toString()}`);
255
+
256
+ if (componentIdsToRemove.length) {
257
+ const compBitIdsToRemove = ComponentIdList.fromArray(componentIdsToRemove);
258
+ await deleteComponentsFiles(consumer, compBitIdsToRemove);
259
+ await consumer.cleanFromBitMap(compBitIdsToRemove);
260
+ }
261
+
262
+ const componentsHasConfigMergeConflicts = allComponentsStatus.some((c) => c.configMergeResult?.hasConflicts());
263
+ const leftUnresolvedConflicts = componentWithConflict && mergeStrategy === 'manual';
264
+ if (!skipDependencyInstallation && !leftUnresolvedConflicts && !componentsHasConfigMergeConflicts) {
265
+ try {
266
+ await this.install.install(undefined, {
267
+ dedupe: true,
268
+ updateExisting: false,
269
+ import: false,
270
+ });
271
+ } catch (err: any) {
272
+ this.logger.error(`failed installing packages`, err);
273
+ this.logger.consoleError(`failed installing packages, see the log for full stacktrace. error: ${err.message}`);
274
+ }
275
+ }
276
+
277
+ const getSnapOrTagResults = async () => {
278
+ // if one of the component has conflict, don't snap-merge. otherwise, some of the components would be snap-merged
279
+ // and some not. besides the fact that it could by mistake tag dependent, it's a confusing state. better not snap.
280
+ if (noSnap || leftUnresolvedConflicts || componentsHasConfigMergeConflicts) {
281
+ return null;
282
+ }
283
+ if (tag) {
284
+ const idsToTag = allComponentsStatus.map((c) => c.id);
285
+ const results = await this.tagAllLaneComponent(idsToTag, snapMessage, build);
286
+ if (!results) return null;
287
+ const { taggedComponents, autoTaggedResults, removedComponents } = results;
288
+ return { snappedComponents: taggedComponents, autoSnappedResults: autoTaggedResults, removedComponents };
289
+ }
290
+ return this.snapResolvedComponents(consumer, snapMessage, build);
291
+ };
292
+ let mergeSnapResults: ApplyVersionResults['mergeSnapResults'] = null;
293
+ let mergeSnapError: Error | undefined;
294
+ const bitMapSnapshot = this.workspace.bitMap.takeSnapshot();
295
+ try {
296
+ mergeSnapResults = await getSnapOrTagResults();
297
+ } catch (err: any) {
298
+ mergeSnapError = err;
299
+ this.workspace.bitMap.restoreFromSnapshot(bitMapSnapshot);
300
+ }
301
+
302
+ return {
303
+ components: componentsResults.map((c) => c.applyVersionResult),
304
+ failedComponents,
305
+ removedComponents: [...componentIdsToRemove, ...(mergeSnapResults?.removedComponents || [])],
306
+ mergeSnapResults,
307
+ mergeSnapError,
308
+ leftUnresolvedConflicts,
309
+ workspaceDepsUpdates,
310
+ };
311
+ }
312
+
313
+ private async generateConfigMergeConflictFileForAll(
314
+ allConfigMerge: ConfigMergeResult[],
315
+ workspaceDepsConflicts?: WorkspaceDepsConflicts
316
+ ) {
317
+ const configMergeFile = this.workspace.getConflictMergeFile();
318
+ if (workspaceDepsConflicts) {
319
+ const workspaceConflict = new ConfigMergeResult('WORKSPACE', 'ours', 'theirs', [
320
+ {
321
+ id: DependencyResolverAspect.id,
322
+ conflict: workspaceDepsConflicts,
323
+ },
324
+ ]);
325
+ allConfigMerge.unshift(workspaceConflict);
326
+ }
327
+ allConfigMerge.forEach((configMerge) => {
328
+ const conflict = configMerge.generateMergeConflictFile();
329
+ if (!conflict) return;
330
+ configMergeFile.addConflict(configMerge.compIdStr, conflict);
331
+ });
332
+ if (configMergeFile.hasConflict()) {
333
+ await configMergeFile.write();
334
+ }
335
+ }
336
+
337
+ private async updateWorkspaceJsoncWithDepsIfNeeded(
338
+ allConfigMerge: ConfigMergeResult[]
339
+ ): Promise<{ workspaceDepsUpdates?: WorkspaceDepsUpdates; workspaceDepsConflicts?: WorkspaceDepsConflicts }> {
340
+ const allResults = allConfigMerge.map((c) => c.getDepsResolverResult());
341
+
342
+ // aggregate all dependencies that can be updated (not conflicting)
343
+ const nonConflictDeps: { [pkgName: string]: string[] } = {};
344
+ const nonConflictSources: { [pkgName: string]: string[] } = {}; // for logging/debugging purposes
345
+ allConfigMerge.forEach((configMerge) => {
346
+ const mergedConfig = configMerge.getDepsResolverResult()?.mergedConfig;
347
+ if (!mergedConfig || mergedConfig === '-') return;
348
+ const mergedConfigPolicy = mergedConfig.policy || {};
349
+ DEPENDENCIES_FIELDS.forEach((depField) => {
350
+ if (!mergedConfigPolicy[depField]) return;
351
+ mergedConfigPolicy[depField].forEach((pkg: PkgEntry) => {
352
+ if (pkg.force) return; // we only care about auto-detected dependencies
353
+ if (nonConflictDeps[pkg.name]) {
354
+ if (!nonConflictDeps[pkg.name].includes(pkg.version)) nonConflictDeps[pkg.name].push(pkg.version);
355
+ nonConflictSources[pkg.name].push(configMerge.compIdStr);
356
+ return;
357
+ }
358
+ nonConflictDeps[pkg.name] = [pkg.version];
359
+ nonConflictSources[pkg.name] = [configMerge.compIdStr];
360
+ });
361
+ });
362
+ });
363
+
364
+ // aggregate all dependencies that have conflicts
365
+ const conflictDeps: { [pkgName: string]: string[] } = {};
366
+ const conflictDepsSources: { [pkgName: string]: string[] } = {}; // for logging/debugging purposes
367
+ allConfigMerge.forEach((configMerge) => {
368
+ const mergedConfigConflict = configMerge.getDepsResolverResult()?.conflict;
369
+ if (!mergedConfigConflict) return;
370
+ DEPENDENCIES_FIELDS.forEach((depField) => {
371
+ if (!mergedConfigConflict[depField]) return;
372
+ mergedConfigConflict[depField].forEach((pkg: PkgEntry) => {
373
+ if (pkg.force) return; // we only care about auto-detected dependencies
374
+ if (conflictDeps[pkg.name]) {
375
+ if (!conflictDeps[pkg.name].includes(pkg.version)) conflictDeps[pkg.name].push(pkg.version);
376
+ conflictDepsSources[pkg.name].push(configMerge.compIdStr);
377
+ return;
378
+ }
379
+ conflictDeps[pkg.name] = [pkg.version];
380
+ conflictDepsSources[pkg.name] = [configMerge.compIdStr];
381
+ });
382
+ });
383
+ });
384
+
385
+ const notConflictedPackages = Object.keys(nonConflictDeps);
386
+ const conflictedPackages = Object.keys(conflictDeps);
387
+ if (!notConflictedPackages.length && !conflictedPackages.length) return {};
388
+
389
+ const workspaceConfig = this.config.workspaceConfig;
390
+ if (!workspaceConfig) throw new Error(`updateWorkspaceJsoncWithDepsIfNeeded unable to get workspace config`);
391
+ const depResolver = workspaceConfig.extensions.findCoreExtension(DependencyResolverAspect.id);
392
+ const policy = depResolver?.config.policy;
393
+ if (!policy) {
394
+ return {};
395
+ }
396
+
397
+ // calculate the workspace.json updates
398
+ const workspaceJsonUpdates = {};
399
+ notConflictedPackages.forEach((pkgName) => {
400
+ if (nonConflictDeps[pkgName].length > 1) {
401
+ // we only want the deps that the other lane has them in the workspace.json and that all comps use the same dep.
402
+ return;
403
+ }
404
+ DEPENDENCIES_FIELDS.forEach((depField) => {
405
+ if (!policy[depField]?.[pkgName]) return; // doesn't exists in the workspace.json
406
+ const currentVer = policy[depField][pkgName];
407
+ const newVer = nonConflictDeps[pkgName][0];
408
+ if (currentVer === newVer) return;
409
+ workspaceJsonUpdates[pkgName] = [currentVer, newVer];
410
+ policy[depField][pkgName] = newVer;
411
+ this.logger.debug(
412
+ `update workspace.jsonc: ${pkgName} from ${currentVer} to ${newVer}. Triggered by: ${nonConflictSources[
413
+ pkgName
414
+ ].join(', ')}`
415
+ );
416
+ });
417
+ });
418
+
419
+ // calculate the workspace.json conflicts
420
+ const WS_DEPS_FIELDS = ['dependencies', 'peerDependencies'];
421
+ const workspaceJsonConflicts = { dependencies: [], peerDependencies: [] };
422
+ const conflictPackagesToRemoveFromConfigMerge: string[] = [];
423
+ conflictedPackages.forEach((pkgName) => {
424
+ if (conflictDeps[pkgName].length > 1) {
425
+ // we only want the deps that the other lane has them in the workspace.json and that all comps use the same dep.
426
+ return;
427
+ }
428
+ const conflictRaw = conflictDeps[pkgName][0];
429
+ const [, currentVal, otherVal] = conflictRaw.split('::');
430
+
431
+ WS_DEPS_FIELDS.forEach((depField) => {
432
+ if (!policy[depField]?.[pkgName]) return;
433
+ const currentVerInWsJson = policy[depField][pkgName];
434
+ if (!currentVerInWsJson) return;
435
+ // the version is coming from the workspace.jsonc
436
+ conflictPackagesToRemoveFromConfigMerge.push(pkgName);
437
+ if (semver.satisfies(otherVal, currentVerInWsJson)) {
438
+ // the other version is compatible with the current version in the workspace.json
439
+ return;
440
+ }
441
+ workspaceJsonConflicts[depField].push({
442
+ name: pkgName,
443
+ version: conflictRaw.replace(currentVal, currentVerInWsJson),
444
+ force: false,
445
+ });
446
+ conflictPackagesToRemoveFromConfigMerge.push(pkgName);
447
+ this.logger.debug(
448
+ `conflict workspace.jsonc: ${pkgName} current: ${currentVerInWsJson}, other: ${otherVal}. Triggered by: ${conflictDepsSources[
449
+ pkgName
450
+ ].join(', ')}`
451
+ );
452
+ });
453
+ });
454
+ WS_DEPS_FIELDS.forEach((depField) => {
455
+ if (isEmpty(workspaceJsonConflicts[depField])) delete workspaceJsonConflicts[depField];
456
+ });
457
+
458
+ if (conflictPackagesToRemoveFromConfigMerge.length) {
459
+ allResults.forEach((result) => {
460
+ if (result?.conflict) {
461
+ DEPENDENCIES_FIELDS.forEach((depField) => {
462
+ if (!result.conflict?.[depField]) return;
463
+ result.conflict[depField] = result.conflict?.[depField].filter(
464
+ (dep) => !conflictPackagesToRemoveFromConfigMerge.includes(dep.name)
465
+ );
466
+ if (!result.conflict[depField].length) delete result.conflict[depField];
467
+ });
468
+ if (isEmpty(result.conflict)) result.conflict = undefined;
469
+ }
470
+ });
471
+ }
472
+
473
+ if (Object.keys(workspaceJsonUpdates).length) {
474
+ await workspaceConfig.write();
475
+ }
476
+
477
+ this.logger.debug('final workspace.jsonc updates', workspaceJsonUpdates);
478
+ this.logger.debug('final workspace.jsonc conflicts', workspaceJsonConflicts);
479
+
480
+ return {
481
+ workspaceDepsUpdates: Object.keys(workspaceJsonUpdates).length ? workspaceJsonUpdates : undefined,
482
+ workspaceDepsConflicts: Object.keys(workspaceJsonConflicts).length ? workspaceJsonConflicts : undefined,
483
+ };
484
+ }
485
+
486
+ /**
487
+ * this function gets called from two different commands:
488
+ * 1. "bit merge <ids...>", when merging a component from a remote to the local.
489
+ * in this case, the remote and local are on the same lane or both on main.
490
+ * 2. "bit lane merge", when merging from one lane to another.
491
+ */
492
+ async getMergeStatus(
493
+ bitIds: ComponentID[], // the id.version is the version we want to merge to the current component
494
+ currentLane: Lane | null, // currently checked out lane. if on main, then it's null.
495
+ otherLane?: Lane | null, // the lane we want to merged to our lane. (null if it's "main").
496
+ options?: MergeStatusProviderOptions
497
+ ): Promise<ComponentMergeStatus[]> {
498
+ const mergeStatusProvider = new MergeStatusProvider(
499
+ this.workspace,
500
+ this.logger,
501
+ this.importer,
502
+ currentLane || undefined,
503
+ otherLane || undefined,
504
+ options
505
+ );
506
+ return mergeStatusProvider.getStatus(bitIds);
507
+ }
508
+
509
+ private async applyVersionMultiple(
510
+ succeededComponents: ComponentMergeStatus[],
511
+ laneId: LaneId,
512
+ mergeStrategy: MergeStrategy,
513
+ localLane: Lane | null
514
+ ): Promise<ApplyVersionWithComps[]> {
515
+ const componentsResults = await mapSeries(
516
+ succeededComponents,
517
+ async ({ currentComponent, id, mergeResults, resolvedUnrelated, configMergeResult }) => {
518
+ const modelComponent = await this.workspace.consumer.scope.getModelComponent(id);
519
+ const updatedLaneId = laneId.isDefault() ? LaneId.from(laneId.name, id.scope as string) : laneId;
520
+ return this.applyVersion({
521
+ currentComponent,
522
+ id,
523
+ mergeResults,
524
+ mergeStrategy,
525
+ remoteHead: modelComponent.getRef(id.version as string) as Ref,
526
+ laneId: updatedLaneId,
527
+ localLane,
528
+ resolvedUnrelated,
529
+ configMergeResult,
530
+ });
531
+ }
532
+ );
533
+
534
+ const compsToWrite = compact(componentsResults.map((c) => c.legacyCompToWrite));
535
+
536
+ const manyComponentsWriterOpts = {
537
+ consumer: this.workspace.consumer,
538
+ components: compsToWrite,
539
+ skipDependencyInstallation: true,
540
+ writeConfig: false, // @todo: should write if config exists before, needs to figure out how to do it.
541
+ reasonForBitmapChange: 'merge',
542
+ };
543
+ await this.componentWriter.writeMany(manyComponentsWriterOpts);
544
+
545
+ return componentsResults;
546
+ }
547
+
548
+ private async applyVersion({
549
+ currentComponent,
550
+ id,
551
+ mergeResults,
552
+ mergeStrategy,
553
+ remoteHead,
554
+ laneId,
555
+ localLane,
556
+ resolvedUnrelated,
557
+ configMergeResult,
558
+ }: {
559
+ currentComponent: ConsumerComponent | null | undefined;
560
+ id: ComponentID;
561
+ mergeResults: MergeResultsThreeWay | null | undefined;
562
+ mergeStrategy: MergeStrategy;
563
+ remoteHead: Ref;
564
+ laneId: LaneId;
565
+ localLane: Lane | null;
566
+ resolvedUnrelated?: ResolveUnrelatedData;
567
+ configMergeResult?: ConfigMergeResult;
568
+ }): Promise<ApplyVersionWithComps> {
569
+ const consumer = this.workspace.consumer;
570
+ let filesStatus = {};
571
+ const unmergedComponent: UnmergedComponent = {
572
+ // @ts-ignore
573
+ id: { name: id.fullName, scope: id.scope },
574
+ head: remoteHead,
575
+ laneId,
576
+ };
577
+ id = currentComponent ? currentComponent.id : id;
578
+
579
+ const modelComponent = await consumer.scope.getModelComponent(id);
580
+ const handleResolveUnrelated = (legacyCompToWrite?: ConsumerComponent) => {
581
+ if (!currentComponent) throw new Error('currentComponent must be defined when resolvedUnrelated');
582
+ // because when on a main, we don't allow merging lanes with unrelated. we asks users to switch to the lane
583
+ // first and then merge with --resolve-unrelated
584
+ if (!localLane) throw new Error('localLane must be defined when resolvedUnrelated');
585
+ if (!resolvedUnrelated) throw new Error('resolvedUnrelated must be populated');
586
+ localLane.addComponent({ id, head: resolvedUnrelated.headOnCurrentLane });
587
+ unmergedComponent.unrelated = {
588
+ unrelatedHead: resolvedUnrelated.unrelatedHead,
589
+ headOnCurrentLane: resolvedUnrelated.headOnCurrentLane,
590
+ unrelatedLaneId: resolvedUnrelated.unrelatedLaneId,
591
+ };
592
+ consumer.scope.objects.unmergedComponents.addEntry(unmergedComponent);
593
+ return { applyVersionResult: { id, filesStatus }, component: currentComponent, legacyCompToWrite };
594
+ };
595
+
596
+ const markAllFilesAsUnchanged = () => {
597
+ if (!currentComponent) throw new Error(`applyVersion expect to get currentComponent for ${id.toString()}`);
598
+ currentComponent.files.forEach((file) => {
599
+ filesStatus[pathNormalizeToLinux(file.relative)] = FileStatus.unchanged;
600
+ });
601
+ };
602
+ if (mergeResults && mergeResults.hasConflicts && mergeStrategy === MergeOptions.ours) {
603
+ markAllFilesAsUnchanged();
604
+ consumer.scope.objects.unmergedComponents.addEntry(unmergedComponent);
605
+ return { applyVersionResult: { id, filesStatus }, component: currentComponent || undefined };
606
+ }
607
+ if (resolvedUnrelated?.strategy === 'ours') {
608
+ markAllFilesAsUnchanged();
609
+ return handleResolveUnrelated();
610
+ }
611
+ const remoteId = id.changeVersion(remoteHead.toString());
612
+ const idToLoad = !mergeResults || mergeStrategy === MergeOptions.theirs ? remoteId : id;
613
+ const legacyComponent = await consumer.loadComponentFromModelImportIfNeeded(idToLoad);
614
+ if (mergeResults && mergeStrategy === MergeOptions.theirs) {
615
+ // in this case, we don't want to update .bitmap with the version of the remote. we want to keep the same version
616
+ legacyComponent.version = id.version;
617
+ }
618
+ const files = legacyComponent.files;
619
+ updateFileStatus(files, filesStatus, currentComponent || undefined);
620
+
621
+ if (mergeResults) {
622
+ // update files according to the merge results
623
+ const { filesStatus: modifiedStatus, modifiedFiles } = applyModifiedVersion(files, mergeResults, mergeStrategy);
624
+ legacyComponent.files = modifiedFiles;
625
+ filesStatus = { ...filesStatus, ...modifiedStatus };
626
+ }
627
+
628
+ await removeFilesIfNeeded(filesStatus, currentComponent || undefined);
629
+
630
+ if (configMergeResult) {
631
+ const successfullyMergedConfig = configMergeResult.getSuccessfullyMergedConfig();
632
+ if (successfullyMergedConfig) {
633
+ unmergedComponent.mergedConfig = successfullyMergedConfig;
634
+ // no need to `unmergedComponents.addEntry` here. it'll be added in the next lines inside `if (mergeResults)`.
635
+ // because if `configMergeResult` is set, `mergeResults` must be set as well. both happen on diverge.
636
+ }
637
+ }
638
+
639
+ // if mergeResults, the head snap is going to be updated on a later phase when snapping with two parents
640
+ // otherwise, update the head of the current lane or main
641
+ if (mergeResults) {
642
+ if (mergeResults.hasConflicts && mergeStrategy === MergeOptions.manual) {
643
+ unmergedComponent.unmergedPaths = mergeResults.modifiedFiles.filter((f) => f.conflict).map((f) => f.filePath);
644
+ }
645
+ consumer.scope.objects.unmergedComponents.addEntry(unmergedComponent);
646
+ } else if (localLane) {
647
+ if (resolvedUnrelated) {
648
+ // must be "theirs"
649
+ return handleResolveUnrelated(legacyComponent);
650
+ }
651
+ localLane.addComponent({ id, head: remoteHead });
652
+ } else {
653
+ // this is main
654
+ modelComponent.setHead(remoteHead);
655
+ // mark it as local, otherwise, when importing this component from a remote, it'll override it.
656
+ modelComponent.markVersionAsLocal(remoteHead.toString());
657
+ consumer.scope.objects.add(modelComponent);
658
+ }
659
+
660
+ return {
661
+ applyVersionResult: { id, filesStatus },
662
+ component: currentComponent || undefined,
663
+ legacyCompToWrite: legacyComponent,
664
+ };
665
+ }
666
+
667
+ private async abortMerge(values: string[]): Promise<ApplyVersionResults> {
668
+ const consumer = this.workspace.consumer;
669
+ const ids = await this.getIdsForUnmerged(values);
670
+ const results = await this.checkout.checkout({ ids, reset: true });
671
+ ids.forEach((id) => consumer.scope.objects.unmergedComponents.removeComponent(id.fullName));
672
+ await consumer.scope.objects.unmergedComponents.write();
673
+ return { abortedComponents: results.components };
674
+ }
675
+
676
+ private async resolveMerge(values: string[], snapMessage: string, build: boolean): Promise<ApplyVersionResults> {
677
+ const ids = await this.getIdsForUnmerged(values);
678
+ // @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX!
679
+ const { snappedComponents } = await this.snapping.snap({
680
+ legacyBitIds: ComponentIdList.fromArray(ids.map((id) => id)),
681
+ build,
682
+ message: snapMessage,
683
+ });
684
+ return { resolvedComponents: snappedComponents };
685
+ }
686
+
687
+ private async getAllComponentsStatus(
688
+ bitIds: ComponentID[],
689
+ laneId: LaneId,
690
+ localLaneObject: Lane | null
691
+ ): Promise<ComponentMergeStatus[]> {
692
+ const ids = await Promise.all(
693
+ bitIds.map(async (bitId) => {
694
+ const remoteScopeName = laneId.isDefault() ? bitId.scope : laneId.scope;
695
+ const remoteLaneId = LaneId.from(laneId.name, remoteScopeName as string);
696
+ const remoteHead = await this.workspace.consumer.scope.objects.remoteLanes.getRef(remoteLaneId, bitId);
697
+ const laneIdStr = remoteLaneId.toString();
698
+ if (!remoteHead) {
699
+ throw new BitError(`unable to find a remote head of "${bitId.toStringWithoutVersion()}" in "${laneIdStr}"`);
700
+ }
701
+ return bitId.changeVersion(remoteHead.toString());
702
+ })
703
+ );
704
+
705
+ return this.getMergeStatus(ids, localLaneObject, localLaneObject, { shouldSquash: false });
706
+ }
707
+
708
+ private async snapResolvedComponents(
709
+ consumer: Consumer,
710
+ snapMessage: string,
711
+ build: boolean
712
+ ): Promise<SnapResults | null> {
713
+ const unmergedComponents = consumer.scope.objects.unmergedComponents.getComponents();
714
+ this.logger.debug(`merge-snaps, snapResolvedComponents, total ${unmergedComponents.length.toString()} components`);
715
+ if (!unmergedComponents.length) return null;
716
+ const ids = ComponentIdList.fromArray(unmergedComponents.map((r) => ComponentID.fromObject(r.id)));
717
+ return this.snapping.snap({
718
+ legacyBitIds: ids,
719
+ build,
720
+ message: snapMessage,
721
+ });
722
+ }
723
+
724
+ private async tagAllLaneComponent(
725
+ idsToTag: ComponentID[],
726
+ tagMessage: string,
727
+ build: boolean
728
+ ): Promise<TagResults | null> {
729
+ const ids = idsToTag.map((id) => {
730
+ return id.toStringWithoutVersion();
731
+ });
732
+ this.logger.debug(`merge-snaps, tagResolvedComponents, total ${idsToTag.length.toString()} components`);
733
+ return this.snapping.tag({
734
+ ids,
735
+ build,
736
+ message: tagMessage,
737
+ unmodified: true,
738
+ });
739
+ }
740
+
741
+ private async getIdsForUnmerged(idsStr?: string[]): Promise<ComponentID[]> {
742
+ if (idsStr && idsStr.length) {
743
+ const componentIds = await this.workspace.resolveMultipleComponentIds(idsStr);
744
+ componentIds.forEach((id) => {
745
+ const entry = this.workspace.consumer.scope.objects.unmergedComponents.getEntry(id.fullName);
746
+ if (!entry) {
747
+ throw new GeneralError(`unable to merge-resolve ${id.toString()}, it is not marked as unresolved`);
748
+ }
749
+ });
750
+ return componentIds;
751
+ }
752
+ const unresolvedComponents = this.workspace.consumer.scope.objects.unmergedComponents.getComponents();
753
+ if (!unresolvedComponents.length) throw new GeneralError(`all components are resolved already, nothing to do`);
754
+ return unresolvedComponents.map((u) => ComponentID.fromObject(u.id));
755
+ }
756
+
757
+ private async getComponentsToMerge(consumer: Consumer, ids: string[]): Promise<ComponentID[]> {
758
+ const componentsList = new ComponentsList(consumer);
759
+ if (!ids.length) {
760
+ const mergePending = await componentsList.listMergePendingComponents();
761
+ return mergePending.map((c) => c.id);
762
+ }
763
+ if (hasWildcard(ids)) {
764
+ return componentsList.listComponentsByIdsWithWildcard(ids);
765
+ }
766
+ return ids.map((id) => consumer.getParsedId(id));
767
+ }
768
+
769
+ static slots = [];
770
+ static dependencies = [
771
+ CLIAspect,
772
+ WorkspaceAspect,
773
+ SnappingAspect,
774
+ CheckoutAspect,
775
+ InstallAspect,
776
+ LoggerAspect,
777
+ ComponentWriterAspect,
778
+ ImporterAspect,
779
+ ConfigAspect,
780
+ RemoveAspect,
781
+ GlobalConfigAspect,
782
+ ];
783
+ static runtime = MainRuntime;
784
+ static async provider([
785
+ cli,
786
+ workspace,
787
+ snapping,
788
+ checkout,
789
+ install,
790
+ loggerMain,
791
+ compWriter,
792
+ importer,
793
+ config,
794
+ remove,
795
+ globalConfig,
796
+ ]: [
797
+ CLIMain,
798
+ Workspace,
799
+ SnappingMain,
800
+ CheckoutMain,
801
+ InstallMain,
802
+ LoggerMain,
803
+ ComponentWriterMain,
804
+ ImporterMain,
805
+ ConfigMain,
806
+ RemoveMain,
807
+ GlobalConfigMain
808
+ ]) {
809
+ const logger = loggerMain.createLogger(MergingAspect.id);
810
+ const merging = new MergingMain(
811
+ workspace,
812
+ install,
813
+ snapping,
814
+ checkout,
815
+ logger,
816
+ compWriter,
817
+ importer,
818
+ config,
819
+ remove
820
+ );
821
+ cli.register(new MergeCmd(merging, globalConfig));
822
+ return merging;
823
+ }
824
+ }
825
+
826
+ MergingAspect.addRuntime(MergingMain);