From 9953a845679cb0f28f31b96acffb2c6dfee04121 Mon Sep 17 00:00:00 2001 From: Sebastian Sdorra Date: Sat, 19 Jan 2013 13:10:10 +0100 Subject: [PATCH] implement recursive option for subversion browse command --- .../scm/repository/spi/SvnBrowseCommand.java | 90 ++++++++++++++++--- .../repository/spi/SvnBrowseCommandTest.java | 20 +++++ 2 files changed, 96 insertions(+), 14 deletions(-) diff --git a/scm-plugins/scm-svn-plugin/src/main/java/sonia/scm/repository/spi/SvnBrowseCommand.java b/scm-plugins/scm-svn-plugin/src/main/java/sonia/scm/repository/spi/SvnBrowseCommand.java index 318c81b0f2..8aa7b3f9c0 100644 --- a/scm-plugins/scm-svn-plugin/src/main/java/sonia/scm/repository/spi/SvnBrowseCommand.java +++ b/scm-plugins/scm-svn-plugin/src/main/java/sonia/scm/repository/spi/SvnBrowseCommand.java @@ -35,6 +35,8 @@ package sonia.scm.repository.spi; //~--- non-JDK imports -------------------------------------------------------- +import com.google.common.collect.Lists; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -57,7 +59,6 @@ import sonia.scm.util.Util; import java.io.IOException; -import java.util.ArrayList; import java.util.Collection; import java.util.List; @@ -126,23 +127,22 @@ public class SvnBrowseCommand extends AbstractSvnCommand Collection entries = svnRepository.getDir(Util.nonNull(path), revisionNumber, null, (Collection) null); - List children = new ArrayList(); - String basePath = Util.EMPTY_STRING; + List children = Lists.newArrayList(); + String basePath = createBasePath(path); - if (Util.isNotEmpty(path)) + if (request.isRecursive()) { - basePath = path; - - if (!basePath.endsWith("/")) - { - basePath = basePath.concat("/"); - } + browseRecursive(svnRepository, revisionNumber, request, children, + entries, basePath); } - - for (SVNDirEntry entry : entries) + else { - children.add(createFileObject(request, svnRepository, revisionNumber, - entry, basePath)); + for (SVNDirEntry entry : entries) + { + children.add(createFileObject(request, svnRepository, revisionNumber, + entry, basePath)); + + } } result = new BrowserResult(); @@ -159,6 +159,68 @@ public class SvnBrowseCommand extends AbstractSvnCommand //~--- methods -------------------------------------------------------------- + /** + * Method description + * + * + * @param svnRepository + * @param revisionNumber + * @param request + * @param children + * @param entries + * @param basePath + * + * @throws SVNException + */ + private void browseRecursive(SVNRepository svnRepository, + long revisionNumber, BrowseCommandRequest request, + List children, Collection entries, String basePath) + throws SVNException + { + for (SVNDirEntry entry : entries) + { + FileObject fo = createFileObject(request, svnRepository, revisionNumber, + entry, basePath); + + children.add(fo); + + if (fo.isDirectory()) + { + Collection subEntries = + svnRepository.getDir(Util.nonNull(fo.getPath()), revisionNumber, + null, (Collection) null); + + browseRecursive(svnRepository, revisionNumber, request, children, + subEntries, createBasePath(fo.getPath())); + } + } + } + + /** + * Method description + * + * + * @param path + * + * @return + */ + private String createBasePath(String path) + { + String basePath = Util.EMPTY_STRING; + + if (Util.isNotEmpty(path)) + { + basePath = path; + + if (!basePath.endsWith("/")) + { + basePath = basePath.concat("/"); + } + } + + return basePath; + } + /** * Method description * diff --git a/scm-plugins/scm-svn-plugin/src/test/java/sonia/scm/repository/spi/SvnBrowseCommandTest.java b/scm-plugins/scm-svn-plugin/src/test/java/sonia/scm/repository/spi/SvnBrowseCommandTest.java index 4420c93a28..369d179fed 100644 --- a/scm-plugins/scm-svn-plugin/src/test/java/sonia/scm/repository/spi/SvnBrowseCommandTest.java +++ b/scm-plugins/scm-svn-plugin/src/test/java/sonia/scm/repository/spi/SvnBrowseCommandTest.java @@ -158,6 +158,26 @@ public class SvnBrowseCommandTest extends AbstractSvnCommandTestBase assertNull(a.getDescription()); assertNull(a.getLastModified()); } + + @Test + public void testRecursive() throws IOException, RepositoryException + { + BrowseCommandRequest request = new BrowseCommandRequest(); + request.setRecursive(true); + BrowserResult result = createCommand().getBrowserResult(request); + + assertNotNull(result); + + List foList = result.getFiles(); + + assertNotNull(foList); + assertFalse(foList.isEmpty()); + assertEquals(4, foList.size()); + + for ( FileObject fo : foList ){ + System.out.println(fo); + } + } /** * Method description