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 String ourCommitName = null;
private MergeStrategy strategy = MergeStrategy.RECURSIVE;
/** /**
* @param repo * @param repo
*/ */
@ -151,8 +153,7 @@ public class CherryPickCommand extends GitCommand<CherryPickResult> {
String cherryPickName = srcCommit.getId().abbreviate(7).name() String cherryPickName = srcCommit.getId().abbreviate(7).name()
+ " " + srcCommit.getShortMessage(); //$NON-NLS-1$ + " " + srcCommit.getShortMessage(); //$NON-NLS-1$
ResolveMerger merger = (ResolveMerger) MergeStrategy.RECURSIVE ResolveMerger merger = (ResolveMerger) strategy.newMerger(repo);
.newMerger(repo);
merger.setWorkingTreeIterator(new FileTreeIterator(repo)); merger.setWorkingTreeIterator(new FileTreeIterator(repo));
merger.setBase(srcParent.getTree()); merger.setBase(srcParent.getTree());
merger.setCommitNames(new String[] { "BASE", ourName, merger.setCommitNames(new String[] { "BASE", ourName,
@ -259,6 +260,17 @@ public class CherryPickCommand extends GitCommand<CherryPickResult> {
return this; 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) { private String calculateOurName(Ref headRef) {
if (ourCommitName != null) if (ourCommitName != null)
return ourCommitName; 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.Ref;
import org.eclipse.jgit.lib.Repository; import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.lib.RepositoryState; import org.eclipse.jgit.lib.RepositoryState;
import org.eclipse.jgit.merge.MergeStrategy;
import org.eclipse.jgit.transport.FetchResult; import org.eclipse.jgit.transport.FetchResult;
/** /**
@ -86,6 +87,8 @@ public class PullCommand extends TransportCommand<PullCommand, PullResult> {
private String remoteBranchName; private String remoteBranchName;
private MergeStrategy strategy = MergeStrategy.RECURSIVE;
private enum PullRebaseMode { private enum PullRebaseMode {
USE_CONFIG, USE_CONFIG,
REBASE, REBASE,
@ -299,13 +302,14 @@ public class PullCommand extends TransportCommand<PullCommand, PullResult> {
if (doRebase) { if (doRebase) {
RebaseCommand rebase = new RebaseCommand(repo); RebaseCommand rebase = new RebaseCommand(repo);
RebaseResult rebaseRes = rebase.setUpstream(commitToMerge) RebaseResult rebaseRes = rebase.setUpstream(commitToMerge)
.setUpstreamName(upstreamName) .setUpstreamName(upstreamName).setProgressMonitor(monitor)
.setProgressMonitor(monitor).setOperation(Operation.BEGIN) .setOperation(Operation.BEGIN).setStrategy(strategy)
.call(); .call();
result = new PullResult(fetchRes, remote, rebaseRes); result = new PullResult(fetchRes, remote, rebaseRes);
} else { } else {
MergeCommand merge = new MergeCommand(repo); MergeCommand merge = new MergeCommand(repo);
merge.include(upstreamName, commitToMerge); merge.include(upstreamName, commitToMerge);
merge.setStrategy(strategy);
MergeResult mergeRes = merge.call(); MergeResult mergeRes = merge.call();
monitor.update(1); monitor.update(1);
result = new PullResult(fetchRes, remote, mergeRes); result = new PullResult(fetchRes, remote, mergeRes);
@ -363,4 +367,15 @@ public class PullCommand extends TransportCommand<PullCommand, PullResult> {
public String getRemoteBranchName() { public String getRemoteBranchName() {
return remoteBranchName; 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;
import org.eclipse.jgit.lib.RefUpdate.Result; import org.eclipse.jgit.lib.RefUpdate.Result;
import org.eclipse.jgit.lib.Repository; import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.merge.MergeStrategy;
import org.eclipse.jgit.revwalk.RevCommit; import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.revwalk.RevWalk; import org.eclipse.jgit.revwalk.RevWalk;
import org.eclipse.jgit.treewalk.TreeWalk; import org.eclipse.jgit.treewalk.TreeWalk;
@ -211,6 +212,8 @@ public class RebaseCommand extends GitCommand<RebaseResult> {
private boolean lastStepWasForward; private boolean lastStepWasForward;
private MergeStrategy strategy = MergeStrategy.RECURSIVE;
/** /**
* @param repo * @param repo
*/ */
@ -375,7 +378,8 @@ public class RebaseCommand extends GitCommand<RebaseResult> {
String stash = rebaseState.readFile(AUTOSTASH); String stash = rebaseState.readFile(AUTOSTASH);
try { try {
Git.wrap(repo).stashApply().setStashRef(stash) Git.wrap(repo).stashApply().setStashRef(stash)
.ignoreRepositoryState(true).call(); .ignoreRepositoryState(true).setStrategy(strategy)
.call();
} catch (StashApplyFailureException e) { } catch (StashApplyFailureException e) {
conflicts = true; conflicts = true;
RevWalk rw = new RevWalk(repo); RevWalk rw = new RevWalk(repo);
@ -474,7 +478,7 @@ public class RebaseCommand extends GitCommand<RebaseResult> {
String ourCommitName = getOurCommitName(); String ourCommitName = getOurCommitName();
CherryPickResult cherryPickResult = new Git(repo).cherryPick() CherryPickResult cherryPickResult = new Git(repo).cherryPick()
.include(commitToPick).setOurCommitName(ourCommitName) .include(commitToPick).setOurCommitName(ourCommitName)
.setReflogPrefix("rebase:").call(); //$NON-NLS-1$ .setReflogPrefix("rebase:").setStrategy(strategy).call(); //$NON-NLS-1$
switch (cherryPickResult.getStatus()) { switch (cherryPickResult.getStatus()) {
case FAILED: case FAILED:
if (operation == Operation.BEGIN) if (operation == Operation.BEGIN)
@ -1302,6 +1306,17 @@ public class RebaseCommand extends GitCommand<RebaseResult> {
return this; 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 * 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 List<String> unmergedPaths;
private MergeStrategy strategy = MergeStrategy.RECURSIVE;
/** /**
* @param repo * @param repo
*/ */
@ -160,8 +162,7 @@ public class RevertCommand extends GitCommand<RevCommit> {
String revertName = srcCommit.getId().abbreviate(7).name() String revertName = srcCommit.getId().abbreviate(7).name()
+ " " + srcCommit.getShortMessage(); //$NON-NLS-1$ + " " + srcCommit.getShortMessage(); //$NON-NLS-1$
ResolveMerger merger = (ResolveMerger) MergeStrategy.RECURSIVE ResolveMerger merger = (ResolveMerger) strategy.newMerger(repo);
.newMerger(repo);
merger.setWorkingTreeIterator(new FileTreeIterator(repo)); merger.setWorkingTreeIterator(new FileTreeIterator(repo));
merger.setBase(srcCommit.getTree()); merger.setBase(srcCommit.getTree());
merger.setCommitNames(new String[] { merger.setCommitNames(new String[] {
@ -194,15 +195,14 @@ public class RevertCommand extends GitCommand<RevCommit> {
merger.getBaseCommitId(), merger.getBaseCommitId(),
new ObjectId[] { headCommit.getId(), new ObjectId[] { headCommit.getId(),
srcParent.getId() }, srcParent.getId() },
MergeStatus.FAILED, MergeStrategy.RECURSIVE, MergeStatus.FAILED, strategy,
merger.getMergeResults(), failingPaths, null); merger.getMergeResults(), failingPaths, null);
else else
failingResult = new MergeResult(null, failingResult = new MergeResult(null,
merger.getBaseCommitId(), merger.getBaseCommitId(),
new ObjectId[] { headCommit.getId(), new ObjectId[] { headCommit.getId(),
srcParent.getId() }, srcParent.getId() },
MergeStatus.CONFLICTING, MergeStatus.CONFLICTING, strategy,
MergeStrategy.RECURSIVE,
merger.getMergeResults(), failingPaths, null); merger.getMergeResults(), failingPaths, null);
if (!merger.failed() && !unmergedPaths.isEmpty()) { if (!merger.failed() && !unmergedPaths.isEmpty()) {
String message = new MergeMessageFormatter() String message = new MergeMessageFormatter()
@ -301,4 +301,15 @@ public class RevertCommand extends GitCommand<RevCommit> {
public List<String> getUnmergedPaths() { public List<String> getUnmergedPaths() {
return unmergedPaths; 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 boolean ignoreRepositoryState;
private MergeStrategy strategy = MergeStrategy.RECURSIVE;
/** /**
* Create command to apply the changes of a stashed commit * Create command to apply the changes of a stashed commit
* *
@ -181,8 +183,7 @@ public class StashApplyCommand extends GitCommand<ObjectId> {
.getParent(1)); .getParent(1));
ObjectId stashHeadCommit = stashCommit.getParent(0); ObjectId stashHeadCommit = stashCommit.getParent(0);
ResolveMerger merger = (ResolveMerger) MergeStrategy.RECURSIVE ResolveMerger merger = (ResolveMerger) strategy.newMerger(repo);
.newMerger(repo);
merger.setCommitNames(new String[] { "stashed HEAD", "HEAD", merger.setCommitNames(new String[] { "stashed HEAD", "HEAD",
"stash" }); "stash" });
merger.setBase(stashHeadCommit); merger.setBase(stashHeadCommit);
@ -194,7 +195,7 @@ public class StashApplyCommand extends GitCommand<ObjectId> {
dco.setFailOnConflict(true); dco.setFailOnConflict(true);
dco.checkout(); // Ignoring failed deletes.... dco.checkout(); // Ignoring failed deletes....
if (applyIndex) { if (applyIndex) {
ResolveMerger ixMerger = (ResolveMerger) MergeStrategy.RECURSIVE ResolveMerger ixMerger = (ResolveMerger) strategy
.newMerger(repo, true); .newMerger(repo, true);
ixMerger.setCommitNames(new String[] { "stashed HEAD", ixMerger.setCommitNames(new String[] { "stashed HEAD",
"HEAD", "stashed index" }); "HEAD", "stashed index" });
@ -231,6 +232,18 @@ public class StashApplyCommand extends GitCommand<ObjectId> {
this.applyIndex = applyIndex; 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 { private void resetIndex(RevTree tree) throws IOException {
DirCache dc = repo.lockDirCache(); DirCache dc = repo.lockDirCache();
TreeWalk walk = null; 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.ProgressMonitor;
import org.eclipse.jgit.lib.RefUpdate; import org.eclipse.jgit.lib.RefUpdate;
import org.eclipse.jgit.lib.Repository; import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.merge.MergeStrategy;
import org.eclipse.jgit.revwalk.RevCommit; import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.revwalk.RevWalk; import org.eclipse.jgit.revwalk.RevWalk;
import org.eclipse.jgit.submodule.SubmoduleWalk; import org.eclipse.jgit.submodule.SubmoduleWalk;
@ -84,6 +85,8 @@ public class SubmoduleUpdateCommand extends
private final Collection<String> paths; private final Collection<String> paths;
private MergeStrategy strategy = MergeStrategy.RECURSIVE;
/** /**
* @param repo * @param repo
*/ */
@ -174,10 +177,12 @@ public class SubmoduleUpdateCommand extends
if (ConfigConstants.CONFIG_KEY_MERGE.equals(update)) { if (ConfigConstants.CONFIG_KEY_MERGE.equals(update)) {
MergeCommand merge = new MergeCommand(submoduleRepo); MergeCommand merge = new MergeCommand(submoduleRepo);
merge.include(commit); merge.include(commit);
merge.setStrategy(strategy);
merge.call(); merge.call();
} else if (ConfigConstants.CONFIG_KEY_REBASE.equals(update)) { } else if (ConfigConstants.CONFIG_KEY_REBASE.equals(update)) {
RebaseCommand rebase = new RebaseCommand(submoduleRepo); RebaseCommand rebase = new RebaseCommand(submoduleRepo);
rebase.setUpstream(commit); rebase.setUpstream(commit);
rebase.setStrategy(strategy);
rebase.call(); rebase.call();
} else { } else {
// Checkout commit referenced in parent repository's // Checkout commit referenced in parent repository's
@ -204,4 +209,15 @@ public class SubmoduleUpdateCommand extends
throw new InvalidConfigurationException(e.getMessage(), e); 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