Refactor CommitCommand to improve readability

Change-Id: Id3cac81cd32c07f677b7f669d58e32b5290e1790
Signed-off-by: Matthias Sohn <matthias.sohn@sap.com>
This commit is contained in:
Matthias Sohn 2021-03-20 11:35:27 +01:00
parent 18c735c474
commit 502bfff7db
1 changed files with 90 additions and 72 deletions

View File

@ -20,6 +20,7 @@
import java.util.List; import java.util.List;
import org.eclipse.jgit.api.errors.AbortedByHookException; import org.eclipse.jgit.api.errors.AbortedByHookException;
import org.eclipse.jgit.api.errors.CanceledException;
import org.eclipse.jgit.api.errors.ConcurrentRefUpdateException; import org.eclipse.jgit.api.errors.ConcurrentRefUpdateException;
import org.eclipse.jgit.api.errors.EmptyCommitException; import org.eclipse.jgit.api.errors.EmptyCommitException;
import org.eclipse.jgit.api.errors.GitAPIException; import org.eclipse.jgit.api.errors.GitAPIException;
@ -36,6 +37,8 @@
import org.eclipse.jgit.dircache.DirCacheBuilder; import org.eclipse.jgit.dircache.DirCacheBuilder;
import org.eclipse.jgit.dircache.DirCacheEntry; import org.eclipse.jgit.dircache.DirCacheEntry;
import org.eclipse.jgit.dircache.DirCacheIterator; import org.eclipse.jgit.dircache.DirCacheIterator;
import org.eclipse.jgit.errors.IncorrectObjectTypeException;
import org.eclipse.jgit.errors.MissingObjectException;
import org.eclipse.jgit.errors.UnmergedPathException; import org.eclipse.jgit.errors.UnmergedPathException;
import org.eclipse.jgit.hooks.CommitMsgHook; import org.eclipse.jgit.hooks.CommitMsgHook;
import org.eclipse.jgit.hooks.Hooks; import org.eclipse.jgit.hooks.Hooks;
@ -230,26 +233,59 @@ public RevCommit call() throws GitAPIException, AbortedByHookException,
if (insertChangeId) if (insertChangeId)
insertChangeId(indexTreeId); insertChangeId(indexTreeId);
// Check for empty commits checkIfEmpty(rw, headId, indexTreeId);
if (headId != null && !allowEmpty.booleanValue()) {
RevCommit headCommit = rw.parseCommit(headId);
headCommit.getTree();
if (indexTreeId.equals(headCommit.getTree())) {
throw new EmptyCommitException(
JGitText.get().emptyCommit);
}
}
// Create a Commit object, populate it and write it // Create a Commit object, populate it and write it
CommitBuilder commit = new CommitBuilder(); CommitBuilder commit = new CommitBuilder();
commit.setCommitter(committer); commit.setCommitter(committer);
commit.setAuthor(author); commit.setAuthor(author);
commit.setMessage(message); commit.setMessage(message);
commit.setParentIds(parents); commit.setParentIds(parents);
commit.setTreeId(indexTreeId); commit.setTreeId(indexTreeId);
if (signCommit.booleanValue()) { 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);
}
}
private void checkIfEmpty(RevWalk rw, ObjectId headId, ObjectId indexTreeId)
throws EmptyCommitException, MissingObjectException,
IncorrectObjectTypeException, IOException {
if (headId != null && !allowEmpty.booleanValue()) {
RevCommit headCommit = rw.parseCommit(headId);
headCommit.getTree();
if (indexTreeId.equals(headCommit.getTree())) {
throw new EmptyCommitException(JGitText.get().emptyCommit);
}
}
}
private void sign(CommitBuilder commit) throws ServiceUnavailableException,
CanceledException, UnsupportedSigningFormatException {
if (gpgSigner == null) { if (gpgSigner == null) {
throw new ServiceUnavailableException( throw new ServiceUnavailableException(
JGitText.get().signingServiceUnavailable); JGitText.get().signingServiceUnavailable);
@ -268,10 +304,9 @@ public RevCommit call() throws GitAPIException, AbortedByHookException,
} }
} }
ObjectId commitId = odi.insert(commit); private void updateRef(RepositoryState state, ObjectId headId,
odi.flush(); RevCommit revCommit, ObjectId commitId)
throws ConcurrentRefUpdateException, IOException {
revCommit = rw.parseCommit(commitId);
RefUpdate ru = repo.updateRef(Constants.HEAD); RefUpdate ru = repo.updateRef(Constants.HEAD);
ru.setNewObjectId(commitId); ru.setNewObjectId(commitId);
if (!useDefaultReflogMessage) { if (!useDefaultReflogMessage) {
@ -283,10 +318,11 @@ public RevCommit call() throws GitAPIException, AbortedByHookException,
ru.setRefLogMessage(prefix + revCommit.getShortMessage(), ru.setRefLogMessage(prefix + revCommit.getShortMessage(),
false); false);
} }
if (headId != null) if (headId != null) {
ru.setExpectedOldObjectId(headId); ru.setExpectedOldObjectId(headId);
else } else {
ru.setExpectedOldObjectId(ObjectId.zeroId()); ru.setExpectedOldObjectId(ObjectId.zeroId());
}
Result rc = ru.forceUpdate(); Result rc = ru.forceUpdate();
switch (rc) { switch (rc) {
case NEW: case NEW:
@ -317,24 +353,6 @@ public RevCommit call() throws GitAPIException, AbortedByHookException,
JGitText.get().updatingRefFailed, Constants.HEAD, JGitText.get().updatingRefFailed, Constants.HEAD,
commitId.toString(), rc)); commitId.toString(), rc));
} }
} 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);
}
} }
private void insertChangeId(ObjectId treeId) { private void insertChangeId(ObjectId treeId) {