Detect proper merge and prevent empty commits

This commit is contained in:
Rene Pfeuffer
2019-10-16 14:59:38 +02:00
parent 668007e084
commit 4cb79ffe5a
5 changed files with 32 additions and 6 deletions

View File

@@ -38,6 +38,7 @@ import com.google.common.base.Strings;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.Status;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.api.errors.RefNotFoundException;
import org.eclipse.jgit.lib.ObjectId;
@@ -214,10 +215,11 @@ class AbstractGitCommand
}
}
Optional<RevCommit> doCommit(String message, Person author, boolean isMergeCommit) {
Optional<RevCommit> doCommit(String message, Person author) {
Person authorToUse = determineAuthor(author);
try {
if (isMergeCommit || !clone.status().call().isClean()) {
Status status = clone.status().call();
if (!status.isClean() || isInMerge()) {
return of(clone.commit()
.setAuthor(authorToUse.getName(), authorToUse.getMail())
.setMessage(message)
@@ -225,11 +227,15 @@ class AbstractGitCommand
} else {
return empty();
}
} catch (GitAPIException e) {
} catch (GitAPIException | IOException e) {
throw new InternalRepositoryException(context.getRepository(), "could not commit changes", e);
}
}
private boolean isInMerge() throws IOException {
return clone.getRepository().readMergeHeads() != null && !clone.getRepository().readMergeHeads().isEmpty();
}
void push() {
try {
clone.push().call();

View File

@@ -99,7 +99,7 @@ public class GitMergeCommand extends AbstractGitCommand implements MergeCommand
private void doCommit() {
logger.debug("merged branch {} into {}", toMerge, target);
doCommit(MessageFormat.format(determineMessageTemplate(), toMerge, target), author, true);
doCommit(MessageFormat.format(determineMessageTemplate(), toMerge, target), author);
}
private String determineMessageTemplate() {

View File

@@ -62,7 +62,7 @@ public class GitModifyCommand extends AbstractGitCommand implements ModifyComman
r.execute(this);
}
failIfNotChanged(() -> new NoChangesMadeException(repository, ModifyWorker.this.request.getBranch()));
Optional<RevCommit> revCommit = doCommit(request.getCommitMessage(), request.getAuthor(), false);
Optional<RevCommit> revCommit = doCommit(request.getCommitMessage(), request.getAuthor());
push();
return revCommit.orElseThrow(() -> new NoChangesMadeException(repository, ModifyWorker.this.request.getBranch())).name();
}