mirror of
https://github.com/mermaid-js/mermaid.git
synced 2025-09-19 15:30:03 +02:00
added Imperative state
This commit is contained in:
@@ -13,19 +13,32 @@ import {
|
|||||||
} from '../common/commonDb.js';
|
} from '../common/commonDb.js';
|
||||||
import defaultConfig from '../../defaultConfig.js';
|
import defaultConfig from '../../defaultConfig.js';
|
||||||
import type { DiagramOrientation, Commit } from './gitGraphTypes.js';
|
import type { DiagramOrientation, Commit } from './gitGraphTypes.js';
|
||||||
|
import { ImperativeState } from '../../utils/imperativeState.js';
|
||||||
|
|
||||||
|
interface GitGraphState {
|
||||||
|
commits: Map<string, Commit>;
|
||||||
|
head: Commit | null;
|
||||||
|
branchConfig: Map<string, { name: string; order: number | undefined }>;
|
||||||
|
branches: Map<string, string | null>;
|
||||||
|
currBranch: string;
|
||||||
|
direction: DiagramOrientation;
|
||||||
|
seq: number;
|
||||||
|
options: any;
|
||||||
|
}
|
||||||
|
|
||||||
const mainBranchName = defaultConfig.gitGraph.mainBranchName;
|
const mainBranchName = defaultConfig.gitGraph.mainBranchName;
|
||||||
const mainBranchOrder = defaultConfig.gitGraph.mainBranchOrder;
|
const mainBranchOrder = defaultConfig.gitGraph.mainBranchOrder;
|
||||||
|
|
||||||
let commits = new Map<string, Commit>();
|
const state = new ImperativeState<GitGraphState>(() => ({
|
||||||
let head: Commit | null = null;
|
commits: new Map(),
|
||||||
let branchesConfig = new Map<string, { name: string; order: number | undefined }>();
|
head: null,
|
||||||
branchesConfig.set(mainBranchName, { name: mainBranchName, order: mainBranchOrder });
|
branchConfig: new Map([[mainBranchName, { name: mainBranchName, order: mainBranchOrder }]]),
|
||||||
let branches = new Map<string, string | null>();
|
branches: new Map([[mainBranchName, null]]),
|
||||||
branches.set(mainBranchName, null);
|
currBranch: mainBranchName,
|
||||||
let curBranch = mainBranchName;
|
direction: 'LR',
|
||||||
let direction: DiagramOrientation = 'LR';
|
seq: 0,
|
||||||
let seq = 0;
|
options: {},
|
||||||
|
}));
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
@@ -34,11 +47,6 @@ function getId() {
|
|||||||
return random({ length: 7 });
|
return random({ length: 7 });
|
||||||
}
|
}
|
||||||
|
|
||||||
// /**
|
|
||||||
// * @param currentCommit
|
|
||||||
// * @param otherCommit
|
|
||||||
// */
|
|
||||||
|
|
||||||
// function isFastForwardable(currentCommit, otherCommit) {
|
// function isFastForwardable(currentCommit, otherCommit) {
|
||||||
// log.debug('Entering isFastForwardable:', currentCommit.id, otherCommit.id);
|
// log.debug('Entering isFastForwardable:', currentCommit.id, otherCommit.id);
|
||||||
// let cnt = 0;
|
// let cnt = 0;
|
||||||
@@ -60,10 +68,6 @@ function getId() {
|
|||||||
// return currentCommit.id === otherCommit.id;
|
// return currentCommit.id === otherCommit.id;
|
||||||
// }
|
// }
|
||||||
|
|
||||||
/**
|
|
||||||
* @param currentCommit - current commit
|
|
||||||
* @param otherCommit - other commit
|
|
||||||
*/
|
|
||||||
// function isReachableFrom(currentCommit, otherCommit) {
|
// function isReachableFrom(currentCommit, otherCommit) {
|
||||||
// const currentSeq = currentCommit.seq;
|
// const currentSeq = currentCommit.seq;
|
||||||
// const otherSeq = otherCommit.seq;
|
// const otherSeq = otherCommit.seq;
|
||||||
@@ -88,23 +92,22 @@ function uniqBy(list: any[], fn: (item: any) => any) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export const setDirection = function (dir: DiagramOrientation) {
|
export const setDirection = function (dir: DiagramOrientation) {
|
||||||
direction = dir;
|
state.records.direction = dir;
|
||||||
};
|
};
|
||||||
|
|
||||||
let options = {};
|
|
||||||
export const setOptions = function (rawOptString: string) {
|
export const setOptions = function (rawOptString: string) {
|
||||||
log.debug('options str', rawOptString);
|
log.debug('options str', rawOptString);
|
||||||
rawOptString = rawOptString?.trim();
|
rawOptString = rawOptString?.trim();
|
||||||
rawOptString = rawOptString || '{}';
|
rawOptString = rawOptString || '{}';
|
||||||
try {
|
try {
|
||||||
options = JSON.parse(rawOptString);
|
state.records.options = JSON.parse(rawOptString);
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
log.error('error while parsing gitGraph options', e.message);
|
log.error('error while parsing gitGraph options', e.message);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
export const getOptions = function () {
|
export const getOptions = function () {
|
||||||
return options;
|
return state.records.options;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const commit = function (msg: string, id: string, type: number, tags: string[] | undefined) {
|
export const commit = function (msg: string, id: string, type: number, tags: string[] | undefined) {
|
||||||
@@ -115,26 +118,26 @@ export const commit = function (msg: string, id: string, type: number, tags: str
|
|||||||
const config = getConfig();
|
const config = getConfig();
|
||||||
tags = tags?.map((tag) => common.sanitizeText(tag, config));
|
tags = tags?.map((tag) => common.sanitizeText(tag, config));
|
||||||
const newCommit: Commit = {
|
const newCommit: Commit = {
|
||||||
id: id ? id : seq + '-' + getId(),
|
id: id ? id : state.records.seq + '-' + getId(),
|
||||||
message: msg,
|
message: msg,
|
||||||
seq: seq++,
|
seq: state.records.seq++,
|
||||||
type: type ? type : commitType.NORMAL,
|
type: type ?? commitType.NORMAL,
|
||||||
tags: tags ? tags : [],
|
tags: tags ?? [],
|
||||||
parents: head == null ? [] : [head.id],
|
parents: state.records.head == null ? [] : [state.records.head.id],
|
||||||
branch: curBranch,
|
branch: state.records.currBranch,
|
||||||
};
|
};
|
||||||
head = newCommit;
|
state.records.head = newCommit;
|
||||||
log.info('main branch', mainBranchName);
|
log.info('main branch', mainBranchName);
|
||||||
commits.set(newCommit.id, newCommit);
|
state.records.commits.set(newCommit.id, newCommit);
|
||||||
branches.set(curBranch, newCommit.id);
|
state.records.branches.set(state.records.currBranch, newCommit.id);
|
||||||
log.debug('in pushCommit ' + newCommit.id);
|
log.debug('in pushCommit ' + newCommit.id);
|
||||||
};
|
};
|
||||||
|
|
||||||
export const branch = function (name: string, order: number | undefined) {
|
export const branch = function (name: string, order: number | undefined) {
|
||||||
name = common.sanitizeText(name, getConfig());
|
name = common.sanitizeText(name, getConfig());
|
||||||
if (!branches.has(name)) {
|
if (!state.records.branches.has(name)) {
|
||||||
branches.set(name, head != null ? head.id : null);
|
state.records.branches.set(name, state.records.head != null ? state.records.head.id : null);
|
||||||
branchesConfig.set(name, { name, order });
|
state.records.branchConfig.set(name, { name, order });
|
||||||
checkout(name);
|
checkout(name);
|
||||||
log.debug('in createBranch');
|
log.debug('in createBranch');
|
||||||
} else {
|
} else {
|
||||||
@@ -146,30 +149,32 @@ export const branch = function (name: string, order: number | undefined) {
|
|||||||
|
|
||||||
export const merge = (
|
export const merge = (
|
||||||
otherBranch: string,
|
otherBranch: string,
|
||||||
custom_id?: string,
|
customId?: string,
|
||||||
override_type?: number,
|
overrideType?: number,
|
||||||
custom_tags?: string[]
|
customTags?: string[]
|
||||||
): void => {
|
): void => {
|
||||||
otherBranch = common.sanitizeText(otherBranch, getConfig());
|
otherBranch = common.sanitizeText(otherBranch, getConfig());
|
||||||
if (custom_id) {
|
if (customId) {
|
||||||
custom_id = common.sanitizeText(custom_id, getConfig());
|
customId = common.sanitizeText(customId, getConfig());
|
||||||
}
|
}
|
||||||
const currentBranchCheck: string | null | undefined = branches.get(curBranch);
|
const currentBranchCheck: string | null | undefined = state.records.branches.get(
|
||||||
const otherBranchCheck: string | null | undefined = branches.get(otherBranch);
|
state.records.currBranch
|
||||||
|
);
|
||||||
|
const otherBranchCheck: string | null | undefined = state.records.branches.get(otherBranch);
|
||||||
const currentCommit: Commit | undefined = currentBranchCheck
|
const currentCommit: Commit | undefined = currentBranchCheck
|
||||||
? commits.get(currentBranchCheck)
|
? state.records.commits.get(currentBranchCheck)
|
||||||
: undefined;
|
: undefined;
|
||||||
const otherCommit: Commit | undefined = otherBranchCheck
|
const otherCommit: Commit | undefined = otherBranchCheck
|
||||||
? commits.get(otherBranchCheck)
|
? state.records.commits.get(otherBranchCheck)
|
||||||
: undefined;
|
: undefined;
|
||||||
if (currentCommit && otherCommit && currentCommit.branch === otherBranch) {
|
if (currentCommit && otherCommit && currentCommit.branch === otherBranch) {
|
||||||
throw new Error(`Cannot merge branch '${otherBranch}' into itself.`);
|
throw new Error(`Cannot merge branch '${otherBranch}' into itself.`);
|
||||||
}
|
}
|
||||||
if (curBranch === otherBranch) {
|
if (state.records.currBranch === otherBranch) {
|
||||||
const error: any = new Error('Incorrect usage of "merge". Cannot merge a branch to itself');
|
const error: any = new Error('Incorrect usage of "merge". Cannot merge a branch to itself');
|
||||||
error.hash = {
|
error.hash = {
|
||||||
text: 'merge ' + otherBranch,
|
text: `merge ${otherBranch}`,
|
||||||
token: 'merge ' + otherBranch,
|
token: `merge ${otherBranch}`,
|
||||||
line: '1',
|
line: '1',
|
||||||
loc: { first_line: 1, last_line: 1, first_column: 1, last_column: 1 },
|
loc: { first_line: 1, last_line: 1, first_column: 1, last_column: 1 },
|
||||||
expected: ['branch abc'],
|
expected: ['branch abc'],
|
||||||
@@ -177,26 +182,26 @@ export const merge = (
|
|||||||
throw error;
|
throw error;
|
||||||
} else if (currentCommit === undefined || !currentCommit) {
|
} else if (currentCommit === undefined || !currentCommit) {
|
||||||
const error: any = new Error(
|
const error: any = new Error(
|
||||||
'Incorrect usage of "merge". Current branch (' + curBranch + ')has no commits'
|
`Incorrect usage of "merge". Current branch (${state.records.currBranch})has no commits`
|
||||||
);
|
);
|
||||||
error.hash = {
|
error.hash = {
|
||||||
text: 'merge ' + otherBranch,
|
text: `merge ${otherBranch}`,
|
||||||
token: 'merge ' + otherBranch,
|
token: `merge ${otherBranch}`,
|
||||||
line: '1',
|
line: '1',
|
||||||
loc: { first_line: 1, last_line: 1, first_column: 1, last_column: 1 },
|
loc: { first_line: 1, last_line: 1, first_column: 1, last_column: 1 },
|
||||||
expected: ['commit'],
|
expected: ['commit'],
|
||||||
};
|
};
|
||||||
throw error;
|
throw error;
|
||||||
} else if (!branches.has(otherBranch)) {
|
} else if (!state.records.branches.has(otherBranch)) {
|
||||||
const error: any = new Error(
|
const error: any = new Error(
|
||||||
'Incorrect usage of "merge". Branch to be merged (' + otherBranch + ') does not exist'
|
'Incorrect usage of "merge". Branch to be merged (' + otherBranch + ') does not exist'
|
||||||
);
|
);
|
||||||
error.hash = {
|
error.hash = {
|
||||||
text: 'merge ' + otherBranch,
|
text: `merge ${otherBranch}`,
|
||||||
token: 'merge ' + otherBranch,
|
token: `merge ${otherBranch}`,
|
||||||
line: '1',
|
line: '1',
|
||||||
loc: { first_line: 1, last_line: 1, first_column: 1, last_column: 1 },
|
loc: { first_line: 1, last_line: 1, first_column: 1, last_column: 1 },
|
||||||
expected: ['branch ' + otherBranch],
|
expected: [`branch ${otherBranch}`],
|
||||||
};
|
};
|
||||||
throw error;
|
throw error;
|
||||||
} else if (otherCommit === undefined || !otherCommit) {
|
} else if (otherCommit === undefined || !otherCommit) {
|
||||||
@@ -204,8 +209,8 @@ export const merge = (
|
|||||||
'Incorrect usage of "merge". Branch to be merged (' + otherBranch + ') has no commits'
|
'Incorrect usage of "merge". Branch to be merged (' + otherBranch + ') has no commits'
|
||||||
);
|
);
|
||||||
error.hash = {
|
error.hash = {
|
||||||
text: 'merge ' + otherBranch,
|
text: `merge ${otherBranch}`,
|
||||||
token: 'merge ' + otherBranch,
|
token: `merge ${otherBranch}`,
|
||||||
line: '1',
|
line: '1',
|
||||||
loc: { first_line: 1, last_line: 1, first_column: 1, last_column: 1 },
|
loc: { first_line: 1, last_line: 1, first_column: 1, last_column: 1 },
|
||||||
expected: ['"commit"'],
|
expected: ['"commit"'],
|
||||||
@@ -214,33 +219,26 @@ export const merge = (
|
|||||||
} else if (currentCommit === otherCommit) {
|
} else if (currentCommit === otherCommit) {
|
||||||
const error: any = new Error('Incorrect usage of "merge". Both branches have same head');
|
const error: any = new Error('Incorrect usage of "merge". Both branches have same head');
|
||||||
error.hash = {
|
error.hash = {
|
||||||
text: 'merge ' + otherBranch,
|
text: `merge ${otherBranch}`,
|
||||||
token: 'merge ' + otherBranch,
|
token: `merge ${otherBranch}`,
|
||||||
line: '1',
|
line: '1',
|
||||||
loc: { first_line: 1, last_line: 1, first_column: 1, last_column: 1 },
|
loc: { first_line: 1, last_line: 1, first_column: 1, last_column: 1 },
|
||||||
expected: ['branch abc'],
|
expected: ['branch abc'],
|
||||||
};
|
};
|
||||||
throw error;
|
throw error;
|
||||||
} else if (custom_id && commits.has(custom_id)) {
|
} else if (customId && state.records.commits.has(customId)) {
|
||||||
const error: any = new Error(
|
const error: any = new Error(
|
||||||
'Incorrect usage of "merge". Commit with id:' +
|
'Incorrect usage of "merge". Commit with id:' +
|
||||||
custom_id +
|
customId +
|
||||||
' already exists, use different custom Id'
|
' already exists, use different custom Id'
|
||||||
);
|
);
|
||||||
error.hash = {
|
error.hash = {
|
||||||
text: 'merge ' + otherBranch + custom_id + override_type + custom_tags?.join(' '),
|
text: `merge ${otherBranch} ${customId} ${overrideType} ${customTags?.join(' ')}`,
|
||||||
token: 'merge ' + otherBranch + custom_id + override_type + custom_tags?.join(' '),
|
token: `merge ${otherBranch} ${customId} ${overrideType} ${customTags?.join(' ')}`,
|
||||||
line: '1',
|
line: '1',
|
||||||
loc: { first_line: 1, last_line: 1, first_column: 1, last_column: 1 },
|
loc: { first_line: 1, last_line: 1, first_column: 1, last_column: 1 },
|
||||||
expected: [
|
expected: [
|
||||||
'merge ' +
|
`merge ${otherBranch} ${customId}_UNIQUE ${overrideType} ${customTags?.join(' ')}`,
|
||||||
otherBranch +
|
|
||||||
' ' +
|
|
||||||
custom_id +
|
|
||||||
'_UNIQUE ' +
|
|
||||||
override_type +
|
|
||||||
' ' +
|
|
||||||
custom_tags?.join(' '),
|
|
||||||
],
|
],
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -260,21 +258,21 @@ export const merge = (
|
|||||||
const verifiedBranch: string = otherBranchCheck ? otherBranchCheck : ''; //figure out a cleaner way to do this
|
const verifiedBranch: string = otherBranchCheck ? otherBranchCheck : ''; //figure out a cleaner way to do this
|
||||||
|
|
||||||
const commit: Commit = {
|
const commit: Commit = {
|
||||||
id: custom_id ? custom_id : seq + '-' + getId(),
|
id: customId ? customId : state.records.seq + '-' + getId(),
|
||||||
message: 'merged branch ' + otherBranch + ' into ' + curBranch,
|
message: `merged branch ${otherBranch} into ${state.records.currBranch}`,
|
||||||
seq: seq++,
|
seq: state.records.seq++,
|
||||||
parents: [head == null ? null : head.id, verifiedBranch],
|
parents: [state.records.head == null ? null : state.records.head.id, verifiedBranch],
|
||||||
branch: curBranch,
|
branch: state.records.currBranch,
|
||||||
type: commitType.MERGE,
|
type: commitType.MERGE,
|
||||||
customType: override_type,
|
customType: overrideType,
|
||||||
customId: custom_id ? true : false,
|
customId: customId ? true : false,
|
||||||
tags: custom_tags ? custom_tags : [],
|
tags: customTags ? customTags : [],
|
||||||
};
|
};
|
||||||
head = commit;
|
state.records.head = commit;
|
||||||
commits.set(commit.id, commit);
|
state.records.commits.set(commit.id, commit);
|
||||||
branches.set(curBranch, commit.id);
|
state.records.branches.set(state.records.currBranch, commit.id);
|
||||||
// }
|
// }
|
||||||
log.debug(branches);
|
log.debug(state.records.branches);
|
||||||
log.debug('in mergeBranch');
|
log.debug('in mergeBranch');
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -291,13 +289,13 @@ export const cherryPick = function (
|
|||||||
tags = tags?.map((tag) => common.sanitizeText(tag, config));
|
tags = tags?.map((tag) => common.sanitizeText(tag, config));
|
||||||
parentCommitId = common.sanitizeText(parentCommitId, getConfig());
|
parentCommitId = common.sanitizeText(parentCommitId, getConfig());
|
||||||
|
|
||||||
if (!sourceId || !commits.has(sourceId)) {
|
if (!sourceId || !state.records.commits.has(sourceId)) {
|
||||||
const error: any = new Error(
|
const error: any = new Error(
|
||||||
'Incorrect usage of "cherryPick". Source commit id should exist and provided'
|
'Incorrect usage of "cherryPick". Source commit id should exist and provided'
|
||||||
);
|
);
|
||||||
error.hash = {
|
error.hash = {
|
||||||
text: 'cherryPick ' + sourceId + ' ' + targetId,
|
text: `cherryPick ${sourceId} ${targetId}`,
|
||||||
token: 'cherryPick ' + sourceId + ' ' + targetId,
|
token: `cherryPick ${sourceId} ${targetId}`,
|
||||||
line: '1',
|
line: '1',
|
||||||
loc: { first_line: 1, last_line: 1, first_column: 1, last_column: 1 },
|
loc: { first_line: 1, last_line: 1, first_column: 1, last_column: 1 },
|
||||||
expected: ['cherry-pick abc'],
|
expected: ['cherry-pick abc'],
|
||||||
@@ -305,7 +303,7 @@ export const cherryPick = function (
|
|||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
|
|
||||||
const sourceCommit = commits.get(sourceId);
|
const sourceCommit = state.records.commits.get(sourceId);
|
||||||
if (sourceCommit === undefined || !sourceCommit) {
|
if (sourceCommit === undefined || !sourceCommit) {
|
||||||
throw new Error('Incorrect usage of "cherryPick". Source commit id should exist and provided');
|
throw new Error('Incorrect usage of "cherryPick". Source commit id should exist and provided');
|
||||||
}
|
}
|
||||||
@@ -325,30 +323,30 @@ export const cherryPick = function (
|
|||||||
);
|
);
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
if (!targetId || !commits.has(targetId)) {
|
if (!targetId || !state.records.commits.has(targetId)) {
|
||||||
// cherry-pick source commit to current branch
|
// cherry-pick source commit to current branch
|
||||||
|
|
||||||
if (sourceCommitBranch === curBranch) {
|
if (sourceCommitBranch === state.records.currBranch) {
|
||||||
const error: any = new Error(
|
const error: any = new Error(
|
||||||
'Incorrect usage of "cherryPick". Source commit is already on current branch'
|
'Incorrect usage of "cherryPick". Source commit is already on current branch'
|
||||||
);
|
);
|
||||||
error.hash = {
|
error.hash = {
|
||||||
text: 'cherryPick ' + sourceId + ' ' + targetId,
|
text: `cherryPick ${sourceId} ${targetId}`,
|
||||||
token: 'cherryPick ' + sourceId + ' ' + targetId,
|
token: `cherryPick ${sourceId} ${targetId}`,
|
||||||
line: '1',
|
line: '1',
|
||||||
loc: { first_line: 1, last_line: 1, first_column: 1, last_column: 1 },
|
loc: { first_line: 1, last_line: 1, first_column: 1, last_column: 1 },
|
||||||
expected: ['cherry-pick abc'],
|
expected: ['cherry-pick abc'],
|
||||||
};
|
};
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
const currentCommitId = branches.get(curBranch);
|
const currentCommitId = state.records.branches.get(state.records.currBranch);
|
||||||
if (currentCommitId === undefined || !currentCommitId) {
|
if (currentCommitId === undefined || !currentCommitId) {
|
||||||
const error: any = new Error(
|
const error: any = new Error(
|
||||||
'Incorrect usage of "cherry-pick". Current branch (' + curBranch + ')has no commits'
|
`Incorrect usage of "cherry-pick". Current branch (${state.records.currBranch})has no commits`
|
||||||
);
|
);
|
||||||
error.hash = {
|
error.hash = {
|
||||||
text: 'cherryPick ' + sourceId + ' ' + targetId,
|
text: `cherryPick ${sourceId} ${targetId}`,
|
||||||
token: 'cherryPick ' + sourceId + ' ' + targetId,
|
token: `cherryPick ${sourceId} ${targetId}`,
|
||||||
line: '1',
|
line: '1',
|
||||||
loc: { first_line: 1, last_line: 1, first_column: 1, last_column: 1 },
|
loc: { first_line: 1, last_line: 1, first_column: 1, last_column: 1 },
|
||||||
expected: ['cherry-pick abc'],
|
expected: ['cherry-pick abc'],
|
||||||
@@ -356,14 +354,14 @@ export const cherryPick = function (
|
|||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
|
|
||||||
const currentCommit = commits.get(currentCommitId);
|
const currentCommit = state.records.commits.get(currentCommitId);
|
||||||
if (currentCommit === undefined || !currentCommit) {
|
if (currentCommit === undefined || !currentCommit) {
|
||||||
const error: any = new Error(
|
const error: any = new Error(
|
||||||
'Incorrect usage of "cherry-pick". Current branch (' + curBranch + ')has no commits'
|
`Incorrect usage of "cherry-pick". Current branch (${state.records.currBranch})has no commits`
|
||||||
);
|
);
|
||||||
error.hash = {
|
error.hash = {
|
||||||
text: 'cherryPick ' + sourceId + ' ' + targetId,
|
text: `cherryPick ${sourceId} ${targetId}`,
|
||||||
token: 'cherryPick ' + sourceId + ' ' + targetId,
|
token: `cherryPick ${sourceId} ${targetId}`,
|
||||||
line: '1',
|
line: '1',
|
||||||
loc: { first_line: 1, last_line: 1, first_column: 1, last_column: 1 },
|
loc: { first_line: 1, last_line: 1, first_column: 1, last_column: 1 },
|
||||||
expected: ['cherry-pick abc'],
|
expected: ['cherry-pick abc'],
|
||||||
@@ -371,11 +369,11 @@ export const cherryPick = function (
|
|||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
const commit = {
|
const commit = {
|
||||||
id: seq + '-' + getId(),
|
id: state.records.seq + '-' + getId(),
|
||||||
message: 'cherry-picked ' + sourceCommit?.message + ' into ' + curBranch,
|
message: `cherry-picked ${sourceCommit?.message} into ${state.records.currBranch}`,
|
||||||
seq: seq++,
|
seq: state.records.seq++,
|
||||||
parents: [head == null ? null : head.id, sourceCommit.id],
|
parents: [state.records.head == null ? null : state.records.head.id, sourceCommit.id],
|
||||||
branch: curBranch,
|
branch: state.records.currBranch,
|
||||||
type: commitType.CHERRY_PICK,
|
type: commitType.CHERRY_PICK,
|
||||||
tags: tags
|
tags: tags
|
||||||
? tags.filter(Boolean)
|
? tags.filter(Boolean)
|
||||||
@@ -385,36 +383,36 @@ export const cherryPick = function (
|
|||||||
}`,
|
}`,
|
||||||
],
|
],
|
||||||
};
|
};
|
||||||
head = commit;
|
state.records.head = commit;
|
||||||
commits.set(commit.id, commit);
|
state.records.commits.set(commit.id, commit);
|
||||||
branches.set(curBranch, commit.id);
|
state.records.branches.set(state.records.currBranch, commit.id);
|
||||||
log.debug(branches);
|
log.debug(state.records.branches);
|
||||||
log.debug('in cherryPick');
|
log.debug('in cherryPick');
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
export const checkout = function (branch: string) {
|
export const checkout = function (branch: string) {
|
||||||
branch = common.sanitizeText(branch, getConfig());
|
branch = common.sanitizeText(branch, getConfig());
|
||||||
if (!branches.has(branch)) {
|
if (!state.records.branches.has(branch)) {
|
||||||
const error: any = new Error(
|
const error: any = new Error(
|
||||||
'Trying to checkout branch which is not yet created. (Help try using "branch ' + branch + '")'
|
`Trying to checkout branch which is not yet created. (Help try using "branch ${branch}")`
|
||||||
);
|
);
|
||||||
error.hash = {
|
error.hash = {
|
||||||
text: 'checkout ' + branch,
|
text: `checkout ${branch}`,
|
||||||
token: 'checkout ' + branch,
|
token: `checkout ${branch}`,
|
||||||
line: '1',
|
line: '1',
|
||||||
loc: { first_line: 1, last_line: 1, first_column: 1, last_column: 1 },
|
loc: { first_line: 1, last_line: 1, first_column: 1, last_column: 1 },
|
||||||
expected: ['"branch ' + branch + '"'],
|
expected: [`branch ${branch}`],
|
||||||
};
|
};
|
||||||
throw error;
|
throw error;
|
||||||
//branches[branch] = head != null ? head.id : null;
|
//branches[branch] = head != null ? head.id : null;
|
||||||
//log.debug('in createBranch');
|
//log.debug('in createBranch');
|
||||||
} else {
|
} else {
|
||||||
curBranch = branch;
|
state.records.currBranch = branch;
|
||||||
const id = branches.get(curBranch);
|
const id = state.records.branches.get(state.records.currBranch);
|
||||||
if (id === undefined || !id) {
|
if (id === undefined || !id) {
|
||||||
head = null;
|
state.records.head = null;
|
||||||
} else {
|
} else {
|
||||||
head = commits.get(id) ?? null;
|
state.records.head = state.records.commits.get(id) ?? null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -469,23 +467,23 @@ function prettyPrintCommitHistory(commitArr: Commit[]) {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
const label = [line, commit.id, commit.seq];
|
const label = [line, commit.id, commit.seq];
|
||||||
for (const branch in branches) {
|
for (const branch in state.records.branches) {
|
||||||
if (branches.get(branch) === commit.id) {
|
if (state.records.branches.get(branch) === commit.id) {
|
||||||
label.push(branch);
|
label.push(branch);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
log.debug(label.join(' '));
|
log.debug(label.join(' '));
|
||||||
if (commit.parents && commit.parents.length == 2 && commit.parents[0] && commit.parents[1]) {
|
if (commit.parents && commit.parents.length == 2 && commit.parents[0] && commit.parents[1]) {
|
||||||
const newCommit = commits.get(commit.parents[0]);
|
const newCommit = state.records.commits.get(commit.parents[0]);
|
||||||
upsert(commitArr, commit, newCommit);
|
upsert(commitArr, commit, newCommit);
|
||||||
if (commit.parents[1]) {
|
if (commit.parents[1]) {
|
||||||
commitArr.push(commits.get(commit.parents[1])!);
|
commitArr.push(state.records.commits.get(commit.parents[1])!);
|
||||||
}
|
}
|
||||||
} else if (commit.parents.length == 0) {
|
} else if (commit.parents.length == 0) {
|
||||||
return;
|
return;
|
||||||
} else {
|
} else {
|
||||||
if (commit.parents[0]) {
|
if (commit.parents[0]) {
|
||||||
const newCommit = commits.get(commit.parents[0]);
|
const newCommit = state.records.commits.get(commit.parents[0]);
|
||||||
upsert(commitArr, commit, newCommit);
|
upsert(commitArr, commit, newCommit);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -494,27 +492,18 @@ function prettyPrintCommitHistory(commitArr: Commit[]) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export const prettyPrint = function () {
|
export const prettyPrint = function () {
|
||||||
log.debug(commits);
|
log.debug(state.records.commits);
|
||||||
const node = getCommitsArray()[0];
|
const node = getCommitsArray()[0];
|
||||||
prettyPrintCommitHistory([node]);
|
prettyPrintCommitHistory([node]);
|
||||||
};
|
};
|
||||||
|
|
||||||
export const clear = function () {
|
export const clear = function () {
|
||||||
commits = new Map();
|
state.reset();
|
||||||
head = null;
|
|
||||||
const mainBranch = defaultConfig.gitGraph.mainBranchName;
|
|
||||||
const mainBranchOrder = defaultConfig.gitGraph.mainBranchOrder;
|
|
||||||
branches = new Map();
|
|
||||||
branches.set(mainBranch, null);
|
|
||||||
branchesConfig = new Map();
|
|
||||||
branchesConfig.set(mainBranch, { name: mainBranch, order: mainBranchOrder });
|
|
||||||
curBranch = mainBranch;
|
|
||||||
seq = 0;
|
|
||||||
commonClear();
|
commonClear();
|
||||||
};
|
};
|
||||||
|
|
||||||
export const getBranchesAsObjArray = function () {
|
export const getBranchesAsObjArray = function () {
|
||||||
const branchesArray = [...branchesConfig.values()]
|
const branchesArray = [...state.records.branchConfig.values()]
|
||||||
.map((branchConfig, i) => {
|
.map((branchConfig, i) => {
|
||||||
if (branchConfig.order !== null && branchConfig.order !== undefined) {
|
if (branchConfig.order !== null && branchConfig.order !== undefined) {
|
||||||
return branchConfig;
|
return branchConfig;
|
||||||
@@ -531,13 +520,13 @@ export const getBranchesAsObjArray = function () {
|
|||||||
};
|
};
|
||||||
|
|
||||||
export const getBranches = function () {
|
export const getBranches = function () {
|
||||||
return branches;
|
return state.records.branches;
|
||||||
};
|
};
|
||||||
export const getCommits = function () {
|
export const getCommits = function () {
|
||||||
return commits;
|
return state.records.commits;
|
||||||
};
|
};
|
||||||
export const getCommitsArray = function () {
|
export const getCommitsArray = function () {
|
||||||
const commitArr = [...commits.values()];
|
const commitArr = [...state.records.commits.values()];
|
||||||
commitArr.forEach(function (o) {
|
commitArr.forEach(function (o) {
|
||||||
log.debug(o.id);
|
log.debug(o.id);
|
||||||
});
|
});
|
||||||
@@ -545,13 +534,13 @@ export const getCommitsArray = function () {
|
|||||||
return commitArr;
|
return commitArr;
|
||||||
};
|
};
|
||||||
export const getCurrentBranch = function () {
|
export const getCurrentBranch = function () {
|
||||||
return curBranch;
|
return state.records.currBranch;
|
||||||
};
|
};
|
||||||
export const getDirection = function () {
|
export const getDirection = function () {
|
||||||
return direction;
|
return state.records.direction;
|
||||||
};
|
};
|
||||||
export const getHead = function () {
|
export const getHead = function () {
|
||||||
return head;
|
return state.records.head;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const commitType = {
|
export const commitType = {
|
||||||
|
Reference in New Issue
Block a user