in org.eclipse.jgit/src/org/eclipse/jgit/api/CommitCommand.java [169:309]
public RevCommit call() throws GitAPIException, AbortedByHookException,
ConcurrentRefUpdateException, NoHeadException, NoMessageException,
ServiceUnavailableException, UnmergedPathsException,
WrongRepositoryStateException {
checkCallable();
Collections.sort(only);
try (RevWalk rw = new RevWalk(repo)) {
RepositoryState state = repo.getRepositoryState();
if (!state.canCommit())
throw new WrongRepositoryStateException(MessageFormat.format(
JGitText.get().cannotCommitOnARepoWithState,
state.name()));
if (!noVerify) {
Hooks.preCommit(repo, hookOutRedirect.get(PreCommitHook.NAME),
hookErrRedirect.get(PreCommitHook.NAME))
.call();
}
processOptions(state, rw);
if (all && !repo.isBare()) {
try (Git git = new Git(repo)) {
git.add().addFilepattern(".") //$NON-NLS-1$
.setUpdate(true).call();
} catch (NoFilepatternException e) {
// should really not happen
throw new JGitInternalException(e.getMessage(), e);
}
}
Ref head = repo.exactRef(Constants.HEAD);
if (head == null)
throw new NoHeadException(
JGitText.get().commitOnRepoWithoutHEADCurrentlyNotSupported);
// determine the current HEAD and the commit it is referring to
ObjectId headId = repo.resolve(Constants.HEAD + "^{commit}"); //$NON-NLS-1$
if (headId == null && amend)
throw new WrongRepositoryStateException(
JGitText.get().commitAmendOnInitialNotPossible);
if (headId != null) {
if (amend) {
RevCommit previousCommit = rw.parseCommit(headId);
for (RevCommit p : previousCommit.getParents())
parents.add(p.getId());
if (author == null)
author = previousCommit.getAuthorIdent();
} else {
parents.add(0, headId);
}
}
if (!noVerify) {
message = Hooks
.commitMsg(repo,
hookOutRedirect.get(CommitMsgHook.NAME),
hookErrRedirect.get(CommitMsgHook.NAME))
.setCommitMessage(message).call();
}
CommitConfig config = null;
if (CleanupMode.DEFAULT.equals(cleanupMode)) {
config = repo.getConfig().get(CommitConfig.KEY);
cleanupMode = config.resolve(cleanupMode, cleanDefaultIsStrip);
}
char comments = (char) 0;
if (CleanupMode.STRIP.equals(cleanupMode)
|| CleanupMode.SCISSORS.equals(cleanupMode)) {
if (commentChar == null) {
if (config == null) {
config = repo.getConfig().get(CommitConfig.KEY);
}
if (config.isAutoCommentChar()) {
// We're supposed to pick a character that isn't used,
// but then cleaning up won't remove any lines. So don't
// bother.
comments = (char) 0;
cleanupMode = CleanupMode.WHITESPACE;
} else {
comments = config.getCommentChar();
}
} else {
comments = commentChar.charValue();
}
}
message = CommitConfig.cleanText(message, cleanupMode, comments);
RevCommit revCommit;
DirCache index = repo.lockDirCache();
try (ObjectInserter odi = repo.newObjectInserter()) {
if (!only.isEmpty())
index = createTemporaryIndex(headId, index, rw);
// 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);
checkIfEmpty(rw, headId, 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);
if (signCommit.booleanValue()) {
sign(commit);
}
ObjectId commitId = odi.insert(commit);
odi.flush();
revCommit = rw.parseCommit(commitId);
updateRef(state, headId, revCommit, commitId);
} finally {
index.unlock();
}
try {
Hooks.postCommit(repo, hookOutRedirect.get(PostCommitHook.NAME),
hookErrRedirect.get(PostCommitHook.NAME)).call();
} catch (Exception e) {
log.error(MessageFormat.format(
JGitText.get().postCommitHookFailed, e.getMessage()),
e);
}
return revCommit;
} catch (UnmergedPathException e) {
throw new UnmergedPathsException(e);
} catch (IOException e) {
throw new JGitInternalException(
JGitText.get().exceptionCaughtDuringExecutionOfCommitCommand, e);
}
}