Browse Source

Allow callers to use different merging strategies

Signed-off-by: Laurent Goubet <laurent.goubet@obeo.fr>
Change-Id: I84e9d7b4b772b4ad7d3e7010aad78291d4d178fe
stable-3.4
Laurent Goubet 11 years ago
parent
commit
7424d58255
  1. 16
      org.eclipse.jgit/src/org/eclipse/jgit/api/CherryPickCommand.java
  2. 19
      org.eclipse.jgit/src/org/eclipse/jgit/api/PullCommand.java
  3. 19
      org.eclipse.jgit/src/org/eclipse/jgit/api/RebaseCommand.java
  4. 21
      org.eclipse.jgit/src/org/eclipse/jgit/api/RevertCommand.java
  5. 19
      org.eclipse.jgit/src/org/eclipse/jgit/api/StashApplyCommand.java
  6. 16
      org.eclipse.jgit/src/org/eclipse/jgit/api/SubmoduleUpdateCommand.java

16
org.eclipse.jgit/src/org/eclipse/jgit/api/CherryPickCommand.java

@ -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;

19
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<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;
}
}

19
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<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
*/

21
org.eclipse.jgit/src/org/eclipse/jgit/api/RevertCommand.java

@ -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;
}
}

19
org.eclipse.jgit/src/org/eclipse/jgit/api/StashApplyCommand.java

@ -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;

16
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<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;
}
}

Loading…
Cancel
Save