From 2fecb4d9456632dd241a772b98b375c1d8ab02fc Mon Sep 17 00:00:00 2001 From: Raghu Rajagopalan Date: Sun, 27 Mar 2016 17:35:29 +0530 Subject: [PATCH] handle case when merge is a noop --- src/diagrams/gitGraph/gitGraphAst.js | 22 ++++++++++++++++---- src/diagrams/gitGraph/gitGraphParser.spec.js | 18 ++++++++++++++++ 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/src/diagrams/gitGraph/gitGraphAst.js b/src/diagrams/gitGraph/gitGraphAst.js index 51eb4c8a5..9795e8c93 100644 --- a/src/diagrams/gitGraph/gitGraphAst.js +++ b/src/diagrams/gitGraph/gitGraphAst.js @@ -1,6 +1,7 @@ var crypto = require("crypto"); var Logger = require('../../logger'); -var log = new Logger.Log(); +//var log = new Logger.Log(); +var log = new Logger.Log(1); var commits = {}; @@ -30,6 +31,15 @@ function isfastforwardable(current, other) { log.debug(currentCommit.id, otherCommit.id); return currentCommit.id == otherCommit.id; } + +function isReachableFrom(current, other) { + var currentCommit = commits[branches[current]]; + var currentSeq = currentCommit.seq; + var otherCommit = commits[branches[other]]; + var otherSeq = otherCommit.seq; + if (currentSeq > otherSeq) return isfastforwardable(other, current); + return false; +} exports.setDirection = function(dir) { direction = dir; } @@ -49,9 +59,13 @@ exports.branch = function(name) { log.debug("in createBranch"); } -exports.merge = function(sourceBranch) { - if (isfastforwardable(curBranch, sourceBranch)){ - branches[curBranch] = branches[sourceBranch]; +exports.merge = function(otherBranch) { + if (isReachableFrom(curBranch, otherBranch)) { + log.debug("Already merged"); + return; + } + if (isfastforwardable(curBranch, otherBranch)){ + branches[curBranch] = branches[otherBranch]; head = commits[branches[curBranch]]; } log.debug(branches); diff --git a/src/diagrams/gitGraph/gitGraphParser.spec.js b/src/diagrams/gitGraph/gitGraphParser.spec.js index 964661887..f0367ea70 100644 --- a/src/diagrams/gitGraph/gitGraphParser.spec.js +++ b/src/diagrams/gitGraph/gitGraphParser.spec.js @@ -114,4 +114,22 @@ describe('when parsing a gitGraph',function() { expect(parser.yy.getHead().id).toEqual(parser.yy.getBranches()["newbranch"]); }); + it('it should handle cases when merge is a noop', function () { + var str = 'gitGraph:\n' + + 'commit\n' + + 'branch newbranch\n' + + 'checkout newbranch\n' + + 'commit\n' + + 'commit\n' + + 'merge master\n'; + + parser.parse(str); + + var commits = parser.yy.getCommits(); + console.log(commits); + expect(Object.keys(commits).length).toBe(3); + expect(parser.yy.getCurrentBranch()).toBe("newbranch"); + expect(parser.yy.getBranches()["newbranch"]).not.toEqual(parser.yy.getBranches()["master"]); + expect(parser.yy.getHead().id).toEqual(parser.yy.getBranches()["newbranch"]); + }); });