mirror of
https://github.com/mermaid-js/mermaid.git
synced 2025-09-13 12:29:42 +02:00
add support for branch ordering
This commit is contained in:
@@ -8,6 +8,8 @@ import common from '../common/common';
|
|||||||
let mainBranchName = getConfig().gitGraph.mainBranchName;
|
let mainBranchName = getConfig().gitGraph.mainBranchName;
|
||||||
let commits = {};
|
let commits = {};
|
||||||
let head = null;
|
let head = null;
|
||||||
|
let branchesConfig = {};
|
||||||
|
branchesConfig[mainBranchName] = { name: mainBranchName, order: 0 };
|
||||||
let branches = {};
|
let branches = {};
|
||||||
branches[mainBranchName] = head;
|
branches[mainBranchName] = head;
|
||||||
let curBranch = mainBranchName;
|
let curBranch = mainBranchName;
|
||||||
@@ -113,10 +115,11 @@ export const commit = function (msg, id, type, tag) {
|
|||||||
log.debug('in pushCommit ' + commit.id);
|
log.debug('in pushCommit ' + commit.id);
|
||||||
};
|
};
|
||||||
|
|
||||||
export const branch = function (name) {
|
export const branch = function (name, order) {
|
||||||
name = common.sanitizeText(name, configApi.getConfig());
|
name = common.sanitizeText(name, configApi.getConfig());
|
||||||
if (typeof branches[name] === 'undefined') {
|
if (typeof branches[name] === 'undefined') {
|
||||||
branches[name] = head != null ? head.id : null;
|
branches[name] = head != null ? head.id : null;
|
||||||
|
branchesConfig[name] = { name, order: order ? parseInt(order, 10) : null };
|
||||||
checkout(name);
|
checkout(name);
|
||||||
log.debug('in createBranch');
|
log.debug('in createBranch');
|
||||||
} else {
|
} else {
|
||||||
@@ -324,17 +327,25 @@ export const clear = function () {
|
|||||||
let mainBranch = getConfig().gitGraph.mainBranchName;
|
let mainBranch = getConfig().gitGraph.mainBranchName;
|
||||||
branches = {};
|
branches = {};
|
||||||
branches[mainBranch] = null;
|
branches[mainBranch] = null;
|
||||||
|
branchesConfig = {};
|
||||||
|
branchesConfig[mainBranch] = { name: mainBranch, order: 0 };
|
||||||
curBranch = mainBranch;
|
curBranch = mainBranch;
|
||||||
seq = 0;
|
seq = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const getBranchesAsObjArray = function () {
|
export const getBranchesAsObjArray = function () {
|
||||||
const branchArr = [];
|
const branchesArray = Object.values(branchesConfig)
|
||||||
for (let branch in branches) {
|
.map((branchConfig, i) => {
|
||||||
// branchArr.push({ name: branch, commit: commits[branches[branch]] });
|
if (branchConfig.order !== null) return branchConfig;
|
||||||
branchArr.push({ name: branch });
|
return {
|
||||||
}
|
...branchConfig,
|
||||||
return branchArr;
|
order: parseFloat(`0.${i}`, 10),
|
||||||
|
};
|
||||||
|
})
|
||||||
|
.sort((a, b) => a.order - b.order)
|
||||||
|
.map(({ name }) => ({ name }));
|
||||||
|
|
||||||
|
return branchesArray;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const getBranches = function () {
|
export const getBranches = function () {
|
||||||
|
@@ -364,6 +364,46 @@ describe('when parsing a gitGraph', function () {
|
|||||||
expect(parser.yy.getDirection()).toBe('LR');
|
expect(parser.yy.getDirection()).toBe('LR');
|
||||||
expect(Object.keys(parser.yy.getBranches()).length).toBe(2);
|
expect(Object.keys(parser.yy.getBranches()).length).toBe(2);
|
||||||
});
|
});
|
||||||
|
it('should handle new branch checkout with order', function () {
|
||||||
|
const str = `gitGraph:
|
||||||
|
commit
|
||||||
|
branch test1 order: 3
|
||||||
|
branch test2 order: 2
|
||||||
|
branch test3 order: 1
|
||||||
|
`;
|
||||||
|
|
||||||
|
parser.parse(str);
|
||||||
|
const commits = parser.yy.getCommits();
|
||||||
|
expect(Object.keys(commits).length).toBe(1);
|
||||||
|
expect(parser.yy.getCurrentBranch()).toBe('test3');
|
||||||
|
expect(Object.keys(parser.yy.getBranches()).length).toBe(4);
|
||||||
|
expect(parser.yy.getBranchesAsObjArray()).toStrictEqual([
|
||||||
|
{ name: 'main' },
|
||||||
|
{ name: 'test3' },
|
||||||
|
{ name: 'test2' },
|
||||||
|
{ name: 'test1' },
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
it('should handle new branch checkout with and without order', function () {
|
||||||
|
const str = `gitGraph:
|
||||||
|
commit
|
||||||
|
branch test1 order: 1
|
||||||
|
branch test2
|
||||||
|
branch test3
|
||||||
|
`;
|
||||||
|
|
||||||
|
parser.parse(str);
|
||||||
|
const commits = parser.yy.getCommits();
|
||||||
|
expect(Object.keys(commits).length).toBe(1);
|
||||||
|
expect(parser.yy.getCurrentBranch()).toBe('test3');
|
||||||
|
expect(Object.keys(parser.yy.getBranches()).length).toBe(4);
|
||||||
|
expect(parser.yy.getBranchesAsObjArray()).toStrictEqual([
|
||||||
|
{ name: 'main' },
|
||||||
|
{ name: 'test2' },
|
||||||
|
{ name: 'test3' },
|
||||||
|
{ name: 'test1' },
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
|
||||||
it('should handle new branch checkout & commit', function () {
|
it('should handle new branch checkout & commit', function () {
|
||||||
const str = `gitGraph:
|
const str = `gitGraph:
|
||||||
|
@@ -36,6 +36,7 @@
|
|||||||
"HIGHLIGHT" return 'HIGHLIGHT';
|
"HIGHLIGHT" return 'HIGHLIGHT';
|
||||||
"tag:" return 'COMMIT_TAG';
|
"tag:" return 'COMMIT_TAG';
|
||||||
"branch" return 'BRANCH';
|
"branch" return 'BRANCH';
|
||||||
|
"order:" return 'ORDER';
|
||||||
"merge" return 'MERGE';
|
"merge" return 'MERGE';
|
||||||
// "reset" return 'RESET';
|
// "reset" return 'RESET';
|
||||||
"checkout" return 'CHECKOUT';
|
"checkout" return 'CHECKOUT';
|
||||||
@@ -49,6 +50,7 @@
|
|||||||
["] this.begin("string");
|
["] this.begin("string");
|
||||||
<string>["] this.popState();
|
<string>["] this.popState();
|
||||||
<string>[^"]* return 'STR';
|
<string>[^"]* return 'STR';
|
||||||
|
[0-9]+ return 'NUM';
|
||||||
[a-zA-Z][-_\./a-zA-Z0-9]*[-_a-zA-Z0-9] return 'ID';
|
[a-zA-Z][-_\./a-zA-Z0-9]*[-_a-zA-Z0-9] return 'ID';
|
||||||
<<EOF>> return 'EOF';
|
<<EOF>> return 'EOF';
|
||||||
|
|
||||||
@@ -90,10 +92,14 @@ line
|
|||||||
statement
|
statement
|
||||||
: commitStatement
|
: commitStatement
|
||||||
| mergeStatement
|
| mergeStatement
|
||||||
| BRANCH ID {yy.branch($2)}
|
| branchStatement
|
||||||
| CHECKOUT ID {yy.checkout($2)}
|
| CHECKOUT ID {yy.checkout($2)}
|
||||||
// | RESET reset_arg {yy.reset($2)}
|
// | RESET reset_arg {yy.reset($2)}
|
||||||
;
|
;
|
||||||
|
branchStatement
|
||||||
|
: BRANCH ID {yy.branch($2)}
|
||||||
|
| BRANCH ID ORDER NUM {yy.branch($2, $4)}
|
||||||
|
;
|
||||||
|
|
||||||
mergeStatement
|
mergeStatement
|
||||||
: MERGE ID {yy.merge($2)}
|
: MERGE ID {yy.merge($2)}
|
||||||
|
Reference in New Issue
Block a user