From da0c6c6c32c9c9b404802ab9fb47bccb547489a3 Mon Sep 17 00:00:00 2001 From: darshanr0107 Date: Wed, 2 Jul 2025 11:59:20 +0530 Subject: [PATCH 1/4] log warning when duplicate commit IDs are encountered --- .../integration/rendering/gitGraph.spec.js | 33 +++++++++++++++++++ .../mermaid/src/diagrams/git/gitGraphAst.ts | 3 ++ 2 files changed, 36 insertions(+) diff --git a/cypress/integration/rendering/gitGraph.spec.js b/cypress/integration/rendering/gitGraph.spec.js index 42dc57439..c65c7969a 100644 --- a/cypress/integration/rendering/gitGraph.spec.js +++ b/cypress/integration/rendering/gitGraph.spec.js @@ -1569,4 +1569,37 @@ gitGraph TB: {} ); }); + it('77:should render gitGraph with duplicate commit IDs and log warnings', () => { + imgSnapshotTest( + `gitGraph + commit id:"initial commit" + commit id:"work on first release" + commit id:"design freeze from here" + branch v1-rc + checkout v1-rc + commit id:"bugfix 1" + commit id:"bigfix 2" tag:"v1.0.1" + branch FORK-v1.0-MDR + checkout FORK-v1.0-MDR + commit id:"working on MDR" + checkout v1-rc + commit id:"minor design changes for MDR" tag:"v1.0.2" + checkout FORK-v1.0-MDR + merge v1-rc + checkout main + commit id:"new feature for v1.1…" + checkout FORK-v1.0-MDR + commit id:"working on MDR" + commit id:"finishing MDR" + branch v1.0-MDR + checkout v1.0-MDR + commit id:"brush up release" tag:"v1.0.2-MDR" + checkout v1-rc + commit id:"bugfix without MDR" + checkout main + commit id:"work on v1.1"`, + {} + ); + cy.log('Warning: Commit ID "working on MDR" already exists'); + }); }); diff --git a/packages/mermaid/src/diagrams/git/gitGraphAst.ts b/packages/mermaid/src/diagrams/git/gitGraphAst.ts index 36595eb51..0dbc1ecb0 100644 --- a/packages/mermaid/src/diagrams/git/gitGraphAst.ts +++ b/packages/mermaid/src/diagrams/git/gitGraphAst.ts @@ -125,6 +125,9 @@ export const commit = function (commitDB: CommitDB) { }; state.records.head = newCommit; log.info('main branch', config.mainBranchName); + if (state.records.commits.has(newCommit.id)) { + log.warn(`Commit ID ${newCommit.id} already exists`); + } state.records.commits.set(newCommit.id, newCommit); state.records.branches.set(state.records.currBranch, newCommit.id); log.debug('in pushCommit ' + newCommit.id); From 592c5bb880c3b942710a2878d386bcb3eb35c137 Mon Sep 17 00:00:00 2001 From: darshanr0107 Date: Wed, 2 Jul 2025 12:16:17 +0530 Subject: [PATCH 2/4] add changeset --- .changeset/full-donuts-give.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/full-donuts-give.md diff --git a/.changeset/full-donuts-give.md b/.changeset/full-donuts-give.md new file mode 100644 index 000000000..e31b8d69c --- /dev/null +++ b/.changeset/full-donuts-give.md @@ -0,0 +1,5 @@ +--- +'mermaid': patch +--- + +fix: Log a warning when duplicate commit IDs are encountered in gitGraph to help identify and debug rendering issues caused by non-unique IDs. From 648698a43ac39b270087c40f52f3586045522e51 Mon Sep 17 00:00:00 2001 From: darshanr0107 Date: Thu, 3 Jul 2025 13:57:36 +0530 Subject: [PATCH 3/4] resolve PR comments --- .../integration/rendering/gitGraph.spec.js | 33 --------------- .../mermaid/src/diagrams/git/gitGraph.spec.ts | 40 ++++++++++++++++++- 2 files changed, 39 insertions(+), 34 deletions(-) diff --git a/cypress/integration/rendering/gitGraph.spec.js b/cypress/integration/rendering/gitGraph.spec.js index c65c7969a..42dc57439 100644 --- a/cypress/integration/rendering/gitGraph.spec.js +++ b/cypress/integration/rendering/gitGraph.spec.js @@ -1569,37 +1569,4 @@ gitGraph TB: {} ); }); - it('77:should render gitGraph with duplicate commit IDs and log warnings', () => { - imgSnapshotTest( - `gitGraph - commit id:"initial commit" - commit id:"work on first release" - commit id:"design freeze from here" - branch v1-rc - checkout v1-rc - commit id:"bugfix 1" - commit id:"bigfix 2" tag:"v1.0.1" - branch FORK-v1.0-MDR - checkout FORK-v1.0-MDR - commit id:"working on MDR" - checkout v1-rc - commit id:"minor design changes for MDR" tag:"v1.0.2" - checkout FORK-v1.0-MDR - merge v1-rc - checkout main - commit id:"new feature for v1.1…" - checkout FORK-v1.0-MDR - commit id:"working on MDR" - commit id:"finishing MDR" - branch v1.0-MDR - checkout v1.0-MDR - commit id:"brush up release" tag:"v1.0.2-MDR" - checkout v1-rc - commit id:"bugfix without MDR" - checkout main - commit id:"work on v1.1"`, - {} - ); - cy.log('Warning: Commit ID "working on MDR" already exists'); - }); }); diff --git a/packages/mermaid/src/diagrams/git/gitGraph.spec.ts b/packages/mermaid/src/diagrams/git/gitGraph.spec.ts index fed21dd19..0d6eb07ec 100644 --- a/packages/mermaid/src/diagrams/git/gitGraph.spec.ts +++ b/packages/mermaid/src/diagrams/git/gitGraph.spec.ts @@ -1,4 +1,4 @@ -import { rejects } from 'assert'; +import { log } from '../../logger.js'; import { db } from './gitGraphAst.js'; import { parser } from './gitGraphParser.js'; @@ -1319,4 +1319,42 @@ describe('when parsing a gitGraph', function () { } }); }); + it('should log a warning when two commits have the same ID', async () => { + const str = `gitGraph + commit id:"initial commit" + commit id:"work on first release" + commit id:"design freeze from here" + branch v1-rc + checkout v1-rc + commit id:"bugfix 1" + commit id:"bigfix 2" tag:"v1.0.1" + branch FORK-v1.0-MDR + checkout FORK-v1.0-MDR + commit id:"working on MDR" + checkout v1-rc + commit id:"minor design changes for MDR" tag:"v1.0.2" + checkout FORK-v1.0-MDR + merge v1-rc + checkout main + commit id:"new feature for v1.1…" + checkout FORK-v1.0-MDR + commit id:"working on MDR" + commit id:"finishing MDR" + branch v1.0-MDR + checkout v1.0-MDR + commit id:"brush up release" tag:"v1.0.2-MDR" + checkout v1-rc + commit id:"bugfix without MDR" + checkout main + commit id:"work on v1.1" + `; + + const spyOn = vi.spyOn(log, 'warn').mockImplementation(() => undefined); + + await parser.parse(str); + + expect(spyOn).toHaveBeenCalledWith('Commit ID working on MDR already exists'); + + spyOn.mockRestore(); + }); }); From fb017bebfdb50675cb7faf8b22fc403d9964208f Mon Sep 17 00:00:00 2001 From: darshanr0107 Date: Thu, 3 Jul 2025 15:39:26 +0530 Subject: [PATCH 4/4] resolve PR comment Co-authored-by: Alois Klink --- packages/mermaid/src/diagrams/git/gitGraph.spec.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/mermaid/src/diagrams/git/gitGraph.spec.ts b/packages/mermaid/src/diagrams/git/gitGraph.spec.ts index 0d6eb07ec..ee20a9aaf 100644 --- a/packages/mermaid/src/diagrams/git/gitGraph.spec.ts +++ b/packages/mermaid/src/diagrams/git/gitGraph.spec.ts @@ -1349,12 +1349,12 @@ describe('when parsing a gitGraph', function () { commit id:"work on v1.1" `; - const spyOn = vi.spyOn(log, 'warn').mockImplementation(() => undefined); + const logWarnSpy = vi.spyOn(log, 'warn').mockImplementation(() => undefined); await parser.parse(str); - expect(spyOn).toHaveBeenCalledWith('Commit ID working on MDR already exists'); + expect(logWarnSpy).toHaveBeenCalledWith('Commit ID working on MDR already exists'); - spyOn.mockRestore(); + logWarnSpy.mockRestore(); }); });