InvalidMergeHeadsException, WrongRepositoryStateException, NoMessageException {
checkCallable();
checkParameters();
RevWalk revWalk = null;
DirCacheCheckout dco = null;
try {
Ref head = repo.getRef(Constants.HEAD);
if (head == null)
throw new NoHeadException(
JGitText.get().commitOnRepoWithoutHEADCurrentlyNotSupported);
StringBuilder refLogMessage = new StringBuilder("merge ");
// Check for FAST_FORWARD, ALREADY_UP_TO_DATE
revWalk = new RevWalk(repo);
// we know for now there is only one commit
Ref ref = commits.get(0);
refLogMessage.append(ref.getName());
// handle annotated tags
ObjectId objectId = ref.getPeeledObjectId();
if (objectId == null)
objectId = ref.getObjectId();
RevCommit srcCommit = revWalk.lookupCommit(objectId);
ObjectId headId = head.getObjectId();
if (headId == null) {
revWalk.parseHeaders(srcCommit);
dco = new DirCacheCheckout(repo,
repo.lockDirCache(), srcCommit.getTree());
dco.setFailOnConflict(true);
dco.checkout();
RefUpdate refUpdate = repo
.updateRef(head.getTarget().getName());
refUpdate.setNewObjectId(objectId);
refUpdate.setExpectedOldObjectId(null);
refUpdate.setRefLogMessage("initial pull", false);
if (refUpdate.update() != Result.NEW)
throw new NoHeadException(
JGitText.get().commitOnRepoWithoutHEADCurrentlyNotSupported);
setCallable(false);
return new MergeResult(srcCommit, srcCommit, new ObjectId[] {
null, srcCommit }, MergeStatus.FAST_FORWARD,
mergeStrategy, null, null);
}
RevCommit headCommit = revWalk.lookupCommit(headId);
if (revWalk.isMergedInto(srcCommit, headCommit)) {
setCallable(false);
return new MergeResult(headCommit, srcCommit, new ObjectId[] {
headCommit, srcCommit },
MergeStatus.ALREADY_UP_TO_DATE, mergeStrategy, null, null);
} else if (revWalk.isMergedInto(headCommit, srcCommit)
&& fastForwardMode == FastForwardMode.FF) {
// FAST_FORWARD detected: skip doing a real merge but only
// update HEAD
refLogMessage.append(": " + MergeStatus.FAST_FORWARD);
dco = new DirCacheCheckout(repo,
headCommit.getTree(), repo.lockDirCache(),
srcCommit.getTree());
dco.setFailOnConflict(true);
dco.checkout();
String msg = null;
ObjectId newHead, base = null;
MergeStatus mergeStatus = null;
if (!squash) {
updateHead(refLogMessage, srcCommit, headId);
newHead = base = srcCommit;
mergeStatus = MergeStatus.FAST_FORWARD;
} else {
msg = JGitText.get().squashCommitNotUpdatingHEAD;
newHead = base = headId;
mergeStatus = MergeStatus.FAST_FORWARD_SQUASHED;
List<RevCommit> squashedCommits = RevWalkUtils.find(
revWalk, srcCommit, headCommit);
String squashMessage = new SquashMessageFormatter().format(
squashedCommits, head);
repo.writeSquashCommitMsg(squashMessage);
}
setCallable(false);
return new MergeResult(newHead, base, new ObjectId[] {
headCommit, srcCommit }, mergeStatus, mergeStrategy,
null, msg);
} else {
if (fastForwardMode == FastForwardMode.FF_ONLY) {
return new MergeResult(headCommit, srcCommit,
new ObjectId[] { headCommit, srcCommit },
MergeStatus.ABORTED, mergeStrategy, null, null);
}
String mergeMessage = "";
if (!squash) {
mergeMessage = new MergeMessageFormatter().format(
commits, head);
repo.writeMergeCommitMsg(mergeMessage);
repo.writeMergeHeads(Arrays.asList(ref.getObjectId()));
} else {
List<RevCommit> squashedCommits = RevWalkUtils.find(
revWalk, srcCommit, headCommit);
String squashMessage = new SquashMessageFormatter().format(
squashedCommits, head);
repo.writeSquashCommitMsg(squashMessage);
}
Merger merger = mergeStrategy.newMerger(repo);
boolean noProblems;
Map<String, org.eclipse.jgit.merge.MergeResult<?>> lowLevelResults = null;
Map<String, MergeFailureReason> failingPaths = null;
List<String> unmergedPaths = null;
if (merger instanceof ResolveMerger) {
ResolveMerger resolveMerger = (ResolveMerger) merger;
resolveMerger.setCommitNames(new String[] {
"BASE", "HEAD", ref.getName() });
resolveMerger.setWorkingTreeIterator(new FileTreeIterator(repo));
noProblems = merger.merge(headCommit, srcCommit);
lowLevelResults = resolveMerger
.getMergeResults();
failingPaths = resolveMerger.getFailingPaths();
unmergedPaths = resolveMerger.getUnmergedPaths();
} else
noProblems = merger.merge(headCommit, srcCommit);
refLogMessage.append(": Merge made by ");
if (!revWalk.isMergedInto(headCommit, srcCommit))
refLogMessage.append(mergeStrategy.getName());
else
refLogMessage.append("recursive");
refLogMessage.append('.');
if (noProblems) {
dco = new DirCacheCheckout(repo,
headCommit.getTree(), repo.lockDirCache(),
merger.getResultTreeId());
dco.setFailOnConflict(true);
dco.checkout();
String msg = null;
RevCommit newHead = null;
MergeStatus mergeStatus = null;
if (!squash) {
newHead = new Git(getRepository()).commit()
.setReflogComment(refLogMessage.toString()).call();
mergeStatus = MergeStatus.MERGED;
} else {
msg = JGitText.get().squashCommitNotUpdatingHEAD;
newHead = headCommit;
mergeStatus = MergeStatus.MERGED_SQUASHED;
}
return new MergeResult(newHead.getId(), null,
new ObjectId[] { headCommit.getId(),
srcCommit.getId() }, mergeStatus,
mergeStrategy, null, msg);
} else {
if (failingPaths != null) {
repo.writeMergeCommitMsg(null);
repo.writeMergeHeads(null);
return new MergeResult(null,
merger.getBaseCommit(0, 1),
new ObjectId[] {
headCommit.getId(), srcCommit.getId() },
MergeStatus.FAILED, mergeStrategy,
lowLevelResults, failingPaths, null);
} else {
String mergeMessageWithConflicts = new MergeMessageFormatter()
.formatWithConflicts(mergeMessage,
unmergedPaths);
repo.writeMergeCommitMsg(mergeMessageWithConflicts);
return new MergeResult(null,
merger.getBaseCommit(0, 1),
new ObjectId[] { headCommit.getId(),
srcCommit.getId() },
MergeStatus.CONFLICTING, mergeStrategy,
lowLevelResults, null);
}
}
}
} catch (org.eclipse.jgit.errors.CheckoutConflictException e) {
List<String> conflicts = (dco == null) ? Collections
.<String> emptyList() : dco.getConflicts();
throw new CheckoutConflictException(conflicts, e);
} catch (IOException e) {
throw new JGitInternalException(
MessageFormat.format(
JGitText.get().exceptionCaughtDuringExecutionOfMergeCommand,