{
// Does the same as the original git-cherryPick
// except commiting after running merger
Repository repo = git.getRepository();
RevCommit newHead = null;
List<Ref> cherryPickedRefs = new LinkedList<Ref>();
RevWalk revWalk = new RevWalk(repo);
try
{
// get the head commit
Ref headRef = repo.getRef(Constants.HEAD);
if (headRef == null)
throw new NoHeadException(
JGitText.get().commitOnRepoWithoutHEADCurrentlyNotSupported);
RevCommit headCommit = revWalk.parseCommit(headRef.getObjectId());
newHead = headCommit;
// get the commit to be cherry-picked
// handle annotated tags
ObjectId srcObjectId = src.getPeeledObjectId();
if (srcObjectId == null)
srcObjectId = src.getObjectId();
RevCommit srcCommit = revWalk.parseCommit(srcObjectId);
// get the parent of the commit to cherry-pick
if (srcCommit.getParentCount() == 0)
throw new CantMergeCommitWithZeroParentsException("Commit with zero parents cannot be merged");
if (srcCommit.getParentCount() > 1)
throw new MultipleParentsNotAllowedException(
MessageFormat.format(
JGitText.get().canOnlyCherryPickCommitsWithOneParent,
srcCommit.name(),
Integer.valueOf(srcCommit.getParentCount())));
RevCommit srcParent = srcCommit.getParent(0);
revWalk.parseHeaders(srcParent);
ResolveMerger merger = (ResolveMerger) MergeStrategy.RESOLVE
.newMerger(repo);
merger.setWorkingTreeIterator(new FileTreeIterator(repo));
merger.setBase(srcParent.getTree());
if (merger.merge(headCommit, srcCommit))
{
DirCacheCheckout dco = new DirCacheCheckout(repo,
headCommit.getTree(), repo.lockDirCache(),
merger.getResultTreeId());