Refactor CommitCommand to improve readability
Change-Id: Id3cac81cd32c07f677b7f669d58e32b5290e1790 Signed-off-by: Matthias Sohn <matthias.sohn@sap.com>
This commit is contained in:
parent
18c735c474
commit
502bfff7db
|
@ -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,93 +233,25 @@ 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()) {
|
||||||
if (gpgSigner == null) {
|
sign(commit);
|
||||||
throw new ServiceUnavailableException(
|
|
||||||
JGitText.get().signingServiceUnavailable);
|
|
||||||
}
|
|
||||||
if (gpgSigner instanceof GpgObjectSigner) {
|
|
||||||
((GpgObjectSigner) gpgSigner).signObject(commit,
|
|
||||||
signingKey, committer, credentialsProvider,
|
|
||||||
gpgConfig);
|
|
||||||
} else {
|
|
||||||
if (gpgConfig.getKeyFormat() != GpgFormat.OPENPGP) {
|
|
||||||
throw new UnsupportedSigningFormatException(JGitText
|
|
||||||
.get().onlyOpenPgpSupportedForSigning);
|
|
||||||
}
|
|
||||||
gpgSigner.sign(commit, signingKey, committer,
|
|
||||||
credentialsProvider);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ObjectId commitId = odi.insert(commit);
|
ObjectId commitId = odi.insert(commit);
|
||||||
odi.flush();
|
odi.flush();
|
||||||
|
|
||||||
revCommit = rw.parseCommit(commitId);
|
revCommit = rw.parseCommit(commitId);
|
||||||
RefUpdate ru = repo.updateRef(Constants.HEAD);
|
|
||||||
ru.setNewObjectId(commitId);
|
updateRef(state, headId, revCommit, commitId);
|
||||||
if (!useDefaultReflogMessage) {
|
|
||||||
ru.setRefLogMessage(reflogComment, false);
|
|
||||||
} else {
|
|
||||||
String prefix = amend ? "commit (amend): " //$NON-NLS-1$
|
|
||||||
: parents.isEmpty() ? "commit (initial): " //$NON-NLS-1$
|
|
||||||
: "commit: "; //$NON-NLS-1$
|
|
||||||
ru.setRefLogMessage(prefix + revCommit.getShortMessage(),
|
|
||||||
false);
|
|
||||||
}
|
|
||||||
if (headId != null)
|
|
||||||
ru.setExpectedOldObjectId(headId);
|
|
||||||
else
|
|
||||||
ru.setExpectedOldObjectId(ObjectId.zeroId());
|
|
||||||
Result rc = ru.forceUpdate();
|
|
||||||
switch (rc) {
|
|
||||||
case NEW:
|
|
||||||
case FORCED:
|
|
||||||
case FAST_FORWARD: {
|
|
||||||
setCallable(false);
|
|
||||||
if (state == RepositoryState.MERGING_RESOLVED
|
|
||||||
|| isMergeDuringRebase(state)) {
|
|
||||||
// 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);
|
|
||||||
} else if (state == RepositoryState.REVERTING_RESOLVED) {
|
|
||||||
repo.writeMergeCommitMsg(null);
|
|
||||||
repo.writeRevertHead(null);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
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 {
|
} finally {
|
||||||
index.unlock();
|
index.unlock();
|
||||||
}
|
}
|
||||||
|
@ -337,6 +272,89 @@ public RevCommit call() throws GitAPIException, AbortedByHookException,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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) {
|
||||||
|
throw new ServiceUnavailableException(
|
||||||
|
JGitText.get().signingServiceUnavailable);
|
||||||
|
}
|
||||||
|
if (gpgSigner instanceof GpgObjectSigner) {
|
||||||
|
((GpgObjectSigner) gpgSigner).signObject(commit,
|
||||||
|
signingKey, committer, credentialsProvider,
|
||||||
|
gpgConfig);
|
||||||
|
} else {
|
||||||
|
if (gpgConfig.getKeyFormat() != GpgFormat.OPENPGP) {
|
||||||
|
throw new UnsupportedSigningFormatException(JGitText
|
||||||
|
.get().onlyOpenPgpSupportedForSigning);
|
||||||
|
}
|
||||||
|
gpgSigner.sign(commit, signingKey, committer,
|
||||||
|
credentialsProvider);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void updateRef(RepositoryState state, ObjectId headId,
|
||||||
|
RevCommit revCommit, ObjectId commitId)
|
||||||
|
throws ConcurrentRefUpdateException, IOException {
|
||||||
|
RefUpdate ru = repo.updateRef(Constants.HEAD);
|
||||||
|
ru.setNewObjectId(commitId);
|
||||||
|
if (!useDefaultReflogMessage) {
|
||||||
|
ru.setRefLogMessage(reflogComment, false);
|
||||||
|
} else {
|
||||||
|
String prefix = amend ? "commit (amend): " //$NON-NLS-1$
|
||||||
|
: parents.isEmpty() ? "commit (initial): " //$NON-NLS-1$
|
||||||
|
: "commit: "; //$NON-NLS-1$
|
||||||
|
ru.setRefLogMessage(prefix + revCommit.getShortMessage(),
|
||||||
|
false);
|
||||||
|
}
|
||||||
|
if (headId != null) {
|
||||||
|
ru.setExpectedOldObjectId(headId);
|
||||||
|
} else {
|
||||||
|
ru.setExpectedOldObjectId(ObjectId.zeroId());
|
||||||
|
}
|
||||||
|
Result rc = ru.forceUpdate();
|
||||||
|
switch (rc) {
|
||||||
|
case NEW:
|
||||||
|
case FORCED:
|
||||||
|
case FAST_FORWARD: {
|
||||||
|
setCallable(false);
|
||||||
|
if (state == RepositoryState.MERGING_RESOLVED
|
||||||
|
|| isMergeDuringRebase(state)) {
|
||||||
|
// 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);
|
||||||
|
} else if (state == RepositoryState.REVERTING_RESOLVED) {
|
||||||
|
repo.writeMergeCommitMsg(null);
|
||||||
|
repo.writeRevertHead(null);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
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));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private void insertChangeId(ObjectId treeId) {
|
private void insertChangeId(ObjectId treeId) {
|
||||||
ObjectId firstParentId = null;
|
ObjectId firstParentId = null;
|
||||||
if (!parents.isEmpty())
|
if (!parents.isEmpty())
|
||||||
|
|
Loading…
Reference in New Issue