]> source.dussan.org Git - jgit.git/commitdiff
Allow callers to use different merging strategies 59/22559/4
authorLaurent Goubet <laurent.goubet@obeo.fr>
Tue, 21 Jan 2014 15:35:26 +0000 (16:35 +0100)
committerLaurent Goubet <laurent.goubet@obeo.fr>
Tue, 6 May 2014 14:30:34 +0000 (16:30 +0200)
Signed-off-by: Laurent Goubet <laurent.goubet@obeo.fr>
Change-Id: I84e9d7b4b772b4ad7d3e7010aad78291d4d178fe

org.eclipse.jgit/src/org/eclipse/jgit/api/CherryPickCommand.java
org.eclipse.jgit/src/org/eclipse/jgit/api/PullCommand.java
org.eclipse.jgit/src/org/eclipse/jgit/api/RebaseCommand.java
org.eclipse.jgit/src/org/eclipse/jgit/api/RevertCommand.java
org.eclipse.jgit/src/org/eclipse/jgit/api/StashApplyCommand.java
org.eclipse.jgit/src/org/eclipse/jgit/api/SubmoduleUpdateCommand.java

index 192a37767852fad1b510780fb7ce74b88d4362d2..b3415b3505c6f2f39fa7a0dc2c988a64118ef7e2 100644 (file)
@@ -88,6 +88,8 @@ public class CherryPickCommand extends GitCommand<CherryPickResult> {
 
        private String ourCommitName = null;
 
+       private MergeStrategy strategy = MergeStrategy.RECURSIVE;
+
        /**
         * @param repo
         */
@@ -151,8 +153,7 @@ public class CherryPickCommand extends GitCommand<CherryPickResult> {
                                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<CherryPickResult> {
                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;
index d92adfe3702f621cadbb97be0db0321e36aa81c0..7634957eb26cc209fdb12a6061d0efb31cfa4d8f 100644 (file)
@@ -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<PullCommand, PullResult> {
 
        private String remoteBranchName;
 
+       private MergeStrategy strategy = MergeStrategy.RECURSIVE;
+
        private enum PullRebaseMode {
                USE_CONFIG,
                REBASE,
@@ -299,13 +302,14 @@ public class PullCommand extends TransportCommand<PullCommand, PullResult> {
                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<PullCommand, PullResult> {
        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;
+       }
 }
index 3b849174216f1320a193b591880dc05e42dc0080..95551c3ee08322e759229d1b867f6170f9ffb674 100644 (file)
@@ -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<RebaseResult> {
 
        private boolean lastStepWasForward;
 
+       private MergeStrategy strategy = MergeStrategy.RECURSIVE;
+
        /**
         * @param repo
         */
@@ -375,7 +378,8 @@ public class RebaseCommand extends GitCommand<RebaseResult> {
                        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<RebaseResult> {
                                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<RebaseResult> {
                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
         */
index bc1b29c2f6b9b4b4ce42c7d4b70762dc345c3ae0..52327d76c72279de728f9233e9c923a8fd3e1774 100644 (file)
@@ -95,6 +95,8 @@ public class RevertCommand extends GitCommand<RevCommit> {
 
        private List<String> unmergedPaths;
 
+       private MergeStrategy strategy = MergeStrategy.RECURSIVE;
+
        /**
         * @param repo
         */
@@ -160,8 +162,7 @@ public class RevertCommand extends GitCommand<RevCommit> {
                                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<RevCommit> {
                                                                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<RevCommit> {
        public List<String> 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;
+       }
 }
index 8440d8b9505318ee3fb373615bfbc19ec5713db2..f73ce831feeb25f917c4735bee786acaaa447ef5 100644 (file)
@@ -92,6 +92,8 @@ public class StashApplyCommand extends GitCommand<ObjectId> {
 
        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<ObjectId> {
                                        .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<ObjectId> {
                                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<ObjectId> {
                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;
index e2f356a08eff3cdfbedcd00016ad6d2d441719df..de1a3e9fd0034aefae7bb7dc4c77838e4e613806 100644 (file)
@@ -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<String> 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;
+       }
 }