diff --git a/src/diagrams/gitGraph/gitGraphAst.js b/src/diagrams/gitGraph/gitGraphAst.js index 9795e8c93..298777650 100644 --- a/src/diagrams/gitGraph/gitGraphAst.js +++ b/src/diagrams/gitGraph/gitGraphAst.js @@ -26,6 +26,7 @@ function isfastforwardable(current, other) { log.debug(currentCommit, otherCommit); while (currentSeq <= otherSeq && currentCommit != otherCommit) { // only if source has more commits + if (otherCommit.parent == null) break; otherCommit = commits[otherCommit.parent]; } log.debug(currentCommit.id, otherCommit.id); @@ -67,6 +68,17 @@ exports.merge = function(otherBranch) { if (isfastforwardable(curBranch, otherBranch)){ branches[curBranch] = branches[otherBranch]; head = commits[branches[curBranch]]; + } else { + // create merge commit + var commit = { + id: getId(), + message: 'merged branch ' + otherBranch + ' into ' + curBranch, + seq: seq++, + parent: [head == null ? null : head.id, commits[branches[otherBranch]]] + }; + head = commit; + commits[commit.id] = commit; + branches[curBranch] = commit.id; } log.debug(branches); log.debug("in mergeBranch"); diff --git a/src/diagrams/gitGraph/gitGraphParser.spec.js b/src/diagrams/gitGraph/gitGraphParser.spec.js index f0367ea70..948594619 100644 --- a/src/diagrams/gitGraph/gitGraphParser.spec.js +++ b/src/diagrams/gitGraph/gitGraphParser.spec.js @@ -132,4 +132,25 @@ describe('when parsing a gitGraph',function() { expect(parser.yy.getBranches()["newbranch"]).not.toEqual(parser.yy.getBranches()["master"]); expect(parser.yy.getHead().id).toEqual(parser.yy.getBranches()["newbranch"]); }); + + it('it should handle merge with 2 parents', function () { + var str = 'gitGraph:\n' + + 'commit\n' + + 'branch newbranch\n' + + 'checkout newbranch\n' + + 'commit\n' + + 'commit\n' + + 'checkout master\n' + + 'commit\n' + + 'merge newbranch\n'; + + parser.parse(str); + + var commits = parser.yy.getCommits(); + console.log(commits); + expect(Object.keys(commits).length).toBe(5); + expect(parser.yy.getCurrentBranch()).toBe("master"); + expect(parser.yy.getBranches()["newbranch"]).not.toEqual(parser.yy.getBranches()["master"]); + expect(parser.yy.getHead().id).toEqual(parser.yy.getBranches()["master"]); + }); });