From 13d85b6ee5d3569b4e2d675c04ba7d7e8c4a8543 Mon Sep 17 00:00:00 2001 From: RounakJoshi09 Date: Thu, 12 Oct 2023 02:35:43 +0530 Subject: [PATCH 01/14] Parser and Logic For Parent Commit Added --- .../mermaid/src/diagrams/git/gitGraphAst.js | 46 +++++++++++++------ .../src/diagrams/git/parser/gitGraph.jison | 10 +++- 2 files changed, 40 insertions(+), 16 deletions(-) diff --git a/packages/mermaid/src/diagrams/git/gitGraphAst.js b/packages/mermaid/src/diagrams/git/gitGraphAst.js index abad68b22b..15e8a3e381 100644 --- a/packages/mermaid/src/diagrams/git/gitGraphAst.js +++ b/packages/mermaid/src/diagrams/git/gitGraphAst.js @@ -256,7 +256,7 @@ export const merge = function (otherBranch, custom_id, override_type, custom_tag log.debug('in mergeBranch'); }; -export const cherryPick = function (sourceId, targetId, tag) { +export const cherryPick = function (sourceId, targetId, tag, parentCommit) { log.debug('Entering cherryPick:', sourceId, targetId, tag); sourceId = common.sanitizeText(sourceId, configApi.getConfig()); targetId = common.sanitizeText(targetId, configApi.getConfig()); @@ -275,21 +275,35 @@ export const cherryPick = function (sourceId, targetId, tag) { }; throw error; } - let sourceCommit = commits[sourceId]; let sourceCommitBranch = sourceCommit.branch; if (sourceCommit.type === commitType.MERGE) { - let error = new Error( - 'Incorrect usage of "cherryPick". Source commit should not be a merge commit' - ); - error.hash = { - text: 'cherryPick ' + sourceId + ' ' + targetId, - token: 'cherryPick ' + sourceId + ' ' + targetId, - line: '1', - loc: { first_line: 1, last_line: 1, first_column: 1, last_column: 1 }, - expected: ['cherry-pick abc'], - }; - throw error; + if (!parentCommit) { + let error = new Error( + 'Incorrect usage of cherryPick: If the source commit is a merge commit, an immediate parent commit must be specified.' + ); + error.hash = { + text: 'cherryPick ' + sourceId + ' ' + targetId, + token: 'cherryPick ' + sourceId + ' ' + targetId, + line: '1', + loc: { first_line: 1, last_line: 1, first_column: 1, last_column: 1 }, + expected: ['cherry-pick abc'], + }; + throw error; + } + if (!(Array.isArray(sourceCommit.parents) && sourceCommit.parents.includes(parentCommit))) { + let error = new Error( + 'Invalid operation: The specified parent commit is not an immediate parent of the cherry-picked commit.' + ); + error.hash = { + text: 'cherryPick ' + sourceId + ' ' + targetId, + token: 'cherryPick ' + sourceId + ' ' + targetId, + line: '1', + loc: { first_line: 1, last_line: 1, first_column: 1, last_column: 1 }, + expected: ['cherry-pick abc'], + }; + throw error; + } } if (!targetId || commits[targetId] === undefined) { // cherry-pick source commit to current branch @@ -328,7 +342,11 @@ export const cherryPick = function (sourceId, targetId, tag) { parents: [head == null ? null : head.id, sourceCommit.id], branch: curBranch, type: commitType.CHERRY_PICK, - tag: tag ?? 'cherry-pick:' + sourceCommit.id, + tag: tag + ? `${tag}${sourceCommit.type === commitType.MERGE ? ` | parent: ${parentCommit}` : ''}` + : `cherry-pick: ${sourceCommit.id}${ + sourceCommit.type === commitType.MERGE ? ` | parent: ${parentCommit}` : '' + }`, }; head = commit; commits[commit.id] = commit; diff --git a/packages/mermaid/src/diagrams/git/parser/gitGraph.jison b/packages/mermaid/src/diagrams/git/parser/gitGraph.jison index 2297db17cd..1e4ca026ed 100644 --- a/packages/mermaid/src/diagrams/git/parser/gitGraph.jison +++ b/packages/mermaid/src/diagrams/git/parser/gitGraph.jison @@ -39,6 +39,7 @@ branch(?=\s|$) return 'BRANCH'; "order:" return 'ORDER'; merge(?=\s|$) return 'MERGE'; cherry\-pick(?=\s|$) return 'CHERRY_PICK'; +"parent:" return 'PARENT_COMMIT' // "reset" return 'RESET'; checkout(?=\s|$) return 'CHECKOUT'; "LR" return 'DIR'; @@ -109,10 +110,15 @@ branchStatement cherryPickStatement : CHERRY_PICK COMMIT_ID STR {yy.cherryPick($3, '', undefined)} + | CHERRY_PICK COMMIT_ID STR PARENT_COMMIT STR {yy.cherryPick($3, '', undefined,$5)} | CHERRY_PICK COMMIT_ID STR COMMIT_TAG STR {yy.cherryPick($3, '', $5)} + | CHERRY_PICK COMMIT_ID STR PARENT_COMMIT STR COMMIT_TAG STR {yy.cherryPick($3, '', $7,$5)} + | CHERRY_PICK COMMIT_ID STR COMMIT_TAG STR PARENT_COMMIT STR {yy.cherryPick($3, '', $5,$7)} | CHERRY_PICK COMMIT_ID STR COMMIT_TAG EMPTYSTR {yy.cherryPick($3, '', '')} - | CHERRY_PICK COMMIT_TAG STR COMMIT_ID STR {yy.cherryPick($5, '', $3)} - | CHERRY_PICK COMMIT_TAG EMPTYSTR COMMIT_ID STR {yy.cherryPick($3, '', '')} + | CHERRY_PICK COMMIT_ID STR PARENT_COMMIT STR COMMIT_TAG EMPTYSTR {yy.cherryPick($3, '', '',$5)} + | CHERRY_PICK COMMIT_ID STR COMMIT_TAG EMPTYSTR PARENT_COMMIT STR {yy.cherryPick($3, '', '',$7)} + | CHERRY_PICK COMMIT_TAG STR COMMIT_ID STR PARENT_COMMIT STR {yy.cherryPick($5, '', $3,$7)} + | CHERRY_PICK COMMIT_TAG EMPTYSTR COMMIT_ID STR PARENT_COMMIT STR{yy.cherryPick($5, '', '',$7)} ; mergeStatement From 3118c7c532a1128f495a527f4d88b34f9abd2a86 Mon Sep 17 00:00:00 2001 From: RounakJoshi09 Date: Fri, 13 Oct 2023 23:49:01 +0530 Subject: [PATCH 02/14] Unit Test Cases Added --- .../mermaid/src/diagrams/git/gitGraphAst.js | 19 +-- .../src/diagrams/git/gitGraphParserV2.spec.js | 139 ++++++++++++++++++ 2 files changed, 149 insertions(+), 9 deletions(-) diff --git a/packages/mermaid/src/diagrams/git/gitGraphAst.js b/packages/mermaid/src/diagrams/git/gitGraphAst.js index 15e8a3e381..2efb263a90 100644 --- a/packages/mermaid/src/diagrams/git/gitGraphAst.js +++ b/packages/mermaid/src/diagrams/git/gitGraphAst.js @@ -256,11 +256,12 @@ export const merge = function (otherBranch, custom_id, override_type, custom_tag log.debug('in mergeBranch'); }; -export const cherryPick = function (sourceId, targetId, tag, parentCommit) { +export const cherryPick = function (sourceId, targetId, tag, parentCommitId) { 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()); + parentCommitId = common.sanitizeText(parentCommitId, configApi.getConfig()); if (!sourceId || commits[sourceId] === undefined) { let error = new Error( @@ -278,9 +279,9 @@ export const cherryPick = function (sourceId, targetId, tag, parentCommit) { let sourceCommit = commits[sourceId]; let sourceCommitBranch = sourceCommit.branch; if (sourceCommit.type === commitType.MERGE) { - if (!parentCommit) { + if (!parentCommitId) { let error = new Error( - 'Incorrect usage of cherryPick: If the source commit is a merge commit, an immediate parent commit must be specified.' + 'Incorrect usage of cherry-pick: If the source commit is a merge commit, an immediate parent commit must be specified.' ); error.hash = { text: 'cherryPick ' + sourceId + ' ' + targetId, @@ -291,7 +292,7 @@ export const cherryPick = function (sourceId, targetId, tag, parentCommit) { }; throw error; } - if (!(Array.isArray(sourceCommit.parents) && sourceCommit.parents.includes(parentCommit))) { + if (!(Array.isArray(sourceCommit.parents) && sourceCommit.parents.includes(parentCommitId))) { let error = new Error( 'Invalid operation: The specified parent commit is not an immediate parent of the cherry-picked commit.' ); @@ -342,11 +343,11 @@ export const cherryPick = function (sourceId, targetId, tag, parentCommit) { parents: [head == null ? null : head.id, sourceCommit.id], branch: curBranch, type: commitType.CHERRY_PICK, - tag: tag - ? `${tag}${sourceCommit.type === commitType.MERGE ? ` | parent: ${parentCommit}` : ''}` - : `cherry-pick: ${sourceCommit.id}${ - sourceCommit.type === commitType.MERGE ? ` | parent: ${parentCommit}` : '' - }`, + tag: + tag ?? + `cherry-pick: ${sourceCommit.id}${ + sourceCommit.type === commitType.MERGE && ` | parent: ${parentCommitId}` + }`, }; head = commit; commits[commit.id] = commit; diff --git a/packages/mermaid/src/diagrams/git/gitGraphParserV2.spec.js b/packages/mermaid/src/diagrams/git/gitGraphParserV2.spec.js index df20a5eb5a..f9abec4b25 100644 --- a/packages/mermaid/src/diagrams/git/gitGraphParserV2.spec.js +++ b/packages/mermaid/src/diagrams/git/gitGraphParserV2.spec.js @@ -673,6 +673,145 @@ describe('when parsing a gitGraph', function () { expect(commits[cherryPickCommitID].branch).toBe('main'); }); + it('should support cherry-picking of merge commits', function () { + const str = `gitGraph + commit id: "ZERO" + branch feature + branch release + checkout feature + commit id: "A" + commit id: "B" + checkout main + merge feature id: "M" + checkout release + cherry-pick id: "M" parent:"B" + `; + + parser.parse(str); + const commits = parser.yy.getCommits(); + const cherryPickCommitID = Object.keys(commits)[4]; + expect(commits[cherryPickCommitID].tag).toBe('cherry-pick: M | parent: B'); + expect(commits[cherryPickCommitID].branch).toBe('release'); + }); + + it('should support cherry-picking of merge commits with tag', function () { + const str = `gitGraph + commit id: "ZERO" + branch feature + branch release + checkout feature + commit id: "A" + commit id: "B" + checkout main + merge feature id: "M" + checkout release + cherry-pick id: "M" parent:"ZERO" tag: "v1.0" + `; + + parser.parse(str); + const commits = parser.yy.getCommits(); + const cherryPickCommitID = Object.keys(commits)[4]; + expect(commits[cherryPickCommitID].tag).toBe('v1.0'); + expect(commits[cherryPickCommitID].branch).toBe('release'); + }); + + it('should support cherry-picking of merge commits with additional commit', function () { + const str = `gitGraph + commit id: "ZERO" + branch feature + branch release + checkout feature + commit id: "A" + commit id: "B" + checkout main + merge feature id: "M" + checkout release + commit id: "C" + cherry-pick id: "M" tag: "v2.1:ZERO" parent:"ZERO" + commit id: "D" + `; + + parser.parse(str); + const commits = parser.yy.getCommits(); + const cherryPickCommitID = Object.keys(commits)[5]; + expect(commits[cherryPickCommitID].tag).toBe('v2.1:ZERO'); + expect(commits[cherryPickCommitID].branch).toBe('release'); + }); + + it('should support cherry-picking of merge commits with empty tag', function () { + const str = `gitGraph + commit id: "ZERO" + branch feature + branch release + checkout feature + commit id: "A" + commit id: "B" + checkout main + merge feature id: "M" + checkout release + commit id: "C" + cherry-pick id:"M" parent: "ZERO" tag:"" + commit id: "D" + cherry-pick id:"M" tag:"" parent: "B" + `; + + parser.parse(str); + const commits = parser.yy.getCommits(); + const cherryPickCommitID = Object.keys(commits)[5]; + const cherryPickCommitID2 = Object.keys(commits)[7]; + expect(commits[cherryPickCommitID].tag).toBe(''); + expect(commits[cherryPickCommitID2].tag).toBe(''); + expect(commits[cherryPickCommitID].branch).toBe('release'); + }); + + it('should fail cherry-picking of merge commits if the parent of merge commits is not specified', function () { + expect(() => + parser + .parse( + `gitGraph + commit id: "ZERO" + branch feature + branch release + checkout feature + commit id: "A" + commit id: "B" + checkout main + merge feature id: "M" + checkout release + commit id: "C" + cherry-pick id:"M" + ` + ) + .toThrow( + 'Incorrect usage of cherry-pick: If the source commit is a merge commit, an immediate parent commit must be specified.' + ) + ); + }); + + it('should fail cherry-picking of merge commits when the parent provided is not an immediate parent of cherry picked commit', function () { + expect(() => + parser + .parse( + `gitGraph + commit id: "ZERO" + branch feature + branch release + checkout feature + commit id: "A" + commit id: "B" + checkout main + merge feature id: "M" + checkout release + commit id: "C" + cherry-pick id:"M" parent: "A" + ` + ) + .toThrow( + 'Invalid operation: The specified parent commit is not an immediate parent of the cherry-picked commit.' + ) + ); + }); + it('should throw error when try to branch existing branch: main', function () { const str = `gitGraph commit From 6e5cd2b3c2336835a6a896b80d9bbb2ef10ecfa4 Mon Sep 17 00:00:00 2001 From: RounakJoshi09 Date: Sat, 14 Oct 2023 05:49:51 +0530 Subject: [PATCH 03/14] All Unit Tests Passing --- packages/mermaid/src/diagrams/git/gitGraphAst.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/mermaid/src/diagrams/git/gitGraphAst.js b/packages/mermaid/src/diagrams/git/gitGraphAst.js index 2efb263a90..f93ffc44dd 100644 --- a/packages/mermaid/src/diagrams/git/gitGraphAst.js +++ b/packages/mermaid/src/diagrams/git/gitGraphAst.js @@ -346,7 +346,7 @@ export const cherryPick = function (sourceId, targetId, tag, parentCommitId) { tag: tag ?? `cherry-pick: ${sourceCommit.id}${ - sourceCommit.type === commitType.MERGE && ` | parent: ${parentCommitId}` + sourceCommit.type === commitType.MERGE ? ` | parent: ${parentCommitId}` : '' }`, }; head = commit; From 4051b42b5a74083d750ee43b38ec7bdc77f2f1fa Mon Sep 17 00:00:00 2001 From: RounakJoshi09 Date: Sat, 14 Oct 2023 07:26:23 +0530 Subject: [PATCH 04/14] documentation added, Tests Fixed --- docs/syntax/gitgraph.md | 60 +++++++++++-------- .../mermaid/src/diagrams/git/gitGraphAst.js | 4 +- .../src/diagrams/git/gitGraphParserV2.spec.js | 2 +- packages/mermaid/src/docs/syntax/gitgraph.md | 31 ++++++---- 4 files changed, 55 insertions(+), 42 deletions(-) diff --git a/docs/syntax/gitgraph.md b/docs/syntax/gitgraph.md index 8d39ddbcbd..c6a397a982 100644 --- a/docs/syntax/gitgraph.md +++ b/docs/syntax/gitgraph.md @@ -366,41 +366,49 @@ A few important rules to note here are: 1. You need to provide the `id` for an existing commit to be cherry-picked. If given commit id does not exist it will result in an error. For this, make use of the `commit id:$value` format of declaring commits. See the examples from above. 2. The given commit must not exist on the current branch. The cherry-picked commit must always be a different branch than the current branch. 3. Current branch must have at least one commit, before you can cherry-pick, otherwise it will cause an error is throw. +4. When cherry-picking a merge commit, providing a parent commit ID is mandatory. If the parent attribute is omitted or an invalid parent commit ID is provided, an error will be thrown. +5. The specified parent commit must be an immediate parent of the merge commit being cherry-picked. Let see an example: ```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" - commit id:"THREE" - checkout develop - commit id:"C" + commit id: "ZERO" + branch develop + branch release + commit id:"A" + checkout main + commit id:"ONE" + checkout develop + commit id:"B" + checkout main + merge develop id:"MERGE" + commit id:"TWO" + checkout release + cherry-pick id:"MERGE" parent:"B" + 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" - commit id:"THREE" - checkout develop - commit id:"C" + commit id: "ZERO" + branch develop + branch release + commit id:"A" + checkout main + commit id:"ONE" + checkout develop + commit id:"B" + checkout main + merge develop id:"MERGE" + commit id:"TWO" + checkout release + cherry-pick id:"MERGE" parent:"B" + commit id:"THREE" + checkout develop + commit id:"C" ``` ## Gitgraph specific configuration options diff --git a/packages/mermaid/src/diagrams/git/gitGraphAst.js b/packages/mermaid/src/diagrams/git/gitGraphAst.js index f93ffc44dd..34cf91b51f 100644 --- a/packages/mermaid/src/diagrams/git/gitGraphAst.js +++ b/packages/mermaid/src/diagrams/git/gitGraphAst.js @@ -345,8 +345,8 @@ export const cherryPick = function (sourceId, targetId, tag, parentCommitId) { type: commitType.CHERRY_PICK, tag: tag ?? - `cherry-pick: ${sourceCommit.id}${ - sourceCommit.type === commitType.MERGE ? ` | parent: ${parentCommitId}` : '' + `cherry-pick:${sourceCommit.id}${ + sourceCommit.type === commitType.MERGE ? `|parent:${parentCommitId}` : '' }`, }; head = commit; diff --git a/packages/mermaid/src/diagrams/git/gitGraphParserV2.spec.js b/packages/mermaid/src/diagrams/git/gitGraphParserV2.spec.js index f9abec4b25..3b56ee2542 100644 --- a/packages/mermaid/src/diagrams/git/gitGraphParserV2.spec.js +++ b/packages/mermaid/src/diagrams/git/gitGraphParserV2.spec.js @@ -690,7 +690,7 @@ describe('when parsing a gitGraph', function () { parser.parse(str); const commits = parser.yy.getCommits(); const cherryPickCommitID = Object.keys(commits)[4]; - expect(commits[cherryPickCommitID].tag).toBe('cherry-pick: M | parent: B'); + expect(commits[cherryPickCommitID].tag).toBe('cherry-pick:M|parent:B'); expect(commits[cherryPickCommitID].branch).toBe('release'); }); diff --git a/packages/mermaid/src/docs/syntax/gitgraph.md b/packages/mermaid/src/docs/syntax/gitgraph.md index 5fa09cb225..3f7e42d436 100644 --- a/packages/mermaid/src/docs/syntax/gitgraph.md +++ b/packages/mermaid/src/docs/syntax/gitgraph.md @@ -244,24 +244,29 @@ A few important rules to note here are: 1. You need to provide the `id` for an existing commit to be cherry-picked. If given commit id does not exist it will result in an error. For this, make use of the `commit id:$value` format of declaring commits. See the examples from above. 2. The given commit must not exist on the current branch. The cherry-picked commit must always be a different branch than the current branch. 3. Current branch must have at least one commit, before you can cherry-pick, otherwise it will cause an error is throw. +4. When cherry-picking a merge commit, providing a parent commit ID is mandatory. If the parent attribute is omitted or an invalid parent commit ID is provided, an error will be thrown. +5. The specified parent commit must be an immediate parent of the merge commit being cherry-picked. Let see an example: ```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" - commit id:"THREE" - checkout develop - commit id:"C" + commit id: "ZERO" + branch develop + branch release + commit id:"A" + checkout main + commit id:"ONE" + checkout develop + commit id:"B" + checkout main + merge develop id:"MERGE" + commit id:"TWO" + checkout release + cherry-pick id:"MERGE" parent:"B" + commit id:"THREE" + checkout develop + commit id:"C" ``` ## Gitgraph specific configuration options From d8500f9e0833062467c39ed52b7763c5fc346a06 Mon Sep 17 00:00:00 2001 From: RounakJoshi09 Date: Wed, 18 Oct 2023 20:59:55 +0530 Subject: [PATCH 05/14] Suggested Changes FOR PR DONE --- packages/mermaid/src/diagrams/git/gitGraphAst.js | 10 ++++------ .../mermaid/src/diagrams/git/parser/gitGraph.jison | 2 ++ 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/packages/mermaid/src/diagrams/git/gitGraphAst.js b/packages/mermaid/src/diagrams/git/gitGraphAst.js index 34cf91b51f..fde3d9af95 100644 --- a/packages/mermaid/src/diagrams/git/gitGraphAst.js +++ b/packages/mermaid/src/diagrams/git/gitGraphAst.js @@ -284,11 +284,10 @@ export const cherryPick = function (sourceId, targetId, tag, parentCommitId) { 'Incorrect usage of cherry-pick: If the source commit is a merge commit, an immediate parent commit must be specified.' ); error.hash = { - text: 'cherryPick ' + sourceId + ' ' + targetId, - token: 'cherryPick ' + sourceId + ' ' + targetId, + text: `cherryPick ${sourceId} ${targetId}`, + token: `cherryPick ${sourceId} ${targetId}`, line: '1', loc: { first_line: 1, last_line: 1, first_column: 1, last_column: 1 }, - expected: ['cherry-pick abc'], }; throw error; } @@ -297,11 +296,10 @@ export const cherryPick = function (sourceId, targetId, tag, parentCommitId) { 'Invalid operation: The specified parent commit is not an immediate parent of the cherry-picked commit.' ); error.hash = { - text: 'cherryPick ' + sourceId + ' ' + targetId, - token: 'cherryPick ' + sourceId + ' ' + targetId, + text: `cherryPick ${sourceId} ${targetId}`, + token: `cherryPick ${sourceId} ${targetId}`, line: '1', loc: { first_line: 1, last_line: 1, first_column: 1, last_column: 1 }, - expected: ['cherry-pick abc'], }; throw error; } diff --git a/packages/mermaid/src/diagrams/git/parser/gitGraph.jison b/packages/mermaid/src/diagrams/git/parser/gitGraph.jison index 1e4ca026ed..40fab8b590 100644 --- a/packages/mermaid/src/diagrams/git/parser/gitGraph.jison +++ b/packages/mermaid/src/diagrams/git/parser/gitGraph.jison @@ -114,6 +114,8 @@ cherryPickStatement | CHERRY_PICK COMMIT_ID STR COMMIT_TAG STR {yy.cherryPick($3, '', $5)} | CHERRY_PICK COMMIT_ID STR PARENT_COMMIT STR COMMIT_TAG STR {yy.cherryPick($3, '', $7,$5)} | CHERRY_PICK COMMIT_ID STR COMMIT_TAG STR PARENT_COMMIT STR {yy.cherryPick($3, '', $5,$7)} + | CHERRY_PICK COMMIT_TAG STR COMMIT_ID STR {yy.cherryPick($5, '', $3)} + | CHERRY_PICK COMMIT_TAG EMPTYSTR COMMIT_ID STR {yy.cherryPick($5, '', '')} | CHERRY_PICK COMMIT_ID STR COMMIT_TAG EMPTYSTR {yy.cherryPick($3, '', '')} | CHERRY_PICK COMMIT_ID STR PARENT_COMMIT STR COMMIT_TAG EMPTYSTR {yy.cherryPick($3, '', '',$5)} | CHERRY_PICK COMMIT_ID STR COMMIT_TAG EMPTYSTR PARENT_COMMIT STR {yy.cherryPick($3, '', '',$7)} From 827808dca39962fdb216646a2d1ffc8b90c49e24 Mon Sep 17 00:00:00 2001 From: RounakJoshi09 Date: Thu, 19 Oct 2023 10:33:32 +0530 Subject: [PATCH 06/14] Merge Conflict Resolved --- packages/mermaid/src/diagrams/git/gitGraphAst.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/mermaid/src/diagrams/git/gitGraphAst.js b/packages/mermaid/src/diagrams/git/gitGraphAst.js index ae8351d83f..da7f4151e7 100644 --- a/packages/mermaid/src/diagrams/git/gitGraphAst.js +++ b/packages/mermaid/src/diagrams/git/gitGraphAst.js @@ -257,10 +257,10 @@ export const merge = function (otherBranch, custom_id, override_type, custom_tag export const cherryPick = function (sourceId, targetId, tag, parentCommitId) { 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()); - parentCommitId = common.sanitizeText(parentCommitId, configApi.getConfig()); + sourceId = common.sanitizeText(sourceId, getConfig()); + targetId = common.sanitizeText(targetId, getConfig()); + tag = common.sanitizeText(tag, getConfig()); + parentCommitId = common.sanitizeText(parentCommitId, getConfig()); if (!sourceId || commits[sourceId] === undefined) { let error = new Error( From 58e9e5658b87c6ce92c7ef1a9d6ab09fa976af85 Mon Sep 17 00:00:00 2001 From: RounakJoshi09 Date: Sun, 22 Oct 2023 21:12:04 +0530 Subject: [PATCH 07/14] e2e test case added --- cypress/integration/rendering/gitGraph.spec.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/cypress/integration/rendering/gitGraph.spec.js b/cypress/integration/rendering/gitGraph.spec.js index 9f040a36f0..a328ae32f5 100644 --- a/cypress/integration/rendering/gitGraph.spec.js +++ b/cypress/integration/rendering/gitGraph.spec.js @@ -701,4 +701,21 @@ gitGraph TB: {} ); }); + it('34: should render a simple gitgraph with cherry pick merge commit', () => { + imgSnapshotTest( + `gitGraph + commit id: "ZERO" + branch feature + branch release + checkout feature + commit id: "A" + commit id: "B" + checkout main + merge feature id: "M" + checkout release + cherry-pick id: "M" parent:"B" + `, + {} + ); + }); }); From b0cfdcc22f704b628cff186aabd78e3751ad5d49 Mon Sep 17 00:00:00 2001 From: RounakJoshi09 Date: Mon, 23 Oct 2023 20:34:54 +0530 Subject: [PATCH 08/14] Documentation Modified New Ex Added --- docs/syntax/gitgraph.md | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/docs/syntax/gitgraph.md b/docs/syntax/gitgraph.md index c6a397a982..7453f2d0ea 100644 --- a/docs/syntax/gitgraph.md +++ b/docs/syntax/gitgraph.md @@ -371,6 +371,42 @@ A few important rules to note here are: Let see an example: +```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" + 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" + commit id:"THREE" + checkout develop + commit id:"C" +``` + +Cherry Picking Merge Commit: + ```mermaid-example gitGraph commit id: "ZERO" From ca96c0f45fbd2e14bea27b2ee12d2c43af793e0e Mon Sep 17 00:00:00 2001 From: RounakJoshi09 Date: Mon, 23 Oct 2023 21:06:51 +0530 Subject: [PATCH 09/14] Linting Issue Fixed --- docs/syntax/gitgraph.md | 74 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) diff --git a/docs/syntax/gitgraph.md b/docs/syntax/gitgraph.md index 7453f2d0ea..89a95f68d3 100644 --- a/docs/syntax/gitgraph.md +++ b/docs/syntax/gitgraph.md @@ -405,6 +405,40 @@ Let see an example: commit id:"C" ``` +```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" + 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" + commit id:"THREE" + checkout develop + commit id:"C" +``` + Cherry Picking Merge Commit: ```mermaid-example @@ -447,6 +481,46 @@ Cherry Picking Merge Commit: commit id:"C" ``` +```mermaid-example + gitGraph + commit id: "ZERO" + branch develop + branch release + commit id:"A" + checkout main + commit id:"ONE" + checkout develop + commit id:"B" + checkout main + merge develop id:"MERGE" + commit id:"TWO" + checkout release + cherry-pick id:"MERGE" parent:"B" + commit id:"THREE" + checkout develop + commit id:"C" +``` + +```mermaid + gitGraph + commit id: "ZERO" + branch develop + branch release + commit id:"A" + checkout main + commit id:"ONE" + checkout develop + commit id:"B" + checkout main + merge develop id:"MERGE" + commit id:"TWO" + checkout release + cherry-pick id:"MERGE" parent:"B" + 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: From 7f1e0ab4225596feb6f7e82ad4565b894659c33e Mon Sep 17 00:00:00 2001 From: RounakJoshi09 Date: Mon, 23 Oct 2023 21:14:38 +0530 Subject: [PATCH 10/14] Updated gitgraph.md --- docs/syntax/gitgraph.md | 74 ----------------------------------------- 1 file changed, 74 deletions(-) diff --git a/docs/syntax/gitgraph.md b/docs/syntax/gitgraph.md index 89a95f68d3..7453f2d0ea 100644 --- a/docs/syntax/gitgraph.md +++ b/docs/syntax/gitgraph.md @@ -405,40 +405,6 @@ Let see an example: commit id:"C" ``` -```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" - 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" - commit id:"THREE" - checkout develop - commit id:"C" -``` - Cherry Picking Merge Commit: ```mermaid-example @@ -481,46 +447,6 @@ Cherry Picking Merge Commit: commit id:"C" ``` -```mermaid-example - gitGraph - commit id: "ZERO" - branch develop - branch release - commit id:"A" - checkout main - commit id:"ONE" - checkout develop - commit id:"B" - checkout main - merge develop id:"MERGE" - commit id:"TWO" - checkout release - cherry-pick id:"MERGE" parent:"B" - commit id:"THREE" - checkout develop - commit id:"C" -``` - -```mermaid - gitGraph - commit id: "ZERO" - branch develop - branch release - commit id:"A" - checkout main - commit id:"ONE" - checkout develop - commit id:"B" - checkout main - merge develop id:"MERGE" - commit id:"TWO" - checkout release - cherry-pick id:"MERGE" parent:"B" - 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: From 5834818ebee0c158628dbf2a8d6c78229433c454 Mon Sep 17 00:00:00 2001 From: RounakJoshi09 Date: Mon, 23 Oct 2023 21:57:38 +0530 Subject: [PATCH 11/14] Linting Issue Fixed --- docs/syntax/gitgraph.md | 36 ------------------------------------ 1 file changed, 36 deletions(-) diff --git a/docs/syntax/gitgraph.md b/docs/syntax/gitgraph.md index 7453f2d0ea..c6a397a982 100644 --- a/docs/syntax/gitgraph.md +++ b/docs/syntax/gitgraph.md @@ -371,42 +371,6 @@ A few important rules to note here are: Let see an example: -```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" - 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" - commit id:"THREE" - checkout develop - commit id:"C" -``` - -Cherry Picking Merge Commit: - ```mermaid-example gitGraph commit id: "ZERO" From aadf5339a446cc824e9f77fd899e0813a5c87c61 Mon Sep 17 00:00:00 2001 From: RounakJoshi09 Date: Fri, 17 Nov 2023 09:51:03 +0530 Subject: [PATCH 12/14] Error Hash Removed --- packages/mermaid/src/diagrams/git/gitGraphAst.js | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/packages/mermaid/src/diagrams/git/gitGraphAst.js b/packages/mermaid/src/diagrams/git/gitGraphAst.js index da7f4151e7..56f48c6d39 100644 --- a/packages/mermaid/src/diagrams/git/gitGraphAst.js +++ b/packages/mermaid/src/diagrams/git/gitGraphAst.js @@ -282,24 +282,12 @@ export const cherryPick = function (sourceId, targetId, tag, parentCommitId) { let error = new Error( 'Incorrect usage of cherry-pick: If the source commit is a merge commit, an immediate parent commit must be specified.' ); - error.hash = { - text: `cherryPick ${sourceId} ${targetId}`, - token: `cherryPick ${sourceId} ${targetId}`, - line: '1', - loc: { first_line: 1, last_line: 1, first_column: 1, last_column: 1 }, - }; throw error; } if (!(Array.isArray(sourceCommit.parents) && sourceCommit.parents.includes(parentCommitId))) { let error = new Error( 'Invalid operation: The specified parent commit is not an immediate parent of the cherry-picked commit.' ); - error.hash = { - text: `cherryPick ${sourceId} ${targetId}`, - token: `cherryPick ${sourceId} ${targetId}`, - line: '1', - loc: { first_line: 1, last_line: 1, first_column: 1, last_column: 1 }, - }; throw error; } } From 31a1de156685539008ffe666ad1ce1ca3435bb71 Mon Sep 17 00:00:00 2001 From: RounakJoshi09 Date: Mon, 4 Dec 2023 00:28:01 +0530 Subject: [PATCH 13/14] Condition of Parent Id Without Merge Commit Added --- .../mermaid/src/diagrams/git/gitGraphAst.js | 24 +++++++++---------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/packages/mermaid/src/diagrams/git/gitGraphAst.js b/packages/mermaid/src/diagrams/git/gitGraphAst.js index 56f48c6d39..3d34283b38 100644 --- a/packages/mermaid/src/diagrams/git/gitGraphAst.js +++ b/packages/mermaid/src/diagrams/git/gitGraphAst.js @@ -277,19 +277,17 @@ export const cherryPick = function (sourceId, targetId, tag, parentCommitId) { } let sourceCommit = commits[sourceId]; let sourceCommitBranch = sourceCommit.branch; - if (sourceCommit.type === commitType.MERGE) { - if (!parentCommitId) { - let error = new Error( - 'Incorrect usage of cherry-pick: If the source commit is a merge commit, an immediate parent commit must be specified.' - ); - throw error; - } - if (!(Array.isArray(sourceCommit.parents) && sourceCommit.parents.includes(parentCommitId))) { - let error = new Error( - 'Invalid operation: The specified parent commit is not an immediate parent of the cherry-picked commit.' - ); - throw error; - } + if (!(Array.isArray(sourceCommit.parents) && sourceCommit.parents.includes(parentCommitId))) { + let error = new Error( + 'Invalid operation: The specified parent commit is not an immediate parent of the cherry-picked commit.' + ); + throw error; + } + if (sourceCommit.type === commitType.MERGE && !parentCommitId) { + let error = new Error( + 'Incorrect usage of cherry-pick: If the source commit is a merge commit, an immediate parent commit must be specified.' + ); + throw error; } if (!targetId || commits[targetId] === undefined) { // cherry-pick source commit to current branch From d22ee8d1d55eb278d61272d69c7a467ed080b1c9 Mon Sep 17 00:00:00 2001 From: Sidharth Vinod Date: Mon, 4 Dec 2023 11:13:52 +0530 Subject: [PATCH 14/14] fix: Check if parentCommit is provided --- packages/mermaid/src/diagrams/git/gitGraphAst.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/mermaid/src/diagrams/git/gitGraphAst.js b/packages/mermaid/src/diagrams/git/gitGraphAst.js index 3d34283b38..bd90c87173 100644 --- a/packages/mermaid/src/diagrams/git/gitGraphAst.js +++ b/packages/mermaid/src/diagrams/git/gitGraphAst.js @@ -277,7 +277,10 @@ export const cherryPick = function (sourceId, targetId, tag, parentCommitId) { } let sourceCommit = commits[sourceId]; let sourceCommitBranch = sourceCommit.branch; - if (!(Array.isArray(sourceCommit.parents) && sourceCommit.parents.includes(parentCommitId))) { + if ( + parentCommitId && + !(Array.isArray(sourceCommit.parents) && sourceCommit.parents.includes(parentCommitId)) + ) { let error = new Error( 'Invalid operation: The specified parent commit is not an immediate parent of the cherry-picked commit.' );