mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-13 00:45:44 +01:00
Implement delete for git
This commit is contained in:
@@ -8,7 +8,7 @@ public interface ModifyCommand {
|
||||
String execute(ModifyCommandRequest request);
|
||||
|
||||
interface Worker {
|
||||
void delete(String toBeDeleted);
|
||||
void delete(String toBeDeleted) throws IOException;
|
||||
|
||||
void create(String toBeCreated, File file, boolean overwrite) throws IOException;
|
||||
|
||||
|
||||
@@ -89,7 +89,7 @@ public class ModifyCommandRequest implements Resetable, Validateable {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute(ModifyCommand.Worker worker) {
|
||||
public void execute(ModifyCommand.Worker worker) throws IOException {
|
||||
worker.delete(path);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ import org.eclipse.jgit.revwalk.RevCommit;
|
||||
import sonia.scm.BadRequestException;
|
||||
import sonia.scm.ConcurrentModificationException;
|
||||
import sonia.scm.ContextEntry;
|
||||
import sonia.scm.NotFoundException;
|
||||
import sonia.scm.repository.GitWorkdirFactory;
|
||||
import sonia.scm.repository.InternalRepositoryException;
|
||||
import sonia.scm.repository.Repository;
|
||||
@@ -15,6 +16,7 @@ import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.FileAlreadyExistsException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.NoSuchFileException;
|
||||
import java.nio.file.Path;
|
||||
import java.util.Optional;
|
||||
|
||||
@@ -76,11 +78,7 @@ public class GitModifyCommand extends AbstractGitCommand implements ModifyComman
|
||||
try {
|
||||
Files.copy(file.toPath(), targetFile);
|
||||
} catch (FileAlreadyExistsException e) {
|
||||
ContextEntry.ContextBuilder contextBuilder = entity("file", toBeCreated);
|
||||
if (!StringUtils.isEmpty(request.getBranch())) {
|
||||
contextBuilder.in("branch", request.getBranch());
|
||||
}
|
||||
throw alreadyExists(contextBuilder.in(context.getRepository()));
|
||||
throw alreadyExists(createFileContext(toBeCreated));
|
||||
}
|
||||
}
|
||||
try {
|
||||
@@ -91,8 +89,27 @@ public class GitModifyCommand extends AbstractGitCommand implements ModifyComman
|
||||
}
|
||||
|
||||
@Override
|
||||
public void delete(String toBeDeleted) {
|
||||
public void delete(String toBeDeleted) throws IOException {
|
||||
Path fileToBeDeleted = new File(workDir, toBeDeleted).toPath();
|
||||
try {
|
||||
Files.delete(fileToBeDeleted);
|
||||
} catch (NoSuchFileException e) {
|
||||
throw NotFoundException.notFound(createFileContext(toBeDeleted));
|
||||
}
|
||||
try {
|
||||
getClone().rm().addFilepattern(toBeDeleted).call();
|
||||
} catch (GitAPIException e) {
|
||||
throwInternalRepositoryException("could not remove file from index", e);
|
||||
}
|
||||
}
|
||||
|
||||
private ContextEntry.ContextBuilder createFileContext(String toBeDeleted) {
|
||||
ContextEntry.ContextBuilder contextBuilder = entity("file", toBeDeleted);
|
||||
if (!StringUtils.isEmpty(request.getBranch())) {
|
||||
contextBuilder.in("branch", request.getBranch());
|
||||
}
|
||||
contextBuilder.in(context.getRepository());
|
||||
return contextBuilder;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -16,6 +16,7 @@ import org.junit.rules.TemporaryFolder;
|
||||
import sonia.scm.AlreadyExistsException;
|
||||
import sonia.scm.BadRequestException;
|
||||
import sonia.scm.ConcurrentModificationException;
|
||||
import sonia.scm.NotFoundException;
|
||||
import sonia.scm.repository.Person;
|
||||
import sonia.scm.repository.util.WorkdirProvider;
|
||||
|
||||
@@ -156,6 +157,47 @@ public class GitModifyCommandTest extends AbstractGitCommandTestBase {
|
||||
command.execute(request);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldDeleteExistingFile() throws IOException, GitAPIException {
|
||||
GitModifyCommand command = createCommand();
|
||||
|
||||
ModifyCommandRequest request = new ModifyCommandRequest();
|
||||
request.setCommitMessage("test commit");
|
||||
request.addRequest(new ModifyCommandRequest.DeleteFileRequest("a.txt"));
|
||||
request.setAuthor(new Person("Dirk Gently", "dirk@holistic.det"));
|
||||
|
||||
command.execute(request);
|
||||
|
||||
TreeAssertions assertions = canonicalTreeParser -> assertThat(canonicalTreeParser.findFile("a.txt")).isFalse();
|
||||
|
||||
assertInTree(assertions);
|
||||
}
|
||||
|
||||
@Test(expected = NotFoundException.class)
|
||||
public void shouldThrowNotFoundExceptionWhenFileToDeleteDoesNotExist() {
|
||||
GitModifyCommand command = createCommand();
|
||||
|
||||
ModifyCommandRequest request = new ModifyCommandRequest();
|
||||
request.setCommitMessage("test commit");
|
||||
request.addRequest(new ModifyCommandRequest.DeleteFileRequest("no/such/file"));
|
||||
request.setAuthor(new Person("Dirk Gently", "dirk@holistic.det"));
|
||||
|
||||
command.execute(request);
|
||||
}
|
||||
|
||||
@Test(expected = NotFoundException.class)
|
||||
public void shouldThrowNotFoundExceptionWhenBranchDoesNotExist() {
|
||||
GitModifyCommand command = createCommand();
|
||||
|
||||
ModifyCommandRequest request = new ModifyCommandRequest();
|
||||
request.setBranch("does-not-exist");
|
||||
request.setCommitMessage("test commit");
|
||||
request.addRequest(new ModifyCommandRequest.DeleteFileRequest("a.txt"));
|
||||
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