@teambit/merging 0.0.455 → 0.0.456

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.
@@ -1,9 +1,7 @@
1
1
  "use strict";
2
2
 
3
3
  var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
- require("core-js/modules/es.array.flat.js");
5
4
  require("core-js/modules/es.array.iterator.js");
6
- require("core-js/modules/es.array.unscopables.flat.js");
7
5
  require("core-js/modules/es.promise.js");
8
6
  require("core-js/modules/es.regexp.exec.js");
9
7
  require("core-js/modules/es.string.replace.js");
@@ -102,13 +100,6 @@ function _laneId() {
102
100
  };
103
101
  return data;
104
102
  }
105
- function _getDivergeData() {
106
- const data = require("@teambit/legacy/dist/scope/component-ops/get-diverge-data");
107
- _getDivergeData = function () {
108
- return data;
109
- };
110
- return data;
111
- }
112
103
  function _chalk() {
113
104
  const data = _interopRequireDefault(require("chalk"));
114
105
  _chalk = function () {
@@ -130,13 +121,6 @@ function _remove() {
130
121
  };
131
122
  return data;
132
123
  }
133
- function _repositories() {
134
- const data = require("@teambit/legacy/dist/scope/repositories");
135
- _repositories = function () {
136
- return data;
137
- };
138
- return data;
139
- }
140
124
  function _utils() {
141
125
  const data = require("@teambit/legacy/dist/utils");
142
126
  _utils = function () {
@@ -172,20 +156,6 @@ function _lodash() {
172
156
  };
173
157
  return data;
174
158
  }
175
- function _threeWayMerge() {
176
- const data = _interopRequireDefault(require("@teambit/legacy/dist/consumer/versions-ops/merge-version/three-way-merge"));
177
- _threeWayMerge = function () {
178
- return data;
179
- };
180
- return data;
181
- }
182
- function _noCommonSnap() {
183
- const data = require("@teambit/legacy/dist/scope/exceptions/no-common-snap");
184
- _noCommonSnap = function () {
185
- return data;
186
- };
187
- return data;
188
- }
189
159
  function _dependencyResolver() {
190
160
  const data = require("@teambit/dependency-resolver");
191
161
  _dependencyResolver = function () {
@@ -242,16 +212,16 @@ function _merging() {
242
212
  };
243
213
  return data;
244
214
  }
245
- function _configMerger() {
246
- const data = require("./config-merger");
247
- _configMerger = function () {
215
+ function _configMergeResult() {
216
+ const data = require("./config-merge-result");
217
+ _configMergeResult = function () {
248
218
  return data;
249
219
  };
250
220
  return data;
251
221
  }
252
- function _configMergeResult() {
253
- const data = require("./config-merge-result");
254
- _configMergeResult = function () {
222
+ function _mergeStatusProvider() {
223
+ const data = require("./merge-status-provider");
224
+ _mergeStatusProvider = function () {
255
225
  return data;
256
226
  };
257
227
  return data;
@@ -610,285 +580,8 @@ class MergingMain {
610
580
  otherLane,
611
581
  // the lane we want to merged to our lane. (null if it's "main").
612
582
  options) {
613
- if (!currentLane && otherLane) {
614
- await this.importer.importObjectsFromMainIfExist(otherLane.toBitIds().toVersionLatest());
615
- }
616
- const componentStatusBeforeMergeAttempt = await (0, _pMapSeries().default)(bitIds, id => this.getComponentStatusBeforeMergeAttempt(id, currentLane, options));
617
- const toImport = componentStatusBeforeMergeAttempt.map(compStatus => {
618
- if (!compStatus.divergeData) return [];
619
- const versionsToImport = (0, _lodash().compact)([...compStatus.divergeData.snapsOnTargetOnly, compStatus.divergeData.commonSnapBeforeDiverge]);
620
- return versionsToImport.map(v => compStatus.id.changeVersion(v.toString()));
621
- }).flat();
622
- await this.workspace.consumer.scope.scopeImporter.importWithoutDeps(_bitId().BitIds.fromArray(toImport), {
623
- lane: otherLane || undefined,
624
- cache: true,
625
- includeVersionHistory: false
626
- });
627
- const compStatusNotNeedMerge = componentStatusBeforeMergeAttempt.filter(c => !c.mergeProps);
628
- const compStatusNeedMerge = componentStatusBeforeMergeAttempt.filter(c => c.mergeProps);
629
- const getComponentsStatusNeedMerge = async () => {
630
- const tmp = new (_repositories().Tmp)(this.workspace.consumer.scope);
631
- try {
632
- const componentsStatus = await Promise.all(compStatusNeedMerge.map(compStatus => this.getComponentMergeStatus(currentLane, otherLane || undefined, compStatus)));
633
- await tmp.clear();
634
- return componentsStatus;
635
- } catch (err) {
636
- await tmp.clear();
637
- throw err;
638
- }
639
- };
640
- const results = await getComponentsStatusNeedMerge();
641
- results.push(...compStatusNotNeedMerge);
642
- return results;
643
- }
644
-
645
- // eslint-disable-next-line complexity
646
- async getComponentStatusBeforeMergeAttempt(id,
647
- // the id.version is the version we want to merge to the current component
648
- localLane,
649
- // currently checked out lane. if on main, then it's null.
650
- options) {
651
- const consumer = this.workspace.consumer;
652
- const componentStatus = {
653
- id
654
- };
655
- const returnUnmerged = (msg, unmergedLegitimately = false) => {
656
- componentStatus.unmergedMessage = msg;
657
- componentStatus.unmergedLegitimately = unmergedLegitimately;
658
- return componentStatus;
659
- };
660
- const modelComponent = await consumer.scope.getModelComponentIfExist(id);
661
- if (!modelComponent) {
662
- return returnUnmerged(`component ${id.toString()} is on the lane/main but its objects were not found, please re-import the lane`);
663
- }
664
- const unmerged = consumer.scope.objects.unmergedComponents.getEntry(id.name);
665
- if (unmerged) {
666
- return returnUnmerged(`component ${id.toStringWithoutVersion()} is in during-merge state a previous merge, please snap/tag it first (or use bit merge --resolve/--abort/ bit lane merge-abort)`);
667
- }
668
- const repo = consumer.scope.objects;
669
- const version = id.version;
670
- const otherLaneHead = modelComponent.getRef(version);
671
- const existingBitMapId = consumer.bitMap.getBitIdIfExist(id, {
672
- ignoreVersion: true
673
- });
674
- const componentOnOther = await modelComponent.loadVersion(version, consumer.scope.objects);
675
- const idOnCurrentLane = localLane === null || localLane === void 0 ? void 0 : localLane.getComponent(id);
676
- if (componentOnOther.isRemoved()) {
677
- // if exist in current lane, we want the current lane to get the soft-remove update.
678
- // or if it was removed with --update-main, we want to merge it so then main will get the update.
679
- const shouldMerge = idOnCurrentLane || componentOnOther.shouldRemoveFromMain();
680
- if (shouldMerge) {
681
- // remove the component from the workspace if exist.
682
- componentStatus.shouldBeRemoved = true;
683
- } else {
684
- // on main, don't merge soft-removed components unless it's marked with removeOnMain.
685
- // on lane, if it's not part of the current lane, don't merge it.
686
- return returnUnmerged(`component has been removed`, true);
687
- }
688
- }
689
- const getCurrentId = () => {
690
- if (existingBitMapId) return existingBitMapId;
691
- if (localLane) {
692
- if (!idOnCurrentLane) return null;
693
- return idOnCurrentLane.id.changeVersion(idOnCurrentLane.head.toString());
694
- }
695
- // it's on main
696
- const head = modelComponent.getHeadAsTagIfExist();
697
- if (head) {
698
- return id.changeVersion(head);
699
- }
700
- return null;
701
- };
702
- const currentId = getCurrentId();
703
- if (!currentId) {
704
- const divergeData = await (0, _getDivergeData().getDivergeData)({
705
- repo,
706
- modelComponent,
707
- targetHead: otherLaneHead,
708
- throws: false
709
- });
710
- return _objectSpread(_objectSpread({}, componentStatus), {}, {
711
- componentFromModel: componentOnOther,
712
- divergeData
713
- });
714
- }
715
- const getCurrentComponent = () => {
716
- if (existingBitMapId) return consumer.loadComponent(existingBitMapId);
717
- return consumer.scope.getConsumerComponent(currentId);
718
- };
719
- const currentComponent = await getCurrentComponent();
720
- if (currentComponent.isRemoved()) {
721
- // we have a few options:
722
- // 1. other is ahead. in this case, other recovered the component. so we can continue with the merge.
723
- // it is possible that it is diverged, in which case, still continue with the merge, and later on, the
724
- // merge-config will show a config conflict of the remove aspect.
725
- // 2. other is not ahead. in this case, just ignore this component, no point to merge it, we want it removed.
726
- // 3. there are errors when calculating the divergeData, e.g. no snap in common. in such cases, we assume
727
- // there are issues with this component, and is better not to merge it.
728
- const divergeData = await (0, _getDivergeData().getDivergeData)({
729
- repo,
730
- modelComponent,
731
- targetHead: otherLaneHead,
732
- throws: false
733
- });
734
- if (divergeData.err || !divergeData.isTargetAhead()) {
735
- return returnUnmerged(`component has been removed`, true);
736
- }
737
- }
738
- const isModified = async () => {
739
- const componentModificationStatus = await consumer.getComponentStatusById(currentComponent.id);
740
- if (!componentModificationStatus.modified) return undefined;
741
- if (!existingBitMapId) return undefined;
742
- const baseComponent = await modelComponent.loadVersion(existingBitMapId.version, consumer.scope.objects);
743
- const isSourceCodeModified = await consumer.isComponentSourceCodeModified(baseComponent, currentComponent);
744
- if (isSourceCodeModified) return 'code';
745
- return 'config';
746
- };
747
- const modifiedType = await isModified();
748
- if (modifiedType === 'config' && !(options !== null && options !== void 0 && options.ignoreConfigChanges)) {
749
- return returnUnmerged(`component has config changes, please snap/tag it first. alternatively, use --ignore-config-changes flag to bypass`);
750
- }
751
- if (modifiedType === 'code') {
752
- return returnUnmerged(`component is modified, please snap/tag it first`);
753
- }
754
- if (!otherLaneHead) {
755
- throw new Error(`merging: unable finding a hash for the version ${version} of ${id.toString()}`);
756
- }
757
- const divergeData = await (0, _getDivergeData().getDivergeData)({
758
- repo,
759
- modelComponent,
760
- targetHead: otherLaneHead,
761
- throws: false
762
- });
763
- if (divergeData.err) {
764
- const mainHead = modelComponent.head;
765
- if (divergeData.err instanceof _noCommonSnap().NoCommonSnap && options !== null && options !== void 0 && options.resolveUnrelated && mainHead) {
766
- const hasResolvedFromMain = async hashToCompare => {
767
- const divergeDataFromMain = await (0, _getDivergeData().getDivergeData)({
768
- repo,
769
- modelComponent,
770
- sourceHead: hashToCompare,
771
- targetHead: mainHead,
772
- throws: false
773
- });
774
- if (!divergeDataFromMain.err) return true;
775
- return !(divergeDataFromMain.err instanceof _noCommonSnap().NoCommonSnap);
776
- };
777
- const hasResolvedLocally = await hasResolvedFromMain(modelComponent.getHeadRegardlessOfLane());
778
- const hasResolvedRemotely = await hasResolvedFromMain(otherLaneHead);
779
- if (!hasResolvedLocally && !hasResolvedRemotely) {
780
- return returnUnmerged(`unable to traverse ${currentComponent.id.toString()} history. the main-head ${mainHead.toString()} doesn't appear in both lanes, it was probably created in each lane separately`);
781
- }
782
- const versionToSaveInLane = hasResolvedLocally ? currentComponent.id.version : id.version;
783
- const resolvedRef = modelComponent.getRef(versionToSaveInLane);
784
- if (!resolvedRef) throw new Error(`unable to get ref of "${versionToSaveInLane}" for "${id.toString()}"`);
785
- if ((options === null || options === void 0 ? void 0 : options.resolveUnrelated) === 'theirs') {
786
- // just override with the model data
787
- return {
788
- currentComponent,
789
- componentFromModel: componentOnOther,
790
- id,
791
- divergeData,
792
- resolvedUnrelated: {
793
- strategy: 'theirs',
794
- head: resolvedRef
795
- }
796
- };
797
- }
798
- if ((options === null || options === void 0 ? void 0 : options.resolveUnrelated) === 'ours') {
799
- return {
800
- currentComponent,
801
- id,
802
- divergeData,
803
- resolvedUnrelated: {
804
- strategy: 'ours',
805
- head: resolvedRef
806
- }
807
- };
808
- }
809
- throw new Error(`unsupported strategy "${options === null || options === void 0 ? void 0 : options.resolveUnrelated}" of resolve-unrelated. supported strategies are: [ours, theirs]`);
810
- }
811
- return returnUnmerged(`unable to traverse ${currentComponent.id.toString()} history. error: ${divergeData.err.message}`);
812
- }
813
- if (!divergeData.isDiverged()) {
814
- if (divergeData.isSourceAhead()) {
815
- // do nothing!
816
- return returnUnmerged(`component ${currentComponent.id.toString()} is ahead, nothing to merge`, true);
817
- }
818
- if (divergeData.isTargetAhead()) {
819
- // just override with the model data
820
- return _objectSpread(_objectSpread({}, componentStatus), {}, {
821
- currentComponent,
822
- componentFromModel: componentOnOther,
823
- divergeData
824
- });
825
- }
826
- // we know that localHead and remoteHead are set, so if none of them is ahead they must be equal
827
- return returnUnmerged(`component ${currentComponent.id.toString()} is already merged`, true);
828
- }
829
-
830
- // it's diverged and needs merge operation
831
- const mergeProps = {
832
- otherLaneHead,
833
- currentId,
834
- modelComponent
835
- };
836
- return _objectSpread(_objectSpread({}, componentStatus), {}, {
837
- currentComponent,
838
- mergeProps,
839
- divergeData
840
- });
841
- }
842
- async getComponentMergeStatus(localLane,
843
- // currently checked out lane. if on main, then it's null.
844
- otherLane,
845
- // the lane name we want to merged to our lane. (can be also "main").
846
- componentMergeStatusBeforeMergeAttempt) {
847
- const {
848
- id,
849
- divergeData,
850
- currentComponent,
851
- mergeProps
852
- } = componentMergeStatusBeforeMergeAttempt;
853
- if (!mergeProps) throw new Error(`getDivergedMergeStatus, mergeProps is missing for ${id.toString()}`);
854
- const {
855
- otherLaneHead,
856
- currentId,
857
- modelComponent
858
- } = mergeProps;
859
- const repo = this.workspace.consumer.scope.objects;
860
- if (!divergeData) throw new Error(`getDivergedMergeStatus, divergeData is missing for ${id.toString()}`);
861
- if (!currentComponent) throw new Error(`getDivergedMergeStatus, currentComponent is missing for ${id.toString()}`);
862
- const baseSnap = divergeData.commonSnapBeforeDiverge; // must be set when isTrueMerge
863
- this.logger.debug(`merging snaps details:
864
- id: ${id.toStringWithoutVersion()}
865
- base: ${baseSnap.toString()}
866
- current: ${currentId.version}
867
- other: ${otherLaneHead.toString()}`);
868
- const baseComponent = await modelComponent.loadVersion(baseSnap.toString(), repo);
869
- const otherComponent = await modelComponent.loadVersion(otherLaneHead.toString(), repo);
870
- const currentLaneName = (localLane === null || localLane === void 0 ? void 0 : localLane.toLaneId().toString()) || 'main';
871
- const otherLaneName = otherLane ? otherLane.toLaneId().toString() : _laneId().DEFAULT_LANE;
872
- const currentLabel = `${currentId.version} (${currentLaneName === otherLaneName ? 'current' : currentLaneName})`;
873
- const otherLabel = `${otherLaneHead.toString()} (${otherLaneName === currentLaneName ? 'incoming' : otherLaneName})`;
874
- const workspaceIds = await this.workspace.listIds();
875
- const configMerger = new (_configMerger().ConfigMerger)(id.toStringWithoutVersion(), workspaceIds, otherLane, currentComponent.extensions, baseComponent.extensions, otherComponent.extensions, currentLabel, otherLabel, this.logger);
876
- const configMergeResult = configMerger.merge();
877
- const mergeResults = await (0, _threeWayMerge().default)({
878
- consumer: this.workspace.consumer,
879
- otherComponent,
880
- otherLabel,
881
- currentComponent,
882
- currentLabel,
883
- baseComponent
884
- });
885
- return {
886
- currentComponent,
887
- id,
888
- mergeResults,
889
- divergeData,
890
- configMergeResult
891
- };
583
+ const mergeStatusProvider = new (_mergeStatusProvider().MergeStatusProvider)(this.workspace, this.logger, this.importer, currentLane || undefined, otherLane || undefined, options);
584
+ return mergeStatusProvider.getStatus(bitIds);
892
585
  }
893
586
  async applyVersion({
894
587
  currentComponent,
@@ -910,25 +603,25 @@ other: ${otherLaneHead.toString()}`);
910
603
  scope: id.scope
911
604
  },
912
605
  head: remoteHead,
913
- remote: laneId.scope,
914
- // @todo: remove. it has been deprecated around 0.0.832
915
- lane: laneId.name,
916
- // @todo: remove. it has been deprecated around 0.0.832
917
606
  laneId
918
607
  };
919
608
  id = currentComponent ? currentComponent.id : id;
920
609
  const modelComponent = await consumer.scope.getModelComponent(id);
921
610
  const handleResolveUnrelated = () => {
922
611
  if (!currentComponent) throw new Error('currentComponent must be defined when resolvedUnrelated');
612
+ // because when on a main, we don't allow merging lanes with unrelated. we asks users to switch to the lane
613
+ // first and then merge with --resolve-unrelated
923
614
  if (!localLane) throw new Error('localLane must be defined when resolvedUnrelated');
924
- if (!(resolvedUnrelated !== null && resolvedUnrelated !== void 0 && resolvedUnrelated.head)) throw new Error('resolvedUnrelated must have head prop');
615
+ if (!resolvedUnrelated) throw new Error('resolvedUnrelated must be populated');
925
616
  localLane.addComponent({
926
617
  id,
927
- head: resolvedUnrelated.head
618
+ head: resolvedUnrelated.headOnCurrentLane
928
619
  });
929
- unmergedComponent.laneId = localLane.toLaneId();
930
- unmergedComponent.head = resolvedUnrelated.head;
931
- unmergedComponent.unrelated = true;
620
+ unmergedComponent.unrelated = {
621
+ unrelatedHead: resolvedUnrelated.unrelatedHead,
622
+ headOnCurrentLane: resolvedUnrelated.headOnCurrentLane,
623
+ unrelatedLaneId: resolvedUnrelated.unrelatedLaneId
624
+ };
932
625
  consumer.scope.objects.unmergedComponents.addEntry(unmergedComponent);
933
626
  return {
934
627
  applyVersionResult: {