Compare commits

...

2 Commits

Author SHA1 Message Date
darshanr0107
256e81bcd2 fix: added E2E test
on-behalf-of: @Mermaid-Chart <hello@mermaidchart.com>
2025-11-12 12:04:49 +05:30
darshanr0107
513a3eef98 fix: handle merging master/main into other branches in GitGraph
on-behalf-of: @Mermaid-Chart <hello@mermaidchart.com>
2025-11-11 20:17:40 +05:30
4 changed files with 63 additions and 20 deletions

View File

@@ -0,0 +1,5 @@
---
'mermaid': patch
---
fix: Handle master/main merges correctly in GitGraph diagrams

View File

@@ -1569,4 +1569,14 @@ gitGraph TB:
{}
);
});
it('77: should render a gitGraph merging main into a newly created branch', () => {
imgSnapshotTest(
`gitGraph
commit
branch stable
checkout stable
merge main`,
{}
);
});
});

View File

@@ -1,6 +1,7 @@
import { log } from '../../logger.js';
import { db } from './gitGraphAst.js';
import { parser } from './gitGraphParser.js';
import { commitType } from './gitGraphTypes.js';
describe('when parsing a gitGraph', function () {
beforeEach(function () {
@@ -843,6 +844,39 @@ describe('when parsing a gitGraph', function () {
expect(db.getBranchesAsObjArray()).toStrictEqual([{ name: 'main' }, { name: 'testBranch' }]);
});
it('should handle merging the same branch multiple times', async () => {
const str = `gitGraph:
commit
branch stable
checkout stable
merge main
checkout main
commit
commit
checkout stable
merge main
`;
await parser.parse(str);
const commits = db.getCommits();
expect(commits.size).toBe(5);
expect(db.getCurrentBranch()).toBe('stable');
expect(db.getDirection()).toBe('LR');
expect(db.getBranches().size).toBe(2);
const commitsArray = db.getCommitsArray();
expect(commitsArray[0].branch).toBe('main');
expect(commitsArray[0].parents).toStrictEqual([]);
expect(commitsArray[1].branch).toBe('stable');
expect(commitsArray[1].type).toBe(commitType.MERGE);
expect(commitsArray[1].parents.length).toBe(2);
expect(commitsArray[2].branch).toBe('main');
expect(commitsArray[3].branch).toBe('main');
expect(commitsArray[4].branch).toBe('stable');
expect(commitsArray[4].type).toBe(commitType.MERGE);
expect(commitsArray[4].parents.length).toBe(2);
});
it('should handle merge with custom ids, tags and type', async () => {
const str = `gitGraph:
commit
@@ -1236,7 +1270,7 @@ describe('when parsing a gitGraph', function () {
);
}
});
it('should throw error when trying to merge branches having same heads', async () => {
it('should allow merging branches having same heads', async () => {
const str = `gitGraph
commit
branch testBranch
@@ -1244,13 +1278,19 @@ describe('when parsing a gitGraph', function () {
merge testBranch
`;
try {
await parser.parse(str);
// Fail test if above expression doesn't throw anything.
expect(true).toBe(false);
} catch (e: any) {
expect(e.message).toBe('Incorrect usage of "merge". Both branches have same head');
}
await parser.parse(str);
const commits = db.getCommits();
expect(commits.size).toBe(2);
expect(db.getCurrentBranch()).toBe('main');
const commitsArray = db.getCommitsArray();
expect(commitsArray[0].branch).toBe('main');
expect(commitsArray[0].parents).toStrictEqual([]);
expect(commitsArray[1].branch).toBe('main');
expect(commitsArray[1].type).toBe(commitType.MERGE);
expect(commitsArray[1].parents.length).toBe(2);
expect(commitsArray[1].parents[0]).toBe(commitsArray[0].id);
expect(commitsArray[1].parents[1]).toBe(commitsArray[0].id);
});
it('should throw error when trying to merge branch which has no commits', async () => {
const str = `gitGraph

View File

@@ -167,9 +167,6 @@ export const merge = (mergeDB: MergeDB): void => {
const otherCommit: Commit | undefined = otherBranchCheck
? state.records.commits.get(otherBranchCheck)
: undefined;
if (currentCommit && otherCommit && currentCommit.branch === otherBranch) {
throw new Error(`Cannot merge branch '${otherBranch}' into itself.`);
}
if (state.records.currBranch === otherBranch) {
const error: any = new Error('Incorrect usage of "merge". Cannot merge a branch to itself');
error.hash = {
@@ -212,15 +209,6 @@ export const merge = (mergeDB: MergeDB): void => {
};
throw error;
}
if (currentCommit === otherCommit) {
const error: any = new Error('Incorrect usage of "merge". Both branches have same head');
error.hash = {
text: `merge ${otherBranch}`,
token: `merge ${otherBranch}`,
expected: ['branch abc'],
};
throw error;
}
if (customId && state.records.commits.has(customId)) {
const error: any = new Error(
'Incorrect usage of "merge". Commit with id:' +