mirror of
https://github.com/mermaid-js/mermaid.git
synced 2025-09-23 17:29:54 +02:00
implemented transfer objects from parser to db
This commit is contained in:
@@ -10,7 +10,15 @@ import {
|
|||||||
setDiagramTitle,
|
setDiagramTitle,
|
||||||
getDiagramTitle,
|
getDiagramTitle,
|
||||||
} from '../common/commonDb.js';
|
} from '../common/commonDb.js';
|
||||||
import type { DiagramOrientation, Commit, GitGraphDB } from './gitGraphTypes.js';
|
import type {
|
||||||
|
DiagramOrientation,
|
||||||
|
Commit,
|
||||||
|
GitGraphDB,
|
||||||
|
CommitDB,
|
||||||
|
MergeDB,
|
||||||
|
BranchDB,
|
||||||
|
CherryPickDB,
|
||||||
|
} from './gitGraphTypes.js';
|
||||||
import { commitType } from './gitGraphTypes.js';
|
import { commitType } from './gitGraphTypes.js';
|
||||||
import { ImperativeState } from '../../utils/imperativeState.js';
|
import { ImperativeState } from '../../utils/imperativeState.js';
|
||||||
|
|
||||||
@@ -86,7 +94,12 @@ export const getOptions = function () {
|
|||||||
return state.records.options;
|
return state.records.options;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const commit = function (msg: string, id: string, type: number, tags?: string[]) {
|
export const commit = function (commitDB: CommitDB) {
|
||||||
|
let msg = commitDB.msg;
|
||||||
|
let id = commitDB.id;
|
||||||
|
const type = commitDB.type;
|
||||||
|
let tags = commitDB.tags;
|
||||||
|
|
||||||
log.info('commit', msg, id, type, tags);
|
log.info('commit', msg, id, type, tags);
|
||||||
log.debug('Entering commit:', msg, id, type, tags);
|
log.debug('Entering commit:', msg, id, type, tags);
|
||||||
const config = getConfig();
|
const config = getConfig();
|
||||||
@@ -109,7 +122,9 @@ export const commit = function (msg: string, id: string, type: number, tags?: st
|
|||||||
log.debug('in pushCommit ' + newCommit.id);
|
log.debug('in pushCommit ' + newCommit.id);
|
||||||
};
|
};
|
||||||
|
|
||||||
export const branch = function (name: string, order?: number) {
|
export const branch = function (branchDB: BranchDB) {
|
||||||
|
let name = branchDB.name;
|
||||||
|
const order = branchDB.order;
|
||||||
name = common.sanitizeText(name, getConfig());
|
name = common.sanitizeText(name, getConfig());
|
||||||
if (state.records.branches.has(name)) {
|
if (state.records.branches.has(name)) {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
@@ -123,12 +138,11 @@ export const branch = function (name: string, order?: number) {
|
|||||||
log.debug('in createBranch');
|
log.debug('in createBranch');
|
||||||
};
|
};
|
||||||
|
|
||||||
export const merge = (
|
export const merge = (mergeDB: MergeDB): void => {
|
||||||
otherBranch: string,
|
let otherBranch = mergeDB.branch;
|
||||||
customId?: string,
|
let customId = mergeDB.id;
|
||||||
overrideType?: number,
|
const overrideType = mergeDB.type;
|
||||||
customTags?: string[]
|
const customTags = mergeDB.tags;
|
||||||
): void => {
|
|
||||||
const config = getConfig();
|
const config = getConfig();
|
||||||
otherBranch = common.sanitizeText(otherBranch, config);
|
otherBranch = common.sanitizeText(otherBranch, config);
|
||||||
if (customId) {
|
if (customId) {
|
||||||
@@ -233,12 +247,11 @@ export const merge = (
|
|||||||
log.debug('in mergeBranch');
|
log.debug('in mergeBranch');
|
||||||
};
|
};
|
||||||
|
|
||||||
export const cherryPick = function (
|
export const cherryPick = function (cherryPickDB: CherryPickDB) {
|
||||||
sourceId: string,
|
let sourceId = cherryPickDB.id;
|
||||||
targetId: string,
|
let targetId = cherryPickDB.targetId;
|
||||||
tags: string[] | undefined,
|
let tags = cherryPickDB.tags;
|
||||||
parentCommitId: string
|
let parentCommitId = cherryPickDB.parent;
|
||||||
) {
|
|
||||||
log.debug('Entering cherryPick:', sourceId, targetId, tags);
|
log.debug('Entering cherryPick:', sourceId, targetId, tags);
|
||||||
const config = getConfig();
|
const config = getConfig();
|
||||||
sourceId = common.sanitizeText(sourceId, config);
|
sourceId = common.sanitizeText(sourceId, config);
|
||||||
|
@@ -12,6 +12,10 @@ import type {
|
|||||||
CommitAst,
|
CommitAst,
|
||||||
BranchAst,
|
BranchAst,
|
||||||
GitGraphDBParseProvider,
|
GitGraphDBParseProvider,
|
||||||
|
CommitDB,
|
||||||
|
BranchDB,
|
||||||
|
MergeDB,
|
||||||
|
CherryPickDB,
|
||||||
} from './gitGraphTypes.js';
|
} from './gitGraphTypes.js';
|
||||||
|
|
||||||
const populate = (ast: GitGraph, db: GitGraphDBParseProvider) => {
|
const populate = (ast: GitGraph, db: GitGraphDBParseProvider) => {
|
||||||
@@ -28,11 +32,11 @@ const populate = (ast: GitGraph, db: GitGraphDBParseProvider) => {
|
|||||||
|
|
||||||
const parseStatement = (statement: any, db: GitGraphDBParseProvider) => {
|
const parseStatement = (statement: any, db: GitGraphDBParseProvider) => {
|
||||||
const parsers: Record<string, (stmt: any) => void> = {
|
const parsers: Record<string, (stmt: any) => void> = {
|
||||||
Commit: (stmt) => db.commit(...parseCommit(stmt)),
|
Commit: (stmt) => db.commit(parseCommit(stmt)),
|
||||||
Branch: (stmt) => db.branch(...parseBranch(stmt)),
|
Branch: (stmt) => db.branch(parseBranch(stmt)),
|
||||||
Merge: (stmt) => db.merge(...parseMerge(stmt)),
|
Merge: (stmt) => db.merge(parseMerge(stmt)),
|
||||||
Checkout: (stmt) => db.checkout(parseCheckout(stmt)),
|
Checkout: (stmt) => db.checkout(parseCheckout(stmt)),
|
||||||
CherryPicking: (stmt) => db.cherryPick(...parseCherryPicking(stmt)),
|
CherryPicking: (stmt) => db.cherryPick(parseCherryPicking(stmt)),
|
||||||
};
|
};
|
||||||
|
|
||||||
const parser = parsers[statement.$type];
|
const parser = parsers[statement.$type];
|
||||||
@@ -43,29 +47,32 @@ const parseStatement = (statement: any, db: GitGraphDBParseProvider) => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const parseCommit = (commit: CommitAst): [string, string, number, string[] | undefined] => {
|
const parseCommit = (commit: CommitAst): CommitDB => {
|
||||||
const id = commit.id;
|
const commitDB: CommitDB = {
|
||||||
const message = commit.message ?? '';
|
id: commit.id,
|
||||||
const type = commit.type !== undefined ? commitType[commit.type] : commitType.NORMAL;
|
msg: commit.message ?? '',
|
||||||
const tags = commit.tags ?? undefined;
|
type: commit.type !== undefined ? commitType[commit.type] : commitType.NORMAL,
|
||||||
|
tags: commit.tags ?? undefined,
|
||||||
return [message, id, type, tags];
|
};
|
||||||
|
return commitDB;
|
||||||
};
|
};
|
||||||
|
|
||||||
const parseBranch = (branch: BranchAst): [string, number] => {
|
const parseBranch = (branch: BranchAst): BranchDB => {
|
||||||
const name = branch.name;
|
const branchDB: BranchDB = {
|
||||||
const order = branch.order ?? 0;
|
name: branch.name,
|
||||||
return [name, order];
|
order: branch.order ?? 0,
|
||||||
|
};
|
||||||
|
return branchDB;
|
||||||
};
|
};
|
||||||
|
|
||||||
const parseMerge = (
|
const parseMerge = (merge: MergeAst): MergeDB => {
|
||||||
merge: MergeAst
|
const mergeDB: MergeDB = {
|
||||||
): [string, string, number | undefined, string[] | undefined] => {
|
branch: merge.branch,
|
||||||
const branch = merge.branch;
|
id: merge.id ?? '',
|
||||||
const id = merge.id ?? '';
|
type: merge.type !== undefined ? commitType[merge.type] : commitType.NORMAL,
|
||||||
const type = merge.type !== undefined ? commitType[merge.type] : undefined;
|
tags: merge.tags ?? undefined,
|
||||||
const tags = merge.tags ?? undefined;
|
};
|
||||||
return [branch, id, type, tags];
|
return mergeDB;
|
||||||
};
|
};
|
||||||
|
|
||||||
const parseCheckout = (checkout: CheckoutAst): string => {
|
const parseCheckout = (checkout: CheckoutAst): string => {
|
||||||
@@ -73,13 +80,14 @@ const parseCheckout = (checkout: CheckoutAst): string => {
|
|||||||
return branch;
|
return branch;
|
||||||
};
|
};
|
||||||
|
|
||||||
const parseCherryPicking = (
|
const parseCherryPicking = (cherryPicking: CherryPickingAst): CherryPickDB => {
|
||||||
cherryPicking: CherryPickingAst
|
const cherryPickDB: CherryPickDB = {
|
||||||
): [string, string, string[] | undefined, string] => {
|
id: cherryPicking.id,
|
||||||
const id = cherryPicking.id;
|
targetId: '',
|
||||||
const tags = cherryPicking.tags?.length === 0 ? undefined : cherryPicking.tags;
|
tags: cherryPicking.tags?.length === 0 ? undefined : cherryPicking.tags,
|
||||||
const parent = cherryPicking.parent;
|
parent: cherryPicking.parent,
|
||||||
return [id, '', tags, parent];
|
};
|
||||||
|
return cherryPickDB;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const parser: ParserDefinition = {
|
export const parser: ParserDefinition = {
|
||||||
@@ -113,7 +121,12 @@ if (import.meta.vitest) {
|
|||||||
type: 'NORMAL',
|
type: 'NORMAL',
|
||||||
};
|
};
|
||||||
parseStatement(commit, mockDB);
|
parseStatement(commit, mockDB);
|
||||||
expect(mockDB.commit).toHaveBeenCalledWith('test', '1', 0, ['tag1', 'tag2']);
|
expect(mockDB.commit).toHaveBeenCalledWith({
|
||||||
|
id: '1',
|
||||||
|
msg: 'test',
|
||||||
|
tags: ['tag1', 'tag2'],
|
||||||
|
type: 0,
|
||||||
|
});
|
||||||
});
|
});
|
||||||
it('should parse a branch statement', () => {
|
it('should parse a branch statement', () => {
|
||||||
const branch = {
|
const branch = {
|
||||||
@@ -122,7 +135,7 @@ if (import.meta.vitest) {
|
|||||||
order: 1,
|
order: 1,
|
||||||
};
|
};
|
||||||
parseStatement(branch, mockDB);
|
parseStatement(branch, mockDB);
|
||||||
expect(mockDB.branch).toHaveBeenCalledWith('newBranch', 1);
|
expect(mockDB.branch).toHaveBeenCalledWith({ name: 'newBranch', order: 1 });
|
||||||
});
|
});
|
||||||
it('should parse a checkout statement', () => {
|
it('should parse a checkout statement', () => {
|
||||||
const checkout = {
|
const checkout = {
|
||||||
@@ -141,7 +154,12 @@ if (import.meta.vitest) {
|
|||||||
type: 'NORMAL',
|
type: 'NORMAL',
|
||||||
};
|
};
|
||||||
parseStatement(merge, mockDB);
|
parseStatement(merge, mockDB);
|
||||||
expect(mockDB.merge).toHaveBeenCalledWith('newBranch', '1', 0, ['tag1', 'tag2']);
|
expect(mockDB.merge).toHaveBeenCalledWith({
|
||||||
|
branch: 'newBranch',
|
||||||
|
id: '1',
|
||||||
|
tags: ['tag1', 'tag2'],
|
||||||
|
type: 0,
|
||||||
|
});
|
||||||
});
|
});
|
||||||
it('should parse a cherry picking statement', () => {
|
it('should parse a cherry picking statement', () => {
|
||||||
const cherryPick = {
|
const cherryPick = {
|
||||||
@@ -151,7 +169,12 @@ if (import.meta.vitest) {
|
|||||||
parent: '2',
|
parent: '2',
|
||||||
};
|
};
|
||||||
parseStatement(cherryPick, mockDB);
|
parseStatement(cherryPick, mockDB);
|
||||||
expect(mockDB.cherryPick).toHaveBeenCalledWith('1', '', ['tag1', 'tag2'], '2');
|
expect(mockDB.cherryPick).toHaveBeenCalledWith({
|
||||||
|
id: '1',
|
||||||
|
targetId: '',
|
||||||
|
parent: '2',
|
||||||
|
tags: ['tag1', 'tag2'],
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should parse a langium generated gitGraph ast', () => {
|
it('should parse a langium generated gitGraph ast', () => {
|
||||||
@@ -201,9 +224,19 @@ if (import.meta.vitest) {
|
|||||||
|
|
||||||
populate(gitGraphAst, mockDB);
|
populate(gitGraphAst, mockDB);
|
||||||
|
|
||||||
expect(mockDB.commit).toHaveBeenCalledWith('test', '1', 0, ['tag1', 'tag2']);
|
expect(mockDB.commit).toHaveBeenCalledWith({
|
||||||
expect(mockDB.branch).toHaveBeenCalledWith('newBranch', 1);
|
id: '1',
|
||||||
expect(mockDB.merge).toHaveBeenCalledWith('newBranch', '1', 0, ['tag1', 'tag2']);
|
msg: 'test',
|
||||||
|
tags: ['tag1', 'tag2'],
|
||||||
|
type: 0,
|
||||||
|
});
|
||||||
|
expect(mockDB.branch).toHaveBeenCalledWith({ name: 'newBranch', order: 1 });
|
||||||
|
expect(mockDB.merge).toHaveBeenCalledWith({
|
||||||
|
branch: 'newBranch',
|
||||||
|
id: '1',
|
||||||
|
tags: ['tag1', 'tag2'],
|
||||||
|
type: 0,
|
||||||
|
});
|
||||||
expect(mockDB.checkout).toHaveBeenCalledWith('newBranch');
|
expect(mockDB.checkout).toHaveBeenCalledWith('newBranch');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@@ -10,9 +10,9 @@ export const commitType = {
|
|||||||
} as const;
|
} as const;
|
||||||
|
|
||||||
export interface CommitDB {
|
export interface CommitDB {
|
||||||
message: string;
|
msg: string;
|
||||||
id: string;
|
id: string;
|
||||||
type: typeof commitType;
|
type: number;
|
||||||
tags?: string[];
|
tags?: string[];
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -24,7 +24,7 @@ export interface BranchDB {
|
|||||||
export interface MergeDB {
|
export interface MergeDB {
|
||||||
branch: string;
|
branch: string;
|
||||||
id: string;
|
id: string;
|
||||||
type?: typeof commitType;
|
type?: number;
|
||||||
tags?: string[];
|
tags?: string[];
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -92,20 +92,10 @@ export interface GitGraphDB extends DiagramDBBase<GitGraphDiagramConfig> {
|
|||||||
setDirection: (dir: DiagramOrientation) => void;
|
setDirection: (dir: DiagramOrientation) => void;
|
||||||
setOptions: (rawOptString: string) => void;
|
setOptions: (rawOptString: string) => void;
|
||||||
getOptions: () => any;
|
getOptions: () => any;
|
||||||
commit: (msg: string, id: string, type: number, tags?: string[]) => void;
|
commit: (commitDB: CommitDB) => void;
|
||||||
branch: (name: string, order?: number) => void;
|
branch: (branchDB: BranchDB) => void;
|
||||||
merge: (
|
merge: (mergeDB: MergeDB) => void;
|
||||||
otherBranch: string,
|
cherryPick: (cherryPickDB: CherryPickDB) => void;
|
||||||
customId?: string,
|
|
||||||
overrideType?: number,
|
|
||||||
customTags?: string[]
|
|
||||||
) => void;
|
|
||||||
cherryPick: (
|
|
||||||
sourceId: string,
|
|
||||||
targetId: string,
|
|
||||||
tags: string[] | undefined,
|
|
||||||
parentCommitId: string
|
|
||||||
) => void;
|
|
||||||
checkout: (branch: string) => void;
|
checkout: (branch: string) => void;
|
||||||
prettyPrint: () => void;
|
prettyPrint: () => void;
|
||||||
clear: () => void;
|
clear: () => void;
|
||||||
@@ -121,20 +111,10 @@ export interface GitGraphDB extends DiagramDBBase<GitGraphDiagramConfig> {
|
|||||||
export interface GitGraphDBParseProvider extends Partial<GitGraphDB> {
|
export interface GitGraphDBParseProvider extends Partial<GitGraphDB> {
|
||||||
commitType: typeof commitType;
|
commitType: typeof commitType;
|
||||||
setDirection: (dir: DiagramOrientation) => void;
|
setDirection: (dir: DiagramOrientation) => void;
|
||||||
commit: (msg: string, id: string, type: number, tags?: string[]) => void;
|
commit: (commitDB: CommitDB) => void;
|
||||||
branch: (name: string, order?: number) => void;
|
branch: (branchDB: BranchDB) => void;
|
||||||
merge: (
|
merge: (mergeDB: MergeDB) => void;
|
||||||
otherBranch: string,
|
cherryPick: (cherryPickDB: CherryPickDB) => void;
|
||||||
customId?: string,
|
|
||||||
overrideType?: number,
|
|
||||||
customTags?: string[]
|
|
||||||
) => void;
|
|
||||||
cherryPick: (
|
|
||||||
sourceId: string,
|
|
||||||
targetId: string,
|
|
||||||
tags: string[] | undefined,
|
|
||||||
parentCommitId: string
|
|
||||||
) => void;
|
|
||||||
checkout: (branch: string) => void;
|
checkout: (branch: string) => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user