Reverting only commits that updated snapshots to other versions; fixes #50

This commit is contained in:
Marcin Grzejszczak
2017-10-23 21:28:05 +02:00
parent f9f9743c22
commit 2af2b2645c
2 changed files with 22 additions and 2 deletions

View File

@@ -200,6 +200,13 @@ class GitRepo {
void revert(File project, String message) {
try(Git git = this.gitFactory.open(file(project))) {
RevCommit commit = git.log().setMaxCount(1).call().iterator().next();
String shortMessage = commit.getShortMessage();
String id = commit.getId().getName();
if (!shortMessage.contains("Update SNAPSHOT to ")) {
throw new IllegalStateException("Won't revert the commit with id [" + id + "] "
+ "and message [" + shortMessage + "]. Only commit that updated "
+ "snapshot to another version can be reverted");
}
log.debug("The commit to be reverted is [{}]", commit);
git.revert().include(commit).call();
git.commit().setAmend(true).setMessage(message).call();

View File

@@ -14,6 +14,7 @@ import java.net.URISyntaxException;
import java.nio.file.Files;
import java.util.List;
import org.assertj.core.api.BDDAssertions;
import org.eclipse.jgit.api.CloneCommand;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException;
@@ -225,15 +226,27 @@ public class GitRepoTests {
@Test
public void should_revert_changes() throws Exception {
File project = this.gitRepo.cloneProject(this.springCloudReleaseProject.toURI());
File foo = new File(project, "foo");
foo.createNewFile();
this.gitRepo.commit(project, "Update SNAPSHOT to 1.0.0.RC1");
this.gitRepo.revert(project, "some message");
this.gitRepo.revert(project, "Reverting the commit");
try(Git git = openGitProject(project)) {
RevCommit revCommit = git.log().call().iterator().next();
then(revCommit.getShortMessage()).isEqualTo("some message");
then(revCommit.getShortMessage()).isEqualTo("Reverting the commit");
}
}
@Test
public void should_not_revert_changes_when_commit_message_is_not_related_to_updating_snapshots() throws Exception {
File project = this.gitRepo.cloneProject(this.springCloudReleaseProject.toURI());
BDDAssertions.thenThrownBy(
() -> this.gitRepo.revert(project, "some message"))
.hasMessageContaining("Won't revert the commit with id");
}
}
class ExceptionThrowingJGitFactory extends GitRepo.JGitFactory {