Check whether directory already exists as a file

This commit is contained in:
Rene Pfeuffer
2019-09-10 10:50:56 +02:00
parent 5e4496e166
commit 0ab8f52e7f
2 changed files with 24 additions and 2 deletions

View File

@@ -77,7 +77,7 @@ public class GitModifyCommand extends AbstractGitCommand implements ModifyComman
@Override @Override
public void create(String toBeCreated, File file, boolean overwrite) throws IOException { public void create(String toBeCreated, File file, boolean overwrite) throws IOException {
Path targetFile = new File(workDir, toBeCreated).toPath(); Path targetFile = new File(workDir, toBeCreated).toPath();
Files.createDirectories(targetFile.getParent()); createDirectories(targetFile);
if (overwrite) { if (overwrite) {
Files.move(file.toPath(), targetFile, REPLACE_EXISTING); Files.move(file.toPath(), targetFile, REPLACE_EXISTING);
} else { } else {
@@ -97,7 +97,7 @@ public class GitModifyCommand extends AbstractGitCommand implements ModifyComman
@Override @Override
public void modify(String path, File file) throws IOException { public void modify(String path, File file) throws IOException {
Path targetFile = new File(workDir, path).toPath(); Path targetFile = new File(workDir, path).toPath();
Files.createDirectories(targetFile.getParent()); createDirectories(targetFile);
if (!targetFile.toFile().exists()) { if (!targetFile.toFile().exists()) {
throw notFound(createFileContext(path)); throw notFound(createFileContext(path));
} }
@@ -124,6 +124,14 @@ public class GitModifyCommand extends AbstractGitCommand implements ModifyComman
} }
} }
private void createDirectories(Path targetFile) throws IOException {
try {
Files.createDirectories(targetFile.getParent());
} catch (FileAlreadyExistsException e) {
throw alreadyExists(createFileContext(targetFile.toString()));
}
}
private ContextEntry.ContextBuilder createFileContext(String path) { private ContextEntry.ContextBuilder createFileContext(String path) {
ContextEntry.ContextBuilder contextBuilder = entity("file", path); ContextEntry.ContextBuilder contextBuilder = entity("file", path);
if (!StringUtils.isEmpty(request.getBranch())) { if (!StringUtils.isEmpty(request.getBranch())) {

View File

@@ -111,6 +111,20 @@ public class GitModifyCommandTest extends AbstractGitCommandTestBase {
command.execute(request); command.execute(request);
} }
@Test(expected = AlreadyExistsException.class)
public void shouldFailIfPathAlreadyExistsAsAFile() 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.CreateFileRequest("a.txt/newFile", newFile, false));
request.setAuthor(new Person("Dirk Gently", "dirk@holistic.det"));
command.execute(request);
}
@Test @Test
public void shouldOverwriteExistingFileIfOverwriteFlagSet() throws IOException, GitAPIException { public void shouldOverwriteExistingFileIfOverwriteFlagSet() throws IOException, GitAPIException {
File newFile = Files.write(temporaryFolder.newFile().toPath(), "new content".getBytes()).toFile(); File newFile = Files.write(temporaryFolder.newFile().toPath(), "new content".getBytes()).toFile();