@ -99,6 +99,30 @@ public class MergeCommand extends GitCommand<MergeResult> {
private boolean squash ;
private FastForwardMode fastForwardMode = FastForwardMode . FF ;
/ * *
* The modes available for fast forward merges ( corresponding to the - - ff ,
* - - no - ff and - - ff - only options ) .
* /
public enum FastForwardMode {
/ * *
* Corresponds to the default - - ff option ( for a fast forward update the
* branch pointer only ) .
* /
FF ,
/ * *
* Corresponds to the - - no - ff option ( create a merge commit even for a
* fast forward ) .
* /
NO_FF ,
/ * *
* Corresponds to the - - ff - only option ( abort unless the merge is a fast
* forward ) .
* /
FF_ONLY ;
}
/ * *
* @param repo
* /
@ -118,14 +142,7 @@ public class MergeCommand extends GitCommand<MergeResult> {
ConcurrentRefUpdateException , CheckoutConflictException ,
InvalidMergeHeadsException , WrongRepositoryStateException , NoMessageException {
checkCallable ( ) ;
if ( commits . size ( ) ! = 1 )
throw new InvalidMergeHeadsException (
commits . isEmpty ( ) ? JGitText . get ( ) . noMergeHeadSpecified
: MessageFormat . format (
JGitText . get ( ) . mergeStrategyDoesNotSupportHeads ,
mergeStrategy . getName ( ) ,
Integer . valueOf ( commits . size ( ) ) ) ) ;
checkParameters ( ) ;
RevWalk revWalk = null ;
DirCacheCheckout dco = null ;
@ -179,7 +196,8 @@ public class MergeCommand extends GitCommand<MergeResult> {
return new MergeResult ( headCommit , srcCommit , new ObjectId [ ] {
headCommit , srcCommit } ,
MergeStatus . ALREADY_UP_TO_DATE , mergeStrategy , null , null ) ;
} else if ( revWalk . isMergedInto ( headCommit , srcCommit ) ) {
} else if ( revWalk . isMergedInto ( headCommit , srcCommit )
& & fastForwardMode = = FastForwardMode . FF ) {
// FAST_FORWARD detected: skip doing a real merge but only
// update HEAD
refLogMessage . append ( ": " + MergeStatus . FAST_FORWARD ) ;
@ -210,6 +228,11 @@ public class MergeCommand extends GitCommand<MergeResult> {
headCommit , srcCommit } , mergeStatus , mergeStrategy ,
null , msg ) ;
} else {
if ( fastForwardMode = = FastForwardMode . FF_ONLY ) {
return new MergeResult ( headCommit , srcCommit ,
new ObjectId [ ] { headCommit , srcCommit } ,
MergeStatus . ABORTED , mergeStrategy , null , null ) ;
}
String mergeMessage = "" ;
if ( ! squash ) {
mergeMessage = new MergeMessageFormatter ( ) . format (
@ -241,7 +264,10 @@ public class MergeCommand extends GitCommand<MergeResult> {
} else
noProblems = merger . merge ( headCommit , srcCommit ) ;
refLogMessage . append ( ": Merge made by " ) ;
refLogMessage . append ( mergeStrategy . getName ( ) ) ;
if ( ! revWalk . isMergedInto ( headCommit , srcCommit ) )
refLogMessage . append ( mergeStrategy . getName ( ) ) ;
else
refLogMessage . append ( "recursive" ) ;
refLogMessage . append ( '.' ) ;
if ( noProblems ) {
dco = new DirCacheCheckout ( repo ,
@ -305,6 +331,21 @@ public class MergeCommand extends GitCommand<MergeResult> {
}
}
private void checkParameters ( ) throws InvalidMergeHeadsException {
if ( squash & & fastForwardMode = = FastForwardMode . NO_FF ) {
throw new JGitInternalException (
JGitText . get ( ) . cannotCombineSquashWithNoff ) ;
}
if ( commits . size ( ) ! = 1 )
throw new InvalidMergeHeadsException (
commits . isEmpty ( ) ? JGitText . get ( ) . noMergeHeadSpecified
: MessageFormat . format (
JGitText . get ( ) . mergeStrategyDoesNotSupportHeads ,
mergeStrategy . getName ( ) ,
Integer . valueOf ( commits . size ( ) ) ) ) ;
}
private void updateHead ( StringBuilder refLogMessage , ObjectId newHeadId ,
ObjectId oldHeadID ) throws IOException ,
ConcurrentRefUpdateException {
@ -392,4 +433,19 @@ public class MergeCommand extends GitCommand<MergeResult> {
this . squash = squash ;
return this ;
}
/ * *
* Sets the fast forward mode .
*
* @param fastForwardMode
* corresponds to the - - ff / - - no - ff / - - ff - only options . - - ff is the
* default option .
* @return { @code this }
* @since 2 . 2
* /
public MergeCommand setFastForward ( FastForwardMode fastForwardMode ) {
checkCallable ( ) ;
this . fastForwardMode = fastForwardMode ;
return this ;
}
}