diff --git a/cypress/integration/rendering/gitGraph.spec.js b/cypress/integration/rendering/gitGraph.spec.js index 283a52c67..52019c430 100644 --- a/cypress/integration/rendering/gitGraph.spec.js +++ b/cypress/integration/rendering/gitGraph.spec.js @@ -74,7 +74,7 @@ describe('Git Graph diagram', () => { {} ); }); - it('7: should render a simple gitgraph with three branches and merge commit', () => { + it('7: should render a simple gitgraph with three branches and tagged merge commit', () => { imgSnapshotTest( `gitGraph commit id: "1" @@ -93,7 +93,7 @@ describe('Git Graph diagram', () => { checkout nice_feature commit id: "7" checkout main - merge nice_feature + merge nice_feature id: "12345" tag: "my merge commit" checkout very_nice_feature commit id: "8" checkout main diff --git a/docs/gitgraph.md b/docs/gitgraph.md index a083fcc2f..53e802101 100644 --- a/docs/gitgraph.md +++ b/docs/gitgraph.md @@ -182,6 +182,8 @@ After this we made use of the `checkout` keyword to set the current branch as `m After this we merge the `develop` branch onto the current branch `main`, resulting in a merge commit. Since the current branch at this point is still `main`, the last two commits are registered against that. +Additionally, you may add a tag to the merge commit, or override the default id: `merge branch id:"1234" tag:"v1.0.0"` + ### Cherry Pick commit from another branch Similar to how 'git' allows you to cherry-pick a commit from **another branch** onto the **current** branch, Mermaid also supports this functionality. You can also cherry-pick a commit from another branch using the `cherry-pick` keyword. diff --git a/src/diagrams/git/gitGraphAst.js b/src/diagrams/git/gitGraphAst.js index eefb84ad5..47c3e9f30 100644 --- a/src/diagrams/git/gitGraphAst.js +++ b/src/diagrams/git/gitGraphAst.js @@ -150,6 +150,8 @@ export const branch = function (name, order) { export const merge = function (otherBranch, custom_id, override_type, custom_tag) { otherBranch = common.sanitizeText(otherBranch, configApi.getConfig()); + custom_id = common.sanitizeText(custom_id, configApi.getConfig()); + const currentCommit = commits[branches[curBranch]]; const otherCommit = commits[branches[otherBranch]]; if (curBranch === otherBranch) { diff --git a/src/diagrams/git/gitGraphParserV2.spec.js b/src/diagrams/git/gitGraphParserV2.spec.js index b57ce1af0..9c8e47443 100644 --- a/src/diagrams/git/gitGraphParserV2.spec.js +++ b/src/diagrams/git/gitGraphParserV2.spec.js @@ -496,6 +496,76 @@ describe('when parsing a gitGraph', function () { ]); }); + it('should handle merge ids', function () { + const str = `gitGraph: + commit + branch testBranch + checkout testBranch + commit + checkout main + %% Merge Tag and ID + merge testBranch tag: "merge-tag" id: "2-222" + branch testBranch2 + checkout testBranch2 + commit + checkout main + %% Merge ID and Tag (reverse order) + merge testBranch2 id: "4-444" tag: "merge-tag2" + branch testBranch3 + checkout testBranch3 + commit + checkout main + %% just Merge ID + merge testBranch3 id: "6-666" + `; + + parser.parse(str); + const commits = parser.yy.getCommits(); + expect(Object.keys(commits).length).toBe(7); + expect(parser.yy.getCurrentBranch()).toBe('main'); + expect(parser.yy.getDirection()).toBe('LR'); + + // The order of these commits is in alphabetical order of IDs + const [ + mainCommit, + testBranchCommit, + testBranchMerge, + testBranch2Commit, + testBranch2Merge, + testBranch3Commit, + testBranch3Merge, + ] = Object.values(commits); + + console.log(Object.keys(commits)); + + expect(mainCommit.branch).toBe('main'); + expect(mainCommit.parents).toStrictEqual([]); + + expect(testBranchCommit.branch).toBe('testBranch'); + expect(testBranchCommit.parents).toStrictEqual([mainCommit.id]); + + expect(testBranchMerge.branch).toBe('main'); + expect(testBranchMerge.parents).toStrictEqual([mainCommit.id, testBranchCommit.id]); + expect(testBranchMerge.tag).toBe('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.id).toBe('4-444'); + + expect(testBranch3Merge.branch).toBe('main'); + expect(testBranch3Merge.parents).toStrictEqual([testBranch2Merge.id, testBranch3Commit.id]); + expect(testBranch3Merge.id).toBe('6-666'); + + expect(parser.yy.getBranchesAsObjArray()).toStrictEqual([ + { name: 'main' }, + { name: 'testBranch' }, + { name: 'testBranch2' }, + { name: 'testBranch3' }, + ]); + }); + it('should throw error when try to branch existing branch: main', function () { const str = `gitGraph commit