mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-10 15:35:49 +01:00
Implement git modify command
This commit is contained in:
@@ -12,7 +12,7 @@ public interface ModifyCommand {
|
|||||||
|
|
||||||
void create(String toBeCreated, File file, boolean overwrite) throws IOException;
|
void create(String toBeCreated, File file, boolean overwrite) throws IOException;
|
||||||
|
|
||||||
void modify(String path, File file);
|
void modify(String path, File file) throws IOException;
|
||||||
|
|
||||||
void move(String sourcePath, String targetPath);
|
void move(String sourcePath, String targetPath);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -158,7 +158,7 @@ public class ModifyCommandRequest implements Resetable, Validateable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void execute(ModifyCommand.Worker worker) {
|
public void execute(ModifyCommand.Worker worker) throws IOException {
|
||||||
worker.modify(path, getContent());
|
worker.modify(path, getContent());
|
||||||
cleanup();
|
cleanup();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,7 +9,6 @@ import org.eclipse.jgit.revwalk.RevCommit;
|
|||||||
import sonia.scm.BadRequestException;
|
import sonia.scm.BadRequestException;
|
||||||
import sonia.scm.ConcurrentModificationException;
|
import sonia.scm.ConcurrentModificationException;
|
||||||
import sonia.scm.ContextEntry;
|
import sonia.scm.ContextEntry;
|
||||||
import sonia.scm.NotFoundException;
|
|
||||||
import sonia.scm.repository.GitWorkdirFactory;
|
import sonia.scm.repository.GitWorkdirFactory;
|
||||||
import sonia.scm.repository.InternalRepositoryException;
|
import sonia.scm.repository.InternalRepositoryException;
|
||||||
import sonia.scm.repository.Repository;
|
import sonia.scm.repository.Repository;
|
||||||
@@ -25,6 +24,7 @@ import java.util.Optional;
|
|||||||
import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
|
import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
|
||||||
import static sonia.scm.AlreadyExistsException.alreadyExists;
|
import static sonia.scm.AlreadyExistsException.alreadyExists;
|
||||||
import static sonia.scm.ContextEntry.ContextBuilder.entity;
|
import static sonia.scm.ContextEntry.ContextBuilder.entity;
|
||||||
|
import static sonia.scm.NotFoundException.notFound;
|
||||||
import static sonia.scm.ScmConstraintViolationException.Builder.doThrow;
|
import static sonia.scm.ScmConstraintViolationException.Builder.doThrow;
|
||||||
|
|
||||||
public class GitModifyCommand extends AbstractGitCommand implements ModifyCommand {
|
public class GitModifyCommand extends AbstractGitCommand implements ModifyCommand {
|
||||||
@@ -79,10 +79,10 @@ public class GitModifyCommand extends AbstractGitCommand implements ModifyComman
|
|||||||
Path targetFile = new File(workDir, toBeCreated).toPath();
|
Path targetFile = new File(workDir, toBeCreated).toPath();
|
||||||
Files.createDirectories(targetFile.getParent());
|
Files.createDirectories(targetFile.getParent());
|
||||||
if (overwrite) {
|
if (overwrite) {
|
||||||
Files.copy(file.toPath(), targetFile, REPLACE_EXISTING);
|
Files.move(file.toPath(), targetFile, REPLACE_EXISTING);
|
||||||
} else {
|
} else {
|
||||||
try {
|
try {
|
||||||
Files.copy(file.toPath(), targetFile);
|
Files.move(file.toPath(), targetFile);
|
||||||
} catch (FileAlreadyExistsException e) {
|
} catch (FileAlreadyExistsException e) {
|
||||||
throw alreadyExists(createFileContext(toBeCreated));
|
throw alreadyExists(createFileContext(toBeCreated));
|
||||||
}
|
}
|
||||||
@@ -94,13 +94,28 @@ public class GitModifyCommand extends AbstractGitCommand implements ModifyComman
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void modify(String path, File file) throws IOException {
|
||||||
|
Path targetFile = new File(workDir, path).toPath();
|
||||||
|
Files.createDirectories(targetFile.getParent());
|
||||||
|
if (!targetFile.toFile().exists()) {
|
||||||
|
throw notFound(createFileContext(path));
|
||||||
|
}
|
||||||
|
Files.move(file.toPath(), targetFile, REPLACE_EXISTING);
|
||||||
|
try {
|
||||||
|
getClone().add().addFilepattern(path).call();
|
||||||
|
} catch (GitAPIException e) {
|
||||||
|
throwInternalRepositoryException("could not add new file to index", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void delete(String toBeDeleted) throws IOException {
|
public void delete(String toBeDeleted) throws IOException {
|
||||||
Path fileToBeDeleted = new File(workDir, toBeDeleted).toPath();
|
Path fileToBeDeleted = new File(workDir, toBeDeleted).toPath();
|
||||||
try {
|
try {
|
||||||
Files.delete(fileToBeDeleted);
|
Files.delete(fileToBeDeleted);
|
||||||
} catch (NoSuchFileException e) {
|
} catch (NoSuchFileException e) {
|
||||||
throw NotFoundException.notFound(createFileContext(toBeDeleted));
|
throw notFound(createFileContext(toBeDeleted));
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
getClone().rm().addFilepattern(toBeDeleted).call();
|
getClone().rm().addFilepattern(toBeDeleted).call();
|
||||||
@@ -109,8 +124,8 @@ public class GitModifyCommand extends AbstractGitCommand implements ModifyComman
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private ContextEntry.ContextBuilder createFileContext(String toBeDeleted) {
|
private ContextEntry.ContextBuilder createFileContext(String path) {
|
||||||
ContextEntry.ContextBuilder contextBuilder = entity("file", toBeDeleted);
|
ContextEntry.ContextBuilder contextBuilder = entity("file", path);
|
||||||
if (!StringUtils.isEmpty(request.getBranch())) {
|
if (!StringUtils.isEmpty(request.getBranch())) {
|
||||||
contextBuilder.in("branch", request.getBranch());
|
contextBuilder.in("branch", request.getBranch());
|
||||||
}
|
}
|
||||||
@@ -118,11 +133,6 @@ public class GitModifyCommand extends AbstractGitCommand implements ModifyComman
|
|||||||
return contextBuilder;
|
return contextBuilder;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void modify(String path, File file) {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void move(String sourcePath, String targetPath) {
|
public void move(String sourcePath, String targetPath) {
|
||||||
|
|
||||||
|
|||||||
@@ -129,6 +129,38 @@ public class GitModifyCommandTest extends AbstractGitCommandTestBase {
|
|||||||
assertInTree(assertions);
|
assertInTree(assertions);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldModifyExistingFile() 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.addRequest(new ModifyCommandRequest.ModifyFileRequest("a.txt", newFile));
|
||||||
|
request.setAuthor(new Person("Dirk Gently", "dirk@holistic.det"));
|
||||||
|
|
||||||
|
command.execute(request);
|
||||||
|
|
||||||
|
TreeAssertions assertions = canonicalTreeParser -> assertThat(canonicalTreeParser.findFile("a.txt")).isTrue();
|
||||||
|
|
||||||
|
assertInTree(assertions);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(expected = NotFoundException.class)
|
||||||
|
public void shouldFailIfFileToModifyDoesNotExist() throws IOException {
|
||||||
|
File newFile = Files.write(temporaryFolder.newFile().toPath(), "new content".getBytes()).toFile();
|
||||||
|
|
||||||
|
GitModifyCommand command = createCommand();
|
||||||
|
|
||||||
|
ModifyCommandRequest request = new ModifyCommandRequest();
|
||||||
|
request.setCommitMessage("test commit");
|
||||||
|
request.addRequest(new ModifyCommandRequest.ModifyFileRequest("no.such.file", newFile));
|
||||||
|
request.setAuthor(new Person("Dirk Gently", "dirk@holistic.det"));
|
||||||
|
|
||||||
|
command.execute(request);
|
||||||
|
}
|
||||||
|
|
||||||
@Test(expected = BadRequestException.class)
|
@Test(expected = BadRequestException.class)
|
||||||
public void shouldFailIfNoChangesMade() throws IOException {
|
public void shouldFailIfNoChangesMade() throws IOException {
|
||||||
File newFile = Files.write(temporaryFolder.newFile().toPath(), "b\n".getBytes()).toFile();
|
File newFile = Files.write(temporaryFolder.newFile().toPath(), "b\n".getBytes()).toFile();
|
||||||
|
|||||||
Reference in New Issue
Block a user