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

feat(3801): Multiple Tags support for Git Graph #5647

Merged
merged 10 commits into from
Jul 18, 2024
36 changes: 36 additions & 0 deletions cypress/integration/rendering/gitGraph.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1532,5 +1532,41 @@ gitGraph TB:
{}
);
});
it('75: should render a gitGraph with multiple tags on a merge commit on bottom-to-top orientation', () => {
imgSnapshotTest(
`gitGraph BT:
commit id: "ZERO"
branch develop
commit id:"A"
checkout main
commit id:"ONE"
checkout develop
commit id:"B"
checkout main
merge develop id:"Release 1.0" type:HIGHLIGHT tag: "SAML v2.0" tag: "OpenID v1.1"
commit id:"TWO"
checkout develop
commit id:"C"`,
{}
);
});
});
it('76: should render a gitGraph with multiple tags on a merge commit on left-to-right orientation', () => {
imgSnapshotTest(
`gitGraph
commit id: "ZERO"
branch develop
commit id:"A"
checkout main
commit id:"ONE"
checkout develop
commit id:"B"
checkout main
merge develop id:"Release 1.0" type:HIGHLIGHT tag: "SAML v2.0" tag: "OpenID v1.1"
commit id:"TWO"
checkout develop
commit id:"C"`,
{}
);
});
});
43 changes: 26 additions & 17 deletions packages/mermaid/src/diagrams/git/gitGraphAst.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,17 +103,17 @@ export const getOptions = function () {
return options;
};

export const commit = function (msg, id, type, tag) {
log.debug('Entering commit:', msg, id, type, tag);
export const commit = function (msg, id, type, tags) {
log.debug('Entering commit:', msg, id, type, tags);
id = common.sanitizeText(id, getConfig());
msg = common.sanitizeText(msg, getConfig());
tag = common.sanitizeText(tag, getConfig());
tags = tags?.map((tag) => common.sanitizeText(tag, getConfig()));
const commit = {
id: id ? id : seq + '-' + getId(),
message: msg,
seq: seq++,
type: type ? type : commitType.NORMAL,
tag: tag ? tag : '',
tags: tags ?? [],
parents: head == null ? [] : [head.id],
branch: curBranch,
};
Expand Down Expand Up @@ -147,7 +147,7 @@ export const branch = function (name, order) {
}
};

export const merge = function (otherBranch, custom_id, override_type, custom_tag) {
export const merge = function (otherBranch, custom_id, override_type, custom_tags) {
otherBranch = common.sanitizeText(otherBranch, getConfig());
custom_id = common.sanitizeText(custom_id, getConfig());

Expand Down Expand Up @@ -216,12 +216,19 @@ export const merge = function (otherBranch, custom_id, override_type, custom_tag
' already exists, use different custom Id'
);
error.hash = {
text: 'merge ' + otherBranch + custom_id + override_type + custom_tag,
token: 'merge ' + otherBranch + custom_id + override_type + custom_tag,
text: 'merge ' + otherBranch + custom_id + override_type + custom_tags?.join(','),
token: 'merge ' + otherBranch + custom_id + override_type + custom_tags?.join(','),
line: '1',
loc: { first_line: 1, last_line: 1, first_column: 1, last_column: 1 },
expected: [
'merge ' + otherBranch + ' ' + custom_id + '_UNIQUE ' + override_type + ' ' + custom_tag,
'merge ' +
otherBranch +
' ' +
custom_id +
'_UNIQUE ' +
override_type +
' ' +
custom_tags?.join(','),
],
};

Expand All @@ -245,7 +252,7 @@ export const merge = function (otherBranch, custom_id, override_type, custom_tag
type: commitType.MERGE,
customType: override_type,
customId: custom_id ? true : false,
tag: custom_tag ? custom_tag : '',
tags: custom_tags ? custom_tags : [],
};
head = commit;
commits.set(commit.id, commit);
Expand All @@ -255,11 +262,11 @@ export const merge = function (otherBranch, custom_id, override_type, custom_tag
log.debug('in mergeBranch');
};

export const cherryPick = function (sourceId, targetId, tag, parentCommitId) {
log.debug('Entering cherryPick:', sourceId, targetId, tag);
export const cherryPick = function (sourceId, targetId, tags, parentCommitId) {
log.debug('Entering cherryPick:', sourceId, targetId, tags);
sourceId = common.sanitizeText(sourceId, getConfig());
targetId = common.sanitizeText(targetId, getConfig());
tag = common.sanitizeText(tag, getConfig());
tags = tags?.map((tag) => common.sanitizeText(tag, getConfig()));
parentCommitId = common.sanitizeText(parentCommitId, getConfig());

if (!sourceId || !commits.has(sourceId)) {
Expand Down Expand Up @@ -329,11 +336,13 @@ export const cherryPick = function (sourceId, targetId, tag, parentCommitId) {
parents: [head == null ? null : head.id, sourceCommit.id],
branch: curBranch,
type: commitType.CHERRY_PICK,
tag:
tag ??
`cherry-pick:${sourceCommit.id}${
sourceCommit.type === commitType.MERGE ? `|parent:${parentCommitId}` : ''
}`,
tags: tags
? tags.filter(Boolean)
: [
`cherry-pick:${sourceCommit.id}${
sourceCommit.type === commitType.MERGE ? `|parent:${parentCommitId}` : ''
}`,
],
};
head = commit;
commits.set(commit.id, commit);
Expand Down
54 changes: 27 additions & 27 deletions packages/mermaid/src/diagrams/git/gitGraphParserV2.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ describe('when parsing a gitGraph', function () {
const key = commits.keys().next().value;
expect(commits.get(key).message).toBe('');
expect(commits.get(key).id).not.toBeNull();
expect(commits.get(key).tag).toBe('');
expect(commits.get(key).tags).toStrictEqual([]);
expect(commits.get(key).type).toBe(0);
});

Expand All @@ -37,7 +37,7 @@ describe('when parsing a gitGraph', function () {
const key = commits.keys().next().value;
expect(commits.get(key).message).toBe('');
expect(commits.get(key).id).toBe('1111');
expect(commits.get(key).tag).toBe('');
expect(commits.get(key).tags).toStrictEqual([]);
expect(commits.get(key).type).toBe(0);
});

Expand All @@ -55,7 +55,7 @@ describe('when parsing a gitGraph', function () {
const key = commits.keys().next().value;
expect(commits.get(key).message).toBe('');
expect(commits.get(key).id).not.toBeNull();
expect(commits.get(key).tag).toBe('test');
expect(commits.get(key).tags).toStrictEqual(['test']);
expect(commits.get(key).type).toBe(0);
});

Expand All @@ -73,7 +73,7 @@ describe('when parsing a gitGraph', function () {
const key = commits.keys().next().value;
expect(commits.get(key).message).toBe('');
expect(commits.get(key).id).not.toBeNull();
expect(commits.get(key).tag).toBe('');
expect(commits.get(key).tags).toStrictEqual([]);
expect(commits.get(key).type).toBe(2);
});

Expand All @@ -91,7 +91,7 @@ describe('when parsing a gitGraph', function () {
const key = commits.keys().next().value;
expect(commits.get(key).message).toBe('');
expect(commits.get(key).id).not.toBeNull();
expect(commits.get(key).tag).toBe('');
expect(commits.get(key).tags).toStrictEqual([]);
expect(commits.get(key).type).toBe(1);
});

Expand All @@ -109,7 +109,7 @@ describe('when parsing a gitGraph', function () {
const key = commits.keys().next().value;
expect(commits.get(key).message).toBe('');
expect(commits.get(key).id).not.toBeNull();
expect(commits.get(key).tag).toBe('');
expect(commits.get(key).tags).toStrictEqual([]);
expect(commits.get(key).type).toBe(0);
});

Expand All @@ -127,7 +127,7 @@ describe('when parsing a gitGraph', function () {
const key = commits.keys().next().value;
expect(commits.get(key).message).toBe('test commit');
expect(commits.get(key).id).not.toBeNull();
expect(commits.get(key).tag).toBe('');
expect(commits.get(key).tags).toStrictEqual([]);
expect(commits.get(key).type).toBe(0);
});

Expand All @@ -145,7 +145,7 @@ describe('when parsing a gitGraph', function () {
const key = commits.keys().next().value;
expect(commits.get(key).message).toBe('test commit');
expect(commits.get(key).id).not.toBeNull();
expect(commits.get(key).tag).toBe('');
expect(commits.get(key).tags).toStrictEqual([]);
expect(commits.get(key).type).toBe(0);
});

Expand All @@ -163,7 +163,7 @@ describe('when parsing a gitGraph', function () {
const key = commits.keys().next().value;
expect(commits.get(key).message).toBe('');
expect(commits.get(key).id).toBe('1111');
expect(commits.get(key).tag).toBe('test tag');
expect(commits.get(key).tags).toStrictEqual(['test tag']);
expect(commits.get(key).type).toBe(0);
});

Expand All @@ -181,7 +181,7 @@ describe('when parsing a gitGraph', function () {
const key = commits.keys().next().value;
expect(commits.get(key).message).toBe('');
expect(commits.get(key).id).not.toBeNull();
expect(commits.get(key).tag).toBe('test tag');
expect(commits.get(key).tags).toStrictEqual(['test tag']);
expect(commits.get(key).type).toBe(2);
});

Expand All @@ -199,7 +199,7 @@ describe('when parsing a gitGraph', function () {
const key = commits.keys().next().value;
expect(commits.get(key).message).toBe('');
expect(commits.get(key).id).not.toBeNull();
expect(commits.get(key).tag).toBe('test tag');
expect(commits.get(key).tags).toStrictEqual(['test tag']);
expect(commits.get(key).type).toBe(2);
});

Expand All @@ -217,7 +217,7 @@ describe('when parsing a gitGraph', function () {
const key = commits.keys().next().value;
expect(commits.get(key).message).toBe('');
expect(commits.get(key).id).toBe('1111');
expect(commits.get(key).tag).toBe('test tag');
expect(commits.get(key).tags).toStrictEqual(['test tag']);
expect(commits.get(key).type).toBe(1);
});

Expand All @@ -235,7 +235,7 @@ describe('when parsing a gitGraph', function () {
const key = commits.keys().next().value;
expect(commits.get(key).message).toBe('test msg');
expect(commits.get(key).id).toBe('1111');
expect(commits.get(key).tag).toBe('test tag');
expect(commits.get(key).tags).toStrictEqual(['test tag']);
expect(commits.get(key).type).toBe(1);
});

Expand All @@ -254,7 +254,7 @@ describe('when parsing a gitGraph', function () {
const key = commits.keys().next().value;
expect(commits.get(key).message).toBe('test msg');
expect(commits.get(key).id).toBe('1111');
expect(commits.get(key).tag).toBe('test tag');
expect(commits.get(key).tags).toStrictEqual(['test tag']);
expect(commits.get(key).type).toBe(1);
});

Expand All @@ -272,7 +272,7 @@ describe('when parsing a gitGraph', function () {
const key = commits.keys().next().value;
expect(commits.get(key).message).toBe('test msg');
expect(commits.get(key).id).toBe('1111');
expect(commits.get(key).tag).toBe('test tag');
expect(commits.get(key).tags).toStrictEqual(['test tag']);
expect(commits.get(key).type).toBe(1);
});

Expand All @@ -290,7 +290,7 @@ describe('when parsing a gitGraph', function () {
const key = commits.keys().next().value;
expect(commits.get(key).message).toBe('test msg');
expect(commits.get(key).id).toBe('1111');
expect(commits.get(key).tag).toBe('test tag');
expect(commits.get(key).tags).toStrictEqual(['test tag']);
expect(commits.get(key).type).toBe(1);
});

Expand Down Expand Up @@ -616,7 +616,7 @@ describe('when parsing a gitGraph', function () {
commits.get(commit1).id,
commits.get(commit2).id,
]);
expect(commits.get(commit3).tag).toBe('merge-tag');
expect(commits.get(commit3).tags).toStrictEqual(['merge-tag']);
expect(parser.yy.getBranchesAsObjArray()).toStrictEqual([
{ name: 'main' },
{ name: 'testBranch' },
Expand Down Expand Up @@ -671,12 +671,12 @@ describe('when parsing a gitGraph', function () {

expect(testBranchMerge.branch).toBe('main');
expect(testBranchMerge.parents).toStrictEqual([mainCommit.id, testBranchCommit.id]);
expect(testBranchMerge.tag).toBe('merge-tag');
expect(testBranchMerge.tags).toStrictEqual(['merge-tag']);
expect(testBranchMerge.id).toBe('2-222');

expect(testBranch2Merge.branch).toBe('main');
expect(testBranch2Merge.parents).toStrictEqual([testBranchMerge.id, testBranch2Commit.id]);
expect(testBranch2Merge.tag).toBe('merge-tag2');
expect(testBranch2Merge.tags).toStrictEqual(['merge-tag2']);
expect(testBranch2Merge.id).toBe('4-444');
expect(testBranch2Merge.customType).toBe(2);
expect(testBranch2Merge.customId).toBe(true);
Expand Down Expand Up @@ -705,7 +705,7 @@ describe('when parsing a gitGraph', function () {
parser.parse(str);
const commits = parser.yy.getCommits();
const cherryPickCommitID = [...commits.keys()][2];
expect(commits.get(cherryPickCommitID).tag).toBe('cherry-pick:A');
expect(commits.get(cherryPickCommitID).tags).toStrictEqual(['cherry-pick:A']);
expect(commits.get(cherryPickCommitID).branch).toBe('main');
});

Expand All @@ -721,7 +721,7 @@ describe('when parsing a gitGraph', function () {
parser.parse(str);
const commits = parser.yy.getCommits();
const cherryPickCommitID = [...commits.keys()][2];
expect(commits.get(cherryPickCommitID).tag).toBe('MyTag');
expect(commits.get(cherryPickCommitID).tags).toStrictEqual(['MyTag']);
expect(commits.get(cherryPickCommitID).branch).toBe('main');
});

Expand All @@ -737,7 +737,7 @@ describe('when parsing a gitGraph', function () {
parser.parse(str);
const commits = parser.yy.getCommits();
const cherryPickCommitID = [...commits.keys()][2];
expect(commits.get(cherryPickCommitID).tag).toBe('');
expect(commits.get(cherryPickCommitID).tags).toStrictEqual([]);
expect(commits.get(cherryPickCommitID).branch).toBe('main');
});

Expand All @@ -758,7 +758,7 @@ describe('when parsing a gitGraph', function () {
parser.parse(str);
const commits = parser.yy.getCommits();
const cherryPickCommitID = [...commits.keys()][4];
expect(commits.get(cherryPickCommitID).tag).toBe('cherry-pick:M|parent:B');
expect(commits.get(cherryPickCommitID).tags).toStrictEqual(['cherry-pick:M|parent:B']);
expect(commits.get(cherryPickCommitID).branch).toBe('release');
});

Expand All @@ -779,7 +779,7 @@ describe('when parsing a gitGraph', function () {
parser.parse(str);
const commits = parser.yy.getCommits();
const cherryPickCommitID = [...commits.keys()][4];
expect(commits.get(cherryPickCommitID).tag).toBe('v1.0');
expect(commits.get(cherryPickCommitID).tags).toStrictEqual(['v1.0']);
expect(commits.get(cherryPickCommitID).branch).toBe('release');
});

Expand All @@ -802,7 +802,7 @@ describe('when parsing a gitGraph', function () {
parser.parse(str);
const commits = parser.yy.getCommits();
const cherryPickCommitID = [...commits.keys()][5];
expect(commits.get(cherryPickCommitID).tag).toBe('v2.1:ZERO');
expect(commits.get(cherryPickCommitID).tags).toStrictEqual(['v2.1:ZERO']);
expect(commits.get(cherryPickCommitID).branch).toBe('release');
});

Expand All @@ -827,8 +827,8 @@ describe('when parsing a gitGraph', function () {
const commits = parser.yy.getCommits();
const cherryPickCommitID = [...commits.keys()][5];
const cherryPickCommitID2 = [...commits.keys()][7];
expect(commits.get(cherryPickCommitID).tag).toBe('');
expect(commits.get(cherryPickCommitID2).tag).toBe('');
expect(commits.get(cherryPickCommitID).tags).toStrictEqual([]);
expect(commits.get(cherryPickCommitID2).tags).toStrictEqual([]);
expect(commits.get(cherryPickCommitID).branch).toBe('release');
});

Expand Down
Loading
Loading