feat(git): allow custom merge commit ids

Currently, merge commits can have a git tag, but they cannot have a
custom git commit ID.

This commit allows modifying the default merge commit id.
It also displays all merge commits IDs, which undoes
3ccf027f42
This commit is contained in:
Alois Klink
2022-08-25 23:18:13 +01:00
parent cde3a7cf70
commit b2f5ba3ee8
6 changed files with 89 additions and 9 deletions

View File

@@ -74,7 +74,7 @@ describe('Git Graph diagram', () => {
{} {}
); );
}); });
it('7: should render a simple gitgraph with three branches and merge commit', () => { it('7: should render a simple gitgraph with three branches and tagged merge commit', () => {
imgSnapshotTest( imgSnapshotTest(
`gitGraph `gitGraph
commit id: "1" commit id: "1"
@@ -93,7 +93,7 @@ describe('Git Graph diagram', () => {
checkout nice_feature checkout nice_feature
commit id: "7" commit id: "7"
checkout main checkout main
merge nice_feature merge nice_feature id: "12345" tag: "my merge commit"
checkout very_nice_feature checkout very_nice_feature
commit id: "8" commit id: "8"
checkout main checkout main

View File

@@ -182,6 +182,8 @@ After this we made use of the `checkout` keyword to set the current branch as `m
After this we merge the `develop` branch onto the current branch `main`, resulting in a merge commit. After this we merge the `develop` branch onto the current branch `main`, resulting in a merge commit.
Since the current branch at this point is still `main`, the last two commits are registered against that. Since the current branch at this point is still `main`, the last two commits are registered against that.
Additionally, you may add a tag to the merge commit, or override the default id: `merge branch id:"1234" tag:"v1.0.0"`
### Cherry Pick commit from another branch ### Cherry Pick commit from another branch
Similar to how 'git' allows you to cherry-pick a commit from **another branch** onto the **current** branch, Mermaid also supports this functionality. You can also cherry-pick a commit from another branch using the `cherry-pick` keyword. Similar to how 'git' allows you to cherry-pick a commit from **another branch** onto the **current** branch, Mermaid also supports this functionality. You can also cherry-pick a commit from another branch using the `cherry-pick` keyword.

View File

@@ -148,8 +148,17 @@ export const branch = function (name, order) {
} }
}; };
export const merge = function (otherBranch, tag) { /**
* Creates a merge commit.
*
* @param {string} otherBranch - Target branch to merge to.
* @param {string} [tag] - Git tag to use on this merge commit.
* @param {string} [id] - Git commit id.
*/
export const merge = function (otherBranch, tag, id) {
otherBranch = common.sanitizeText(otherBranch, configApi.getConfig()); otherBranch = common.sanitizeText(otherBranch, configApi.getConfig());
id = common.sanitizeText(id, configApi.getConfig());
const currentCommit = commits[branches[curBranch]]; const currentCommit = commits[branches[curBranch]];
const otherCommit = commits[branches[otherBranch]]; const otherCommit = commits[branches[otherBranch]];
if (curBranch === otherBranch) { if (curBranch === otherBranch) {
@@ -219,7 +228,7 @@ export const merge = function (otherBranch, tag) {
// } else { // } else {
// create merge commit // create merge commit
const commit = { const commit = {
id: seq + '-' + getId(), id: id || seq + '-' + getId(),
message: 'merged branch ' + otherBranch + ' into ' + curBranch, message: 'merged branch ' + otherBranch + ' into ' + curBranch,
seq: seq++, seq: seq++,
parents: [head == null ? null : head.id, branches[otherBranch]], parents: [head == null ? null : head.id, branches[otherBranch]],

View File

@@ -496,6 +496,76 @@ describe('when parsing a gitGraph', function () {
]); ]);
}); });
it('should handle merge ids', function () {
const str = `gitGraph:
commit
branch testBranch
checkout testBranch
commit
checkout main
%% Merge Tag and ID
merge testBranch tag: "merge-tag" id: "2-222"
branch testBranch2
checkout testBranch2
commit
checkout main
%% Merge ID and Tag (reverse order)
merge testBranch2 id: "4-444" tag: "merge-tag2"
branch testBranch3
checkout testBranch3
commit
checkout main
%% just Merge ID
merge testBranch3 id: "6-666"
`;
parser.parse(str);
const commits = parser.yy.getCommits();
expect(Object.keys(commits).length).toBe(7);
expect(parser.yy.getCurrentBranch()).toBe('main');
expect(parser.yy.getDirection()).toBe('LR');
// The order of these commits is in alphabetical order of IDs
const [
mainCommit,
testBranchCommit,
testBranchMerge,
testBranch2Commit,
testBranch2Merge,
testBranch3Commit,
testBranch3Merge,
] = Object.values(commits);
console.log(Object.keys(commits));
expect(mainCommit.branch).toBe('main');
expect(mainCommit.parents).toStrictEqual([]);
expect(testBranchCommit.branch).toBe('testBranch');
expect(testBranchCommit.parents).toStrictEqual([mainCommit.id]);
expect(testBranchMerge.branch).toBe('main');
expect(testBranchMerge.parents).toStrictEqual([mainCommit.id, testBranchCommit.id]);
expect(testBranchMerge.tag).toBe('merge-tag');
expect(testBranchMerge.id).toBe('2-222');
expect(testBranch2Merge.branch).toBe('main');
expect(testBranch2Merge.parents).toStrictEqual([testBranchMerge.id, testBranch2Commit.id]);
expect(testBranch2Merge.tag).toBe('merge-tag2');
expect(testBranch2Merge.id).toBe('4-444');
expect(testBranch3Merge.branch).toBe('main');
expect(testBranch3Merge.parents).toStrictEqual([testBranch2Merge.id, testBranch3Commit.id]);
expect(testBranch3Merge.id).toBe('6-666');
expect(parser.yy.getBranchesAsObjArray()).toStrictEqual([
{ name: 'main' },
{ name: 'testBranch' },
{ name: 'testBranch2' },
{ name: 'testBranch3' },
]);
});
it('should throw error when try to branch existing branch: main', function () { it('should throw error when try to branch existing branch: main', function () {
const str = `gitGraph const str = `gitGraph
commit commit

View File

@@ -215,11 +215,7 @@ const drawCommits = (svg, commits, modifyGraph) => {
const px = 4; const px = 4;
const py = 2; const py = 2;
// Draw the commit label // Draw the commit label
if ( if (commit.type !== commitType.CHERRY_PICK && gitGraphConfig.showCommitLabel) {
commit.type !== commitType.CHERRY_PICK &&
commit.type !== commitType.MERGE &&
gitGraphConfig.showCommitLabel
) {
const wrapper = gLabels.append('g'); const wrapper = gLabels.append('g');
const labelBkg = wrapper.insert('rect').attr('class', 'commit-label-bkg'); const labelBkg = wrapper.insert('rect').attr('class', 'commit-label-bkg');

View File

@@ -123,6 +123,9 @@ cherryPickStatement
mergeStatement mergeStatement
: MERGE ID {yy.merge($2)} : MERGE ID {yy.merge($2)}
| MERGE ID COMMIT_TAG STR {yy.merge($2, $4)} | MERGE ID COMMIT_TAG STR {yy.merge($2, $4)}
| MERGE ID COMMIT_ID STR {yy.merge($2, '', $4)}
| MERGE ID COMMIT_TAG STR COMMIT_ID STR {yy.merge($2, $4, $6)}
| MERGE ID COMMIT_ID STR COMMIT_TAG STR {yy.merge($2, $6, $4)}
; ;
commitStatement commitStatement