diff --git a/CHANGELOG.md b/CHANGELOG.md index 549cb9893f..4b4b179256 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,10 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## Unreleased +### Fixed +- Not found error when using browse command in empty hg repository ([#1355](https://github.com/scm-manager/scm-manager/pull/1355)) + ## [2.6.0] - 2020-09-25 ### Added - Add support for pr merge with prior rebase ([#1332](https://github.com/scm-manager/scm-manager/pull/1332)) diff --git a/scm-plugins/scm-hg-plugin/src/main/java/sonia/scm/repository/spi/javahg/HgFileviewCommandResultReader.java b/scm-plugins/scm-hg-plugin/src/main/java/sonia/scm/repository/spi/javahg/HgFileviewCommandResultReader.java index c9e315ab00..14c8910653 100644 --- a/scm-plugins/scm-hg-plugin/src/main/java/sonia/scm/repository/spi/javahg/HgFileviewCommandResultReader.java +++ b/scm-plugins/scm-hg-plugin/src/main/java/sonia/scm/repository/spi/javahg/HgFileviewCommandResultReader.java @@ -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 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(); diff --git a/scm-plugins/scm-hg-plugin/src/test/java/sonia/scm/repository/spi/javahg/HgFileviewCommandResultReaderTest.java b/scm-plugins/scm-hg-plugin/src/test/java/sonia/scm/repository/spi/javahg/HgFileviewCommandResultReaderTest.java index c268175108..c7dab313fa 100644 --- a/scm-plugins/scm-hg-plugin/src/test/java/sonia/scm/repository/spi/javahg/HgFileviewCommandResultReaderTest.java +++ b/scm-plugins/scm-hg-plugin/src/test/java/sonia/scm/repository/spi/javahg/HgFileviewCommandResultReaderTest.java @@ -269,6 +269,17 @@ class HgFileviewCommandResultReaderTest { assertThat(fileObject).isEmpty(); } + @Test + void shouldReturnEmptyRootDir() throws IOException { + HgFileviewCommandResultReader reader = new MockInput() + .dir("") + .build(); + + Optional fileObject = reader.parseResult(); + + assertThat(fileObject).isNotEmpty(); + } + private HgInputStream createInputStream(String input) { return new HgInputStream(new ByteArrayInputStream(input.getBytes(UTF_8)), UTF_8.newDecoder()); }