Skip to content

Commit 5c9617d

Browse files
author
Michelle Tilley
committed
Merge pull request #18 from kuychaco/mkt-fix-ff-merges
Fix isAncestor(Of) and fast-forward merges
2 parents 027d8f2 + 1d0b7d4 commit 5c9617d

File tree

1 file changed

+21
-47
lines changed

1 file changed

+21
-47
lines changed

js/historyview.js

+21-47
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,7 @@ define(['d3'], function () {
271271
getCommit: function getCommit(ref) {
272272
// Optimization, doesn't seem to break anything
273273
if (!ref) return null;
274+
if (ref.id) return ref
274275

275276
var commitData = this.commitData,
276277
matchedCommit = null;
@@ -807,46 +808,6 @@ define(['d3'], function () {
807808
return this;
808809
},
809810

810-
/**
811-
* @method isAncestor
812-
* @param ref1
813-
* @param ref2
814-
* @return {Boolean} whether or not ref1 is an ancestor of ref2
815-
*/
816-
isAncestor: function isAncestor(ref1, ref2) {
817-
var currentCommit = this.getCommit(ref1),
818-
targetTree = this.getCommit(ref2),
819-
inTree = false,
820-
additionalTrees = [];
821-
822-
if (!currentCommit) {
823-
return false;
824-
}
825-
826-
while (targetTree) {
827-
if (targetTree.id === currentCommit.id) {
828-
inTree = true;
829-
targetTree = null;
830-
} else {
831-
if (targetTree.parent2) {
832-
additionalTrees.push(targetTree.parent2);
833-
}
834-
targetTree = this.getCommit(targetTree.parent);
835-
}
836-
}
837-
838-
if (inTree) {
839-
return true;
840-
}
841-
842-
for (var i = 0; i < additionalTrees.length; i++) {
843-
inTree = isAncestor.call(this, currentCommit, additionalTrees[i]);
844-
if (inTree) break;
845-
}
846-
847-
return inTree;
848-
},
849-
850811
commit: function (commit, message) {
851812
commit = commit || {};
852813

@@ -866,10 +827,8 @@ define(['d3'], function () {
866827
this.renderCommits();
867828

868829
if (this.currentBranch) {
869-
console.log('branch', this.currentBranch)
870830
this.checkout(this.currentBranch);
871831
} else {
872-
console.log('commit', commit.id)
873832
this.checkout(commit.id)
874833
}
875834
return this;
@@ -980,7 +939,7 @@ define(['d3'], function () {
980939
throw new Error('Cannot find ref: ' + ref);
981940
}
982941

983-
if (this.isAncestor(commit, 'HEAD')) {
942+
if (this.isAncestorOf(commit, 'HEAD')) {
984943
commit.reverted = true;
985944
this.commit({reverts: commit.id});
986945
} else {
@@ -999,18 +958,33 @@ define(['d3'], function () {
999958
}
1000959
},
1001960

961+
isAncestorOf: function(search, start) {
962+
var startCommit = this.getCommit(start),
963+
searchCommit = this.getCommit(search)
964+
965+
if (startCommit === searchCommit) {
966+
return true
967+
} else {
968+
var ancestorOnFirstParent = startCommit.parent && this.isAncestorOf(searchCommit.id, startCommit.parent)
969+
var ancestorOnSecondParent = startCommit.parent2 && this.isAncestorOf(searchCommit.id, startCommit.parent2)
970+
return ancestorOnFirstParent || ancestorOnSecondParent
971+
}
972+
},
973+
1002974
merge: function (ref, noFF) {
1003975
var mergeTarget = this.getCommit(ref),
1004976
currentCommit = this.getCommit('HEAD');
1005-
1006977
if (!mergeTarget) {
1007978
throw new Error('Cannot find ref: ' + ref);
1008979
}
1009980

981+
1010982
if (currentCommit.id === mergeTarget.id) {
1011983
throw new Error('Already up-to-date.');
1012984
} else if (currentCommit.parent2 === mergeTarget.id) {
1013985
throw new Error('Already up-to-date.');
986+
} else if (this.isAncestorOf(mergeTarget, currentCommit)) {
987+
throw new Error('Already up-to-date.');
1014988
} else if (noFF === true) {
1015989
var branchStartCommit = this.getCommit(mergeTarget.parent);
1016990
while (branchStartCommit.parent !== currentCommit.id) {
@@ -1020,7 +994,7 @@ define(['d3'], function () {
1020994
branchStartCommit.isNoFFBranch = true;
1021995

1022996
this.commit({parent2: mergeTarget.id, isNoFFCommit: true});
1023-
} else if (this.isAncestor(currentCommit, mergeTarget)) {
997+
} else if (this.isAncestorOf(currentCommit.id, mergeTarget.id)) {
1024998
this.fastForward(mergeTarget);
1025999
return 'Fast-Forward';
10261000
} else {
@@ -1047,7 +1021,7 @@ define(['d3'], function () {
10471021
throw new Error('Already up-to-date.');
10481022
}
10491023

1050-
isCommonAncestor = this.isAncestor(currentCommit, rebaseTarget);
1024+
isCommonAncestor = this.isAncestorOf(currentCommit, rebaseTarget);
10511025

10521026
if (isCommonAncestor) {
10531027
this.fastForward(rebaseTarget);
@@ -1059,7 +1033,7 @@ define(['d3'], function () {
10591033
while (!isCommonAncestor) {
10601034
toRebase.unshift(currentCommit);
10611035
currentCommit = this.getCommit(currentCommit.parent);
1062-
isCommonAncestor = this.isAncestor(currentCommit, rebaseTarget);
1036+
isCommonAncestor = this.isAncestorOf(currentCommit, rebaseTarget);
10631037
}
10641038

10651039
for (var i = 0; i < toRebase.length; i++) {

0 commit comments

Comments
 (0)