diff options
author | Tomasz Zarna <tomasz.zarna@tasktop.com> | 2012-10-29 16:21:59 +0100 |
---|---|---|
committer | Tomasz Zarna <tomasz.zarna@tasktop.com> | 2012-11-16 11:04:13 +0100 |
commit | 318f3d464307e3efd8342852310c17e71a7282fe (patch) | |
tree | bcd251a7110555cd46c5ab18e5e673f52c214cd2 /org.eclipse.jgit/src/org/eclipse/jgit/api/MergeCommand.java | |
parent | cb0f0ad4cfe2733ff09c2ce4d3b72265ccfee281 (diff) | |
download | jgit-318f3d464307e3efd8342852310c17e71a7282fe.tar.gz jgit-318f3d464307e3efd8342852310c17e71a7282fe.zip |
Add support for --no-ff while merging
Bug: 394432
Change-Id: I373128c0ba949f9b24248874f77f3d68b50ccfd1
Signed-off-by: Chris Aniszczyk <zx@twitter.com>
Diffstat (limited to 'org.eclipse.jgit/src/org/eclipse/jgit/api/MergeCommand.java')
-rw-r--r-- | org.eclipse.jgit/src/org/eclipse/jgit/api/MergeCommand.java | 76 |
1 files changed, 66 insertions, 10 deletions
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/api/MergeCommand.java b/org.eclipse.jgit/src/org/eclipse/jgit/api/MergeCommand.java index 3ca861c06c..a884c70cb0 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/api/MergeCommand.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/api/MergeCommand.java @@ -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; + } } |