Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improvement(merge), support component-pattern #8990

Merged
merged 1 commit into from
Jun 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions scopes/component/merging/merge-cmd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Command, CommandOptions } from '@teambit/cli';
import { ComponentID } from '@teambit/component-id';
import { compact } from 'lodash';
import {
WILDCARD_HELP,
COMPONENT_PATTERN_HELP,
AUTO_SNAPPED_MSG,
MergeConfigFilename,
FILE_CHANGES_CHECKOUT_MSG,
Expand All @@ -16,15 +16,15 @@ import { FileStatus, MergeStrategy } from './merge-version';
import { ApplyVersionResults, MergingMain, ApplyVersionResult } from './merging.main.runtime';

export class MergeCmd implements Command {
name = 'merge [ids...]';
name = 'merge [component-pattern]';
description = 'merge changes of the remote head into local - auto-snaps all merged components';
helpUrl = 'reference/components/merging-changes';
group = 'development';
arguments = [{ name: 'component-pattern', description: COMPONENT_PATTERN_HELP }];
extendedDescription = `merge changes of the remote head into local when they are diverged. when on a lane, merge the remote head of the lane into the local
and creates snaps for merged components that have diverged, on the lane.
if no ids are specified, all pending-merge components will be merged. (run "bit status" to list them).
optionally use '--abort' to revert the last merge. to revert a lane merge, use "bit lane merge-abort" command.
${WILDCARD_HELP('merge')}`;
optionally use '--abort' to revert the last merge. to revert a lane merge, use "bit lane merge-abort" command.`;
alias = '';
options = [
['', 'ours', 'DEPRECATED. use --auto-merge-resolve. in case of a conflict, keep the local modification'],
Expand Down Expand Up @@ -56,7 +56,7 @@ ${WILDCARD_HELP('merge')}`;
constructor(private merging: MergingMain, private globalConfig: GlobalConfigMain) {}

async report(
[ids = []]: [string[]],
[pattern]: [string],
{
ours = false,
theirs = false,
Expand Down Expand Up @@ -107,7 +107,7 @@ ${WILDCARD_HELP('merge')}`;
mergeSnapResults,
mergeSnapError,
}: ApplyVersionResults = await this.merging.merge(
ids,
pattern,
autoMergeResolve as any,
abort,
resolve,
Expand Down
38 changes: 17 additions & 21 deletions scopes/component/merging/merging.main.runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { WorkspaceAspect, OutsideWorkspaceError, Workspace } from '@teambit/work
import { Consumer } from '@teambit/legacy/dist/consumer';
import ComponentsList from '@teambit/legacy/dist/consumer/component/components-list';
import { SnappingAspect, SnappingMain, TagResults } from '@teambit/snapping';
import hasWildcard from '@teambit/legacy/dist/utils/string/has-wildcard';
import mapSeries from 'p-map-series';
import { ComponentID, ComponentIdList } from '@teambit/component-id';
import { BitError } from '@teambit/bit-error';
Expand Down Expand Up @@ -128,7 +127,7 @@ export class MergingMain {
) {}

async merge(
ids: string[],
pattern: string,
mergeStrategy: MergeStrategy,
abort: boolean,
resolve: boolean,
Expand All @@ -141,11 +140,11 @@ export class MergingMain {
const consumer: Consumer = this.workspace.consumer;
let mergeResults;
if (resolve) {
mergeResults = await this.resolveMerge(ids, message, build);
mergeResults = await this.resolveMerge(pattern, message, build);
} else if (abort) {
mergeResults = await this.abortMerge(ids);
mergeResults = await this.abortMerge(pattern);
} else {
const bitIds = await this.getComponentsToMerge(consumer, ids);
const bitIds = await this.getComponentsToMerge(consumer, pattern);
mergeResults = await this.mergeComponentsFromRemote(
consumer,
bitIds,
Expand Down Expand Up @@ -562,17 +561,17 @@ export class MergingMain {
};
}

private async abortMerge(values: string[]): Promise<ApplyVersionResults> {
private async abortMerge(pattern: string): Promise<ApplyVersionResults> {
const consumer = this.workspace.consumer;
const ids = await this.getIdsForUnmerged(values);
const ids = await this.getIdsForUnmerged(pattern);
const results = await this.checkout.checkout({ ids, reset: true });
ids.forEach((id) => consumer.scope.objects.unmergedComponents.removeComponent(id));
await consumer.scope.objects.unmergedComponents.write();
return { abortedComponents: results.components };
}

private async resolveMerge(values: string[], snapMessage: string, build: boolean): Promise<ApplyVersionResults> {
const ids = await this.getIdsForUnmerged(values);
private async resolveMerge(pattern: string, snapMessage: string, build: boolean): Promise<ApplyVersionResults> {
const ids = await this.getIdsForUnmerged(pattern);
// @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX!
const { snappedComponents } = await this.snapping.snap({
legacyBitIds: ComponentIdList.fromArray(ids.map((id) => id)),
Expand Down Expand Up @@ -648,9 +647,9 @@ export class MergingMain {
});
}

private async getIdsForUnmerged(idsStr?: string[]): Promise<ComponentID[]> {
if (idsStr && idsStr.length) {
const componentIds = await this.workspace.resolveMultipleComponentIds(idsStr);
private async getIdsForUnmerged(pattern?: string): Promise<ComponentID[]> {
if (pattern) {
const componentIds = await this.workspace.idsByPattern(pattern);
componentIds.forEach((id) => {
const entry = this.workspace.consumer.scope.objects.unmergedComponents.getEntry(id);
if (!entry) {
Expand All @@ -664,16 +663,13 @@ export class MergingMain {
return unresolvedComponents.map((u) => ComponentID.fromObject(u.id));
}

private async getComponentsToMerge(consumer: Consumer, ids: string[]): Promise<ComponentID[]> {
const componentsList = new ComponentsList(consumer);
if (!ids.length) {
const mergePending = await componentsList.listMergePendingComponents();
return mergePending.map((c) => c.id);
}
if (hasWildcard(ids)) {
return componentsList.listComponentsByIdsWithWildcard(ids);
private async getComponentsToMerge(consumer: Consumer, pattern?: string): Promise<ComponentID[]> {
if (pattern) {
return this.workspace.idsByPattern(pattern);
}
return ids.map((id) => consumer.getParsedId(id));
const componentsList = new ComponentsList(consumer);
const mergePending = await componentsList.listMergePendingComponents();
return mergePending.map((c) => c.id);
}

static slots = [];
Expand Down
8 changes: 0 additions & 8 deletions src/consumer/component/components-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { compact } from 'lodash';
import pFilter from 'p-filter';
import { ComponentID, ComponentIdList } from '@teambit/component-id';
import R from 'ramda';
import NoIdMatchWildcard from '../../api/consumer/lib/exceptions/no-id-match-wildcard';
import { LATEST } from '../../constants';
import { SnapsDistance } from '../../scope/component-ops/snaps-distance';
import { getDivergeData } from '../../scope/component-ops/get-diverge-data';
Expand Down Expand Up @@ -509,11 +508,4 @@ export default class ComponentsList {
static getUniqueComponents(components: Component[]): Component[] {
return R.uniqBy((component) => JSON.stringify(component.componentId), components);
}

listComponentsByIdsWithWildcard(idsWithWildcard: string[]): ComponentID[] {
const allIds = this.bitMap.getAllBitIds();
const matchedIds = ComponentsList.filterComponentsByWildcard(allIds, idsWithWildcard);
if (!matchedIds.length) throw new NoIdMatchWildcard(idsWithWildcard);
return matchedIds;
}
}