String ourName = calculateOurName(headRef);
String revertName = srcCommit.getId().abbreviate(7).name()
+ " " + srcCommit.getShortMessage(); //$NON-NLS-1$
ResolveMerger merger = (ResolveMerger) strategy.newMerger(repo);
merger.setWorkingTreeIterator(new FileTreeIterator(repo));
merger.setBase(srcCommit.getTree());
merger.setCommitNames(new String[] {
"BASE", ourName, revertName }); //$NON-NLS-1$
String shortMessage = "Revert \"" + srcCommit.getShortMessage() //$NON-NLS-1$
+ "\""; //$NON-NLS-1$
String newMessage = shortMessage + "\n\n" //$NON-NLS-1$
+ "This reverts commit " + srcCommit.getId().getName() //$NON-NLS-1$
+ ".\n"; //$NON-NLS-1$
if (merger.merge(headCommit, srcParent)) {
if (AnyObjectId.equals(headCommit.getTree().getId(), merger
.getResultTreeId()))
continue;
DirCacheCheckout dco = new DirCacheCheckout(repo,
headCommit.getTree(), repo.lockDirCache(),
merger.getResultTreeId());
dco.setFailOnConflict(true);
dco.checkout();
newHead = new Git(getRepository()).commit()
.setMessage(newMessage)
.setReflogComment("revert: " + shortMessage).call(); //$NON-NLS-1$
revertedRefs.add(src);
headCommit = newHead;
} else {
unmergedPaths = merger.getUnmergedPaths();
Map<String, MergeFailureReason> failingPaths = merger
.getFailingPaths();
if (failingPaths != null)
failingResult = new MergeResult(null,
merger.getBaseCommitId(),
new ObjectId[] { headCommit.getId(),
srcParent.getId() },
MergeStatus.FAILED, strategy,
merger.getMergeResults(), failingPaths, null);
else
failingResult = new MergeResult(null,
merger.getBaseCommitId(),
new ObjectId[] { headCommit.getId(),
srcParent.getId() },
MergeStatus.CONFLICTING, strategy,
merger.getMergeResults(), failingPaths, null);
if (!merger.failed() && !unmergedPaths.isEmpty()) {
String message = new MergeMessageFormatter()
.formatWithConflicts(newMessage,
merger.getUnmergedPaths());
repo.writeRevertHead(srcCommit.getId());
repo.writeMergeCommitMsg(message);
}
return null;
}