@@ -269,18 +269,33 @@ define(['d3'], function () {
269
269
* @return {Object } the commit datum object
270
270
*/
271
271
getCommit : function getCommit ( ref ) {
272
+ // Optimization, doesn't seem to break anything
273
+ if ( ! ref ) return null ;
274
+
272
275
var commitData = this . commitData ,
273
- headMatcher = / H E A D ( \^ + ) / . exec ( ref ) ,
274
276
matchedCommit = null ;
275
277
278
+ var parts = / ^ ( [ ^ \^ \~ ] + ) ( .* ) $ / . exec ( ref ) ,
279
+ ref = parts [ 1 ] ,
280
+ modifier = parts [ 2 ] ;
281
+
276
282
if ( ref === 'initial' ) {
277
283
return this . initialCommit ;
278
284
}
279
285
280
- if ( headMatcher ) {
286
+ if ( ref . toLowerCase ( ) === 'head' ) {
281
287
ref = 'HEAD' ;
282
288
}
283
289
290
+ var commitsThatStartWith = commitData
291
+ . filter ( function ( c ) { return c . id . indexOf ( ref ) === 0 } )
292
+
293
+ if ( commitsThatStartWith . length === 1 ) {
294
+ return commitsThatStartWith [ 0 ]
295
+ } else if ( commitsThatStartWith . length > 1 ) {
296
+ throw new Error ( "Ref " + ref + " is ambiguous" )
297
+ }
298
+
284
299
for ( var i = 0 ; i < commitData . length ; i ++ ) {
285
300
var commit = commitData [ i ] ;
286
301
if ( commit === ref ) {
@@ -293,14 +308,14 @@ define(['d3'], function () {
293
308
break ;
294
309
}
295
310
296
- var matchedTag = function ( ) {
311
+ var matchedTag = function ( ) {
297
312
for ( var j = 0 ; j < commit . tags . length ; j ++ ) {
298
313
var tag = commit . tags [ j ] ;
299
314
if ( tag === ref ) {
300
315
matchedCommit = commit ;
301
316
return true ;
302
317
}
303
-
318
+
304
319
if ( tag . indexOf ( '[' ) === 0 && tag . indexOf ( ']' ) === tag . length - 1 ) {
305
320
tag = tag . substring ( 1 , tag . length - 1 ) ;
306
321
}
@@ -315,10 +330,35 @@ define(['d3'], function () {
315
330
}
316
331
}
317
332
318
- if ( headMatcher && matchedCommit ) {
319
- for ( var h = 0 ; h < headMatcher [ 1 ] . length ; h ++ ) {
320
- matchedCommit = getCommit . call ( this , matchedCommit . parent ) ;
333
+ if ( matchedCommit && modifier ) {
334
+ while ( modifier ) {
335
+ var nextToken = modifier [ 0 ]
336
+ modifier = modifier . substr ( 1 )
337
+ var amountMatch = modifier . match ( / ^ ( \d + ) ( .* ) $ / ) ,
338
+ amount = 1 ;
339
+
340
+ if ( amountMatch ) {
341
+ var amount = ~ ~ amountMatch [ 1 ]
321
342
}
343
+
344
+ if ( nextToken === '^' ) {
345
+ if ( amount === 0 ) {
346
+ /* do nothing, refers to this commit */
347
+ } else if ( amount === 1 ) {
348
+ matchedCommit = this . getCommit ( matchedCommit . parent )
349
+ } else if ( amount === 2 ) {
350
+ matchedCommit = this . getCommit ( matchedCommit . parent2 )
351
+ } else {
352
+ matchedCommit = null
353
+ }
354
+ } else if ( nextToken === '~' ) {
355
+ for ( var i = 0 ; i < amount ; i ++ ) {
356
+ if ( matchedCommit && matchedCommit . parent ) {
357
+ matchedCommit = this . getCommit ( matchedCommit . parent )
358
+ }
359
+ }
360
+ }
361
+ }
322
362
}
323
363
324
364
return matchedCommit ;
@@ -360,7 +400,7 @@ define(['d3'], function () {
360
400
svgContainer = container . append ( 'div' )
361
401
. classed ( 'svg-container' , true )
362
402
. classed ( 'remote-container' , this . isRemote ) ;
363
-
403
+
364
404
svg = svgContainer . append ( 'svg:svg' ) ;
365
405
366
406
svg . attr ( 'id' , this . name )
@@ -417,7 +457,7 @@ define(['d3'], function () {
417
457
preventOverlap ( commit , this ) ;
418
458
}
419
459
} ,
420
-
460
+
421
461
_resizeSvg : function ( ) {
422
462
var ele = document . getElementById ( this . svg . node ( ) . id ) ;
423
463
var container = ele . parentNode ;
@@ -453,7 +493,7 @@ define(['d3'], function () {
453
493
this . _renderMergePointers ( ) ;
454
494
this . _renderIdLabels ( ) ;
455
495
this . _resizeSvg ( ) ;
456
- this . checkout ( this . currentBranch ) ;
496
+ this . currentBranch && this . checkout ( this . currentBranch ) ;
457
497
} ,
458
498
459
499
_renderCircles : function ( ) {
@@ -726,8 +766,8 @@ define(['d3'], function () {
726
766
newTags . append ( 'svg:text' )
727
767
. text ( function ( d ) {
728
768
if ( d . name . indexOf ( '[' ) === 0 && d . name . indexOf ( ']' ) === d . name . length - 1 )
729
- return d . name . substring ( 1 , d . name . length - 1 ) ;
730
- return d . name ;
769
+ return d . name . substring ( 1 , d . name . length - 1 ) ;
770
+ return d . name ;
731
771
} )
732
772
. attr ( 'y' , function ( d ) {
733
773
return tagY ( d , view ) + 14 ;
@@ -815,19 +855,23 @@ define(['d3'], function () {
815
855
816
856
commit . message = message ;
817
857
if ( ! commit . parent ) {
818
- if ( ! this . currentBranch ) {
819
- throw new Error ( 'Not a good idea to make commits while in a detached HEAD state.' ) ;
820
- }
821
-
822
- commit . parent = this . getCommit ( this . currentBranch ) . id ;
858
+ commit . parent = this . getCommit ( 'HEAD' ) . id ;
823
859
}
824
860
825
861
this . commitData . push ( commit ) ;
826
- this . moveTag ( this . currentBranch , commit . id ) ;
862
+ if ( this . currentBranch ) {
863
+ this . moveTag ( this . currentBranch , commit . id ) ;
864
+ }
827
865
828
866
this . renderCommits ( ) ;
829
867
830
- this . checkout ( this . currentBranch ) ;
868
+ if ( this . currentBranch ) {
869
+ console . log ( 'branch' , this . currentBranch )
870
+ this . checkout ( this . currentBranch ) ;
871
+ } else {
872
+ console . log ( 'commit' , commit . id )
873
+ this . checkout ( commit . id )
874
+ }
831
875
return this ;
832
876
} ,
833
877
@@ -887,6 +931,7 @@ define(['d3'], function () {
887
931
} ,
888
932
889
933
checkout : function ( ref ) {
934
+ console . log ( "checking out" , ref )
890
935
var commit = this . getCommit ( ref ) ;
891
936
892
937
if ( ! commit ) {
@@ -900,7 +945,9 @@ define(['d3'], function () {
900
945
previousHead . classed ( 'checked-out' , false ) ;
901
946
}
902
947
903
- this . _setCurrentBranch ( ref === commit . id ? null : ref ) ;
948
+ var startsWithCommit = commit . id . indexOf ( ref ) === 0
949
+ var startsWithHead = ref . toLowerCase ( ) . indexOf ( 'head' ) === 0
950
+ this . _setCurrentBranch ( startsWithCommit || startsWithHead ? null : ref ) ;
904
951
this . moveTag ( 'HEAD' , commit . id ) ;
905
952
this . renderTags ( ) ;
906
953
@@ -969,9 +1016,9 @@ define(['d3'], function () {
969
1016
while ( branchStartCommit . parent !== currentCommit . id ) {
970
1017
branchStartCommit = this . getCommit ( branchStartCommit . parent ) ;
971
1018
}
972
-
1019
+
973
1020
branchStartCommit . isNoFFBranch = true ;
974
-
1021
+
975
1022
this . commit ( { parent2 : mergeTarget . id , isNoFFCommit : true } ) ;
976
1023
} else if ( this . isAncestor ( currentCommit , mergeTarget ) ) {
977
1024
this . fastForward ( mergeTarget ) ;
0 commit comments