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. diff --git a/packages/mermaid/src/diagrams/git/gitGraph.spec.ts b/packages/mermaid/src/diagrams/git/gitGraph.spec.ts index fed21dd19..ee20a9aaf 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 logWarnSpy = vi.spyOn(log, 'warn').mockImplementation(() => undefined); + + await parser.parse(str); + + expect(logWarnSpy).toHaveBeenCalledWith('Commit ID working on MDR already exists'); + + logWarnSpy.mockRestore(); + }); }); 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);