mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-12-22 16:29:51 +01:00
Throw exception when no changes were made
This commit is contained in:
@@ -43,6 +43,7 @@ import org.eclipse.jgit.api.errors.RefNotFoundException;
|
||||
import org.eclipse.jgit.lib.ObjectId;
|
||||
import org.eclipse.jgit.lib.Ref;
|
||||
import org.eclipse.jgit.lib.Repository;
|
||||
import org.eclipse.jgit.revwalk.RevCommit;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import sonia.scm.repository.GitUtil;
|
||||
@@ -55,7 +56,10 @@ import sonia.scm.user.User;
|
||||
import java.io.IOException;
|
||||
import java.util.Optional;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import static java.util.Optional.empty;
|
||||
import static java.util.Optional.of;
|
||||
import static sonia.scm.ContextEntry.ContextBuilder.entity;
|
||||
import static sonia.scm.NotFoundException.notFound;
|
||||
|
||||
@@ -200,14 +204,26 @@ class AbstractGitCommand
|
||||
}
|
||||
}
|
||||
|
||||
void doCommit(String message, Person author) {
|
||||
void failIfNotChanged(Supplier<RuntimeException> doThrow) {
|
||||
try {
|
||||
if (clone.status().call().isClean()) {
|
||||
throw doThrow.get();
|
||||
}
|
||||
} catch (GitAPIException e) {
|
||||
throw new InternalRepositoryException(context.getRepository(), "could not read status of repository", e);
|
||||
}
|
||||
}
|
||||
|
||||
Optional<RevCommit> doCommit(String message, Person author) {
|
||||
Person authorToUse = determineAuthor(author);
|
||||
try {
|
||||
if (!clone.status().call().isClean()) {
|
||||
clone.commit()
|
||||
return of(clone.commit()
|
||||
.setAuthor(authorToUse.getName(), authorToUse.getMail())
|
||||
.setMessage(message)
|
||||
.call();
|
||||
.call());
|
||||
} else {
|
||||
return empty();
|
||||
}
|
||||
} catch (GitAPIException e) {
|
||||
throw new InternalRepositoryException(context.getRepository(), "could not commit changes", e);
|
||||
|
||||
@@ -2,6 +2,8 @@ package sonia.scm.repository.spi;
|
||||
|
||||
import org.eclipse.jgit.api.Git;
|
||||
import org.eclipse.jgit.api.errors.GitAPIException;
|
||||
import sonia.scm.BadRequestException;
|
||||
import sonia.scm.ContextEntry;
|
||||
import sonia.scm.repository.GitWorkdirFactory;
|
||||
import sonia.scm.repository.InternalRepositoryException;
|
||||
import sonia.scm.repository.Repository;
|
||||
@@ -44,9 +46,10 @@ public class GitModifyCommand extends AbstractGitCommand implements ModifyComman
|
||||
@Override
|
||||
String run() throws IOException {
|
||||
checkOutBranch(request.getBranch());
|
||||
for (ModifyCommandRequest.PartialRequest r: request.getRequests()) {
|
||||
for (ModifyCommandRequest.PartialRequest r : request.getRequests()) {
|
||||
r.execute(this);
|
||||
}
|
||||
failIfNotChanged(NoChangesMadeException::new);
|
||||
doCommit(request.getCommitMessage(), request.getAuthor());
|
||||
push();
|
||||
return getClone().getRepository().getRefDatabase().findRef("HEAD").getObjectId().name();
|
||||
@@ -86,6 +89,17 @@ public class GitModifyCommand extends AbstractGitCommand implements ModifyComman
|
||||
public void move(String sourcePath, String targetPath) {
|
||||
|
||||
}
|
||||
|
||||
private class NoChangesMadeException extends BadRequestException {
|
||||
public NoChangesMadeException() {
|
||||
super(ContextEntry.ContextBuilder.entity(context.getRepository()).build(), "no changes detected to branch " + ModifyWorker.this.request.getBranch());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getCode() {
|
||||
return "40RaYIeeR1";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private String throwInternalRepositoryException(String message, Exception e) {
|
||||
|
||||
@@ -14,6 +14,7 @@ import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.TemporaryFolder;
|
||||
import sonia.scm.AlreadyExistsException;
|
||||
import sonia.scm.BadRequestException;
|
||||
import sonia.scm.repository.Person;
|
||||
import sonia.scm.repository.util.WorkdirProvider;
|
||||
|
||||
@@ -108,6 +109,21 @@ public class GitModifyCommandTest extends AbstractGitCommandTestBase {
|
||||
assertInTree(assertions);
|
||||
}
|
||||
|
||||
@Test(expected = BadRequestException.class)
|
||||
public void shouldFailIfNoChangesMade() throws IOException, GitAPIException {
|
||||
File newFile = Files.write(temporaryFolder.newFile().toPath(), "b\n".getBytes()).toFile();
|
||||
|
||||
GitModifyCommand command = createCommand();
|
||||
|
||||
ModifyCommandRequest request = new ModifyCommandRequest();
|
||||
request.setBranch("master");
|
||||
request.setCommitMessage("test commit");
|
||||
request.addRequest(new ModifyCommandRequest.CreateFileRequest("b.txt", newFile, true));
|
||||
request.setAuthor(new Person("Dirk Gently", "dirk@holistic.det"));
|
||||
|
||||
command.execute(request);
|
||||
}
|
||||
|
||||
private void assertInTree(TreeAssertions assertions) throws IOException, GitAPIException {
|
||||
try (Git git = new Git(createContext().open())) {
|
||||
RevCommit lastCommit = getLastCommit(git);
|
||||
|
||||
Reference in New Issue
Block a user