handle case when merge is a noop

This commit is contained in:
Raghu Rajagopalan 2016-03-27 17:35:29 +05:30
parent 462c3d3a6c
commit 2fecb4d945
2 changed files with 36 additions and 4 deletions

View File

@ -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);

View File

@ -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"]);
});
});