Handle merge commit with two parents

This commit is contained in:
Raghu Rajagopalan
2016-03-27 17:53:01 +05:30
parent 92be61dc48
commit 2d99ccfcda
2 changed files with 33 additions and 0 deletions

View File

@@ -26,6 +26,7 @@ function isfastforwardable(current, other) {
log.debug(currentCommit, otherCommit); log.debug(currentCommit, otherCommit);
while (currentSeq <= otherSeq && currentCommit != otherCommit) { while (currentSeq <= otherSeq && currentCommit != otherCommit) {
// only if source has more commits // only if source has more commits
if (otherCommit.parent == null) break;
otherCommit = commits[otherCommit.parent]; otherCommit = commits[otherCommit.parent];
} }
log.debug(currentCommit.id, otherCommit.id); log.debug(currentCommit.id, otherCommit.id);
@@ -67,6 +68,17 @@ exports.merge = function(otherBranch) {
if (isfastforwardable(curBranch, otherBranch)){ if (isfastforwardable(curBranch, otherBranch)){
branches[curBranch] = branches[otherBranch]; branches[curBranch] = branches[otherBranch];
head = commits[branches[curBranch]]; 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(branches);
log.debug("in mergeBranch"); log.debug("in mergeBranch");

View File

@@ -132,4 +132,25 @@ describe('when parsing a gitGraph',function() {
expect(parser.yy.getBranches()["newbranch"]).not.toEqual(parser.yy.getBranches()["master"]); expect(parser.yy.getBranches()["newbranch"]).not.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 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"]);
});
}); });