From ae920eaa9384a9f11f8aff11ba73968f19e59c27 Mon Sep 17 00:00:00 2001 From: Elliot Nelson Date: Sat, 17 Sep 2022 23:57:32 -0400 Subject: [PATCH] feat(git): cherry-pick keyword supports tag attribute --- .../integration/rendering/gitGraph.spec.js | 23 ++++++++++-- docs/gitgraph.md | 36 +++++++++++++++++++ src/diagrams/git/gitGraphAst.js | 8 +++-- src/diagrams/git/parser/gitGraph.jison | 4 ++- 4 files changed, 65 insertions(+), 6 deletions(-) diff --git a/cypress/integration/rendering/gitGraph.spec.js b/cypress/integration/rendering/gitGraph.spec.js index b0d65d0cc..3331f0d33 100644 --- a/cypress/integration/rendering/gitGraph.spec.js +++ b/cypress/integration/rendering/gitGraph.spec.js @@ -180,7 +180,27 @@ describe('Git Graph diagram', () => { {} ); }); - + it('11: should render a gitgraph with cherry pick commit with custom tag', () => { + imgSnapshotTest( + ` + gitGraph + commit id: "ZERO" + branch develop + commit id:"A" + checkout main + commit id:"ONE" + checkout develop + commit id:"B" + checkout main + commit id:"TWO" + cherry-pick id:"A" tag: "snapshot" + commit id:"THREE" + checkout develop + commit id:"C" + `, + {} + ); + }); it('11: should render a simple gitgraph with two cherry pick commit', () => { imgSnapshotTest( ` @@ -207,7 +227,6 @@ describe('Git Graph diagram', () => { {} ); }); - it('12: should render commits for more than 8 branches', () => { imgSnapshotTest( ` diff --git a/docs/gitgraph.md b/docs/gitgraph.md index 5f86cf53c..bdf2f3a5a 100644 --- a/docs/gitgraph.md +++ b/docs/gitgraph.md @@ -393,6 +393,42 @@ Let see an example: commit id:"C" ``` +By default, the cherry-picked commit from commit with id `A` will be labeled `cherry-pick:A`. You can provide your own custom tag instead to override this behavior, using the same syntax as the `commit` keyword: + +```mermaid-example + gitGraph + commit id: "ZERO" + branch develop + commit id:"A" + checkout main + commit id:"ONE" + checkout develop + commit id:"B" + checkout main + commit id:"TWO" + cherry-pick id:"A" tag:"fix" + commit id:"THREE" + checkout develop + commit id:"C" +``` + +```mermaid + gitGraph + commit id: "ZERO" + branch develop + commit id:"A" + checkout main + commit id:"ONE" + checkout develop + commit id:"B" + checkout main + commit id:"TWO" + cherry-pick id:"A" tag:"fix" + commit id:"THREE" + checkout develop + commit id:"C" +``` + ## Gitgraph specific configuration options In Mermaid, you have the option to configure the gitgraph diagram. You can configure the following options: diff --git a/src/diagrams/git/gitGraphAst.js b/src/diagrams/git/gitGraphAst.js index fb9bb100d..e222a046b 100644 --- a/src/diagrams/git/gitGraphAst.js +++ b/src/diagrams/git/gitGraphAst.js @@ -258,9 +258,11 @@ export const merge = function (otherBranch, custom_id, override_type, custom_tag log.debug('in mergeBranch'); }; -export const cherryPick = function (sourceId, targetId) { +export const cherryPick = function (sourceId, targetId, tag) { + log.debug('Entering cherryPick:', sourceId, targetId, tag); sourceId = common.sanitizeText(sourceId, configApi.getConfig()); targetId = common.sanitizeText(targetId, configApi.getConfig()); + tag = common.sanitizeText(tag, configApi.getConfig()); if (!sourceId || typeof commits[sourceId] === 'undefined') { let error = new Error( @@ -328,13 +330,13 @@ export const cherryPick = function (sourceId, targetId) { parents: [head == null ? null : head.id, sourceCommit.id], branch: curBranch, type: commitType.CHERRY_PICK, - tag: 'cherry-pick:' + sourceCommit.id, + tag: tag ? tag : 'cherry-pick:' + sourceCommit.id, }; head = commit; commits[commit.id] = commit; branches[curBranch] = commit.id; log.debug(branches); - log.debug('in cheeryPick'); + log.debug('in cherryPick'); } }; export const checkout = function (branch) { diff --git a/src/diagrams/git/parser/gitGraph.jison b/src/diagrams/git/parser/gitGraph.jison index f35dbcde3..b3fb805af 100644 --- a/src/diagrams/git/parser/gitGraph.jison +++ b/src/diagrams/git/parser/gitGraph.jison @@ -117,7 +117,9 @@ branchStatement ; cherryPickStatement - : CHERRY_PICK COMMIT_ID STR {yy.cherryPick($3)} + : CHERRY_PICK COMMIT_ID STR {yy.cherryPick($3, '')} + | CHERRY_PICK COMMIT_ID STR COMMIT_TAG STR {yy.cherryPick($3, $4)} + | CHERRY_PICK COMMIT_TAG STR COMMIT_ID STR {yy.cherryPick($4, $3)} ; mergeStatement