From 3f812326a673253ad7f53c99fb7112e38b78d23c Mon Sep 17 00:00:00 2001 From: Yuriy Husnay Date: Tue, 19 Apr 2022 18:02:41 +0300 Subject: [PATCH] gitgraph #2934 support tags for merge commits --- src/diagrams/git/gitGraphAst.js | 3 +- src/diagrams/git/gitGraphParserV2.spec.js | 37 ++++++++++++++++++++++- src/diagrams/git/parser/gitGraph.jison | 8 ++++- 3 files changed, 45 insertions(+), 3 deletions(-) diff --git a/src/diagrams/git/gitGraphAst.js b/src/diagrams/git/gitGraphAst.js index 76bd44b4c..1beb1a89e 100644 --- a/src/diagrams/git/gitGraphAst.js +++ b/src/diagrams/git/gitGraphAst.js @@ -136,7 +136,7 @@ export const branch = function (name) { } }; -export const merge = function (otherBranch) { +export const merge = function (otherBranch, tag) { otherBranch = common.sanitizeText(otherBranch, configApi.getConfig()); const currentCommit = commits[branches[curBranch]]; const otherCommit = commits[branches[otherBranch]]; @@ -213,6 +213,7 @@ export const merge = function (otherBranch) { parents: [head == null ? null : head.id, branches[otherBranch]], branch: curBranch, type: commitType.MERGE, + tag: tag ? tag : '', }; head = commit; commits[commit.id] = commit; diff --git a/src/diagrams/git/gitGraphParserV2.spec.js b/src/diagrams/git/gitGraphParserV2.spec.js index 6ef4e9428..1489e6ca8 100644 --- a/src/diagrams/git/gitGraphParserV2.spec.js +++ b/src/diagrams/git/gitGraphParserV2.spec.js @@ -255,7 +255,7 @@ describe('when parsing a gitGraph', function () { it('should handle a gitGraph commit with custom type,tag, msg, commit id,', function () { const str = `gitGraph: commit type:REVERSE tag: "test tag" msg: "test msg" id: "1111" - + `; parser.parse(str); @@ -411,6 +411,41 @@ describe('when parsing a gitGraph', function () { ]); }); + it('should handle merge tags', function () { + const str = `gitGraph: + commit + branch testBranch + checkout testBranch + commit + checkout main + merge testBranch tag: "merge-tag" + `; + + parser.parse(str); + const commits = parser.yy.getCommits(); + expect(Object.keys(commits).length).toBe(3); + expect(parser.yy.getCurrentBranch()).toBe('main'); + expect(parser.yy.getDirection()).toBe('LR'); + expect(Object.keys(parser.yy.getBranches()).length).toBe(2); + const commit1 = Object.keys(commits)[0]; + const commit2 = Object.keys(commits)[1]; + const commit3 = Object.keys(commits)[2]; + + expect(commits[commit1].branch).toBe('main'); + expect(commits[commit1].parents).toStrictEqual([]); + + expect(commits[commit2].branch).toBe('testBranch'); + expect(commits[commit2].parents).toStrictEqual([commits[commit1].id]); + + expect(commits[commit3].branch).toBe('main'); + expect(commits[commit3].parents).toStrictEqual([commits[commit1].id, commits[commit2].id]); + expect(commits[commit3].tag).toBe('merge-tag'); + expect(parser.yy.getBranchesAsObjArray()).toStrictEqual([ + { name: 'main' }, + { name: 'testBranch' }, + ]); + }); + it('should throw error when try to branch existing branch: main', function () { const str = `gitGraph commit diff --git a/src/diagrams/git/parser/gitGraph.jison b/src/diagrams/git/parser/gitGraph.jison index d67712d12..04254d580 100644 --- a/src/diagrams/git/parser/gitGraph.jison +++ b/src/diagrams/git/parser/gitGraph.jison @@ -89,11 +89,17 @@ line statement : commitStatement + | mergeStatement | BRANCH ID {yy.branch($2)} | CHECKOUT ID {yy.checkout($2)} - | MERGE ID {yy.merge($2)} // | RESET reset_arg {yy.reset($2)} ; + +mergeStatement + : MERGE ID {yy.merge($2)} + | MERGE ID COMMIT_TAG STR {yy.merge($2, $4)} + ; + commitStatement : COMMIT commit_arg {yy.commit($2)} | COMMIT COMMIT_TAG STR {yy.commit('','',yy.commitType.NORMAL,$3)}