@@ -271,6 +271,7 @@ define(['d3'], function () {
271
271
getCommit : function getCommit ( ref ) {
272
272
// Optimization, doesn't seem to break anything
273
273
if ( ! ref ) return null ;
274
+ if ( ref . id ) return ref
274
275
275
276
var commitData = this . commitData ,
276
277
matchedCommit = null ;
@@ -807,46 +808,6 @@ define(['d3'], function () {
807
808
return this ;
808
809
} ,
809
810
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
-
850
811
commit : function ( commit , message ) {
851
812
commit = commit || { } ;
852
813
@@ -866,10 +827,8 @@ define(['d3'], function () {
866
827
this . renderCommits ( ) ;
867
828
868
829
if ( this . currentBranch ) {
869
- console . log ( 'branch' , this . currentBranch )
870
830
this . checkout ( this . currentBranch ) ;
871
831
} else {
872
- console . log ( 'commit' , commit . id )
873
832
this . checkout ( commit . id )
874
833
}
875
834
return this ;
@@ -980,7 +939,7 @@ define(['d3'], function () {
980
939
throw new Error ( 'Cannot find ref: ' + ref ) ;
981
940
}
982
941
983
- if ( this . isAncestor ( commit , 'HEAD' ) ) {
942
+ if ( this . isAncestorOf ( commit , 'HEAD' ) ) {
984
943
commit . reverted = true ;
985
944
this . commit ( { reverts : commit . id } ) ;
986
945
} else {
@@ -999,18 +958,33 @@ define(['d3'], function () {
999
958
}
1000
959
} ,
1001
960
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
+
1002
974
merge : function ( ref , noFF ) {
1003
975
var mergeTarget = this . getCommit ( ref ) ,
1004
976
currentCommit = this . getCommit ( 'HEAD' ) ;
1005
-
1006
977
if ( ! mergeTarget ) {
1007
978
throw new Error ( 'Cannot find ref: ' + ref ) ;
1008
979
}
1009
980
981
+
1010
982
if ( currentCommit . id === mergeTarget . id ) {
1011
983
throw new Error ( 'Already up-to-date.' ) ;
1012
984
} else if ( currentCommit . parent2 === mergeTarget . id ) {
1013
985
throw new Error ( 'Already up-to-date.' ) ;
986
+ } else if ( this . isAncestorOf ( mergeTarget , currentCommit ) ) {
987
+ throw new Error ( 'Already up-to-date.' ) ;
1014
988
} else if ( noFF === true ) {
1015
989
var branchStartCommit = this . getCommit ( mergeTarget . parent ) ;
1016
990
while ( branchStartCommit . parent !== currentCommit . id ) {
@@ -1020,7 +994,7 @@ define(['d3'], function () {
1020
994
branchStartCommit . isNoFFBranch = true ;
1021
995
1022
996
this . commit ( { parent2 : mergeTarget . id , isNoFFCommit : true } ) ;
1023
- } else if ( this . isAncestor ( currentCommit , mergeTarget ) ) {
997
+ } else if ( this . isAncestorOf ( currentCommit . id , mergeTarget . id ) ) {
1024
998
this . fastForward ( mergeTarget ) ;
1025
999
return 'Fast-Forward' ;
1026
1000
} else {
@@ -1047,7 +1021,7 @@ define(['d3'], function () {
1047
1021
throw new Error ( 'Already up-to-date.' ) ;
1048
1022
}
1049
1023
1050
- isCommonAncestor = this . isAncestor ( currentCommit , rebaseTarget ) ;
1024
+ isCommonAncestor = this . isAncestorOf ( currentCommit , rebaseTarget ) ;
1051
1025
1052
1026
if ( isCommonAncestor ) {
1053
1027
this . fastForward ( rebaseTarget ) ;
@@ -1059,7 +1033,7 @@ define(['d3'], function () {
1059
1033
while ( ! isCommonAncestor ) {
1060
1034
toRebase . unshift ( currentCommit ) ;
1061
1035
currentCommit = this . getCommit ( currentCommit . parent ) ;
1062
- isCommonAncestor = this . isAncestor ( currentCommit , rebaseTarget ) ;
1036
+ isCommonAncestor = this . isAncestorOf ( currentCommit , rebaseTarget ) ;
1063
1037
}
1064
1038
1065
1039
for ( var i = 0 ; i < toRebase . length ; i ++ ) {
0 commit comments