AnyObjectId headId = headRef.getObjectId();
RevCommit headCommit = headId == null ? null : revWalk
.parseCommit(headId);
RevCommit newCommit = revWalk.parseCommit(branch);
RevTree headTree = headCommit == null ? null : headCommit.getTree();
DirCacheCheckout dco;
DirCache dc = repo.lockDirCache();
try {
dco = new DirCacheCheckout(repo, headTree, dc,
newCommit.getTree());
dco.setFailOnConflict(true);
try {
dco.checkout();
} catch (org.eclipse.jgit.errors.CheckoutConflictException e) {
status = new CheckoutResult(Status.CONFLICTS,
dco.getConflicts());
throw new CheckoutConflictException(dco.getConflicts(), e);
}
} finally {
dc.unlock();
}
Ref ref = repo.getRef(name);
if (ref != null && !ref.getName().startsWith(Constants.R_HEADS))
ref = null;
String toName = Repository.shortenRefName(name);
RefUpdate refUpdate = repo.updateRef(Constants.HEAD, ref == null);
refUpdate.setForceUpdate(force);
refUpdate.setRefLogMessage(refLogMessage + " to " + toName, false); //$NON-NLS-1$
Result updateResult;
if (ref != null)
updateResult = refUpdate.link(ref.getName());
else if (orphan) {
updateResult = refUpdate.link(getBranchName());
ref = repo.getRef(Constants.HEAD);
} else {
refUpdate.setNewObjectId(newCommit);
updateResult = refUpdate.forceUpdate();
}
setCallable(false);
boolean ok = false;
switch (updateResult) {
case NEW:
ok = true;
break;
case NO_CHANGE:
case FAST_FORWARD:
case FORCED:
ok = true;
break;
default:
break;
}
if (!ok)
throw new JGitInternalException(MessageFormat.format(JGitText
.get().checkoutUnexpectedResult, updateResult.name()));
if (!dco.getToBeDeleted().isEmpty()) {
status = new CheckoutResult(Status.NONDELETED,
dco.getToBeDeleted());
} else
status = new CheckoutResult(new ArrayList<String>(dco
.getUpdated().keySet()), dco.getRemoved());
return ref;
} catch (IOException ioe) {
throw new JGitInternalException(ioe.getMessage(), ioe);
} finally {