This commit is contained in:
Eduard Heimbuch
2019-09-04 10:20:42 +02:00
3 changed files with 32 additions and 9 deletions

View File

@@ -65,7 +65,7 @@ public class ModifyCommandRequest implements Resetable, Validateable {
@Override
public boolean isValid() {
return StringUtils.isNotEmpty(commitMessage) && StringUtils.isNotEmpty(branch) && !requests.isEmpty();
return StringUtils.isNotEmpty(commitMessage) && !requests.isEmpty();
}
public interface PartialRequest {

View File

@@ -1,5 +1,6 @@
package sonia.scm.repository.spi;
import org.apache.commons.lang.StringUtils;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.revwalk.RevCommit;
@@ -47,7 +48,9 @@ public class GitModifyCommand extends AbstractGitCommand implements ModifyComman
@Override
String run() throws IOException {
checkOutBranch(request.getBranch());
if (!StringUtils.isEmpty(request.getBranch())) {
checkOutBranch(request.getBranch());
}
for (ModifyCommandRequest.PartialRequest r : request.getRequests()) {
r.execute(this);
}
@@ -67,7 +70,11 @@ public class GitModifyCommand extends AbstractGitCommand implements ModifyComman
try {
Files.copy(file.toPath(), targetFile);
} catch (FileAlreadyExistsException e) {
throw alreadyExists(entity("file", toBeCreated).in("branch", request.getBranch()).in(context.getRepository()));
ContextEntry.ContextBuilder contextBuilder = entity("file", toBeCreated);
if (!StringUtils.isEmpty(request.getBranch())) {
contextBuilder.in("branch", request.getBranch());
}
throw alreadyExists(contextBuilder.in(context.getRepository()));
}
}
try {

View File

@@ -41,7 +41,6 @@ public class GitModifyCommandTest extends AbstractGitCommandTestBase {
GitModifyCommand command = createCommand();
ModifyCommandRequest request = new ModifyCommandRequest();
request.setBranch("master");
request.setCommitMessage("test commit");
request.addRequest(new ModifyCommandRequest.CreateFileRequest("new_file", newFile, false));
request.setAuthor(new Person("Dirk Gently", "dirk@holistic.det"));
@@ -56,6 +55,27 @@ public class GitModifyCommandTest extends AbstractGitCommandTestBase {
}
}
@Test
public void shouldCreateCommitOnSelectedBranch() throws IOException, GitAPIException {
File newFile = Files.write(temporaryFolder.newFile().toPath(), "new content".getBytes()).toFile();
GitModifyCommand command = createCommand();
ModifyCommandRequest request = new ModifyCommandRequest();
request.setCommitMessage("test commit");
request.setBranch("test-branch");
request.addRequest(new ModifyCommandRequest.CreateFileRequest("new_file", newFile, false));
request.setAuthor(new Person("Dirk Gently", "dirk@holistic.det"));
String newRef = command.execute(request);
ObjectId commitId = ObjectId.fromString(newRef);
try (RevWalk revWalk = new RevWalk(createContext().open())) {
RevCommit commit = revWalk.parseCommit(commitId);
assertThat(commit.getParent(0).name()).isEqualTo("3f76a12f08a6ba0dc988c68b7f0b2cd190efc3c4");
}
}
@Test
public void shouldCreateNewFile() throws IOException, GitAPIException {
File newFile = Files.write(temporaryFolder.newFile().toPath(), "new content".getBytes()).toFile();
@@ -63,7 +83,6 @@ public class GitModifyCommandTest extends AbstractGitCommandTestBase {
GitModifyCommand command = createCommand();
ModifyCommandRequest request = new ModifyCommandRequest();
request.setBranch("master");
request.setCommitMessage("test commit");
request.addRequest(new ModifyCommandRequest.CreateFileRequest("new_file", newFile, false));
request.setAuthor(new Person("Dirk Gently", "dirk@holistic.det"));
@@ -82,7 +101,6 @@ public class GitModifyCommandTest extends AbstractGitCommandTestBase {
GitModifyCommand command = createCommand();
ModifyCommandRequest request = new ModifyCommandRequest();
request.setBranch("master");
request.setCommitMessage("test commit");
request.addRequest(new ModifyCommandRequest.CreateFileRequest("a.txt", newFile, false));
request.setAuthor(new Person("Dirk Gently", "dirk@holistic.det"));
@@ -97,7 +115,6 @@ public class GitModifyCommandTest extends AbstractGitCommandTestBase {
GitModifyCommand command = createCommand();
ModifyCommandRequest request = new ModifyCommandRequest();
request.setBranch("master");
request.setCommitMessage("test commit");
request.addRequest(new ModifyCommandRequest.CreateFileRequest("a.txt", newFile, true));
request.setAuthor(new Person("Dirk Gently", "dirk@holistic.det"));
@@ -110,13 +127,12 @@ public class GitModifyCommandTest extends AbstractGitCommandTestBase {
}
@Test(expected = BadRequestException.class)
public void shouldFailIfNoChangesMade() throws IOException, GitAPIException {
public void shouldFailIfNoChangesMade() throws IOException {
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"));