handle fast forward merges

This commit is contained in:
Raghu Rajagopalan
2016-03-27 16:13:07 +05:30
parent 422d78cbcd
commit 462c3d3a6c
2 changed files with 47 additions and 4 deletions

View File

@@ -8,17 +8,35 @@ var head = null;
var branches = { "master" : head }; var branches = { "master" : head };
var curBranch = "master"; var curBranch = "master";
var direction = "LR"; var direction = "LR";
var seq = 0;
function getId() { function getId() {
return crypto.randomBytes(20).toString('hex').substring(0, 7); return crypto.randomBytes(20).toString('hex').substring(0, 7);
} }
function isfastforwardable(current, other) {
var currentCommit = commits[branches[current]];
var currentSeq = currentCommit.seq;
var otherCommit = commits[branches[other]];
var otherSeq = otherCommit.seq;
log.debug(commits);
log.debug(currentCommit, otherCommit);
while (currentSeq <= otherSeq && currentCommit != otherCommit) {
// only if source has more commits
otherCommit = commits[otherCommit.parent];
}
log.debug(currentCommit.id, otherCommit.id);
return currentCommit.id == otherCommit.id;
}
exports.setDirection = function(dir) { exports.setDirection = function(dir) {
direction = dir; direction = dir;
} }
exports.commit = function(msg) { exports.commit = function(msg) {
var commit = { id: getId(), var commit = { id: getId(),
message: msg, message: msg,
seq: seq++,
parent: head == null ? null : head.id}; parent: head == null ? null : head.id};
head = commit; head = commit;
commits[commit.id] = commit; commits[commit.id] = commit;
@@ -31,7 +49,12 @@ exports.branch = function(name) {
log.debug("in createBranch"); log.debug("in createBranch");
} }
exports.merge = function() { exports.merge = function(sourceBranch) {
if (isfastforwardable(curBranch, sourceBranch)){
branches[curBranch] = branches[sourceBranch];
head = commits[branches[curBranch]];
}
log.debug(branches);
log.debug("in mergeBranch"); log.debug("in mergeBranch");
} }
@@ -54,6 +77,7 @@ exports.clear = function () {
head = null; head = null;
branches = { "master" : head }; branches = { "master" : head };
curBranch = "master"; curBranch = "master";
seq =0;
} }
exports.getBranches = function() { return branches; } exports.getBranches = function() { return branches; }

View File

@@ -85,14 +85,33 @@ describe('when parsing a gitGraph',function() {
'commit\n' + 'commit\n' +
'reset master\n'; 'reset master\n';
console.log(parser.parse(str)); parser.parse(str);
var commits = parser.yy.getCommits();
console.log(commits);
var commits = parser.yy.getCommits();
expect(Object.keys(commits).length).toBe(3); expect(Object.keys(commits).length).toBe(3);
expect(parser.yy.getCurrentBranch()).toBe("newbranch"); expect(parser.yy.getCurrentBranch()).toBe("newbranch");
expect(parser.yy.getBranches()["newbranch"]).toEqual(parser.yy.getBranches()["master"]); expect(parser.yy.getBranches()["newbranch"]).toEqual(parser.yy.getBranches()["master"]);
expect(parser.yy.getHead().id).toEqual(parser.yy.getBranches()["newbranch"]); expect(parser.yy.getHead().id).toEqual(parser.yy.getBranches()["newbranch"]);
}); });
it('it should handle fast forwardable merges', function () {
var str = 'gitGraph:\n' +
'commit\n' +
'branch newbranch\n' +
'checkout newbranch\n' +
'commit\n' +
'commit\n' +
'checkout master\n'+
'merge newbranch\n';
parser.parse(str);
var commits = parser.yy.getCommits();
console.log(commits);
expect(Object.keys(commits).length).toBe(3);
expect(parser.yy.getCurrentBranch()).toBe("master");
expect(parser.yy.getBranches()["newbranch"]).toEqual(parser.yy.getBranches()["master"]);
expect(parser.yy.getHead().id).toEqual(parser.yy.getBranches()["newbranch"]);
});
}); });