Fix not found error when reading empty hg repository

This commit is contained in:
René Pfeuffer
2020-09-29 10:41:13 +02:00
parent 4762a491ad
commit a6af1d0e34
3 changed files with 27 additions and 2 deletions

View File

@@ -87,8 +87,8 @@ class HgFileviewCommandResultReader {
if (stack.isEmpty()) {
// if the stack is empty, the requested path is probably a file
return of(last);
} else if (stack.size() == 1 && stack.getFirst().isDirectory() && stack.getFirst().getChildren().isEmpty()) {
// There are no empty directories in hg. When we get this,
} else if (isEmptySubDirectory(stack)) {
// There are no empty directories in hg (except the root). When we get this,
// we just get the requested path as a directory, but it does not exist.
return empty();
} else {
@@ -100,6 +100,16 @@ class HgFileviewCommandResultReader {
}
}
private boolean isEmptySubDirectory(Deque<FileObject> stack) {
if (stack.size() != 1) {
return false;
}
final FileObject singleEntry = stack.getFirst();
return singleEntry.isDirectory()
&& singleEntry.getChildren().isEmpty()
&& !singleEntry.getName().isEmpty();
}
private FileObject read(HgInputStream stream) throws IOException {
char type = (char) stream.read();

View File

@@ -269,6 +269,17 @@ class HgFileviewCommandResultReaderTest {
assertThat(fileObject).isEmpty();
}
@Test
void shouldReturnEmptyRootDir() throws IOException {
HgFileviewCommandResultReader reader = new MockInput()
.dir("")
.build();
Optional<FileObject> fileObject = reader.parseResult();
assertThat(fileObject).isNotEmpty();
}
private HgInputStream createInputStream(String input) {
return new HgInputStream(new ByteArrayInputStream(input.getBytes(UTF_8)), UTF_8.newDecoder());
}