} else {
parents.add(0, headId);
}
// lock the index
DirCache index = repo.lockDirCache();
try {
if (!only.isEmpty())
index = createTemporaryIndex(headId, index);
ObjectInserter odi = repo.newObjectInserter();
try {
// Write the index as tree to the object database. This may
// fail for example when the index contains unmerged paths
// (unresolved conflicts)
ObjectId indexTreeId = index.writeTree(odi);
if (insertChangeId)
insertChangeId(indexTreeId);
// Create a Commit object, populate it and write it
CommitBuilder commit = new CommitBuilder();
commit.setCommitter(committer);
commit.setAuthor(author);
commit.setMessage(message);
commit.setParentIds(parents);
commit.setTreeId(indexTreeId);
ObjectId commitId = odi.insert(commit);
odi.flush();
RevWalk revWalk = new RevWalk(repo);
try {
RevCommit revCommit = revWalk.parseCommit(commitId);
RefUpdate ru = repo.updateRef(Constants.HEAD);
ru.setNewObjectId(commitId);
String prefix = amend ? "commit (amend): " : "commit: ";
ru.setRefLogMessage(
prefix + revCommit.getShortMessage(), false);
ru.setExpectedOldObjectId(headId);
Result rc = ru.forceUpdate();
switch (rc) {
case NEW:
case FORCED:
case FAST_FORWARD: {
setCallable(false);
if (state == RepositoryState.MERGING_RESOLVED) {
// Commit was successful. Now delete the files
// used for merge commits
repo.writeMergeCommitMsg(null);
repo.writeMergeHeads(null);
} else if (state == RepositoryState.CHERRY_PICKING_RESOLVED) {
repo.writeMergeCommitMsg(null);
repo.writeCherryPickHead(null);
}
return revCommit;
}
case REJECTED:
case LOCK_FAILURE:
throw new ConcurrentRefUpdateException(JGitText
.get().couldNotLockHEAD, ru.getRef(), rc);
default:
throw new JGitInternalException(MessageFormat
.format(JGitText.get().updatingRefFailed,
Constants.HEAD,
commitId.toString(), rc));
}
} finally {
revWalk.release();
}
} finally {
odi.release();
}
} finally {
index.unlock();
}
} catch (UnmergedPathException e) {
// since UnmergedPathException is a subclass of IOException
// which should not be wrapped by a JGitInternalException we
// have to catch and re-throw it here