Reverting only commits that updated snapshots to other versions; fixes #50
This commit is contained in:
@@ -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();
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user