diff --git a/scm-core/src/main/java/sonia/scm/repository/api/BrowseCommandBuilder.java b/scm-core/src/main/java/sonia/scm/repository/api/BrowseCommandBuilder.java index c312b20534..20c79e60dd 100644 --- a/scm-core/src/main/java/sonia/scm/repository/api/BrowseCommandBuilder.java +++ b/scm-core/src/main/java/sonia/scm/repository/api/BrowseCommandBuilder.java @@ -306,7 +306,7 @@ public final class BrowseCommandBuilder * severe performance implications. Reading a repository with thousands of files in one folder * can generate a huge load for a longer time. * - * @param limit The maximal number of files this request shall return. + * @param limit The maximal number of files this request shall return (directories are not counted). * * @since 2.0.0 */ @@ -318,8 +318,10 @@ public final class BrowseCommandBuilder /** * Proceed the list from the given number on (zero based). * - * @param offset The number of the entry, the result should start with (zero based). - * All preceding entries will be omitted. + * @param offset The number of the file, the result should start with (zero based). + * All preceding files will be omitted. Directories are not + * counted. Therefore directories are only listed in results without + * offset. * @since 2.0.0 */ public BrowseCommandBuilder setOffset(int offset) { diff --git a/scm-plugins/scm-git-plugin/src/main/java/sonia/scm/repository/spi/GitBrowseCommand.java b/scm-plugins/scm-git-plugin/src/main/java/sonia/scm/repository/spi/GitBrowseCommand.java index f4b6f15eba..15086e89ba 100644 --- a/scm-plugins/scm-git-plugin/src/main/java/sonia/scm/repository/spi/GitBrowseCommand.java +++ b/scm-plugins/scm-git-plugin/src/main/java/sonia/scm/repository/spi/GitBrowseCommand.java @@ -264,25 +264,28 @@ public class GitBrowseCommand extends AbstractGitCommand private void convertToFileObject(FileObject parent, org.eclipse.jgit.lib.Repository repo, BrowseCommandRequest request, ObjectId revId, List entries) throws IOException { List files = Lists.newArrayList(); Iterator entryIterator = entries.iterator(); - while (entryIterator.hasNext() && ++resultCount <= request.getLimit() + request.getOffset()) + boolean hasNext; + while ((hasNext = entryIterator.hasNext()) && resultCount < request.getLimit() + request.getOffset()) { TreeEntry entry = entryIterator.next(); FileObject fileObject = createFileObject(repo, request, revId, entry); + if (!fileObject.isDirectory()) { + ++resultCount; + } + if (request.isRecursive() && fileObject.isDirectory()) { convertToFileObject(fileObject, repo, request, revId, entry.getChildren()); } - if (resultCount > request.getOffset()) { + if (resultCount > request.getOffset() || fileObject.isDirectory()) { files.add(fileObject); } } parent.setChildren(files); - if (resultCount > request.getLimit() + request.getOffset()) { - parent.setTruncated(true); - } + parent.setTruncated(hasNext); } private Optional createTree(String path, TreeEntry parent, org.eclipse.jgit.lib.Repository repo, BrowseCommandRequest request, TreeWalk treeWalk) throws IOException { diff --git a/scm-plugins/scm-git-plugin/src/test/java/sonia/scm/repository/spi/GitBrowseCommandTest.java b/scm-plugins/scm-git-plugin/src/test/java/sonia/scm/repository/spi/GitBrowseCommandTest.java index 8684837d37..48e7a6e9ff 100644 --- a/scm-plugins/scm-git-plugin/src/test/java/sonia/scm/repository/spi/GitBrowseCommandTest.java +++ b/scm-plugins/scm-git-plugin/src/test/java/sonia/scm/repository/spi/GitBrowseCommandTest.java @@ -242,21 +242,37 @@ public class GitBrowseCommandTest extends AbstractGitCommandTestBase { @Test public void testBrowseLimit() throws IOException { BrowseCommandRequest request = new BrowseCommandRequest(); - request.setLimit(2); + request.setLimit(1); FileObject root = createCommand() .getBrowserResult(request).getFile(); assertNotNull(root); Collection foList = root.getChildren(); + assertThat(foList).extracting("name").containsExactly("c", "a.txt"); assertThat(foList).hasSize(2); - assertTrue(root.isTruncated()); + assertTrue("result should be marked as trunctated", root.isTruncated()); + } + + @Test + public void testBrowseLimitWithoutTruncation() throws IOException { + BrowseCommandRequest request = new BrowseCommandRequest(); + request.setLimit(3); + FileObject root = createCommand() + .getBrowserResult(request).getFile(); + assertNotNull(root); + + Collection foList = root.getChildren(); + + assertThat(foList).extracting("name").containsExactly("c", "a.txt", "b.txt", "f.txt"); + assertThat(foList).hasSize(4); + assertFalse("result should not be marked as trunctated", root.isTruncated()); } @Test public void testBrowseOffset() throws IOException { BrowseCommandRequest request = new BrowseCommandRequest(); - request.setLimit(2); + request.setLimit(1); request.setOffset(2); FileObject root = createCommand() .getBrowserResult(request).getFile(); @@ -264,15 +280,15 @@ public class GitBrowseCommandTest extends AbstractGitCommandTestBase { Collection foList = root.getChildren(); - assertThat(foList).extracting("name").contains("b.txt", "f.txt"); - assertFalse(root.isTruncated()); + assertThat(foList).extracting("name").contains("f.txt"); + assertFalse("result should not be marked as trunctated", root.isTruncated()); } @Test public void testRecursiveLimit() throws IOException { BrowseCommandRequest request = new BrowseCommandRequest(); - request.setLimit(4); + request.setLimit(3); request.setRecursive(true); FileObject root = createCommand().getBrowserResult(request).getFile(); @@ -295,7 +311,7 @@ public class GitBrowseCommandTest extends AbstractGitCommandTestBase { public void testRecursiveLimitInSubDir() throws IOException { BrowseCommandRequest request = new BrowseCommandRequest(); - request.setLimit(2); + request.setLimit(1); request.setRecursive(true); FileObject root = createCommand().getBrowserResult(request).getFile(); @@ -318,7 +334,7 @@ public class GitBrowseCommandTest extends AbstractGitCommandTestBase { public void testRecursiveOffset() throws IOException { BrowseCommandRequest request = new BrowseCommandRequest(); - request.setOffset(2); + request.setOffset(1); request.setRecursive(true); FileObject root = createCommand().getBrowserResult(request).getFile();