From 7424d58255340fa44d9191a7436fbe70d72a457c Mon Sep 17 00:00:00 2001 From: Laurent Goubet Date: Tue, 21 Jan 2014 16:35:26 +0100 Subject: [PATCH] Allow callers to use different merging strategies Signed-off-by: Laurent Goubet Change-Id: I84e9d7b4b772b4ad7d3e7010aad78291d4d178fe --- .../eclipse/jgit/api/CherryPickCommand.java | 16 ++++++++++++-- .../src/org/eclipse/jgit/api/PullCommand.java | 19 +++++++++++++++-- .../org/eclipse/jgit/api/RebaseCommand.java | 19 +++++++++++++++-- .../org/eclipse/jgit/api/RevertCommand.java | 21 ++++++++++++++----- .../eclipse/jgit/api/StashApplyCommand.java | 19 ++++++++++++++--- .../jgit/api/SubmoduleUpdateCommand.java | 16 ++++++++++++++ 6 files changed, 96 insertions(+), 14 deletions(-) diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/api/CherryPickCommand.java b/org.eclipse.jgit/src/org/eclipse/jgit/api/CherryPickCommand.java index 192a377678..b3415b3505 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/api/CherryPickCommand.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/api/CherryPickCommand.java @@ -88,6 +88,8 @@ public class CherryPickCommand extends GitCommand { private String ourCommitName = null; + private MergeStrategy strategy = MergeStrategy.RECURSIVE; + /** * @param repo */ @@ -151,8 +153,7 @@ public class CherryPickCommand extends GitCommand { String cherryPickName = srcCommit.getId().abbreviate(7).name() + " " + srcCommit.getShortMessage(); //$NON-NLS-1$ - ResolveMerger merger = (ResolveMerger) MergeStrategy.RECURSIVE - .newMerger(repo); + ResolveMerger merger = (ResolveMerger) strategy.newMerger(repo); merger.setWorkingTreeIterator(new FileTreeIterator(repo)); merger.setBase(srcParent.getTree()); merger.setCommitNames(new String[] { "BASE", ourName, @@ -259,6 +260,17 @@ public class CherryPickCommand extends GitCommand { return this; } + /** + * @param strategy + * The merge strategy to use during this Cherry-pick. + * @return {@code this} + * @since 3.4 + */ + public CherryPickCommand setStrategy(MergeStrategy strategy) { + this.strategy = strategy; + return this; + } + private String calculateOurName(Ref headRef) { if (ourCommitName != null) return ourCommitName; diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/api/PullCommand.java b/org.eclipse.jgit/src/org/eclipse/jgit/api/PullCommand.java index d92adfe370..7634957eb2 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/api/PullCommand.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/api/PullCommand.java @@ -66,6 +66,7 @@ import org.eclipse.jgit.lib.ProgressMonitor; import org.eclipse.jgit.lib.Ref; import org.eclipse.jgit.lib.Repository; import org.eclipse.jgit.lib.RepositoryState; +import org.eclipse.jgit.merge.MergeStrategy; import org.eclipse.jgit.transport.FetchResult; /** @@ -86,6 +87,8 @@ public class PullCommand extends TransportCommand { private String remoteBranchName; + private MergeStrategy strategy = MergeStrategy.RECURSIVE; + private enum PullRebaseMode { USE_CONFIG, REBASE, @@ -299,13 +302,14 @@ public class PullCommand extends TransportCommand { if (doRebase) { RebaseCommand rebase = new RebaseCommand(repo); RebaseResult rebaseRes = rebase.setUpstream(commitToMerge) - .setUpstreamName(upstreamName) - .setProgressMonitor(monitor).setOperation(Operation.BEGIN) + .setUpstreamName(upstreamName).setProgressMonitor(monitor) + .setOperation(Operation.BEGIN).setStrategy(strategy) .call(); result = new PullResult(fetchRes, remote, rebaseRes); } else { MergeCommand merge = new MergeCommand(repo); merge.include(upstreamName, commitToMerge); + merge.setStrategy(strategy); MergeResult mergeRes = merge.call(); monitor.update(1); result = new PullResult(fetchRes, remote, mergeRes); @@ -363,4 +367,15 @@ public class PullCommand extends TransportCommand { public String getRemoteBranchName() { return remoteBranchName; } + + /** + * @param strategy + * The merge strategy to use during this pull operation. + * @return {@code this} + * @since 3.4 + */ + public PullCommand setStrategy(MergeStrategy strategy) { + this.strategy = strategy; + return this; + } } diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/api/RebaseCommand.java b/org.eclipse.jgit/src/org/eclipse/jgit/api/RebaseCommand.java index 3b84917421..95551c3ee0 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/api/RebaseCommand.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/api/RebaseCommand.java @@ -93,6 +93,7 @@ import org.eclipse.jgit.lib.Ref; import org.eclipse.jgit.lib.RefUpdate; import org.eclipse.jgit.lib.RefUpdate.Result; import org.eclipse.jgit.lib.Repository; +import org.eclipse.jgit.merge.MergeStrategy; import org.eclipse.jgit.revwalk.RevCommit; import org.eclipse.jgit.revwalk.RevWalk; import org.eclipse.jgit.treewalk.TreeWalk; @@ -211,6 +212,8 @@ public class RebaseCommand extends GitCommand { private boolean lastStepWasForward; + private MergeStrategy strategy = MergeStrategy.RECURSIVE; + /** * @param repo */ @@ -375,7 +378,8 @@ public class RebaseCommand extends GitCommand { String stash = rebaseState.readFile(AUTOSTASH); try { Git.wrap(repo).stashApply().setStashRef(stash) - .ignoreRepositoryState(true).call(); + .ignoreRepositoryState(true).setStrategy(strategy) + .call(); } catch (StashApplyFailureException e) { conflicts = true; RevWalk rw = new RevWalk(repo); @@ -474,7 +478,7 @@ public class RebaseCommand extends GitCommand { String ourCommitName = getOurCommitName(); CherryPickResult cherryPickResult = new Git(repo).cherryPick() .include(commitToPick).setOurCommitName(ourCommitName) - .setReflogPrefix("rebase:").call(); //$NON-NLS-1$ + .setReflogPrefix("rebase:").setStrategy(strategy).call(); //$NON-NLS-1$ switch (cherryPickResult.getStatus()) { case FAILED: if (operation == Operation.BEGIN) @@ -1302,6 +1306,17 @@ public class RebaseCommand extends GitCommand { return this; } + /** + * @param strategy + * The merge strategy to use during this rebase operation. + * @return {@code this} + * @since 3.4 + */ + public RebaseCommand setStrategy(MergeStrategy strategy) { + this.strategy = strategy; + return this; + } + /** * Allows configure rebase interactive process and modify commit message */ diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/api/RevertCommand.java b/org.eclipse.jgit/src/org/eclipse/jgit/api/RevertCommand.java index bc1b29c2f6..52327d76c7 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/api/RevertCommand.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/api/RevertCommand.java @@ -95,6 +95,8 @@ public class RevertCommand extends GitCommand { private List unmergedPaths; + private MergeStrategy strategy = MergeStrategy.RECURSIVE; + /** * @param repo */ @@ -160,8 +162,7 @@ public class RevertCommand extends GitCommand { String revertName = srcCommit.getId().abbreviate(7).name() + " " + srcCommit.getShortMessage(); //$NON-NLS-1$ - ResolveMerger merger = (ResolveMerger) MergeStrategy.RECURSIVE - .newMerger(repo); + ResolveMerger merger = (ResolveMerger) strategy.newMerger(repo); merger.setWorkingTreeIterator(new FileTreeIterator(repo)); merger.setBase(srcCommit.getTree()); merger.setCommitNames(new String[] { @@ -194,15 +195,14 @@ public class RevertCommand extends GitCommand { merger.getBaseCommitId(), new ObjectId[] { headCommit.getId(), srcParent.getId() }, - MergeStatus.FAILED, MergeStrategy.RECURSIVE, + MergeStatus.FAILED, strategy, merger.getMergeResults(), failingPaths, null); else failingResult = new MergeResult(null, merger.getBaseCommitId(), new ObjectId[] { headCommit.getId(), srcParent.getId() }, - MergeStatus.CONFLICTING, - MergeStrategy.RECURSIVE, + MergeStatus.CONFLICTING, strategy, merger.getMergeResults(), failingPaths, null); if (!merger.failed() && !unmergedPaths.isEmpty()) { String message = new MergeMessageFormatter() @@ -301,4 +301,15 @@ public class RevertCommand extends GitCommand { public List getUnmergedPaths() { return unmergedPaths; } + + /** + * @param strategy + * The merge strategy to use during this revert command. + * @return {@code this} + * @since 3.4 + */ + public RevertCommand setStrategy(MergeStrategy strategy) { + this.strategy = strategy; + return this; + } } diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/api/StashApplyCommand.java b/org.eclipse.jgit/src/org/eclipse/jgit/api/StashApplyCommand.java index 8440d8b950..f73ce831fe 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/api/StashApplyCommand.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/api/StashApplyCommand.java @@ -92,6 +92,8 @@ public class StashApplyCommand extends GitCommand { private boolean ignoreRepositoryState; + private MergeStrategy strategy = MergeStrategy.RECURSIVE; + /** * Create command to apply the changes of a stashed commit * @@ -181,8 +183,7 @@ public class StashApplyCommand extends GitCommand { .getParent(1)); ObjectId stashHeadCommit = stashCommit.getParent(0); - ResolveMerger merger = (ResolveMerger) MergeStrategy.RECURSIVE - .newMerger(repo); + ResolveMerger merger = (ResolveMerger) strategy.newMerger(repo); merger.setCommitNames(new String[] { "stashed HEAD", "HEAD", "stash" }); merger.setBase(stashHeadCommit); @@ -194,7 +195,7 @@ public class StashApplyCommand extends GitCommand { dco.setFailOnConflict(true); dco.checkout(); // Ignoring failed deletes.... if (applyIndex) { - ResolveMerger ixMerger = (ResolveMerger) MergeStrategy.RECURSIVE + ResolveMerger ixMerger = (ResolveMerger) strategy .newMerger(repo, true); ixMerger.setCommitNames(new String[] { "stashed HEAD", "HEAD", "stashed index" }); @@ -231,6 +232,18 @@ public class StashApplyCommand extends GitCommand { this.applyIndex = applyIndex; } + /** + * @param strategy + * The merge strategy to use in order to merge during this + * command execution. + * @return {@code this} + * @since 3.4 + */ + public StashApplyCommand setStrategy(MergeStrategy strategy) { + this.strategy = strategy; + return this; + } + private void resetIndex(RevTree tree) throws IOException { DirCache dc = repo.lockDirCache(); TreeWalk walk = null; diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/api/SubmoduleUpdateCommand.java b/org.eclipse.jgit/src/org/eclipse/jgit/api/SubmoduleUpdateCommand.java index e2f356a08e..de1a3e9fd0 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/api/SubmoduleUpdateCommand.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/api/SubmoduleUpdateCommand.java @@ -65,6 +65,7 @@ import org.eclipse.jgit.lib.NullProgressMonitor; import org.eclipse.jgit.lib.ProgressMonitor; import org.eclipse.jgit.lib.RefUpdate; import org.eclipse.jgit.lib.Repository; +import org.eclipse.jgit.merge.MergeStrategy; import org.eclipse.jgit.revwalk.RevCommit; import org.eclipse.jgit.revwalk.RevWalk; import org.eclipse.jgit.submodule.SubmoduleWalk; @@ -84,6 +85,8 @@ public class SubmoduleUpdateCommand extends private final Collection paths; + private MergeStrategy strategy = MergeStrategy.RECURSIVE; + /** * @param repo */ @@ -174,10 +177,12 @@ public class SubmoduleUpdateCommand extends if (ConfigConstants.CONFIG_KEY_MERGE.equals(update)) { MergeCommand merge = new MergeCommand(submoduleRepo); merge.include(commit); + merge.setStrategy(strategy); merge.call(); } else if (ConfigConstants.CONFIG_KEY_REBASE.equals(update)) { RebaseCommand rebase = new RebaseCommand(submoduleRepo); rebase.setUpstream(commit); + rebase.setStrategy(strategy); rebase.call(); } else { // Checkout commit referenced in parent repository's @@ -204,4 +209,15 @@ public class SubmoduleUpdateCommand extends throw new InvalidConfigurationException(e.getMessage(), e); } } + + /** + * @param strategy + * The merge strategy to use during this update operation. + * @return {@code this} + * @since 3.4 + */ + public SubmoduleUpdateCommand setStrategy(MergeStrategy strategy) { + this.strategy = strategy; + return this; + } } -- 2.39.5